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 402] [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  /**
  18   * List of deprecated mod_quiz functions.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Internal function used in quiz_get_completion_state. Check passing grade (or no attempts left) requirement for completion.
  27   *
  28   * @deprecated since Moodle 3.11
  29   * @todo MDL-71196 Final deprecation in Moodle 4.3
  30   * @see \mod_quiz\completion\custom_completion
  31   * @param stdClass $course
  32   * @param cm_info|stdClass $cm
  33   * @param int $userid
  34   * @param stdClass $quiz
  35   * @return bool True if the passing grade (or no attempts left) requirement is disabled or met.
  36   * @throws coding_exception
  37   */
  38  function quiz_completion_check_passing_grade_or_all_attempts($course, $cm, $userid, $quiz) {
  39      global $CFG;
  40  
  41      debugging('quiz_completion_check_passing_grade_or_all_attempts has been deprecated.', DEBUG_DEVELOPER);
  42  
  43      if (!$cm->completionpassgrade) {
  44          return true;
  45      }
  46  
  47      // Check for passing grade.
  48      require_once($CFG->libdir . '/gradelib.php');
  49      $item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod',
  50              'itemmodule' => 'quiz', 'iteminstance' => $cm->instance, 'outcomeid' => null));
  51      if ($item) {
  52          $grades = grade_grade::fetch_users_grades($item, array($userid), false);
  53          if (!empty($grades[$userid]) && $grades[$userid]->is_passed($item)) {
  54              return true;
  55          }
  56      }
  57  
  58      // If a passing grade is required and exhausting all available attempts is not accepted for completion,
  59      // then this quiz is not complete.
  60      if (!$quiz->completionattemptsexhausted) {
  61          return false;
  62      }
  63  
  64      // Check if all attempts are used up.
  65      $attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished', true);
  66      if (!$attempts) {
  67          return false;
  68      }
  69      $lastfinishedattempt = end($attempts);
  70      $context = context_module::instance($cm->id);
  71      $quizobj = quiz::create($quiz->id, $userid);
  72      $accessmanager = new quiz_access_manager($quizobj, time(),
  73              has_capability('mod/quiz:ignoretimelimits', $context, $userid, false));
  74  
  75      return $accessmanager->is_finished(count($attempts), $lastfinishedattempt);
  76  }
  77  
  78  /**
  79   * Internal function used in quiz_get_completion_state. Check minimum attempts requirement for completion.
  80   *
  81   * @deprecated since Moodle 3.11
  82   * @todo MDL-71196 Final deprecation in Moodle 4.3
  83   * @see \mod_quiz\completion\custom_completion
  84   * @param int $userid
  85   * @param stdClass $quiz
  86   * @return bool True if minimum attempts requirement is disabled or met.
  87   */
  88  function quiz_completion_check_min_attempts($userid, $quiz) {
  89  
  90      debugging('quiz_completion_check_min_attempts has been deprecated.', DEBUG_DEVELOPER);
  91  
  92      if (empty($quiz->completionminattempts)) {
  93          return true;
  94      }
  95  
  96      // Check if the user has done enough attempts.
  97      $attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished', true);
  98      return $quiz->completionminattempts <= count($attempts);
  99  }
 100  
 101  /**
 102   * Obtains the automatic completion state for this quiz on any conditions
 103   * in quiz settings, such as if all attempts are used or a certain grade is achieved.
 104   *
 105   * @deprecated since Moodle 3.11
 106   * @todo MDL-71196 Final deprecation in Moodle 4.3
 107   * @see \mod_quiz\completion\custom_completion
 108   * @param stdClass $course Course
 109   * @param cm_info|stdClass $cm Course-module
 110   * @param int $userid User ID
 111   * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 112   * @return bool True if completed, false if not. (If no conditions, then return
 113   *   value depends on comparison type)
 114   */
 115  function quiz_get_completion_state($course, $cm, $userid, $type) {
 116      global $DB;
 117  
 118      // No need to call debugging here. Deprecation debugging notice already being called in \completion_info::internal_get_state().
 119  
 120      $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
 121      if (!$quiz->completionattemptsexhausted && !$cm->completionpassgrade && !$quiz->completionminattempts) {
 122          return $type;
 123      }
 124  
 125      if (!quiz_completion_check_passing_grade_or_all_attempts($course, $cm, $userid, $quiz)) {
 126          return false;
 127      }
 128  
 129      if (!quiz_completion_check_min_attempts($userid, $quiz)) {
 130          return false;
 131      }
 132  
 133      return true;
 134  }