Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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_quiz\completion;
  20  
  21  use context_module;
  22  use core_completion\activity_custom_completion;
  23  use grade_grade;
  24  use grade_item;
  25  use quiz;
  26  use quiz_access_manager;
  27  
  28  /**
  29   * Activity custom completion subclass for the quiz activity.
  30   *
  31   * Class for defining mod_quiz's custom completion rules and fetching the completion statuses
  32   * of the custom completion rules for a given quiz instance and a user.
  33   *
  34   * @package   mod_quiz
  35   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class custom_completion extends activity_custom_completion {
  39  
  40      /**
  41       * Check passing grade (or no attempts left) requirement for completion.
  42       *
  43       * @return bool True if the passing grade (or no attempts left) requirement is disabled or met.
  44       */
  45      protected function check_passing_grade_or_all_attempts(): bool {
  46          global $CFG;
  47          require_once($CFG->libdir . '/gradelib.php');
  48  
  49          $completionpassorattempts = $this->cm->customdata['customcompletionrules']['completionpassorattemptsexhausted'];
  50  
  51          if (empty($completionpassorattempts['completionpass'])) {
  52              return true;
  53          }
  54  
  55          // Check for passing grade.
  56          $item = grade_item::fetch([
  57              'courseid' => $this->cm->get_course()->id,
  58              'itemtype' => 'mod',
  59              'itemmodule' => 'quiz',
  60              'iteminstance' => $this->cm->instance,
  61              'outcomeid' => null
  62          ]);
  63          if ($item) {
  64              $grades = grade_grade::fetch_users_grades($item, [$this->userid], false);
  65              if (!empty($grades[$this->userid]) && $grades[$this->userid]->is_passed($item)) {
  66                  return true;
  67              }
  68          }
  69  
  70          // If a passing grade is required and exhausting all available attempts is not accepted for completion,
  71          // then this quiz is not complete.
  72          if (empty($completionpassorattempts['completionattemptsexhausted'])) {
  73              return false;
  74          }
  75  
  76          // Check if all attempts are used up.
  77          $attempts = quiz_get_user_attempts($this->cm->instance, $this->userid, 'finished', true);
  78          if (!$attempts) {
  79              return false;
  80          }
  81          $lastfinishedattempt = end($attempts);
  82          $context = context_module::instance($this->cm->id);
  83          $quizobj = quiz::create($this->cm->instance, $this->userid);
  84          $accessmanager = new quiz_access_manager(
  85              $quizobj,
  86              time(),
  87              has_capability('mod/quiz:ignoretimelimits', $context, $this->userid, false)
  88          );
  89  
  90          return $accessmanager->is_finished(count($attempts), $lastfinishedattempt);
  91      }
  92  
  93      /**
  94       * Check minimum attempts requirement for completion.
  95       *
  96       * @return bool True if minimum attempts requirement is disabled or met.
  97       */
  98      protected function check_min_attempts() {
  99          $minattempts = $this->cm->customdata['customcompletionrules']['completionminattempts'];
 100          if (!$minattempts) {
 101              return true;
 102          }
 103  
 104          // Check if the user has done enough attempts.
 105          $attempts = quiz_get_user_attempts($this->cm->instance, $this->userid, 'finished', true);
 106          return $minattempts <= count($attempts);
 107      }
 108  
 109      /**
 110       * Fetches the completion state for a given completion rule.
 111       *
 112       * @param string $rule The completion rule.
 113       * @return int The completion state.
 114       */
 115      public function get_state(string $rule): int {
 116          $this->validate_rule($rule);
 117  
 118          switch ($rule) {
 119              case 'completionpassorattemptsexhausted':
 120                  $status = static::check_passing_grade_or_all_attempts();
 121                  break;
 122              case 'completionminattempts':
 123                  $status = static::check_min_attempts();
 124                  break;
 125          }
 126  
 127          return empty($status) ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
 128      }
 129  
 130      /**
 131       * Fetch the list of custom completion rules that this module defines.
 132       *
 133       * @return array
 134       */
 135      public static function get_defined_custom_rules(): array {
 136          return [
 137              'completionpassorattemptsexhausted',
 138              'completionminattempts',
 139          ];
 140      }
 141  
 142      /**
 143       * Returns an associative array of the descriptions of custom completion rules.
 144       *
 145       * @return array
 146       */
 147      public function get_custom_rule_descriptions(): array {
 148          $minattempts = $this->cm->customdata['customcompletionrules']['completionminattempts'] ?? 0;
 149  
 150          $completionpassorattempts = $this->cm->customdata['customcompletionrules']['completionpassorattemptsexhausted'] ?? [];
 151          if (!empty($completionpassorattempts['completionattemptsexhausted'])) {
 152              $passorallattemptslabel = get_string('completiondetail:passorexhaust', 'mod_quiz');
 153          } else {
 154              $passorallattemptslabel = get_string('completiondetail:passgrade', 'mod_quiz');
 155          }
 156  
 157          return [
 158              'completionpassorattemptsexhausted' => $passorallattemptslabel,
 159              'completionminattempts' => get_string('completiondetail:minattempts', 'mod_quiz', $minattempts),
 160          ];
 161      }
 162  
 163      /**
 164       * Returns an array of all completion rules, in the order they should be displayed to users.
 165       *
 166       * @return array
 167       */
 168      public function get_sort_order(): array {
 169          return [
 170              'completionview',
 171              'completionminattempts',
 172              'completionusegrade',
 173              'completionpassorattemptsexhausted',
 174          ];
 175      }
 176  }