Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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  // This page prints a particular instance of aicc/scorm package.
  18  
  19  require_once('../../config.php');
  20  require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  21  require_once($CFG->libdir . '/completionlib.php');
  22  
  23  $id = optional_param('cm', '', PARAM_INT);                          // Course Module ID, or
  24  $a = optional_param('a', '', PARAM_INT);                            // scorm ID
  25  $scoid = required_param('scoid', PARAM_INT);                        // sco ID
  26  $mode = optional_param('mode', 'normal', PARAM_ALPHA);              // navigation mode
  27  $currentorg = optional_param('currentorg', '', PARAM_RAW);          // selected organization
  28  $newattempt = optional_param('newattempt', 'off', PARAM_ALPHA);     // the user request to start a new attempt.
  29  $displaymode = optional_param('display', '', PARAM_ALPHA);
  30  
  31  if (!empty($id)) {
  32      if (! $cm = get_coursemodule_from_id('scorm', $id, 0, true)) {
  33          throw new \moodle_exception('invalidcoursemodule');
  34      }
  35      if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  36          throw new \moodle_exception('coursemisconf');
  37      }
  38      if (! $scorm = $DB->get_record("scorm", array("id" => $cm->instance))) {
  39          throw new \moodle_exception('invalidcoursemodule');
  40      }
  41  } else if (!empty($a)) {
  42      if (! $scorm = $DB->get_record("scorm", array("id" => $a))) {
  43          throw new \moodle_exception('invalidcoursemodule');
  44      }
  45      if (! $course = $DB->get_record("course", array("id" => $scorm->course))) {
  46          throw new \moodle_exception('coursemisconf');
  47      }
  48      if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id, true)) {
  49          throw new \moodle_exception('invalidcoursemodule');
  50      }
  51  } else {
  52      throw new \moodle_exception('missingparameter');
  53  }
  54  
  55  // PARAM_RAW is used for $currentorg, validate it against records stored in the table.
  56  if (!empty($currentorg)) {
  57      if (!$DB->record_exists('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $currentorg))) {
  58          $currentorg = '';
  59      }
  60  }
  61  
  62  // If new attempt is being triggered set normal mode and increment attempt number.
  63  $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
  64  
  65  // Check mode is correct and set/validate mode/attempt/newattempt (uses pass by reference).
  66  scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
  67  
  68  if (!empty($scoid)) {
  69      $scoid = scorm_check_launchable_sco($scorm, $scoid);
  70  }
  71  
  72  $url = new moodle_url('/mod/scorm/player.php', array('scoid' => $scoid, 'cm' => $cm->id));
  73  if ($mode !== 'normal') {
  74      $url->param('mode', $mode);
  75  }
  76  if ($currentorg !== '') {
  77      $url->param('currentorg', $currentorg);
  78  }
  79  if ($newattempt !== 'off') {
  80      $url->param('newattempt', $newattempt);
  81  }
  82  if ($displaymode !== '') {
  83      $url->param('display', $displaymode);
  84  }
  85  $PAGE->set_url($url);
  86  $PAGE->set_secondary_active_tab("modulepage");
  87  
  88  $forcejs = get_config('scorm', 'forcejavascript');
  89  if (!empty($forcejs)) {
  90      $PAGE->add_body_class('forcejavascript');
  91  }
  92  $collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
  93  if (empty($collapsetocwinsize)) {
  94      // Set as default window size to collapse TOC.
  95      $collapsetocwinsize = 767;
  96  } else {
  97      $collapsetocwinsize = intval($collapsetocwinsize);
  98  }
  99  
 100  require_login($course, false, $cm);
 101  
 102  $strscorms = get_string('modulenameplural', 'scorm');
 103  $strscorm  = get_string('modulename', 'scorm');
 104  $strpopup = get_string('popup', 'scorm');
 105  $strexit = get_string('exitactivity', 'scorm');
 106  
 107  $coursecontext = context_course::instance($course->id);
 108  
 109  if ($displaymode == 'popup') {
 110      $PAGE->set_pagelayout('embedded');
 111  } else {
 112      $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
 113      $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
 114      $PAGE->set_title($pagetitle);
 115      $PAGE->set_heading($course->fullname);
 116  }
 117  if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', context_module::instance($cm->id))) {
 118      echo $OUTPUT->header();
 119      notice(get_string("activityiscurrentlyhidden"));
 120      echo $OUTPUT->footer();
 121      die;
 122  }
 123  
 124  // Check if SCORM available.
 125  list($available, $warnings) = scorm_get_availability_status($scorm);
 126  if (!$available) {
 127      $reason = current(array_keys($warnings));
 128      echo $OUTPUT->header();
 129      echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
 130      echo $OUTPUT->footer();
 131      die;
 132  }
 133  
 134  // TOC processing
 135  $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe.
 136  if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
 137      $scorm->version = 'scorm_12';
 138  }
 139  require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
 140  
 141  $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
 142  $sco = $result->sco;
 143  if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
 144      echo $OUTPUT->header();
 145      echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
 146      echo $OUTPUT->footer();
 147      exit;
 148  }
 149  
 150  $scoidstr = '&amp;scoid='.$sco->id;
 151  $modestr = '&amp;mode='.$mode;
 152  
 153  $SESSION->scorm = new stdClass();
 154  $SESSION->scorm->scoid = $sco->id;
 155  $SESSION->scorm->scormstatus = 'Not Initialized';
 156  $SESSION->scorm->scormmode = $mode;
 157  $SESSION->scorm->attempt = $attempt;
 158  
 159  // Mark module viewed.
 160  $completion = new completion_info($course);
 161  $completion->set_module_viewed($cm);
 162  
 163  // Generate the exit button.
 164  $exiturl = "";
 165  if (empty($scorm->popup) || $displaymode == 'popup') {
 166      if ($course->format == 'singleactivity' && $scorm->skipview == SCORM_SKIPVIEW_ALWAYS
 167          && !has_capability('mod/scorm:viewreport', context_module::instance($cm->id))) {
 168          // Redirect students back to site home to avoid redirect loop.
 169          $exiturl = $CFG->wwwroot;
 170      } else {
 171          // Redirect back to the correct section if one section per page is being used.
 172          $exiturl = course_get_url($course, $cm->sectionnum)->out();
 173      }
 174  }
 175  
 176  // Print the page header.
 177  $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
 178                                                         'currentorg' => '',
 179                                                         'sco' => 0,
 180                                                         'scorm' => 0,
 181                                                         'courseid' => $scorm->course,
 182                                                         'cwidth' => $scorm->width,
 183                                                         'cheight' => $scorm->height,
 184                                                         'popupoptions' => $scorm->options), true);
 185  $PAGE->requires->js('/mod/scorm/request.js', true);
 186  $PAGE->requires->js('/lib/cookies.js', true);
 187  
 188  if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js')) {
 189      $PAGE->requires->js('/mod/scorm/datamodels/'.$scorm->version.'.js', true);
 190  } else {
 191      $PAGE->requires->js('/mod/scorm/datamodels/scorm_12.js', true);
 192  }
 193  $activityheader = $PAGE->activityheader;
 194  $headerconfig = [
 195      'description' => '',
 196      'hidecompletion' => true
 197  ];
 198  
 199  $activityheader->set_attrs($headerconfig);
 200  echo $OUTPUT->header();
 201  
 202  $PAGE->requires->string_for_js('navigation', 'scorm');
 203  $PAGE->requires->string_for_js('toc', 'scorm');
 204  $PAGE->requires->string_for_js('hide', 'moodle');
 205  $PAGE->requires->string_for_js('show', 'moodle');
 206  $PAGE->requires->string_for_js('popupsblocked', 'scorm');
 207  
 208  $name = false;
 209  
 210  // Exit button should ONLY be displayed when in the current window.
 211  if ($displaymode !== 'popup') {
 212      $renderer = $PAGE->get_renderer('mod_scorm');
 213      echo $renderer->generate_exitbar($exiturl);
 214  }
 215  
 216  echo html_writer::start_div('', array('id' => 'scormpage'));
 217  echo html_writer::start_div('', array('id' => 'tocbox'));
 218  echo html_writer::div(html_writer::tag('script', '', array('id' => 'external-scormapi', 'type' => 'text/JavaScript')), '',
 219                          array('id' => 'scormapi-parent'));
 220  
 221  if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode == 'browse' or $mode == 'review') {
 222      echo html_writer::start_div('mb-3', array('id' => 'scormtop'));
 223      if ($mode == 'browse' || $mode == 'review') {
 224          echo html_writer::div(get_string("{$mode}mode", 'scorm'), 'scorm-left h3', ['id' => 'scormmode']);
 225      }
 226      if ($scorm->hidetoc == SCORM_TOC_POPUP) {
 227          echo html_writer::div($result->tocmenu, 'scorm-right', array('id' => 'scormnav'));
 228      }
 229      echo html_writer::end_div();
 230  }
 231  
 232  echo html_writer::start_div('', array('id' => 'toctree'));
 233  
 234  if (empty($scorm->popup) || $displaymode == 'popup') {
 235      echo $result->toc;
 236  } else {
 237      // Added incase javascript popups are blocked we don't provide a direct link
 238      // to the pop-up as JS communication can fail - the user must disable their pop-up blocker.
 239      $linkcourse = html_writer::link($CFG->wwwroot.'/course/view.php?id='.
 240                      $scorm->course, get_string('finishscormlinkname', 'scorm'));
 241      echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
 242  }
 243  echo html_writer::end_div(); // Toc tree ends.
 244  echo html_writer::end_div(); // Toc box ends.
 245  echo html_writer::tag('noscript', html_writer::div(get_string('noscriptnoscorm', 'scorm'), '', array('id' => 'noscript')));
 246  
 247  if ($result->prerequisites) {
 248      if ($scorm->popup != 0 && $displaymode !== 'popup') {
 249          // Clean the name for the window as IE is fussy.
 250          $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
 251          if (!$name) {
 252              $name = 'DefaultPlayerWindow';
 253          }
 254          $name = 'scorm_'.$name;
 255          echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
 256          $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
 257          echo html_writer::script(
 258              js_writer::function_call('scorm_openpopup', Array($url->out(false),
 259                                                         $name, $scorm->options,
 260                                                         $scorm->width, $scorm->height)));
 261          echo html_writer::tag('noscript', html_writer::tag('iframe', '', array('id' => 'main',
 262                                  'class' => 'scoframe', 'name' => 'main', 'src' => 'loadSCO.php?id='.$cm->id.$scoidstr.$modestr)));
 263      }
 264  } else {
 265      echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
 266  }
 267  echo html_writer::end_div(); // Scorm page ends.
 268  
 269  $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
 270  $adlnav = scorm_get_adlnav_json($scoes['scoes']);
 271  
 272  if (empty($scorm->popup) || $displaymode == 'popup') {
 273      if (!isset($result->toctitle)) {
 274          $result->toctitle = get_string('toc', 'scorm');
 275      }
 276      $jsmodule = array(
 277          'name' => 'mod_scorm',
 278          'fullpath' => '/mod/scorm/module.js',
 279          'requires' => array('json'),
 280      );
 281      $scorm->nav = intval($scorm->nav);
 282      $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop,
 283                              $scorm->hidetoc, $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
 284  }
 285  if (!empty($forcejs)) {
 286      $message = $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
 287      echo html_writer::tag('noscript', $message);
 288  }
 289  
 290  if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
 291      include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
 292  } else {
 293      include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
 294  }
 295  
 296  // Add the keepalive system to keep checking for a connection.
 297  \core\session\manager::keepalive('networkdropped', 'mod_scorm', 30, 10);
 298  
 299  echo $OUTPUT->footer();
 300  
 301  // Set the start time of this SCO.
 302  scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());