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.
/mod/scorm/ -> aicc.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  // Prevent Caching Headers.
  18  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  19  header("Cache-Control: no-cache");
  20  header("Pragma: no-cache");
  21  
  22  require_once('../../config.php');
  23  require_once($CFG->dirroot.'/mod/scorm/lib.php');
  24  require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  25  require_once($CFG->dirroot.'/mod/scorm/datamodels/aicclib.php');
  26  
  27  foreach ($_POST as $key => $value) {
  28      $tempkey = strtolower($key);
  29      $_POST[$tempkey] = $value;
  30  }
  31  
  32  $command = required_param('command', PARAM_ALPHA);
  33  $sessionid = required_param('session_id', PARAM_ALPHANUM);
  34  $aiccdata = optional_param('aicc_data', '', PARAM_RAW);
  35  
  36  $cfgscorm = get_config('scorm');
  37  
  38  $url = new moodle_url('/mod/scorm/aicc.php', array('command' => $command, 'session_id' => $sessionid));
  39  if ($aiccdata !== 0) {
  40      $url->param('aicc_data', $aiccdata);
  41  }
  42  $PAGE->set_url($url);
  43  
  44  if (empty($cfgscorm->allowaicchacp)) {
  45      require_login();
  46      if (!confirm_sesskey($sessionid)) {
  47          throw new \moodle_exception('invalidsesskey');
  48      }
  49      $aiccuser = $USER;
  50      $scormsession = $SESSION->scorm;
  51  } else {
  52      $scormsession = scorm_aicc_confirm_hacp_session($sessionid);
  53      if (empty($scormsession)) {
  54          throw new \moodle_exception('invalidhacpsession', 'scorm');
  55      }
  56      $aiccuser = $DB->get_record('user', array('id' => $scormsession->userid), 'id,username,lastname,firstname', MUST_EXIST);
  57  }
  58  
  59  if (!empty($command)) {
  60      $command = strtolower($command);
  61  
  62      if (isset($scormsession->scoid)) {
  63          $scoid = $scormsession->scoid;
  64      } else {
  65          throw new \moodle_exception('cannotcallscript');
  66      }
  67      $mode = 'normal';
  68      if (isset($scormsession->scormmode)) {
  69          $mode = $scormsession->scormmode;
  70      }
  71      $status = 'Not Initialized';
  72      if (isset($scormsession->scormstatus)) {
  73          $status = $scormsession->scormstatus;
  74      }
  75      if (isset($scormsession->attempt)) {
  76          $attempt = $scormsession->attempt;
  77      } else {
  78          $attempt = 1;
  79      }
  80      $attemptobject = scorm_get_attempt($aiccuser->id, $scormsession->scormid, $attempt);
  81      if ($sco = scorm_get_sco($scoid, SCO_ONLY)) {
  82          if (!$scorm = $DB->get_record('scorm', array('id' => $sco->scorm))) {
  83              throw new \moodle_exception('cannotcallscript');
  84          }
  85      } else {
  86          throw new \moodle_exception('cannotcallscript');
  87      }
  88      $aiccrequest = "MOODLE scoid: $scoid"
  89                   . "\r\nMOODLE mode: $mode"
  90                   . "\r\nMOODLE status: $status"
  91                   . "\r\nMOODLE attempt: $attempt"
  92                   . "\r\nAICC sessionid: $sessionid"
  93                   . "\r\nAICC command: $command"
  94                   . "\r\nAICC aiccdata:\r\n$aiccdata";
  95      scorm_debug_log_write("aicc", "HACP Request:\r\n$aiccrequest", $scoid);
  96      ob_start();
  97  
  98      if ($scorm = $DB->get_record('scorm', array('id' => $sco->scorm))) {
  99          switch ($command) {
 100              case 'getparam':
 101                  if ($status == 'Not Initialized') {
 102                      $scormsession->scormstatus = 'Running';
 103                      $status = 'Running';
 104                  }
 105                  if ($status != 'Running') {
 106                      echo "error=101\r\nerror_text=Terminated\r\n";
 107                  } else {
 108                      if ($usertrack = scorm_get_tracks($scoid, $aiccuser->id, $attempt)) {
 109                          $userdata = $usertrack;
 110                      } else {
 111                          $userdata->status = '';
 112                          $userdata->score_raw = '';
 113                      }
 114                      $aiccuserid = get_config('scorm', 'aiccuserid');
 115                      if (!empty($aiccuserid)) {
 116                          $userdata->student_id = $aiccuser->id;
 117                      } else {
 118                          $userdata->student_id = $aiccuser->username;
 119                      }
 120                      $userdata->student_name = $aiccuser->lastname .', '. $aiccuser->firstname;
 121                      $userdata->mode = $mode;
 122                      if ($userdata->mode == 'normal') {
 123                          $userdata->credit = 'credit';
 124                      } else {
 125                          $userdata->credit = 'no-credit';
 126                      }
 127  
 128                      if ($sco = scorm_get_sco($scoid)) {
 129                          $userdata->course_id = $sco->identifier;
 130                          $userdata->datafromlms = isset($sco->datafromlms) ? $sco->datafromlms : '';
 131                          $userdata->mastery_score = isset($sco->mastery_score) && is_numeric($sco->mastery_score) ?
 132                                                          trim($sco->mastery_score) : '';
 133                          $userdata->max_time_allowed = isset($sco->max_time_allowed) ? $sco->max_time_allowed : '';
 134                          $userdata->time_limit_action = isset($sco->time_limit_action) ? $sco->time_limit_action : '';
 135  
 136                          echo "error=0\r\nerror_text=Successful\r\naicc_data=";
 137                          echo "[Core]\r\n";
 138                          echo 'Student_ID='.$userdata->student_id."\r\n";
 139                          echo 'Student_Name='.$userdata->student_name."\r\n";
 140                          if (isset($userdata->{'cmi.core.lesson_location'})) {
 141                              echo 'Lesson_Location='.$userdata->{'cmi.core.lesson_location'}."\r\n";
 142                          } else {
 143                              echo 'Lesson_Location='."\r\n";
 144                          }
 145                          echo 'Credit='.$userdata->credit."\r\n";
 146                          if (isset($userdata->status)) {
 147                              if ($userdata->status == '') {
 148                                  $userdata->entry = ', ab-initio';
 149                              } else {
 150                                  if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
 151                                      $userdata->entry = ', resume';
 152                                  } else {
 153                                      $userdata->entry = '';
 154                                  }
 155                              }
 156                          }
 157                          if (isset($userdata->{'cmi.core.lesson_status'})) {
 158                              echo 'Lesson_Status='.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\r\n";
 159                              $scormsession->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
 160                          } else {
 161                              echo 'Lesson_Status=not attempted'.$userdata->entry."\r\n";
 162                              $scormsession->scorm_lessonstatus = 'not attempted';
 163                          }
 164                          if (isset($userdata->{'cmi.core.score.raw'})) {
 165                              $max = '';
 166                              $min = '';
 167                              if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
 168                                  $max = ', '.$userdata->{'cmi.core.score.max'};
 169                                  if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
 170                                      $min = ', '.$userdata->{'cmi.core.score.min'};
 171                                  }
 172                              }
 173                              echo 'Score='.$userdata->{'cmi.core.score.raw'}.$max.$min."\r\n";
 174                          } else {
 175                              echo 'Score='."\r\n";
 176                          }
 177                          if (isset($userdata->{'cmi.core.total_time'})) {
 178                              echo 'Time='.$userdata->{'cmi.core.total_time'}."\r\n";
 179                          } else {
 180                              echo 'Time='.'00:00:00'."\r\n";
 181                          }
 182                          echo 'Lesson_Mode='.$userdata->mode."\r\n";
 183                          if (isset($userdata->{'cmi.suspend_data'})) {
 184                              echo "[Core_Lesson]\r\n".rawurldecode($userdata->{'cmi.suspend_data'})."\r\n";
 185                          } else {
 186                              echo "[Core_Lesson]\r\n";
 187                          }
 188                          echo "[Core_Vendor]\r\n".$userdata->datafromlms."\r\n";
 189                          echo "[Evaluation]\r\nCourse_ID = {".$userdata->course_id."}\r\n";
 190                          echo "[Student_Data]\r\n";
 191                          echo 'Mastery_Score='.$userdata->mastery_score."\r\n";
 192                          echo 'Max_Time_Allowed='.$userdata->max_time_allowed."\r\n";
 193                          echo 'Time_Limit_Action='.$userdata->time_limit_action."\r\n";
 194                      } else {
 195                          throw new \moodle_exception('cannotfindsco', 'scorm');
 196                      }
 197                  }
 198              break;
 199              case 'putparam':
 200                  if ($status == 'Running') {
 201                      if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
 202                          echo "error=1\r\nerror_text=Unknown\r\n"; // No one must see this error message if not hacked.
 203                      }
 204                      $savetrack = has_capability('mod/scorm:savetrack', context_module::instance($cm->id), $aiccuser->id);
 205                      if (!empty($aiccdata) && $savetrack) {
 206                          $initlessonstatus = 'not attempted';
 207                          $lessonstatus = 'not attempted';
 208                          if (isset($scormsession->scorm_lessonstatus)) {
 209                              $initlessonstatus = $scormsession->scorm_lessonstatus;
 210                          }
 211                          $score = '';
 212                          $datamodel['lesson_location'] = 'cmi.core.lesson_location';
 213                          $datamodel['lesson_status'] = 'cmi.core.lesson_status';
 214                          $datamodel['score'] = 'cmi.core.score.raw';
 215                          $datamodel['time'] = 'cmi.core.session_time';
 216                          $datamodel['[core_lesson]'] = 'cmi.suspend_data';
 217                          $datamodel['[comments]'] = 'cmi.comments';
 218                          $datarows = explode("\r\n", $aiccdata);
 219                          reset($datarows);
 220                          $multirowvalue = '';
 221                          $multirowelement = '';
 222                          foreach ($datarows as $did => $datarow) {
 223                              $equal = strpos($datarow, '=');
 224                              if ($equal === false || !empty($multirowelement)) {
 225                                  if (empty($multirowelement)) {
 226                                      if (isset($datamodel[strtolower(trim($datarow))])) {
 227                                          $multirowelement = $datamodel[strtolower(trim($datarow))];
 228                                      } else {
 229                                          // An element was passed by the external AICC package is not one we care about.
 230                                          continue;
 231                                      }
 232                                  }
 233                                  $multirowvalue .= $datarow."\r\n";
 234                                  if (isset($datarows[$did + 1]) && substr($datarows[$did + 1], 0, 1) != '[') {
 235                                      // This is a multiline row, we haven't found the end yet.
 236                                      continue;
 237                                  }
 238                                  $value = rawurlencode($multirowvalue);
 239                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id, $attemptobject,
 240                                                           $multirowelement, $value);
 241                                  $multirowvalue = $multirowelement = '';
 242                              } else {
 243                                  $element = strtolower(trim(substr($datarow, 0, $equal)));
 244                                  $value = trim(substr($datarow, $equal + 1));
 245                                  if (isset($datamodel[$element])) {
 246                                      $element = $datamodel[$element];
 247                                      switch ($element) {
 248                                          case 'cmi.core.lesson_location':
 249                                              $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 250                                                                       $attemptobject, $element, $value);
 251                                          break;
 252                                          case 'cmi.core.lesson_status':
 253                                              $statuses = array(
 254                                                         'passed' => 'passed',
 255                                                         'completed' => 'completed',
 256                                                         'failed' => 'failed',
 257                                                         'incomplete' => 'incomplete',
 258                                                         'browsed' => 'browsed',
 259                                                         'not attempted' => 'not attempted',
 260                                                         'p' => 'passed',
 261                                                         'c' => 'completed',
 262                                                         'f' => 'failed',
 263                                                         'i' => 'incomplete',
 264                                                         'b' => 'browsed',
 265                                                         'n' => 'not attempted'
 266                                                         );
 267                                              $exites = array(
 268                                                         'logout' => 'logout',
 269                                                         'time-out' => 'time-out',
 270                                                         'suspend' => 'suspend',
 271                                                         'l' => 'logout',
 272                                                         't' => 'time-out',
 273                                                         's' => 'suspend',
 274                                                         );
 275                                              $values = explode(',', $value);
 276                                              $value = '';
 277                                              if (count($values) > 1) {
 278                                                  $value = trim(strtolower($values[1]));
 279                                                  $value = $value[0];
 280                                                  if (isset($exites[$value])) {
 281                                                      $value = $exites[$value];
 282                                                  }
 283                                              }
 284                                              if (empty($value) || isset($exites[$value])) {
 285                                                  $subelement = 'cmi.core.exit';
 286                                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 287                                                                           $attemptobject, $subelement, $value);
 288                                              }
 289                                              $value = trim(strtolower($values[0]));
 290                                              $value = $value[0];
 291                                              if (isset($statuses[$value]) && ($mode == 'normal')) {
 292                                                  $value = $statuses[$value];
 293                                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 294                                                                           $attemptobject, $element, $value);
 295                                              }
 296                                              $lessonstatus = $value;
 297                                          break;
 298                                          case 'cmi.core.score.raw':
 299                                              $values = explode(',', $value);
 300                                              if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
 301                                                  $subelement = 'cmi.core.score.max';
 302                                                  $value = trim($values[1]);
 303                                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 304                                                                           $attemptobject, $subelement, $value);
 305                                                  if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
 306                                                      $subelement = 'cmi.core.score.min';
 307                                                      $value = trim($values[2]);
 308                                                      $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 309                                                                               $attemptobject, $subelement, $value);
 310                                                  }
 311                                              }
 312  
 313                                              $value = '';
 314                                              if (is_numeric($values[0])) {
 315                                                  $value = trim($values[0]);
 316                                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 317                                                                           $attemptobject, $element, $value);
 318                                              }
 319                                              $score = $value;
 320                                          break;
 321                                          case 'cmi.core.session_time':
 322                                               $scormsession->sessiontime = $value;
 323                                          break;
 324                                      }
 325                                  }
 326                              }
 327                          }
 328                          if (($mode == 'browse') && ($initlessonstatus == 'not attempted')) {
 329                              $lessonstatus = 'browsed';
 330                              $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 331                                                       $attemptobject, 'cmi.core.lesson_status', 'browsed');
 332                          }
 333                          if ($mode == 'normal') {
 334                              if ($sco = scorm_get_sco($scoid)) {
 335                                  if (isset($sco->mastery_score) && is_numeric($sco->mastery_score)) {
 336                                      if ($score != '') { // Score is correctly initialized w/ an empty string, see above.
 337                                          if ($score >= trim($sco->mastery_score)) {
 338                                              $lessonstatus = 'passed';
 339                                          } else {
 340                                              $lessonstatus = 'failed';
 341                                          }
 342                                      }
 343                                  }
 344                                  $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
 345                                                           $attemptobject, 'cmi.core.lesson_status', $lessonstatus);
 346                              }
 347                          }
 348                      }
 349                      echo "error=0\r\nerror_text=Successful\r\n";
 350                  } else if ($status == 'Terminated') {
 351                      echo "error=1\r\nerror_text=Terminated\r\n";
 352                  } else {
 353                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 354                  }
 355              break;
 356              case 'putcomments':
 357                  if ($status == 'Running') {
 358                      echo "error=0\r\nerror_text=Successful\r\n";
 359                  } else if ($status == 'Terminated') {
 360                      echo "error=1\r\nerror_text=Terminated\r\n";
 361                  } else {
 362                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 363                  }
 364              break;
 365              case 'putinteractions':
 366                  if ($status == 'Running') {
 367                      echo "error=0\r\nerror_text=Successful\r\n";
 368                  } else if ($status == 'Terminated') {
 369                      echo "error=1\r\nerror_text=Terminated\r\n";
 370                  } else {
 371                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 372                  }
 373              break;
 374              case 'putobjectives':
 375                  if ($status == 'Running') {
 376                      echo "error=0\r\nerror_text=Successful\r\n";
 377                  } else if ($status == 'Terminated') {
 378                      echo "error=1\r\nerror_text=Terminated\r\n";
 379                  } else {
 380                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 381                  }
 382              break;
 383              case 'putpath':
 384                  if ($status == 'Running') {
 385                      echo "error=0\r\nerror_text=Successful\r\n";
 386                  } else if ($status == 'Terminated') {
 387                      echo "error=1\r\nerror_text=Terminated\r\n";
 388                  } else {
 389                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 390                  }
 391              break;
 392              case 'putperformance':
 393                  if ($status == 'Running') {
 394                      echo "error=0\r\nerror_text=Successful\r\n";
 395                  } else if ($status == 'Terminated') {
 396                      echo "error=1\r\nerror_text=Terminated\r\n";
 397                  } else {
 398                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 399                  }
 400              break;
 401              case 'exitau':
 402                  if ($status == 'Running') {
 403                      if (isset($scormsession->sessiontime) && ($scormsession->sessiontime != '')) {
 404                          $track = scorm_get_sco_value($sco->id, $aiccuser->id, 'cmi.core.total_time', $attempt);
 405                          if (!empty($track)) {
 406                              // Add session_time to total_time.
 407                              $value = scorm_add_time($track->value, $scormsession->sessiontime);
 408                              $v = new stdClass();
 409                              $v->id = $track->valueid;
 410                              $v->value = $value;
 411                              $v->timemodified = time();
 412                              $DB->update_record('scorm_scoes_value', $v);
 413                          } else {
 414                              $track = new stdClass();
 415                              $track->scoid = $sco->id;
 416                              $track->element = scorm_get_elementid('cmi.core.total_time');
 417                              $track->value = $scormsession->sessiontime;
 418                              $atobject = scorm_get_attempt($aiccuser->id, $scormsession->scormid, $attempt);
 419                              $track->attempt = $atobject->id;
 420                              $track->timemodified = time();
 421                              $id = $DB->insert_record('scorm_scoes_value', $track);
 422                          }
 423                          scorm_update_grades($scorm, $aiccuser->id);
 424                      }
 425                      $scormsession->scormstatus = 'Terminated';
 426                      $scormsession->session_time = '';
 427                      echo "error=0\r\nerror_text=Successful\r\n";
 428                  } else if ($status == 'Terminated') {
 429                      echo "error=1\r\nerror_text=Terminated\r\n";
 430                  } else {
 431                      echo "error=1\r\nerror_text=Not Initialized\r\n";
 432                  }
 433              break;
 434              default:
 435                  echo "error=1\r\nerror_text=Invalid Command\r\n";
 436              break;
 437          }
 438      }
 439  } else {
 440      if (empty($command)) {
 441          echo "error=1\r\nerror_text=Invalid Command\r\n";
 442      } else {
 443          echo "error=3\r\nerror_text=Invalid Session ID\r\n";
 444      }
 445  }
 446  if (empty($cfgscorm->allowaicchacp)) {
 447      $SESSION->scorm = $scormsession;
 448  } else {
 449      $scormsession->timemodified = time();
 450      $DB->update_record('scorm_aicc_session', $scormsession);
 451  }
 452  
 453  $aiccresponse = ob_get_contents();
 454  scorm_debug_log_write("aicc", "HACP Response:\r\n$aiccresponse", $scoid);
 455  ob_end_flush();