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 311 and 401] [Versions 401 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  declare(strict_types=1);
  18  
  19  namespace mod_scorm\completion;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  use core_completion\activity_custom_completion;
  24  
  25  require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  26  
  27  /**
  28   * Activity custom completion subclass for the scorm activity.
  29   *
  30   * Contains the class for defining mod_scorm's custom completion rules
  31   * and fetching a scorm instance's completion statuses for a user.
  32   *
  33   * @package mod_scorm
  34   * @copyright Michael Hawkins <michaelh@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class custom_completion extends activity_custom_completion {
  38  
  39      /**
  40       * Fetches the completion state for a given completion rule.
  41       *
  42       * @param string $rule The completion rule.
  43       * @return int The completion state.
  44       */
  45      public function get_state(string $rule): int {
  46          global $DB;
  47  
  48          $this->validate_rule($rule);
  49  
  50          // Base query used when fetching user's tracks data.
  51          $basequery = "SELECT id, scoid, element, value
  52                          FROM {scorm_scoes_track}
  53                         WHERE scormid = ?
  54                           AND userid = ?";
  55  
  56          switch ($rule) {
  57              case 'completionstatusrequired':
  58                  $status = COMPLETION_INCOMPLETE;
  59                  $query = $basequery .
  60                      " AND element IN (
  61                            'cmi.core.lesson_status',
  62                            'cmi.completion_status',
  63                            'cmi.success_status'
  64                      )";
  65  
  66                  $tracks = $DB->get_records_sql($query, [$this->cm->instance, $this->userid]);
  67  
  68                  // Get available status list.
  69                  $statuses = array_flip(\scorm_status_options());
  70  
  71                  $requiredcompletionstatusid = $this->cm->customdata['customcompletionrules']['completionstatusrequired'] ?? 0;
  72                  $isanystatus = ($requiredcompletionstatusid == array_sum($statuses));
  73  
  74                  // Check at least one track meets the required completion status value(s).
  75                  foreach ($tracks as $track) {
  76                      if (array_key_exists($track->value, $statuses)
  77                          && ($isanystatus || $statuses[$track->value] == $requiredcompletionstatusid)) {
  78                          $status = COMPLETION_COMPLETE;
  79                          break;
  80                      }
  81                  }
  82  
  83                  break;
  84  
  85              case 'completionscorerequired':
  86                  $status = COMPLETION_INCOMPLETE;
  87                  $query = $basequery .
  88                      " AND element IN (
  89                            'cmi.core.score.raw',
  90                            'cmi.score.raw'
  91                      )";
  92  
  93                  $tracks = $DB->get_records_sql($query, [$this->cm->instance, $this->userid]);
  94  
  95                  $requiredscore = $this->cm->customdata['customcompletionrules']['completionscorerequired'];
  96  
  97                  // Check if any track meets or exceeds the minimum score required.
  98                  foreach ($tracks as $track) {
  99                      if (strlen($track->value) && (floatval($track->value) >= $requiredscore)) {
 100                          $status = COMPLETION_COMPLETE;
 101  
 102                          // No need to check any other tracks once condition is confirmed completed.
 103                          break;
 104                      }
 105                  }
 106  
 107                  break;
 108  
 109              case 'completionstatusallscos':
 110                  // Assume complete unless we find a sco that is not complete.
 111                  $status = COMPLETION_COMPLETE;
 112                  $query = $basequery .
 113                      " AND element IN (
 114                            'cmi.core.lesson_status',
 115                            'cmi.completion_status',
 116                            'cmi.success_status'
 117                      )";
 118  
 119                  $tracks = $DB->get_records_sql($query, [$this->cm->instance, $this->userid]);
 120  
 121                  // Get available status list.
 122                  $statuses = array_flip(\scorm_status_options());
 123  
 124                  // Make a list of all sco IDs.
 125                  $scoids = [];
 126                  foreach ($tracks as $track) {
 127                      if (array_key_exists($track->value, $statuses)) {
 128                          $scoids[] = $track->scoid;
 129                      }
 130                  }
 131  
 132                  // Iterate over all scos and ensure each has a lesson_status.
 133                  $scos = $DB->get_records('scorm_scoes', ['scorm' => $this->cm->instance, 'scormtype' => 'sco']);
 134  
 135                  foreach ($scos as $sco) {
 136                      // If we find a sco without a lesson status, this condition is not completed.
 137                      if (!in_array($sco->id, $scoids)) {
 138                          $status = COMPLETION_INCOMPLETE;
 139                      }
 140                  }
 141  
 142                  break;
 143  
 144              default:
 145                  $status = COMPLETION_INCOMPLETE;
 146                  break;
 147          }
 148  
 149          // If not yet meeting the requirement and no attempts remain to complete it, mark it as failed.
 150          if ($status === COMPLETION_INCOMPLETE) {
 151              $scorm = $DB->get_record('scorm', ['id' => $this->cm->instance]);
 152              $attemptcount = scorm_get_attempt_count($this->userid, $scorm);
 153  
 154              if ($scorm->maxattempt > 0 && $attemptcount >= $scorm->maxattempt) {
 155                  $status = COMPLETION_COMPLETE_FAIL;
 156              }
 157          }
 158  
 159          return $status;
 160      }
 161  
 162      /**
 163       * Fetch the list of custom completion rules that this module defines.
 164       *
 165       * @return array
 166       */
 167      public static function get_defined_custom_rules(): array {
 168          return [
 169              'completionstatusrequired',
 170              'completionscorerequired',
 171              'completionstatusallscos',
 172          ];
 173      }
 174  
 175      /**
 176       * Returns an associative array of the descriptions of custom completion rules.
 177       *
 178       * @return array
 179       */
 180      public function get_custom_rule_descriptions(): array {
 181          $scorerequired = $this->cm->customdata['customcompletionrules']['completionscorerequired'] ?? 0;
 182  
 183          // Prepare completion status requirements string.
 184          $statusstrings = \scorm_status_options();
 185          $completionstatusid = $this->cm->customdata['customcompletionrules']['completionstatusrequired'] ?? 0;
 186  
 187          if (array_key_exists($completionstatusid, $statusstrings)) {
 188              // Single status required.
 189              $statusrequired = $statusstrings[$completionstatusid];
 190          } else {
 191              // All statuses required.
 192              $statusrequired = 'completedorpassed';
 193          }
 194  
 195          return [
 196              'completionstatusrequired' => get_string("completiondetail:completionstatus{$statusrequired}", 'scorm'),
 197              'completionscorerequired' => get_string('completiondetail:completionscore', 'scorm', $scorerequired),
 198              'completionstatusallscos' => get_string('completiondetail:allscos', 'scorm'),
 199          ];
 200      }
 201  
 202      /**
 203       * Returns an array of all completion rules, in the order they should be displayed to users.
 204       *
 205       * @return array
 206       */
 207      public function get_sort_order(): array {
 208          return [
 209              'completionview',
 210              'completionstatusallscos',
 211              'completionstatusrequired',
 212              'completionusegrade',
 213              'completionpassgrade',
 214              'completionscorerequired',
 215          ];
 216      }
 217  }
 218