Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  require_once('../../config.php');
  18  require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  19  
  20  $id    = optional_param('id', '', PARAM_INT);    // Course Module ID, or
  21  $a     = optional_param('a', '', PARAM_INT);     // scorm ID
  22  $scoid = required_param('scoid', PARAM_INT);     // sco ID.
  23  
  24  $delayseconds = 2;  // Delay time before sco launch, used to give time to browser to define API.
  25  
  26  if (!empty($id)) {
  27      if (! $cm = get_coursemodule_from_id('scorm', $id)) {
  28          throw new \moodle_exception('invalidcoursemodule');
  29      }
  30      if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
  31          throw new \moodle_exception('coursemisconf');
  32      }
  33      if (! $scorm = $DB->get_record('scorm', array('id' => $cm->instance))) {
  34          throw new \moodle_exception('invalidcoursemodule');
  35      }
  36  } else if (!empty($a)) {
  37      if (! $scorm = $DB->get_record('scorm', array('id' => $a))) {
  38          throw new \moodle_exception('coursemisconf');
  39      }
  40      if (! $course = $DB->get_record('course', array('id' => $scorm->course))) {
  41          throw new \moodle_exception('coursemisconf');
  42      }
  43      if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
  44          throw new \moodle_exception('invalidcoursemodule');
  45      }
  46  } else {
  47      throw new \moodle_exception('missingparameter');
  48  }
  49  
  50  $PAGE->set_url('/mod/scorm/loadSCO.php', array('scoid' => $scoid, 'id' => $cm->id));
  51  
  52  if (!isloggedin()) { // Prevent login page from being shown in iframe.
  53      // Using simple html instead of exceptions here as shown inside iframe/object.
  54      echo html_writer::start_tag('html');
  55      echo html_writer::tag('head', '');
  56      echo html_writer::tag('body', get_string('loggedinnot'));
  57      echo html_writer::end_tag('html');
  58      exit;
  59  }
  60  
  61  require_login($course, false, $cm, false); // Call require_login anyway to set up globals correctly.
  62  
  63  // Check if SCORM is available.
  64  scorm_require_available($scorm);
  65  
  66  $context = context_module::instance($cm->id);
  67  
  68  // Forge SCO URL.
  69  list($sco, $scolaunchurl) = scorm_get_sco_and_launch_url($scorm, $scoid, $context);
  70  
  71  if ($sco->scormtype == 'asset') {
  72      $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
  73      $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status' : 'cmi.core.lesson_status';
  74      $value = 'completed';
  75      scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
  76  }
  77  
  78  // Trigger the SCO launched event.
  79  scorm_launch_sco($scorm, $sco, $cm, $context, $scolaunchurl);
  80  
  81  header('Content-Type: text/html; charset=UTF-8');
  82  
  83  if ($sco->scormtype == 'asset') {
  84      // HTTP 302 Found => Moved Temporarily.
  85      header('Location: ' . $scolaunchurl);
  86      // Provide a short feedback in case of slow network connection.
  87      echo html_writer::start_tag('html');
  88      echo html_writer::tag('body', html_writer::tag('p', get_string('activitypleasewait', 'scorm')));
  89      echo html_writer::end_tag('html');
  90      exit;
  91  }
  92  
  93  // We expect a SCO: select which API are we looking for.
  94  $lmsapi = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
  95  
  96  echo html_writer::start_tag('html');
  97  echo html_writer::start_tag('head');
  98  echo html_writer::tag('title', 'LoadSCO');
  99  ?>
 100      <script type="text/javascript">
 101      //<![CDATA[
 102      var myApiHandle = null;
 103      var myFindAPITries = 0;
 104  
 105      function myGetAPIHandle() {
 106         myFindAPITries = 0;
 107         if (myApiHandle == null) {
 108            myApiHandle = myGetAPI();
 109         }
 110         return myApiHandle;
 111      }
 112  
 113      function myFindAPI(win) {
 114         while ((win.<?php echo $lmsapi; ?> == null) && (win.parent != null) && (win.parent != win)) {
 115            myFindAPITries++;
 116            // Note: 7 is an arbitrary number, but should be more than sufficient
 117            if (myFindAPITries > 7) {
 118               return null;
 119            }
 120            win = win.parent;
 121         }
 122         return win.<?php echo $lmsapi; ?>;
 123      }
 124  
 125      // hun for the API - needs to be loaded before we can launch the package
 126      function myGetAPI() {
 127         var theAPI = myFindAPI(window);
 128         if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
 129            theAPI = myFindAPI(window.opener);
 130         }
 131         if (theAPI == null) {
 132            return null;
 133         }
 134         return theAPI;
 135      }
 136  
 137     function doredirect() {
 138          if (myGetAPIHandle() != null) {
 139              location = "<?php echo $scolaunchurl ?>";
 140          }
 141          else {
 142              document.body.innerHTML = "<p><?php echo get_string('activityloading', 'scorm');?>" +
 143                                          "<span id='countdown'><?php echo $delayseconds ?></span> " +
 144                                          "<?php echo get_string('numseconds', 'moodle', '');?>. &nbsp; " +
 145                                          "<?php echo addslashes($OUTPUT->pix_icon('wait', '', 'scorm')); ?></p>";
 146              var e = document.getElementById("countdown");
 147              var cSeconds = parseInt(e.innerHTML);
 148              var timer = setInterval(function() {
 149                                              if( cSeconds && myGetAPIHandle() == null ) {
 150                                                  e.innerHTML = --cSeconds;
 151                                              } else {
 152                                                  clearInterval(timer);
 153                                                  document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
 154                                                  location = "<?php echo $scolaunchurl ?>";
 155                                              }
 156                                          }, 1000);
 157          }
 158      }
 159      //]]>
 160      </script>
 161      <noscript>
 162          <meta http-equiv="refresh" content="0;url=<?php echo $scolaunchurl ?>" />
 163      </noscript>
 164  <?php
 165  echo html_writer::end_tag('head');
 166  echo html_writer::tag('body', html_writer::tag('p', get_string('activitypleasewait', 'scorm')), array('onload' => "doredirect();"));
 167  echo html_writer::end_tag('html');