| 1 | 
           efrain | 
           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 | 
            * Library of functions for the quiz module.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * This contains functions that are called also from outside the quiz module
  | 
        
        
            | 
            | 
           21 | 
            * Functions that are only called by the quiz module itself are in {@link locallib.php}
  | 
        
        
            | 
            | 
           22 | 
            *
  | 
        
        
            | 
            | 
           23 | 
            * @package    mod_quiz
  | 
        
        
            | 
            | 
           24 | 
            * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
  | 
        
        
            | 
            | 
           25 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           28 | 
           use core_question\local\bank\question_bank_helper;
  | 
        
        
           | 1 | 
           efrain | 
           29 | 
           use qbank_managecategories\helper;
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           use mod_quiz\access_manager;
  | 
        
        
            | 
            | 
           34 | 
           use mod_quiz\grade_calculator;
  | 
        
        
            | 
            | 
           35 | 
           use mod_quiz\question\bank\custom_view;
  | 
        
        
            | 
            | 
           36 | 
           use mod_quiz\question\bank\qbank_helper;
  | 
        
        
            | 
            | 
           37 | 
           use mod_quiz\question\display_options;
  | 
        
        
            | 
            | 
           38 | 
           use mod_quiz\question\qubaids_for_quiz;
  | 
        
        
            | 
            | 
           39 | 
           use mod_quiz\question\qubaids_for_users_attempts;
  | 
        
        
            | 
            | 
           40 | 
           use core_question\statistics\questions\all_calculated_for_qubaid_condition;
  | 
        
        
            | 
            | 
           41 | 
           use mod_quiz\local\override_cache;
  | 
        
        
            | 
            | 
           42 | 
           use mod_quiz\quiz_attempt;
  | 
        
        
            | 
            | 
           43 | 
           use mod_quiz\quiz_settings;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           require_once($CFG->dirroot . '/calendar/lib.php');
  | 
        
        
            | 
            | 
           46 | 
           require_once($CFG->dirroot . '/question/editlib.php');
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
           /**#@+
  | 
        
        
            | 
            | 
           49 | 
            * Option controlling what options are offered on the quiz settings form.
  | 
        
        
            | 
            | 
           50 | 
            */
  | 
        
        
            | 
            | 
           51 | 
           define('QUIZ_MAX_ATTEMPT_OPTION', 10);
  | 
        
        
            | 
            | 
           52 | 
           define('QUIZ_MAX_QPP_OPTION', 50);
  | 
        
        
            | 
            | 
           53 | 
           define('QUIZ_MAX_DECIMAL_OPTION', 5);
  | 
        
        
            | 
            | 
           54 | 
           define('QUIZ_MAX_Q_DECIMAL_OPTION', 7);
  | 
        
        
            | 
            | 
           55 | 
           /**#@-*/
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
           /**#@+
  | 
        
        
            | 
            | 
           58 | 
            * Options determining how the grades from individual attempts are combined to give
  | 
        
        
            | 
            | 
           59 | 
            * the overall grade for a user
  | 
        
        
            | 
            | 
           60 | 
            */
  | 
        
        
            | 
            | 
           61 | 
           define('QUIZ_GRADEHIGHEST', '1');
  | 
        
        
            | 
            | 
           62 | 
           define('QUIZ_GRADEAVERAGE', '2');
  | 
        
        
            | 
            | 
           63 | 
           define('QUIZ_ATTEMPTFIRST', '3');
  | 
        
        
            | 
            | 
           64 | 
           define('QUIZ_ATTEMPTLAST',  '4');
  | 
        
        
            | 
            | 
           65 | 
           /**#@-*/
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
           /**
  | 
        
        
            | 
            | 
           68 | 
            * @var int If start and end date for the quiz are more than this many seconds apart
  | 
        
        
            | 
            | 
           69 | 
            * they will be represented by two separate events in the calendar
  | 
        
        
            | 
            | 
           70 | 
            */
  | 
        
        
            | 
            | 
           71 | 
           define('QUIZ_MAX_EVENT_LENGTH', 5*24*60*60); // 5 days.
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
           /**#@+
  | 
        
        
            | 
            | 
           74 | 
            * Options for navigation method within quizzes.
  | 
        
        
            | 
            | 
           75 | 
            */
  | 
        
        
            | 
            | 
           76 | 
           define('QUIZ_NAVMETHOD_FREE', 'free');
  | 
        
        
            | 
            | 
           77 | 
           define('QUIZ_NAVMETHOD_SEQ',  'sequential');
  | 
        
        
            | 
            | 
           78 | 
           /**#@-*/
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
           /**
  | 
        
        
            | 
            | 
           81 | 
            * Event types.
  | 
        
        
            | 
            | 
           82 | 
            */
  | 
        
        
            | 
            | 
           83 | 
           define('QUIZ_EVENT_TYPE_OPEN', 'open');
  | 
        
        
            | 
            | 
           84 | 
           define('QUIZ_EVENT_TYPE_CLOSE', 'close');
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
           require_once(__DIR__ . '/deprecatedlib.php');
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
           /**
  | 
        
        
            | 
            | 
           89 | 
            * Given an object containing all the necessary data,
  | 
        
        
            | 
            | 
           90 | 
            * (defined by the form in mod_form.php) this function
  | 
        
        
            | 
            | 
           91 | 
            * will create a new instance and return the id number
  | 
        
        
            | 
            | 
           92 | 
            * of the new instance.
  | 
        
        
            | 
            | 
           93 | 
            *
  | 
        
        
            | 
            | 
           94 | 
            * @param stdClass $quiz the data that came from the form.
  | 
        
        
            | 
            | 
           95 | 
            * @return mixed the id of the new instance on success,
  | 
        
        
            | 
            | 
           96 | 
            *          false or a string error message on failure.
  | 
        
        
            | 
            | 
           97 | 
            */
  | 
        
        
            | 
            | 
           98 | 
           function quiz_add_instance($quiz) {
  | 
        
        
            | 
            | 
           99 | 
               global $DB;
  | 
        
        
            | 
            | 
           100 | 
               $cmid = $quiz->coursemodule;
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
               // Process the options from the form.
  | 
        
        
            | 
            | 
           103 | 
               $quiz->timecreated = time();
  | 
        
        
            | 
            | 
           104 | 
               $result = quiz_process_options($quiz);
  | 
        
        
            | 
            | 
           105 | 
               if ($result && is_string($result)) {
  | 
        
        
            | 
            | 
           106 | 
                   return $result;
  | 
        
        
            | 
            | 
           107 | 
               }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
               // Try to store it in the database.
  | 
        
        
            | 
            | 
           110 | 
               $quiz->id = $DB->insert_record('quiz', $quiz);
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
               // Create the first section for this quiz.
  | 
        
        
            | 
            | 
           113 | 
               $DB->insert_record('quiz_sections', ['quizid' => $quiz->id,
  | 
        
        
            | 
            | 
           114 | 
                       'firstslot' => 1, 'heading' => '', 'shufflequestions' => 0]);
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
               // Do the processing required after an add or an update.
  | 
        
        
            | 
            | 
           117 | 
               quiz_after_add_or_update($quiz);
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
               return $quiz->id;
  | 
        
        
            | 
            | 
           120 | 
           }
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
           /**
  | 
        
        
            | 
            | 
           123 | 
            * Given an object containing all the necessary data,
  | 
        
        
            | 
            | 
           124 | 
            * (defined by the form in mod_form.php) this function
  | 
        
        
            | 
            | 
           125 | 
            * will update an existing instance with new data.
  | 
        
        
            | 
            | 
           126 | 
            *
  | 
        
        
            | 
            | 
           127 | 
            * @param stdClass $quiz the data that came from the form.
  | 
        
        
            | 
            | 
           128 | 
            * @param stdClass $mform no longer used.
  | 
        
        
            | 
            | 
           129 | 
            * @return mixed true on success, false or a string error message on failure.
  | 
        
        
            | 
            | 
           130 | 
            */
  | 
        
        
            | 
            | 
           131 | 
           function quiz_update_instance($quiz, $mform) {
  | 
        
        
            | 
            | 
           132 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           133 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
               // Process the options from the form.
  | 
        
        
            | 
            | 
           136 | 
               $result = quiz_process_options($quiz);
  | 
        
        
            | 
            | 
           137 | 
               if ($result && is_string($result)) {
  | 
        
        
            | 
            | 
           138 | 
                   return $result;
  | 
        
        
            | 
            | 
           139 | 
               }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
               // Get the current value, so we can see what changed.
  | 
        
        
            | 
            | 
           142 | 
               $oldquiz = $DB->get_record('quiz', ['id' => $quiz->instance]);
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
               // We need two values from the existing DB record that are not in the form,
  | 
        
        
            | 
            | 
           145 | 
               // in some of the function calls below.
  | 
        
        
            | 
            | 
           146 | 
               $quiz->sumgrades = $oldquiz->sumgrades;
  | 
        
        
            | 
            | 
           147 | 
               $quiz->grade     = $oldquiz->grade;
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
               // Update the database.
  | 
        
        
            | 
            | 
           150 | 
               $quiz->id = $quiz->instance;
  | 
        
        
            | 
            | 
           151 | 
               $DB->update_record('quiz', $quiz);
  | 
        
        
            | 
            | 
           152 | 
              | 
        
        
            | 
            | 
           153 | 
               // Do the processing required after an add or an update.
  | 
        
        
            | 
            | 
           154 | 
               quiz_after_add_or_update($quiz);
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
               if ($oldquiz->grademethod != $quiz->grademethod) {
  | 
        
        
            | 
            | 
           157 | 
                   $gradecalculator = quiz_settings::create($quiz->id)->get_grade_calculator();
  | 
        
        
            | 
            | 
           158 | 
                   $gradecalculator->recompute_all_final_grades();
  | 
        
        
            | 
            | 
           159 | 
                   quiz_update_grades($quiz);
  | 
        
        
            | 
            | 
           160 | 
               }
  | 
        
        
            | 
            | 
           161 | 
              | 
        
        
            | 
            | 
           162 | 
               $quizdateschanged = $oldquiz->timelimit   != $quiz->timelimit
  | 
        
        
            | 
            | 
           163 | 
                                || $oldquiz->timeclose   != $quiz->timeclose
  | 
        
        
            | 
            | 
           164 | 
                                || $oldquiz->graceperiod != $quiz->graceperiod;
  | 
        
        
            | 
            | 
           165 | 
               if ($quizdateschanged) {
  | 
        
        
            | 
            | 
           166 | 
                   quiz_update_open_attempts(['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           167 | 
               }
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
               // Delete any previous preview attempts.
  | 
        
        
            | 
            | 
           170 | 
               quiz_delete_previews($quiz);
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
               // Repaginate, if asked to.
  | 
        
        
            | 
            | 
           173 | 
               if (!empty($quiz->repaginatenow) && !quiz_has_attempts($quiz->id)) {
  | 
        
        
            | 
            | 
           174 | 
                   quiz_repaginate_questions($quiz->id, $quiz->questionsperpage);
  | 
        
        
            | 
            | 
           175 | 
               }
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
               return true;
  | 
        
        
            | 
            | 
           178 | 
           }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
           /**
  | 
        
        
            | 
            | 
           181 | 
            * Given an ID of an instance of this module,
  | 
        
        
            | 
            | 
           182 | 
            * this function will permanently delete the instance
  | 
        
        
            | 
            | 
           183 | 
            * and any data that depends on it.
  | 
        
        
            | 
            | 
           184 | 
            *
  | 
        
        
            | 
            | 
           185 | 
            * @param int $id the id of the quiz to delete.
  | 
        
        
            | 
            | 
           186 | 
            * @return bool success or failure.
  | 
        
        
            | 
            | 
           187 | 
            */
  | 
        
        
            | 
            | 
           188 | 
           function quiz_delete_instance($id) {
  | 
        
        
            | 
            | 
           189 | 
               global $DB;
  | 
        
        
            | 
            | 
           190 | 
              | 
        
        
            | 
            | 
           191 | 
               $quiz = $DB->get_record('quiz', ['id' => $id], '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           192 | 
              | 
        
        
            | 
            | 
           193 | 
               quiz_delete_all_attempts($quiz);
  | 
        
        
            | 
            | 
           194 | 
              | 
        
        
            | 
            | 
           195 | 
               // Delete all overrides, and for performance do not log or check permissions.
  | 
        
        
            | 
            | 
           196 | 
               $quizobj = quiz_settings::create($quiz->id);
  | 
        
        
            | 
            | 
           197 | 
               $quizobj->get_override_manager()->delete_all_overrides(shouldlog: false);
  | 
        
        
            | 
            | 
           198 | 
              | 
        
        
            | 
            | 
           199 | 
               quiz_delete_references($quiz->id);
  | 
        
        
            | 
            | 
           200 | 
              | 
        
        
            | 
            | 
           201 | 
               // We need to do the following deletes before we try and delete randoms, otherwise they would still be 'in use'.
  | 
        
        
            | 
            | 
           202 | 
               $DB->delete_records('quiz_slots', ['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           203 | 
               $DB->delete_records('quiz_sections', ['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           204 | 
              | 
        
        
            | 
            | 
           205 | 
               $DB->delete_records('quiz_feedback', ['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
               access_manager::delete_settings($quiz);
  | 
        
        
            | 
            | 
           208 | 
              | 
        
        
            | 
            | 
           209 | 
               $events = $DB->get_records('event', ['modulename' => 'quiz', 'instance' => $quiz->id]);
  | 
        
        
            | 
            | 
           210 | 
               foreach ($events as $event) {
  | 
        
        
            | 
            | 
           211 | 
                   $event = calendar_event::load($event);
  | 
        
        
            | 
            | 
           212 | 
                   $event->delete();
  | 
        
        
            | 
            | 
           213 | 
               }
  | 
        
        
            | 
            | 
           214 | 
              | 
        
        
            | 
            | 
           215 | 
               quiz_grade_item_delete($quiz);
  | 
        
        
            | 
            | 
           216 | 
               // We must delete the module record after we delete the grade item.
  | 
        
        
            | 
            | 
           217 | 
               $DB->delete_records('quiz', ['id' => $quiz->id]);
  | 
        
        
            | 
            | 
           218 | 
              | 
        
        
            | 
            | 
           219 | 
               return true;
  | 
        
        
            | 
            | 
           220 | 
           }
  | 
        
        
            | 
            | 
           221 | 
              | 
        
        
            | 
            | 
           222 | 
           /**
  | 
        
        
            | 
            | 
           223 | 
            * Updates a quiz object with override information for a user.
  | 
        
        
            | 
            | 
           224 | 
            *
  | 
        
        
            | 
            | 
           225 | 
            * Algorithm:  For each quiz setting, if there is a matching user-specific override,
  | 
        
        
            | 
            | 
           226 | 
            *   then use that otherwise, if there are group-specific overrides, return the most
  | 
        
        
            | 
            | 
           227 | 
            *   lenient combination of them.  If neither applies, leave the quiz setting unchanged.
  | 
        
        
            | 
            | 
           228 | 
            *
  | 
        
        
            | 
            | 
           229 | 
            *   Special case: if there is more than one password that applies to the user, then
  | 
        
        
            | 
            | 
           230 | 
            *   quiz->extrapasswords will contain an array of strings giving the remaining
  | 
        
        
            | 
            | 
           231 | 
            *   passwords.
  | 
        
        
            | 
            | 
           232 | 
            *
  | 
        
        
            | 
            | 
           233 | 
            * @param stdClass $quiz The quiz object.
  | 
        
        
            | 
            | 
           234 | 
            * @param int $userid The userid.
  | 
        
        
            | 
            | 
           235 | 
            * @return stdClass $quiz The updated quiz object.
  | 
        
        
            | 
            | 
           236 | 
            */
  | 
        
        
            | 
            | 
           237 | 
           function quiz_update_effective_access($quiz, $userid) {
  | 
        
        
            | 
            | 
           238 | 
               global $DB;
  | 
        
        
            | 
            | 
           239 | 
              | 
        
        
            | 
            | 
           240 | 
               // Check for user override.
  | 
        
        
            | 
            | 
           241 | 
               $override = $DB->get_record('quiz_overrides', ['quiz' => $quiz->id, 'userid' => $userid]);
  | 
        
        
            | 
            | 
           242 | 
              | 
        
        
            | 
            | 
           243 | 
               if (!$override) {
  | 
        
        
            | 
            | 
           244 | 
                   $override = new stdClass();
  | 
        
        
            | 
            | 
           245 | 
                   $override->timeopen = null;
  | 
        
        
            | 
            | 
           246 | 
                   $override->timeclose = null;
  | 
        
        
            | 
            | 
           247 | 
                   $override->timelimit = null;
  | 
        
        
            | 
            | 
           248 | 
                   $override->attempts = null;
  | 
        
        
            | 
            | 
           249 | 
                   $override->password = null;
  | 
        
        
            | 
            | 
           250 | 
               }
  | 
        
        
            | 
            | 
           251 | 
              | 
        
        
            | 
            | 
           252 | 
               // Check for group overrides.
  | 
        
        
            | 
            | 
           253 | 
               $groupings = groups_get_user_groups($quiz->course, $userid);
  | 
        
        
            | 
            | 
           254 | 
              | 
        
        
            | 
            | 
           255 | 
               if (!empty($groupings[0])) {
  | 
        
        
            | 
            | 
           256 | 
                   // Select all overrides that apply to the User's groups.
  | 
        
        
           | 1441 | 
           ariadna | 
           257 | 
                   [$extra, $params] = $DB->get_in_or_equal(array_values($groupings[0]));
  | 
        
        
           | 1 | 
           efrain | 
           258 | 
                   $sql = "SELECT * FROM {quiz_overrides}
  | 
        
        
            | 
            | 
           259 | 
                           WHERE groupid $extra AND quiz = ?";
  | 
        
        
            | 
            | 
           260 | 
                   $params[] = $quiz->id;
  | 
        
        
            | 
            | 
           261 | 
                   $records = $DB->get_records_sql($sql, $params);
  | 
        
        
            | 
            | 
           262 | 
              | 
        
        
            | 
            | 
           263 | 
                   // Combine the overrides.
  | 
        
        
            | 
            | 
           264 | 
                   $opens = [];
  | 
        
        
            | 
            | 
           265 | 
                   $closes = [];
  | 
        
        
            | 
            | 
           266 | 
                   $limits = [];
  | 
        
        
            | 
            | 
           267 | 
                   $attempts = [];
  | 
        
        
            | 
            | 
           268 | 
                   $passwords = [];
  | 
        
        
            | 
            | 
           269 | 
              | 
        
        
            | 
            | 
           270 | 
                   foreach ($records as $gpoverride) {
  | 
        
        
            | 
            | 
           271 | 
                       if (isset($gpoverride->timeopen)) {
  | 
        
        
            | 
            | 
           272 | 
                           $opens[] = $gpoverride->timeopen;
  | 
        
        
            | 
            | 
           273 | 
                       }
  | 
        
        
            | 
            | 
           274 | 
                       if (isset($gpoverride->timeclose)) {
  | 
        
        
            | 
            | 
           275 | 
                           $closes[] = $gpoverride->timeclose;
  | 
        
        
            | 
            | 
           276 | 
                       }
  | 
        
        
            | 
            | 
           277 | 
                       if (isset($gpoverride->timelimit)) {
  | 
        
        
            | 
            | 
           278 | 
                           $limits[] = $gpoverride->timelimit;
  | 
        
        
            | 
            | 
           279 | 
                       }
  | 
        
        
            | 
            | 
           280 | 
                       if (isset($gpoverride->attempts)) {
  | 
        
        
            | 
            | 
           281 | 
                           $attempts[] = $gpoverride->attempts;
  | 
        
        
            | 
            | 
           282 | 
                       }
  | 
        
        
            | 
            | 
           283 | 
                       if (isset($gpoverride->password)) {
  | 
        
        
            | 
            | 
           284 | 
                           $passwords[] = $gpoverride->password;
  | 
        
        
            | 
            | 
           285 | 
                       }
  | 
        
        
            | 
            | 
           286 | 
                   }
  | 
        
        
            | 
            | 
           287 | 
                   // If there is a user override for a setting, ignore the group override.
  | 
        
        
            | 
            | 
           288 | 
                   if (is_null($override->timeopen) && count($opens)) {
  | 
        
        
            | 
            | 
           289 | 
                       $override->timeopen = min($opens);
  | 
        
        
            | 
            | 
           290 | 
                   }
  | 
        
        
            | 
            | 
           291 | 
                   if (is_null($override->timeclose) && count($closes)) {
  | 
        
        
            | 
            | 
           292 | 
                       if (in_array(0, $closes)) {
  | 
        
        
            | 
            | 
           293 | 
                           $override->timeclose = 0;
  | 
        
        
            | 
            | 
           294 | 
                       } else {
  | 
        
        
            | 
            | 
           295 | 
                           $override->timeclose = max($closes);
  | 
        
        
            | 
            | 
           296 | 
                       }
  | 
        
        
            | 
            | 
           297 | 
                   }
  | 
        
        
            | 
            | 
           298 | 
                   if (is_null($override->timelimit) && count($limits)) {
  | 
        
        
            | 
            | 
           299 | 
                       if (in_array(0, $limits)) {
  | 
        
        
            | 
            | 
           300 | 
                           $override->timelimit = 0;
  | 
        
        
            | 
            | 
           301 | 
                       } else {
  | 
        
        
            | 
            | 
           302 | 
                           $override->timelimit = max($limits);
  | 
        
        
            | 
            | 
           303 | 
                       }
  | 
        
        
            | 
            | 
           304 | 
                   }
  | 
        
        
            | 
            | 
           305 | 
                   if (is_null($override->attempts) && count($attempts)) {
  | 
        
        
            | 
            | 
           306 | 
                       if (in_array(0, $attempts)) {
  | 
        
        
            | 
            | 
           307 | 
                           $override->attempts = 0;
  | 
        
        
            | 
            | 
           308 | 
                       } else {
  | 
        
        
            | 
            | 
           309 | 
                           $override->attempts = max($attempts);
  | 
        
        
            | 
            | 
           310 | 
                       }
  | 
        
        
            | 
            | 
           311 | 
                   }
  | 
        
        
            | 
            | 
           312 | 
                   if (is_null($override->password) && count($passwords)) {
  | 
        
        
            | 
            | 
           313 | 
                       $override->password = array_shift($passwords);
  | 
        
        
            | 
            | 
           314 | 
                       if (count($passwords)) {
  | 
        
        
            | 
            | 
           315 | 
                           $override->extrapasswords = $passwords;
  | 
        
        
            | 
            | 
           316 | 
                       }
  | 
        
        
            | 
            | 
           317 | 
                   }
  | 
        
        
            | 
            | 
           318 | 
              | 
        
        
            | 
            | 
           319 | 
               }
  | 
        
        
            | 
            | 
           320 | 
              | 
        
        
            | 
            | 
           321 | 
               // Merge with quiz defaults.
  | 
        
        
            | 
            | 
           322 | 
               $keys = ['timeopen', 'timeclose', 'timelimit', 'attempts', 'password', 'extrapasswords'];
  | 
        
        
            | 
            | 
           323 | 
               foreach ($keys as $key) {
  | 
        
        
            | 
            | 
           324 | 
                   if (isset($override->{$key})) {
  | 
        
        
            | 
            | 
           325 | 
                       $quiz->{$key} = $override->{$key};
  | 
        
        
            | 
            | 
           326 | 
                   }
  | 
        
        
            | 
            | 
           327 | 
               }
  | 
        
        
            | 
            | 
           328 | 
              | 
        
        
            | 
            | 
           329 | 
               return $quiz;
  | 
        
        
            | 
            | 
           330 | 
           }
  | 
        
        
            | 
            | 
           331 | 
              | 
        
        
            | 
            | 
           332 | 
           /**
  | 
        
        
            | 
            | 
           333 | 
            * Delete all the attempts belonging to a quiz.
  | 
        
        
            | 
            | 
           334 | 
            *
  | 
        
        
            | 
            | 
           335 | 
            * @param stdClass $quiz The quiz object.
  | 
        
        
            | 
            | 
           336 | 
            */
  | 
        
        
            | 
            | 
           337 | 
           function quiz_delete_all_attempts($quiz) {
  | 
        
        
            | 
            | 
           338 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           339 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           340 | 
               question_engine::delete_questions_usage_by_activities(new qubaids_for_quiz($quiz->id));
  | 
        
        
            | 
            | 
           341 | 
               $DB->delete_records('quiz_attempts', ['quiz' => $quiz->id]);
  | 
        
        
            | 
            | 
           342 | 
               $DB->delete_records('quiz_grades', ['quiz' => $quiz->id]);
  | 
        
        
            | 
            | 
           343 | 
           }
  | 
        
        
            | 
            | 
           344 | 
              | 
        
        
            | 
            | 
           345 | 
           /**
  | 
        
        
            | 
            | 
           346 | 
            * Delete all the attempts belonging to a user in a particular quiz.
  | 
        
        
            | 
            | 
           347 | 
            *
  | 
        
        
            | 
            | 
           348 | 
            * @param \mod_quiz\quiz_settings $quiz The quiz object.
  | 
        
        
            | 
            | 
           349 | 
            * @param stdClass $user The user object.
  | 
        
        
            | 
            | 
           350 | 
            */
  | 
        
        
            | 
            | 
           351 | 
           function quiz_delete_user_attempts($quiz, $user) {
  | 
        
        
            | 
            | 
           352 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           353 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           354 | 
               question_engine::delete_questions_usage_by_activities(new qubaids_for_users_attempts(
  | 
        
        
            | 
            | 
           355 | 
                       $quiz->get_quizid(), $user->id, 'all'));
  | 
        
        
            | 
            | 
           356 | 
               $params = [
  | 
        
        
            | 
            | 
           357 | 
                   'quiz' => $quiz->get_quizid(),
  | 
        
        
            | 
            | 
           358 | 
                   'userid' => $user->id,
  | 
        
        
            | 
            | 
           359 | 
               ];
  | 
        
        
            | 
            | 
           360 | 
               $DB->delete_records('quiz_attempts', $params);
  | 
        
        
            | 
            | 
           361 | 
               $DB->delete_records('quiz_grades', $params);
  | 
        
        
            | 
            | 
           362 | 
           }
  | 
        
        
            | 
            | 
           363 | 
              | 
        
        
            | 
            | 
           364 | 
           /**
  | 
        
        
            | 
            | 
           365 | 
            * Get the best current grade for a particular user in a quiz.
  | 
        
        
            | 
            | 
           366 | 
            *
  | 
        
        
            | 
            | 
           367 | 
            * @param stdClass $quiz the quiz settings.
  | 
        
        
            | 
            | 
           368 | 
            * @param int $userid the id of the user.
  | 
        
        
            | 
            | 
           369 | 
            * @return float the user's current grade for this quiz, or null if this user does
  | 
        
        
            | 
            | 
           370 | 
            * not have a grade on this quiz.
  | 
        
        
            | 
            | 
           371 | 
            */
  | 
        
        
            | 
            | 
           372 | 
           function quiz_get_best_grade($quiz, $userid) {
  | 
        
        
            | 
            | 
           373 | 
               global $DB;
  | 
        
        
            | 
            | 
           374 | 
               $grade = $DB->get_field('quiz_grades', 'grade',
  | 
        
        
            | 
            | 
           375 | 
                       ['quiz' => $quiz->id, 'userid' => $userid]);
  | 
        
        
            | 
            | 
           376 | 
              | 
        
        
            | 
            | 
           377 | 
               // Need to detect errors/no result, without catching 0 grades.
  | 
        
        
            | 
            | 
           378 | 
               if ($grade === false) {
  | 
        
        
            | 
            | 
           379 | 
                   return null;
  | 
        
        
            | 
            | 
           380 | 
               }
  | 
        
        
            | 
            | 
           381 | 
              | 
        
        
            | 
            | 
           382 | 
               return $grade + 0; // Convert to number.
  | 
        
        
            | 
            | 
           383 | 
           }
  | 
        
        
            | 
            | 
           384 | 
              | 
        
        
            | 
            | 
           385 | 
           /**
  | 
        
        
            | 
            | 
           386 | 
            * Is this a graded quiz? If this method returns true, you can assume that
  | 
        
        
            | 
            | 
           387 | 
            * $quiz->grade and $quiz->sumgrades are non-zero (for example, if you want to
  | 
        
        
            | 
            | 
           388 | 
            * divide by them).
  | 
        
        
            | 
            | 
           389 | 
            *
  | 
        
        
            | 
            | 
           390 | 
            * @param stdClass $quiz a row from the quiz table.
  | 
        
        
            | 
            | 
           391 | 
            * @return bool whether this is a graded quiz.
  | 
        
        
            | 
            | 
           392 | 
            */
  | 
        
        
            | 
            | 
           393 | 
           function quiz_has_grades($quiz) {
  | 
        
        
            | 
            | 
           394 | 
               return $quiz->grade >= grade_calculator::ALMOST_ZERO && $quiz->sumgrades >= grade_calculator::ALMOST_ZERO;
  | 
        
        
            | 
            | 
           395 | 
           }
  | 
        
        
            | 
            | 
           396 | 
              | 
        
        
            | 
            | 
           397 | 
           /**
  | 
        
        
            | 
            | 
           398 | 
            * Does this quiz allow multiple tries?
  | 
        
        
            | 
            | 
           399 | 
            *
  | 
        
        
            | 
            | 
           400 | 
            * @return bool
  | 
        
        
            | 
            | 
           401 | 
            */
  | 
        
        
            | 
            | 
           402 | 
           function quiz_allows_multiple_tries($quiz) {
  | 
        
        
            | 
            | 
           403 | 
               $bt = question_engine::get_behaviour_type($quiz->preferredbehaviour);
  | 
        
        
            | 
            | 
           404 | 
               return $bt->allows_multiple_submitted_responses();
  | 
        
        
            | 
            | 
           405 | 
           }
  | 
        
        
            | 
            | 
           406 | 
              | 
        
        
            | 
            | 
           407 | 
           /**
  | 
        
        
            | 
            | 
           408 | 
            * Return a small object with summary information about what a
  | 
        
        
            | 
            | 
           409 | 
            * user has done with a given particular instance of this module
  | 
        
        
            | 
            | 
           410 | 
            * Used for user activity reports.
  | 
        
        
            | 
            | 
           411 | 
            * $return->time = the time they did it
  | 
        
        
            | 
            | 
           412 | 
            * $return->info = a short text description
  | 
        
        
            | 
            | 
           413 | 
            *
  | 
        
        
            | 
            | 
           414 | 
            * @param stdClass $course
  | 
        
        
            | 
            | 
           415 | 
            * @param stdClass $user
  | 
        
        
            | 
            | 
           416 | 
            * @param stdClass $mod
  | 
        
        
            | 
            | 
           417 | 
            * @param stdClass $quiz
  | 
        
        
            | 
            | 
           418 | 
            * @return stdClass|null
  | 
        
        
            | 
            | 
           419 | 
            */
  | 
        
        
            | 
            | 
           420 | 
           function quiz_user_outline($course, $user, $mod, $quiz) {
  | 
        
        
            | 
            | 
           421 | 
               global $DB, $CFG;
  | 
        
        
            | 
            | 
           422 | 
               require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           423 | 
               $grades = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $user->id);
  | 
        
        
            | 
            | 
           424 | 
              | 
        
        
            | 
            | 
           425 | 
               if (empty($grades->items[0]->grades)) {
  | 
        
        
            | 
            | 
           426 | 
                   return null;
  | 
        
        
            | 
            | 
           427 | 
               } else {
  | 
        
        
            | 
            | 
           428 | 
                   $grade = reset($grades->items[0]->grades);
  | 
        
        
            | 
            | 
           429 | 
               }
  | 
        
        
            | 
            | 
           430 | 
              | 
        
        
            | 
            | 
           431 | 
               $result = new stdClass();
  | 
        
        
            | 
            | 
           432 | 
               // If the user can't see hidden grades, don't return that information.
  | 
        
        
            | 
            | 
           433 | 
               $gitem = grade_item::fetch(['id' => $grades->items[0]->id]);
  | 
        
        
            | 
            | 
           434 | 
               if (!$gitem->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  | 
        
        
            | 
            | 
           435 | 
                   $result->info = get_string('gradenoun') . ': ' . $grade->str_long_grade;
  | 
        
        
            | 
            | 
           436 | 
               } else {
  | 
        
        
            | 
            | 
           437 | 
                   $result->info = get_string('gradenoun') . ': ' . get_string('hidden', 'grades');
  | 
        
        
            | 
            | 
           438 | 
               }
  | 
        
        
            | 
            | 
           439 | 
              | 
        
        
            | 
            | 
           440 | 
               $result->time = grade_get_date_for_user_grade($grade, $user);
  | 
        
        
            | 
            | 
           441 | 
              | 
        
        
            | 
            | 
           442 | 
               return $result;
  | 
        
        
            | 
            | 
           443 | 
           }
  | 
        
        
            | 
            | 
           444 | 
              | 
        
        
            | 
            | 
           445 | 
           /**
  | 
        
        
            | 
            | 
           446 | 
            * Print a detailed representation of what a  user has done with
  | 
        
        
            | 
            | 
           447 | 
            * a given particular instance of this module, for user activity reports.
  | 
        
        
            | 
            | 
           448 | 
            *
  | 
        
        
            | 
            | 
           449 | 
            * @param stdClass $course
  | 
        
        
            | 
            | 
           450 | 
            * @param stdClass $user
  | 
        
        
            | 
            | 
           451 | 
            * @param stdClass $mod
  | 
        
        
            | 
            | 
           452 | 
            * @param stdClass $quiz
  | 
        
        
            | 
            | 
           453 | 
            * @return bool
  | 
        
        
            | 
            | 
           454 | 
            */
  | 
        
        
            | 
            | 
           455 | 
           function quiz_user_complete($course, $user, $mod, $quiz) {
  | 
        
        
            | 
            | 
           456 | 
               global $DB, $CFG, $OUTPUT;
  | 
        
        
            | 
            | 
           457 | 
               require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           458 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           459 | 
              | 
        
        
            | 
            | 
           460 | 
               $grades = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $user->id);
  | 
        
        
            | 
            | 
           461 | 
               if (!empty($grades->items[0]->grades)) {
  | 
        
        
            | 
            | 
           462 | 
                   $grade = reset($grades->items[0]->grades);
  | 
        
        
            | 
            | 
           463 | 
                   // If the user can't see hidden grades, don't return that information.
  | 
        
        
            | 
            | 
           464 | 
                   $gitem = grade_item::fetch(['id' => $grades->items[0]->id]);
  | 
        
        
            | 
            | 
           465 | 
                   if (!$gitem->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  | 
        
        
            | 
            | 
           466 | 
                       echo $OUTPUT->container(get_string('gradenoun').': '.$grade->str_long_grade);
  | 
        
        
            | 
            | 
           467 | 
                       if ($grade->str_feedback) {
  | 
        
        
            | 
            | 
           468 | 
                           echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
  | 
        
        
            | 
            | 
           469 | 
                       }
  | 
        
        
            | 
            | 
           470 | 
                   } else {
  | 
        
        
            | 
            | 
           471 | 
                       echo $OUTPUT->container(get_string('gradenoun') . ': ' . get_string('hidden', 'grades'));
  | 
        
        
            | 
            | 
           472 | 
                       if ($grade->str_feedback) {
  | 
        
        
            | 
            | 
           473 | 
                           echo $OUTPUT->container(get_string('feedback').': '.get_string('hidden', 'grades'));
  | 
        
        
            | 
            | 
           474 | 
                       }
  | 
        
        
            | 
            | 
           475 | 
                   }
  | 
        
        
            | 
            | 
           476 | 
               }
  | 
        
        
            | 
            | 
           477 | 
              | 
        
        
            | 
            | 
           478 | 
               if ($attempts = $DB->get_records('quiz_attempts',
  | 
        
        
            | 
            | 
           479 | 
                       ['userid' => $user->id, 'quiz' => $quiz->id], 'attempt')) {
  | 
        
        
            | 
            | 
           480 | 
                   foreach ($attempts as $attempt) {
  | 
        
        
            | 
            | 
           481 | 
                       echo get_string('attempt', 'quiz', $attempt->attempt) . ': ';
  | 
        
        
            | 
            | 
           482 | 
                       if ($attempt->state != quiz_attempt::FINISHED) {
  | 
        
        
            | 
            | 
           483 | 
                           echo quiz_attempt_state_name($attempt->state);
  | 
        
        
            | 
            | 
           484 | 
                       } else {
  | 
        
        
            | 
            | 
           485 | 
                           if (!isset($gitem)) {
  | 
        
        
            | 
            | 
           486 | 
                               if (!empty($grades->items[0]->grades)) {
  | 
        
        
            | 
            | 
           487 | 
                                   $gitem = grade_item::fetch(['id' => $grades->items[0]->id]);
  | 
        
        
            | 
            | 
           488 | 
                               } else {
  | 
        
        
            | 
            | 
           489 | 
                                   $gitem = new stdClass();
  | 
        
        
            | 
            | 
           490 | 
                                   $gitem->hidden = true;
  | 
        
        
            | 
            | 
           491 | 
                               }
  | 
        
        
            | 
            | 
           492 | 
                           }
  | 
        
        
            | 
            | 
           493 | 
                           if (!$gitem->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  | 
        
        
            | 
            | 
           494 | 
                               echo quiz_format_grade($quiz, $attempt->sumgrades) . '/' . quiz_format_grade($quiz, $quiz->sumgrades);
  | 
        
        
            | 
            | 
           495 | 
                           } else {
  | 
        
        
            | 
            | 
           496 | 
                               echo get_string('hidden', 'grades');
  | 
        
        
            | 
            | 
           497 | 
                           }
  | 
        
        
            | 
            | 
           498 | 
                           echo ' - '.userdate($attempt->timefinish).'<br />';
  | 
        
        
            | 
            | 
           499 | 
                       }
  | 
        
        
            | 
            | 
           500 | 
                   }
  | 
        
        
            | 
            | 
           501 | 
               } else {
  | 
        
        
            | 
            | 
           502 | 
                   print_string('noattempts', 'quiz');
  | 
        
        
            | 
            | 
           503 | 
               }
  | 
        
        
            | 
            | 
           504 | 
              | 
        
        
            | 
            | 
           505 | 
               return true;
  | 
        
        
            | 
            | 
           506 | 
           }
  | 
        
        
            | 
            | 
           507 | 
              | 
        
        
            | 
            | 
           508 | 
              | 
        
        
            | 
            | 
           509 | 
           /**
  | 
        
        
            | 
            | 
           510 | 
            * @param int|array $quizids A quiz ID, or an array of quiz IDs.
  | 
        
        
            | 
            | 
           511 | 
            * @param int $userid the userid.
  | 
        
        
            | 
            | 
           512 | 
            * @param string $status 'all', 'finished' or 'unfinished' to control
  | 
        
        
            | 
            | 
           513 | 
            * @param bool $includepreviews
  | 
        
        
            | 
            | 
           514 | 
            * @return array of all the user's attempts at this quiz. Returns an empty
  | 
        
        
            | 
            | 
           515 | 
            *      array if there are none.
  | 
        
        
            | 
            | 
           516 | 
            */
  | 
        
        
            | 
            | 
           517 | 
           function quiz_get_user_attempts($quizids, $userid, $status = 'finished', $includepreviews = false) {
  | 
        
        
            | 
            | 
           518 | 
               global $DB;
  | 
        
        
            | 
            | 
           519 | 
              | 
        
        
            | 
            | 
           520 | 
               $params = [];
  | 
        
        
            | 
            | 
           521 | 
               switch ($status) {
  | 
        
        
            | 
            | 
           522 | 
                   case 'all':
  | 
        
        
            | 
            | 
           523 | 
                       $statuscondition = '';
  | 
        
        
            | 
            | 
           524 | 
                       break;
  | 
        
        
            | 
            | 
           525 | 
              | 
        
        
            | 
            | 
           526 | 
                   case 'finished':
  | 
        
        
           | 1441 | 
           ariadna | 
           527 | 
                       $statuscondition = ' AND state IN (:state1, :state2, :state3)';
  | 
        
        
           | 1 | 
           efrain | 
           528 | 
                       $params['state1'] = quiz_attempt::FINISHED;
  | 
        
        
            | 
            | 
           529 | 
                       $params['state2'] = quiz_attempt::ABANDONED;
  | 
        
        
           | 1441 | 
           ariadna | 
           530 | 
                       $params['state3'] = quiz_attempt::SUBMITTED;
  | 
        
        
           | 1 | 
           efrain | 
           531 | 
                       break;
  | 
        
        
            | 
            | 
           532 | 
              | 
        
        
            | 
            | 
           533 | 
                   case 'unfinished':
  | 
        
        
            | 
            | 
           534 | 
                       $statuscondition = ' AND state IN (:state1, :state2)';
  | 
        
        
            | 
            | 
           535 | 
                       $params['state1'] = quiz_attempt::IN_PROGRESS;
  | 
        
        
            | 
            | 
           536 | 
                       $params['state2'] = quiz_attempt::OVERDUE;
  | 
        
        
            | 
            | 
           537 | 
                       break;
  | 
        
        
            | 
            | 
           538 | 
               }
  | 
        
        
            | 
            | 
           539 | 
              | 
        
        
            | 
            | 
           540 | 
               $quizids = (array) $quizids;
  | 
        
        
           | 1441 | 
           ariadna | 
           541 | 
               [$insql, $inparams] = $DB->get_in_or_equal($quizids, SQL_PARAMS_NAMED);
  | 
        
        
           | 1 | 
           efrain | 
           542 | 
               $params += $inparams;
  | 
        
        
            | 
            | 
           543 | 
               $params['userid'] = $userid;
  | 
        
        
            | 
            | 
           544 | 
              | 
        
        
            | 
            | 
           545 | 
               $previewclause = '';
  | 
        
        
            | 
            | 
           546 | 
               if (!$includepreviews) {
  | 
        
        
            | 
            | 
           547 | 
                   $previewclause = ' AND preview = 0';
  | 
        
        
            | 
            | 
           548 | 
               }
  | 
        
        
            | 
            | 
           549 | 
              | 
        
        
            | 
            | 
           550 | 
               return $DB->get_records_select('quiz_attempts',
  | 
        
        
            | 
            | 
           551 | 
                       "quiz $insql AND userid = :userid" . $previewclause . $statuscondition,
  | 
        
        
            | 
            | 
           552 | 
                       $params, 'quiz, attempt ASC');
  | 
        
        
            | 
            | 
           553 | 
           }
  | 
        
        
            | 
            | 
           554 | 
              | 
        
        
            | 
            | 
           555 | 
           /**
  | 
        
        
            | 
            | 
           556 | 
            * Return grade for given user or all users.
  | 
        
        
            | 
            | 
           557 | 
            *
  | 
        
        
            | 
            | 
           558 | 
            * @param int $quizid id of quiz
  | 
        
        
            | 
            | 
           559 | 
            * @param int $userid optional user id, 0 means all users
  | 
        
        
            | 
            | 
           560 | 
            * @return array array of grades, false if none. These are raw grades. They should
  | 
        
        
            | 
            | 
           561 | 
            * be processed with quiz_format_grade for display.
  | 
        
        
            | 
            | 
           562 | 
            */
  | 
        
        
            | 
            | 
           563 | 
           function quiz_get_user_grades($quiz, $userid = 0) {
  | 
        
        
            | 
            | 
           564 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           565 | 
              | 
        
        
            | 
            | 
           566 | 
               $params = [$quiz->id];
  | 
        
        
            | 
            | 
           567 | 
               $usertest = '';
  | 
        
        
            | 
            | 
           568 | 
               if ($userid) {
  | 
        
        
            | 
            | 
           569 | 
                   $params[] = $userid;
  | 
        
        
            | 
            | 
           570 | 
                   $usertest = 'AND u.id = ?';
  | 
        
        
            | 
            | 
           571 | 
               }
  | 
        
        
            | 
            | 
           572 | 
               return $DB->get_records_sql("
  | 
        
        
            | 
            | 
           573 | 
                       SELECT
  | 
        
        
            | 
            | 
           574 | 
                           u.id,
  | 
        
        
            | 
            | 
           575 | 
                           u.id AS userid,
  | 
        
        
            | 
            | 
           576 | 
                           qg.grade AS rawgrade,
  | 
        
        
            | 
            | 
           577 | 
                           qg.timemodified AS dategraded,
  | 
        
        
            | 
            | 
           578 | 
                           MAX(qa.timefinish) AS datesubmitted
  | 
        
        
            | 
            | 
           579 | 
              | 
        
        
            | 
            | 
           580 | 
                       FROM {user} u
  | 
        
        
            | 
            | 
           581 | 
                       JOIN {quiz_grades} qg ON u.id = qg.userid
  | 
        
        
            | 
            | 
           582 | 
                       JOIN {quiz_attempts} qa ON qa.quiz = qg.quiz AND qa.userid = u.id
  | 
        
        
            | 
            | 
           583 | 
              | 
        
        
            | 
            | 
           584 | 
                       WHERE qg.quiz = ?
  | 
        
        
            | 
            | 
           585 | 
                       $usertest
  | 
        
        
            | 
            | 
           586 | 
                       GROUP BY u.id, qg.grade, qg.timemodified", $params);
  | 
        
        
            | 
            | 
           587 | 
           }
  | 
        
        
            | 
            | 
           588 | 
              | 
        
        
            | 
            | 
           589 | 
           /**
  | 
        
        
            | 
            | 
           590 | 
            * Round a grade to the correct number of decimal places, and format it for display.
  | 
        
        
            | 
            | 
           591 | 
            *
  | 
        
        
            | 
            | 
           592 | 
            * @param stdClass $quiz The quiz table row, only $quiz->decimalpoints is used.
  | 
        
        
            | 
            | 
           593 | 
            * @param float|null $grade The grade to round and display (or null meaning no grade).
  | 
        
        
            | 
            | 
           594 | 
            * @return string
  | 
        
        
            | 
            | 
           595 | 
            */
  | 
        
        
            | 
            | 
           596 | 
           function quiz_format_grade($quiz, $grade) {
  | 
        
        
            | 
            | 
           597 | 
               if (is_null($grade)) {
  | 
        
        
            | 
            | 
           598 | 
                   return get_string('notyetgraded', 'quiz');
  | 
        
        
            | 
            | 
           599 | 
               }
  | 
        
        
            | 
            | 
           600 | 
               return format_float($grade, $quiz->decimalpoints);
  | 
        
        
            | 
            | 
           601 | 
           }
  | 
        
        
            | 
            | 
           602 | 
              | 
        
        
            | 
            | 
           603 | 
           /**
  | 
        
        
            | 
            | 
           604 | 
            * Determine the correct number of decimal places required to format a grade.
  | 
        
        
            | 
            | 
           605 | 
            *
  | 
        
        
            | 
            | 
           606 | 
            * @param stdClass $quiz The quiz table row, only $quiz->decimalpoints and
  | 
        
        
            | 
            | 
           607 | 
            *      ->questiondecimalpoints are used.
  | 
        
        
            | 
            | 
           608 | 
            * @return integer
  | 
        
        
            | 
            | 
           609 | 
            */
  | 
        
        
            | 
            | 
           610 | 
           function quiz_get_grade_format($quiz) {
  | 
        
        
            | 
            | 
           611 | 
               if (empty($quiz->questiondecimalpoints)) {
  | 
        
        
            | 
            | 
           612 | 
                   $quiz->questiondecimalpoints = -1;
  | 
        
        
            | 
            | 
           613 | 
               }
  | 
        
        
            | 
            | 
           614 | 
              | 
        
        
            | 
            | 
           615 | 
               if ($quiz->questiondecimalpoints == -1) {
  | 
        
        
            | 
            | 
           616 | 
                   return $quiz->decimalpoints;
  | 
        
        
            | 
            | 
           617 | 
               }
  | 
        
        
            | 
            | 
           618 | 
              | 
        
        
            | 
            | 
           619 | 
               return $quiz->questiondecimalpoints;
  | 
        
        
            | 
            | 
           620 | 
           }
  | 
        
        
            | 
            | 
           621 | 
              | 
        
        
            | 
            | 
           622 | 
           /**
  | 
        
        
            | 
            | 
           623 | 
            * Round a grade to the correct number of decimal places, and format it for display.
  | 
        
        
            | 
            | 
           624 | 
            *
  | 
        
        
            | 
            | 
           625 | 
            * @param stdClass $quiz The quiz table row, only $quiz->decimalpoints is used.
  | 
        
        
            | 
            | 
           626 | 
            * @param float $grade The grade to round.
  | 
        
        
            | 
            | 
           627 | 
            * @return string
  | 
        
        
            | 
            | 
           628 | 
            */
  | 
        
        
            | 
            | 
           629 | 
           function quiz_format_question_grade($quiz, $grade) {
  | 
        
        
            | 
            | 
           630 | 
               return format_float($grade, quiz_get_grade_format($quiz));
  | 
        
        
            | 
            | 
           631 | 
           }
  | 
        
        
            | 
            | 
           632 | 
              | 
        
        
            | 
            | 
           633 | 
           /**
  | 
        
        
            | 
            | 
           634 | 
            * Update grades in central gradebook
  | 
        
        
            | 
            | 
           635 | 
            *
  | 
        
        
            | 
            | 
           636 | 
            * @category grade
  | 
        
        
            | 
            | 
           637 | 
            * @param stdClass $quiz the quiz settings.
  | 
        
        
            | 
            | 
           638 | 
            * @param int $userid specific user only, 0 means all users.
  | 
        
        
            | 
            | 
           639 | 
            * @param bool $nullifnone If a single user is specified and $nullifnone is true a grade item with a null rawgrade will be inserted
  | 
        
        
            | 
            | 
           640 | 
            */
  | 
        
        
            | 
            | 
           641 | 
           function quiz_update_grades($quiz, $userid = 0, $nullifnone = true) {
  | 
        
        
            | 
            | 
           642 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           643 | 
               require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           644 | 
              | 
        
        
            | 
            | 
           645 | 
               if ($quiz->grade == 0) {
  | 
        
        
            | 
            | 
           646 | 
                   quiz_grade_item_update($quiz);
  | 
        
        
            | 
            | 
           647 | 
              | 
        
        
            | 
            | 
           648 | 
               } else if ($grades = quiz_get_user_grades($quiz, $userid)) {
  | 
        
        
            | 
            | 
           649 | 
                   quiz_grade_item_update($quiz, $grades);
  | 
        
        
            | 
            | 
           650 | 
              | 
        
        
            | 
            | 
           651 | 
               } else if ($userid && $nullifnone) {
  | 
        
        
            | 
            | 
           652 | 
                   $grade = new stdClass();
  | 
        
        
            | 
            | 
           653 | 
                   $grade->userid = $userid;
  | 
        
        
            | 
            | 
           654 | 
                   $grade->rawgrade = null;
  | 
        
        
            | 
            | 
           655 | 
                   quiz_grade_item_update($quiz, $grade);
  | 
        
        
            | 
            | 
           656 | 
              | 
        
        
            | 
            | 
           657 | 
               } else {
  | 
        
        
            | 
            | 
           658 | 
                   quiz_grade_item_update($quiz);
  | 
        
        
            | 
            | 
           659 | 
               }
  | 
        
        
            | 
            | 
           660 | 
           }
  | 
        
        
            | 
            | 
           661 | 
              | 
        
        
            | 
            | 
           662 | 
           /**
  | 
        
        
            | 
            | 
           663 | 
            * Create or update the grade item for given quiz
  | 
        
        
            | 
            | 
           664 | 
            *
  | 
        
        
            | 
            | 
           665 | 
            * @category grade
  | 
        
        
            | 
            | 
           666 | 
            * @param stdClass $quiz object with extra cmidnumber
  | 
        
        
            | 
            | 
           667 | 
            * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
  | 
        
        
            | 
            | 
           668 | 
            * @return int 0 if ok, error code otherwise
  | 
        
        
            | 
            | 
           669 | 
            */
  | 
        
        
            | 
            | 
           670 | 
           function quiz_grade_item_update($quiz, $grades = null) {
  | 
        
        
            | 
            | 
           671 | 
               global $CFG, $OUTPUT;
  | 
        
        
            | 
            | 
           672 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           673 | 
               require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           674 | 
              | 
        
        
            | 
            | 
           675 | 
               if (property_exists($quiz, 'cmidnumber')) { // May not be always present.
  | 
        
        
            | 
            | 
           676 | 
                   $params = ['itemname' => $quiz->name, 'idnumber' => $quiz->cmidnumber];
  | 
        
        
            | 
            | 
           677 | 
               } else {
  | 
        
        
            | 
            | 
           678 | 
                   $params = ['itemname' => $quiz->name];
  | 
        
        
            | 
            | 
           679 | 
               }
  | 
        
        
            | 
            | 
           680 | 
              | 
        
        
            | 
            | 
           681 | 
               if ($quiz->grade > 0) {
  | 
        
        
            | 
            | 
           682 | 
                   $params['gradetype'] = GRADE_TYPE_VALUE;
  | 
        
        
            | 
            | 
           683 | 
                   $params['grademax']  = $quiz->grade;
  | 
        
        
            | 
            | 
           684 | 
                   $params['grademin']  = 0;
  | 
        
        
            | 
            | 
           685 | 
              | 
        
        
            | 
            | 
           686 | 
               } else {
  | 
        
        
            | 
            | 
           687 | 
                   $params['gradetype'] = GRADE_TYPE_NONE;
  | 
        
        
            | 
            | 
           688 | 
               }
  | 
        
        
            | 
            | 
           689 | 
              | 
        
        
            | 
            | 
           690 | 
               // What this is trying to do:
  | 
        
        
            | 
            | 
           691 | 
               // 1. If the quiz is set to not show grades while the quiz is still open,
  | 
        
        
            | 
            | 
           692 | 
               //    and is set to show grades after the quiz is closed, then create the
  | 
        
        
            | 
            | 
           693 | 
               //    grade_item with a show-after date that is the quiz close date.
  | 
        
        
            | 
            | 
           694 | 
               // 2. If the quiz is set to not show grades at either of those times,
  | 
        
        
            | 
            | 
           695 | 
               //    create the grade_item as hidden.
  | 
        
        
            | 
            | 
           696 | 
               // 3. If the quiz is set to show grades, create the grade_item visible.
  | 
        
        
            | 
            | 
           697 | 
               $openreviewoptions = display_options::make_from_quiz($quiz,
  | 
        
        
            | 
            | 
           698 | 
                       display_options::LATER_WHILE_OPEN);
  | 
        
        
            | 
            | 
           699 | 
               $closedreviewoptions = display_options::make_from_quiz($quiz,
  | 
        
        
            | 
            | 
           700 | 
                       display_options::AFTER_CLOSE);
  | 
        
        
            | 
            | 
           701 | 
               if ($openreviewoptions->marks < question_display_options::MARK_AND_MAX &&
  | 
        
        
            | 
            | 
           702 | 
                       $closedreviewoptions->marks < question_display_options::MARK_AND_MAX) {
  | 
        
        
            | 
            | 
           703 | 
                   $params['hidden'] = 1;
  | 
        
        
            | 
            | 
           704 | 
              | 
        
        
            | 
            | 
           705 | 
               } else if ($openreviewoptions->marks < question_display_options::MARK_AND_MAX &&
  | 
        
        
            | 
            | 
           706 | 
                       $closedreviewoptions->marks >= question_display_options::MARK_AND_MAX) {
  | 
        
        
            | 
            | 
           707 | 
                   if ($quiz->timeclose) {
  | 
        
        
            | 
            | 
           708 | 
                       $params['hidden'] = $quiz->timeclose;
  | 
        
        
            | 
            | 
           709 | 
                   } else {
  | 
        
        
            | 
            | 
           710 | 
                       $params['hidden'] = 1;
  | 
        
        
            | 
            | 
           711 | 
                   }
  | 
        
        
            | 
            | 
           712 | 
              | 
        
        
            | 
            | 
           713 | 
               } else {
  | 
        
        
            | 
            | 
           714 | 
                   // Either
  | 
        
        
            | 
            | 
           715 | 
                   // a) both open and closed enabled
  | 
        
        
            | 
            | 
           716 | 
                   // b) open enabled, closed disabled - we can not "hide after",
  | 
        
        
            | 
            | 
           717 | 
                   //    grades are kept visible even after closing.
  | 
        
        
            | 
            | 
           718 | 
                   $params['hidden'] = 0;
  | 
        
        
            | 
            | 
           719 | 
               }
  | 
        
        
            | 
            | 
           720 | 
              | 
        
        
            | 
            | 
           721 | 
               if (!$params['hidden']) {
  | 
        
        
            | 
            | 
           722 | 
                   // If the grade item is not hidden by the quiz logic, then we need to
  | 
        
        
            | 
            | 
           723 | 
                   // hide it if the quiz is hidden from students.
  | 
        
        
            | 
            | 
           724 | 
                   if (property_exists($quiz, 'visible')) {
  | 
        
        
            | 
            | 
           725 | 
                       // Saving the quiz form, and cm not yet updated in the database.
  | 
        
        
            | 
            | 
           726 | 
                       $params['hidden'] = !$quiz->visible;
  | 
        
        
            | 
            | 
           727 | 
                   } else {
  | 
        
        
            | 
            | 
           728 | 
                       $cm = get_coursemodule_from_instance('quiz', $quiz->id);
  | 
        
        
            | 
            | 
           729 | 
                       $params['hidden'] = !$cm->visible;
  | 
        
        
            | 
            | 
           730 | 
                   }
  | 
        
        
            | 
            | 
           731 | 
               }
  | 
        
        
            | 
            | 
           732 | 
              | 
        
        
            | 
            | 
           733 | 
               if ($grades  === 'reset') {
  | 
        
        
            | 
            | 
           734 | 
                   $params['reset'] = true;
  | 
        
        
            | 
            | 
           735 | 
                   $grades = null;
  | 
        
        
            | 
            | 
           736 | 
               }
  | 
        
        
            | 
            | 
           737 | 
              | 
        
        
            | 
            | 
           738 | 
               $gradebook_grades = grade_get_grades($quiz->course, 'mod', 'quiz', $quiz->id);
  | 
        
        
            | 
            | 
           739 | 
               if (!empty($gradebook_grades->items)) {
  | 
        
        
            | 
            | 
           740 | 
                   $grade_item = $gradebook_grades->items[0];
  | 
        
        
            | 
            | 
           741 | 
                   if ($grade_item->locked) {
  | 
        
        
            | 
            | 
           742 | 
                       // NOTE: this is an extremely nasty hack! It is not a bug if this confirmation fails badly. --skodak.
  | 
        
        
            | 
            | 
           743 | 
                       $confirm_regrade = optional_param('confirm_regrade', 0, PARAM_INT);
  | 
        
        
            | 
            | 
           744 | 
                       if (!$confirm_regrade) {
  | 
        
        
            | 
            | 
           745 | 
                           if (!AJAX_SCRIPT) {
  | 
        
        
            | 
            | 
           746 | 
                               $message = get_string('gradeitemislocked', 'grades');
  | 
        
        
            | 
            | 
           747 | 
                               $back_link = $CFG->wwwroot . '/mod/quiz/report.php?q=' . $quiz->id .
  | 
        
        
            | 
            | 
           748 | 
                                       '&mode=overview';
  | 
        
        
            | 
            | 
           749 | 
                               $regrade_link = qualified_me() . '&confirm_regrade=1';
  | 
        
        
            | 
            | 
           750 | 
                               echo $OUTPUT->box_start('generalbox', 'notice');
  | 
        
        
            | 
            | 
           751 | 
                               echo '<p>'. $message .'</p>';
  | 
        
        
            | 
            | 
           752 | 
                               echo $OUTPUT->container_start('buttons');
  | 
        
        
            | 
            | 
           753 | 
                               echo $OUTPUT->single_button($regrade_link, get_string('regradeanyway', 'grades'));
  | 
        
        
            | 
            | 
           754 | 
                               echo $OUTPUT->single_button($back_link,  get_string('cancel'));
  | 
        
        
            | 
            | 
           755 | 
                               echo $OUTPUT->container_end();
  | 
        
        
            | 
            | 
           756 | 
                               echo $OUTPUT->box_end();
  | 
        
        
            | 
            | 
           757 | 
                           }
  | 
        
        
            | 
            | 
           758 | 
                           return GRADE_UPDATE_ITEM_LOCKED;
  | 
        
        
            | 
            | 
           759 | 
                       }
  | 
        
        
            | 
            | 
           760 | 
                   }
  | 
        
        
            | 
            | 
           761 | 
               }
  | 
        
        
            | 
            | 
           762 | 
              | 
        
        
            | 
            | 
           763 | 
               return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades, $params);
  | 
        
        
            | 
            | 
           764 | 
           }
  | 
        
        
            | 
            | 
           765 | 
              | 
        
        
            | 
            | 
           766 | 
           /**
  | 
        
        
            | 
            | 
           767 | 
            * Delete grade item for given quiz
  | 
        
        
            | 
            | 
           768 | 
            *
  | 
        
        
            | 
            | 
           769 | 
            * @category grade
  | 
        
        
            | 
            | 
           770 | 
            * @param stdClass $quiz object
  | 
        
        
            | 
            | 
           771 | 
            * @return int
  | 
        
        
            | 
            | 
           772 | 
            */
  | 
        
        
            | 
            | 
           773 | 
           function quiz_grade_item_delete($quiz) {
  | 
        
        
            | 
            | 
           774 | 
               global $CFG;
  | 
        
        
            | 
            | 
           775 | 
               require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           776 | 
              | 
        
        
            | 
            | 
           777 | 
               return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0,
  | 
        
        
            | 
            | 
           778 | 
                       null, ['deleted' => 1]);
  | 
        
        
            | 
            | 
           779 | 
           }
  | 
        
        
            | 
            | 
           780 | 
              | 
        
        
            | 
            | 
           781 | 
           /**
  | 
        
        
            | 
            | 
           782 | 
            * This standard function will check all instances of this module
  | 
        
        
            | 
            | 
           783 | 
            * and make sure there are up-to-date events created for each of them.
  | 
        
        
            | 
            | 
           784 | 
            * If courseid = 0, then every quiz event in the site is checked, else
  | 
        
        
            | 
            | 
           785 | 
            * only quiz events belonging to the course specified are checked.
  | 
        
        
            | 
            | 
           786 | 
            * This function is used, in its new format, by restore_refresh_events()
  | 
        
        
            | 
            | 
           787 | 
            *
  | 
        
        
            | 
            | 
           788 | 
            * @param int $courseid
  | 
        
        
            | 
            | 
           789 | 
            * @param int|stdClass $instance Quiz module instance or ID.
  | 
        
        
            | 
            | 
           790 | 
            * @param int|stdClass $cm Course module object or ID (not used in this module).
  | 
        
        
            | 
            | 
           791 | 
            * @return bool
  | 
        
        
            | 
            | 
           792 | 
            */
  | 
        
        
            | 
            | 
           793 | 
           function quiz_refresh_events($courseid = 0, $instance = null, $cm = null) {
  | 
        
        
            | 
            | 
           794 | 
               global $DB;
  | 
        
        
            | 
            | 
           795 | 
              | 
        
        
            | 
            | 
           796 | 
               // If we have instance information then we can just update the one event instead of updating all events.
  | 
        
        
            | 
            | 
           797 | 
               if (isset($instance)) {
  | 
        
        
            | 
            | 
           798 | 
                   if (!is_object($instance)) {
  | 
        
        
            | 
            | 
           799 | 
                       $instance = $DB->get_record('quiz', ['id' => $instance], '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           800 | 
                   }
  | 
        
        
            | 
            | 
           801 | 
                   quiz_update_events($instance);
  | 
        
        
            | 
            | 
           802 | 
                   return true;
  | 
        
        
            | 
            | 
           803 | 
               }
  | 
        
        
            | 
            | 
           804 | 
              | 
        
        
            | 
            | 
           805 | 
               if ($courseid == 0) {
  | 
        
        
            | 
            | 
           806 | 
                   if (!$quizzes = $DB->get_records('quiz')) {
  | 
        
        
            | 
            | 
           807 | 
                       return true;
  | 
        
        
            | 
            | 
           808 | 
                   }
  | 
        
        
            | 
            | 
           809 | 
               } else {
  | 
        
        
            | 
            | 
           810 | 
                   if (!$quizzes = $DB->get_records('quiz', ['course' => $courseid])) {
  | 
        
        
            | 
            | 
           811 | 
                       return true;
  | 
        
        
            | 
            | 
           812 | 
                   }
  | 
        
        
            | 
            | 
           813 | 
               }
  | 
        
        
            | 
            | 
           814 | 
              | 
        
        
            | 
            | 
           815 | 
               foreach ($quizzes as $quiz) {
  | 
        
        
            | 
            | 
           816 | 
                   quiz_update_events($quiz);
  | 
        
        
            | 
            | 
           817 | 
               }
  | 
        
        
            | 
            | 
           818 | 
              | 
        
        
            | 
            | 
           819 | 
               return true;
  | 
        
        
            | 
            | 
           820 | 
           }
  | 
        
        
            | 
            | 
           821 | 
              | 
        
        
            | 
            | 
           822 | 
           /**
  | 
        
        
            | 
            | 
           823 | 
            * Returns all quiz graded users since a given time for specified quiz
  | 
        
        
            | 
            | 
           824 | 
            */
  | 
        
        
            | 
            | 
           825 | 
           function quiz_get_recent_mod_activity(&$activities, &$index, $timestart,
  | 
        
        
            | 
            | 
           826 | 
                   $courseid, $cmid, $userid = 0, $groupid = 0) {
  | 
        
        
            | 
            | 
           827 | 
               global $CFG, $USER, $DB;
  | 
        
        
            | 
            | 
           828 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           829 | 
              | 
        
        
            | 
            | 
           830 | 
               $course = get_course($courseid);
  | 
        
        
            | 
            | 
           831 | 
               $modinfo = get_fast_modinfo($course);
  | 
        
        
            | 
            | 
           832 | 
              | 
        
        
            | 
            | 
           833 | 
               $cm = $modinfo->cms[$cmid];
  | 
        
        
            | 
            | 
           834 | 
               $quiz = $DB->get_record('quiz', ['id' => $cm->instance]);
  | 
        
        
            | 
            | 
           835 | 
              | 
        
        
            | 
            | 
           836 | 
               if ($userid) {
  | 
        
        
            | 
            | 
           837 | 
                   $userselect = "AND u.id = :userid";
  | 
        
        
            | 
            | 
           838 | 
                   $params['userid'] = $userid;
  | 
        
        
            | 
            | 
           839 | 
               } else {
  | 
        
        
            | 
            | 
           840 | 
                   $userselect = '';
  | 
        
        
            | 
            | 
           841 | 
               }
  | 
        
        
            | 
            | 
           842 | 
              | 
        
        
            | 
            | 
           843 | 
               if ($groupid) {
  | 
        
        
            | 
            | 
           844 | 
                   $groupselect = 'AND gm.groupid = :groupid';
  | 
        
        
            | 
            | 
           845 | 
                   $groupjoin   = 'JOIN {groups_members} gm ON  gm.userid=u.id';
  | 
        
        
            | 
            | 
           846 | 
                   $params['groupid'] = $groupid;
  | 
        
        
            | 
            | 
           847 | 
               } else {
  | 
        
        
            | 
            | 
           848 | 
                   $groupselect = '';
  | 
        
        
            | 
            | 
           849 | 
                   $groupjoin   = '';
  | 
        
        
            | 
            | 
           850 | 
               }
  | 
        
        
            | 
            | 
           851 | 
              | 
        
        
            | 
            | 
           852 | 
               $params['timestart'] = $timestart;
  | 
        
        
            | 
            | 
           853 | 
               $params['quizid'] = $quiz->id;
  | 
        
        
            | 
            | 
           854 | 
              | 
        
        
            | 
            | 
           855 | 
               $userfieldsapi = \core_user\fields::for_userpic();
  | 
        
        
            | 
            | 
           856 | 
               $ufields = $userfieldsapi->get_sql('u', false, '', 'useridagain', false)->selects;
  | 
        
        
            | 
            | 
           857 | 
               if (!$attempts = $DB->get_records_sql("
  | 
        
        
            | 
            | 
           858 | 
                         SELECT qa.*,
  | 
        
        
            | 
            | 
           859 | 
                                {$ufields}
  | 
        
        
            | 
            | 
           860 | 
                           FROM {quiz_attempts} qa
  | 
        
        
            | 
            | 
           861 | 
                                JOIN {user} u ON u.id = qa.userid
  | 
        
        
            | 
            | 
           862 | 
                                $groupjoin
  | 
        
        
            | 
            | 
           863 | 
                          WHERE qa.timefinish > :timestart
  | 
        
        
            | 
            | 
           864 | 
                            AND qa.quiz = :quizid
  | 
        
        
            | 
            | 
           865 | 
                            AND qa.preview = 0
  | 
        
        
            | 
            | 
           866 | 
                                $userselect
  | 
        
        
            | 
            | 
           867 | 
                                $groupselect
  | 
        
        
            | 
            | 
           868 | 
                       ORDER BY qa.timefinish ASC", $params)) {
  | 
        
        
            | 
            | 
           869 | 
                   return;
  | 
        
        
            | 
            | 
           870 | 
               }
  | 
        
        
            | 
            | 
           871 | 
              | 
        
        
            | 
            | 
           872 | 
               $context         = context_module::instance($cm->id);
  | 
        
        
            | 
            | 
           873 | 
               $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
  | 
        
        
            | 
            | 
           874 | 
               $viewfullnames   = has_capability('moodle/site:viewfullnames', $context);
  | 
        
        
            | 
            | 
           875 | 
               $grader          = has_capability('mod/quiz:viewreports', $context);
  | 
        
        
            | 
            | 
           876 | 
               $groupmode       = groups_get_activity_groupmode($cm, $course);
  | 
        
        
            | 
            | 
           877 | 
              | 
        
        
            | 
            | 
           878 | 
               $usersgroups = null;
  | 
        
        
            | 
            | 
           879 | 
               $aname = format_string($cm->name, true);
  | 
        
        
            | 
            | 
           880 | 
               foreach ($attempts as $attempt) {
  | 
        
        
            | 
            | 
           881 | 
                   if ($attempt->userid != $USER->id) {
  | 
        
        
            | 
            | 
           882 | 
                       if (!$grader) {
  | 
        
        
            | 
            | 
           883 | 
                           // Grade permission required.
  | 
        
        
            | 
            | 
           884 | 
                           continue;
  | 
        
        
            | 
            | 
           885 | 
                       }
  | 
        
        
            | 
            | 
           886 | 
              | 
        
        
            | 
            | 
           887 | 
                       if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
  | 
        
        
            | 
            | 
           888 | 
                           $usersgroups = groups_get_all_groups($course->id,
  | 
        
        
            | 
            | 
           889 | 
                                   $attempt->userid, $cm->groupingid);
  | 
        
        
            | 
            | 
           890 | 
                           $usersgroups = array_keys($usersgroups);
  | 
        
        
            | 
            | 
           891 | 
                           if (!array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid))) {
  | 
        
        
            | 
            | 
           892 | 
                               continue;
  | 
        
        
            | 
            | 
           893 | 
                           }
  | 
        
        
            | 
            | 
           894 | 
                       }
  | 
        
        
            | 
            | 
           895 | 
                   }
  | 
        
        
            | 
            | 
           896 | 
              | 
        
        
            | 
            | 
           897 | 
                   $options = quiz_get_review_options($quiz, $attempt, $context);
  | 
        
        
            | 
            | 
           898 | 
              | 
        
        
            | 
            | 
           899 | 
                   $tmpactivity = new stdClass();
  | 
        
        
            | 
            | 
           900 | 
              | 
        
        
            | 
            | 
           901 | 
                   $tmpactivity->type       = 'quiz';
  | 
        
        
            | 
            | 
           902 | 
                   $tmpactivity->cmid       = $cm->id;
  | 
        
        
            | 
            | 
           903 | 
                   $tmpactivity->name       = $aname;
  | 
        
        
            | 
            | 
           904 | 
                   $tmpactivity->sectionnum = $cm->sectionnum;
  | 
        
        
            | 
            | 
           905 | 
                   $tmpactivity->timestamp  = $attempt->timefinish;
  | 
        
        
            | 
            | 
           906 | 
              | 
        
        
            | 
            | 
           907 | 
                   $tmpactivity->content = new stdClass();
  | 
        
        
            | 
            | 
           908 | 
                   $tmpactivity->content->attemptid = $attempt->id;
  | 
        
        
            | 
            | 
           909 | 
                   $tmpactivity->content->attempt   = $attempt->attempt;
  | 
        
        
            | 
            | 
           910 | 
                   if (quiz_has_grades($quiz) && $options->marks >= question_display_options::MARK_AND_MAX) {
  | 
        
        
            | 
            | 
           911 | 
                       $tmpactivity->content->sumgrades = quiz_format_grade($quiz, $attempt->sumgrades);
  | 
        
        
            | 
            | 
           912 | 
                       $tmpactivity->content->maxgrade  = quiz_format_grade($quiz, $quiz->sumgrades);
  | 
        
        
            | 
            | 
           913 | 
                   } else {
  | 
        
        
            | 
            | 
           914 | 
                       $tmpactivity->content->sumgrades = null;
  | 
        
        
            | 
            | 
           915 | 
                       $tmpactivity->content->maxgrade  = null;
  | 
        
        
            | 
            | 
           916 | 
                   }
  | 
        
        
            | 
            | 
           917 | 
              | 
        
        
            | 
            | 
           918 | 
                   $tmpactivity->user = user_picture::unalias($attempt, null, 'useridagain');
  | 
        
        
            | 
            | 
           919 | 
                   $tmpactivity->user->fullname  = fullname($tmpactivity->user, $viewfullnames);
  | 
        
        
            | 
            | 
           920 | 
              | 
        
        
            | 
            | 
           921 | 
                   $activities[$index++] = $tmpactivity;
  | 
        
        
            | 
            | 
           922 | 
               }
  | 
        
        
            | 
            | 
           923 | 
           }
  | 
        
        
            | 
            | 
           924 | 
              | 
        
        
            | 
            | 
           925 | 
           function quiz_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
  | 
        
        
            | 
            | 
           926 | 
               global $CFG, $OUTPUT;
  | 
        
        
            | 
            | 
           927 | 
              | 
        
        
            | 
            | 
           928 | 
               echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
  | 
        
        
            | 
            | 
           929 | 
              | 
        
        
            | 
            | 
           930 | 
               echo '<tr><td class="userpicture" valign="top">';
  | 
        
        
            | 
            | 
           931 | 
               echo $OUTPUT->user_picture($activity->user, ['courseid' => $courseid]);
  | 
        
        
            | 
            | 
           932 | 
               echo '</td><td>';
  | 
        
        
            | 
            | 
           933 | 
              | 
        
        
            | 
            | 
           934 | 
               if ($detail) {
  | 
        
        
            | 
            | 
           935 | 
                   $modname = $modnames[$activity->type];
  | 
        
        
            | 
            | 
           936 | 
                   echo '<div class="title">';
  | 
        
        
            | 
            | 
           937 | 
                   echo $OUTPUT->image_icon('monologo', $modname, $activity->type);
  | 
        
        
            | 
            | 
           938 | 
                   echo '<a href="' . $CFG->wwwroot . '/mod/quiz/view.php?id=' .
  | 
        
        
            | 
            | 
           939 | 
                           $activity->cmid . '">' . $activity->name . '</a>';
  | 
        
        
            | 
            | 
           940 | 
                   echo '</div>';
  | 
        
        
            | 
            | 
           941 | 
               }
  | 
        
        
            | 
            | 
           942 | 
              | 
        
        
            | 
            | 
           943 | 
               echo '<div class="grade">';
  | 
        
        
            | 
            | 
           944 | 
               echo  get_string('attempt', 'quiz', $activity->content->attempt);
  | 
        
        
            | 
            | 
           945 | 
               if (isset($activity->content->maxgrade)) {
  | 
        
        
            | 
            | 
           946 | 
                   $grades = $activity->content->sumgrades . ' / ' . $activity->content->maxgrade;
  | 
        
        
            | 
            | 
           947 | 
                   echo ': (<a href="' . $CFG->wwwroot . '/mod/quiz/review.php?attempt=' .
  | 
        
        
            | 
            | 
           948 | 
                           $activity->content->attemptid . '">' . $grades . '</a>)';
  | 
        
        
            | 
            | 
           949 | 
               }
  | 
        
        
            | 
            | 
           950 | 
               echo '</div>';
  | 
        
        
            | 
            | 
           951 | 
              | 
        
        
            | 
            | 
           952 | 
               echo '<div class="user">';
  | 
        
        
            | 
            | 
           953 | 
               echo '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $activity->user->id .
  | 
        
        
            | 
            | 
           954 | 
                       '&course=' . $courseid . '">' . $activity->user->fullname .
  | 
        
        
            | 
            | 
           955 | 
                       '</a> - ' . userdate($activity->timestamp);
  | 
        
        
            | 
            | 
           956 | 
               echo '</div>';
  | 
        
        
            | 
            | 
           957 | 
              | 
        
        
            | 
            | 
           958 | 
               echo '</td></tr></table>';
  | 
        
        
            | 
            | 
           959 | 
              | 
        
        
            | 
            | 
           960 | 
               return;
  | 
        
        
            | 
            | 
           961 | 
           }
  | 
        
        
            | 
            | 
           962 | 
              | 
        
        
            | 
            | 
           963 | 
           /**
  | 
        
        
            | 
            | 
           964 | 
            * Pre-process the quiz options form data, making any necessary adjustments.
  | 
        
        
            | 
            | 
           965 | 
            * Called by add/update instance in this file.
  | 
        
        
            | 
            | 
           966 | 
            *
  | 
        
        
            | 
            | 
           967 | 
            * @param stdClass $quiz The variables set on the form.
  | 
        
        
            | 
            | 
           968 | 
            */
  | 
        
        
            | 
            | 
           969 | 
           function quiz_process_options($quiz) {
  | 
        
        
            | 
            | 
           970 | 
               global $CFG;
  | 
        
        
            | 
            | 
           971 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           972 | 
               require_once($CFG->libdir . '/questionlib.php');
  | 
        
        
            | 
            | 
           973 | 
              | 
        
        
            | 
            | 
           974 | 
               $quiz->timemodified = time();
  | 
        
        
            | 
            | 
           975 | 
              | 
        
        
            | 
            | 
           976 | 
               // Quiz name.
  | 
        
        
            | 
            | 
           977 | 
               if (!empty($quiz->name)) {
  | 
        
        
            | 
            | 
           978 | 
                   $quiz->name = trim($quiz->name);
  | 
        
        
            | 
            | 
           979 | 
               }
  | 
        
        
            | 
            | 
           980 | 
              | 
        
        
            | 
            | 
           981 | 
               // Password field - different in form to stop browsers that remember passwords
  | 
        
        
            | 
            | 
           982 | 
               // getting confused.
  | 
        
        
            | 
            | 
           983 | 
               $quiz->password = $quiz->quizpassword;
  | 
        
        
            | 
            | 
           984 | 
               unset($quiz->quizpassword);
  | 
        
        
            | 
            | 
           985 | 
              | 
        
        
            | 
            | 
           986 | 
               // Quiz feedback.
  | 
        
        
            | 
            | 
           987 | 
               if (isset($quiz->feedbacktext)) {
  | 
        
        
            | 
            | 
           988 | 
                   // Clean up the boundary text.
  | 
        
        
            | 
            | 
           989 | 
                   for ($i = 0; $i < count($quiz->feedbacktext); $i += 1) {
  | 
        
        
            | 
            | 
           990 | 
                       if (empty($quiz->feedbacktext[$i]['text'])) {
  | 
        
        
            | 
            | 
           991 | 
                           $quiz->feedbacktext[$i]['text'] = '';
  | 
        
        
            | 
            | 
           992 | 
                       } else {
  | 
        
        
            | 
            | 
           993 | 
                           $quiz->feedbacktext[$i]['text'] = trim($quiz->feedbacktext[$i]['text']);
  | 
        
        
            | 
            | 
           994 | 
                       }
  | 
        
        
            | 
            | 
           995 | 
                   }
  | 
        
        
            | 
            | 
           996 | 
              | 
        
        
            | 
            | 
           997 | 
                   // Check the boundary value is a number or a percentage, and in range.
  | 
        
        
            | 
            | 
           998 | 
                   $i = 0;
  | 
        
        
            | 
            | 
           999 | 
                   while (!empty($quiz->feedbackboundaries[$i])) {
  | 
        
        
            | 
            | 
           1000 | 
                       $boundary = trim($quiz->feedbackboundaries[$i]);
  | 
        
        
            | 
            | 
           1001 | 
                       if (!is_numeric($boundary)) {
  | 
        
        
            | 
            | 
           1002 | 
                           if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
  | 
        
        
            | 
            | 
           1003 | 
                               $boundary = trim(substr($boundary, 0, -1));
  | 
        
        
            | 
            | 
           1004 | 
                               if (is_numeric($boundary)) {
  | 
        
        
            | 
            | 
           1005 | 
                                   $boundary = $boundary * $quiz->grade / 100.0;
  | 
        
        
            | 
            | 
           1006 | 
                               } else {
  | 
        
        
            | 
            | 
           1007 | 
                                   return get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
  | 
        
        
            | 
            | 
           1008 | 
                               }
  | 
        
        
            | 
            | 
           1009 | 
                           }
  | 
        
        
            | 
            | 
           1010 | 
                       }
  | 
        
        
            | 
            | 
           1011 | 
                       if ($boundary <= 0 || $boundary >= $quiz->grade) {
  | 
        
        
            | 
            | 
           1012 | 
                           return get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
  | 
        
        
            | 
            | 
           1013 | 
                       }
  | 
        
        
            | 
            | 
           1014 | 
                       if ($i > 0 && $boundary >= $quiz->feedbackboundaries[$i - 1]) {
  | 
        
        
            | 
            | 
           1015 | 
                           return get_string('feedbackerrororder', 'quiz', $i + 1);
  | 
        
        
            | 
            | 
           1016 | 
                       }
  | 
        
        
            | 
            | 
           1017 | 
                       $quiz->feedbackboundaries[$i] = $boundary;
  | 
        
        
            | 
            | 
           1018 | 
                       $i += 1;
  | 
        
        
            | 
            | 
           1019 | 
                   }
  | 
        
        
            | 
            | 
           1020 | 
                   $numboundaries = $i;
  | 
        
        
            | 
            | 
           1021 | 
              | 
        
        
            | 
            | 
           1022 | 
                   // Check there is nothing in the remaining unused fields.
  | 
        
        
            | 
            | 
           1023 | 
                   if (!empty($quiz->feedbackboundaries)) {
  | 
        
        
            | 
            | 
           1024 | 
                       for ($i = $numboundaries; $i < count($quiz->feedbackboundaries); $i += 1) {
  | 
        
        
            | 
            | 
           1025 | 
                           if (!empty($quiz->feedbackboundaries[$i]) &&
  | 
        
        
            | 
            | 
           1026 | 
                                   trim($quiz->feedbackboundaries[$i]) != '') {
  | 
        
        
            | 
            | 
           1027 | 
                               return get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
  | 
        
        
            | 
            | 
           1028 | 
                           }
  | 
        
        
            | 
            | 
           1029 | 
                       }
  | 
        
        
            | 
            | 
           1030 | 
                   }
  | 
        
        
            | 
            | 
           1031 | 
                   for ($i = $numboundaries + 1; $i < count($quiz->feedbacktext); $i += 1) {
  | 
        
        
            | 
            | 
           1032 | 
                       if (!empty($quiz->feedbacktext[$i]['text']) &&
  | 
        
        
            | 
            | 
           1033 | 
                               trim($quiz->feedbacktext[$i]['text']) != '') {
  | 
        
        
            | 
            | 
           1034 | 
                           return get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
  | 
        
        
            | 
            | 
           1035 | 
                       }
  | 
        
        
            | 
            | 
           1036 | 
                   }
  | 
        
        
            | 
            | 
           1037 | 
                   // Needs to be bigger than $quiz->grade because of '<' test in quiz_feedback_for_grade().
  | 
        
        
            | 
            | 
           1038 | 
                   $quiz->feedbackboundaries[-1] = $quiz->grade + 1;
  | 
        
        
            | 
            | 
           1039 | 
                   $quiz->feedbackboundaries[$numboundaries] = 0;
  | 
        
        
            | 
            | 
           1040 | 
                   $quiz->feedbackboundarycount = $numboundaries;
  | 
        
        
            | 
            | 
           1041 | 
               } else {
  | 
        
        
            | 
            | 
           1042 | 
                   $quiz->feedbackboundarycount = -1;
  | 
        
        
            | 
            | 
           1043 | 
               }
  | 
        
        
            | 
            | 
           1044 | 
              | 
        
        
            | 
            | 
           1045 | 
               // Combing the individual settings into the review columns.
  | 
        
        
            | 
            | 
           1046 | 
               $quiz->reviewattempt = quiz_review_option_form_to_db($quiz, 'attempt');
  | 
        
        
            | 
            | 
           1047 | 
               $quiz->reviewcorrectness = quiz_review_option_form_to_db($quiz, 'correctness');
  | 
        
        
            | 
            | 
           1048 | 
               $quiz->reviewmaxmarks = quiz_review_option_form_to_db($quiz, 'maxmarks');
  | 
        
        
            | 
            | 
           1049 | 
               $quiz->reviewmarks = quiz_review_option_form_to_db($quiz, 'marks');
  | 
        
        
            | 
            | 
           1050 | 
               $quiz->reviewspecificfeedback = quiz_review_option_form_to_db($quiz, 'specificfeedback');
  | 
        
        
            | 
            | 
           1051 | 
               $quiz->reviewgeneralfeedback = quiz_review_option_form_to_db($quiz, 'generalfeedback');
  | 
        
        
            | 
            | 
           1052 | 
               $quiz->reviewrightanswer = quiz_review_option_form_to_db($quiz, 'rightanswer');
  | 
        
        
            | 
            | 
           1053 | 
               $quiz->reviewoverallfeedback = quiz_review_option_form_to_db($quiz, 'overallfeedback');
  | 
        
        
            | 
            | 
           1054 | 
               $quiz->reviewattempt |= display_options::DURING;
  | 
        
        
            | 
            | 
           1055 | 
               $quiz->reviewoverallfeedback &= ~display_options::DURING;
  | 
        
        
            | 
            | 
           1056 | 
              | 
        
        
            | 
            | 
           1057 | 
               // Ensure that disabled checkboxes in completion settings are set to 0.
  | 
        
        
            | 
            | 
           1058 | 
               // But only if the completion settinsg are unlocked.
  | 
        
        
            | 
            | 
           1059 | 
               if (!empty($quiz->completionunlocked)) {
  | 
        
        
            | 
            | 
           1060 | 
                   if (empty($quiz->completionusegrade)) {
  | 
        
        
            | 
            | 
           1061 | 
                       $quiz->completionpassgrade = 0;
  | 
        
        
            | 
            | 
           1062 | 
                   }
  | 
        
        
            | 
            | 
           1063 | 
                   if (empty($quiz->completionpassgrade)) {
  | 
        
        
            | 
            | 
           1064 | 
                       $quiz->completionattemptsexhausted = 0;
  | 
        
        
            | 
            | 
           1065 | 
                   }
  | 
        
        
            | 
            | 
           1066 | 
                   if (empty($quiz->completionminattemptsenabled)) {
  | 
        
        
            | 
            | 
           1067 | 
                       $quiz->completionminattempts = 0;
  | 
        
        
            | 
            | 
           1068 | 
                   }
  | 
        
        
            | 
            | 
           1069 | 
               }
  | 
        
        
            | 
            | 
           1070 | 
           }
  | 
        
        
            | 
            | 
           1071 | 
              | 
        
        
            | 
            | 
           1072 | 
           /**
  | 
        
        
            | 
            | 
           1073 | 
            * Helper function for {@link quiz_process_options()}.
  | 
        
        
            | 
            | 
           1074 | 
            * @param stdClass $fromform the sumbitted form date.
  | 
        
        
            | 
            | 
           1075 | 
            * @param string $field one of the review option field names.
  | 
        
        
            | 
            | 
           1076 | 
            */
  | 
        
        
            | 
            | 
           1077 | 
           function quiz_review_option_form_to_db($fromform, $field) {
  | 
        
        
            | 
            | 
           1078 | 
               static $times = [
  | 
        
        
            | 
            | 
           1079 | 
                   'during' => display_options::DURING,
  | 
        
        
            | 
            | 
           1080 | 
                   'immediately' => display_options::IMMEDIATELY_AFTER,
  | 
        
        
            | 
            | 
           1081 | 
                   'open' => display_options::LATER_WHILE_OPEN,
  | 
        
        
            | 
            | 
           1082 | 
                   'closed' => display_options::AFTER_CLOSE,
  | 
        
        
            | 
            | 
           1083 | 
               ];
  | 
        
        
            | 
            | 
           1084 | 
              | 
        
        
            | 
            | 
           1085 | 
               $review = 0;
  | 
        
        
            | 
            | 
           1086 | 
               foreach ($times as $whenname => $when) {
  | 
        
        
            | 
            | 
           1087 | 
                   $fieldname = $field . $whenname;
  | 
        
        
            | 
            | 
           1088 | 
                   if (!empty($fromform->$fieldname)) {
  | 
        
        
            | 
            | 
           1089 | 
                       $review |= $when;
  | 
        
        
            | 
            | 
           1090 | 
                       unset($fromform->$fieldname);
  | 
        
        
            | 
            | 
           1091 | 
                   }
  | 
        
        
            | 
            | 
           1092 | 
               }
  | 
        
        
            | 
            | 
           1093 | 
              | 
        
        
            | 
            | 
           1094 | 
               return $review;
  | 
        
        
            | 
            | 
           1095 | 
           }
  | 
        
        
            | 
            | 
           1096 | 
              | 
        
        
            | 
            | 
           1097 | 
           /**
  | 
        
        
            | 
            | 
           1098 | 
            * In place editable callback for slot displaynumber.
  | 
        
        
            | 
            | 
           1099 | 
            *
  | 
        
        
            | 
            | 
           1100 | 
            * @param string $itemtype slotdisplarnumber
  | 
        
        
            | 
            | 
           1101 | 
            * @param int $itemid the id of the slot in the quiz_slots table
  | 
        
        
            | 
            | 
           1102 | 
            * @param string $newvalue the new value for displaynumber field for a given slot in the quiz_slots table
  | 
        
        
            | 
            | 
           1103 | 
            * @return \core\output\inplace_editable|void
  | 
        
        
            | 
            | 
           1104 | 
            */
  | 
        
        
            | 
            | 
           1105 | 
           function mod_quiz_inplace_editable(string $itemtype, int $itemid, string $newvalue): \core\output\inplace_editable {
  | 
        
        
            | 
            | 
           1106 | 
               global $DB;
  | 
        
        
            | 
            | 
           1107 | 
              | 
        
        
            | 
            | 
           1108 | 
               if ($itemtype === 'slotdisplaynumber') {
  | 
        
        
            | 
            | 
           1109 | 
                   // Work out which quiz and slot this is.
  | 
        
        
            | 
            | 
           1110 | 
                   $slot = $DB->get_record('quiz_slots', ['id' => $itemid], '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           1111 | 
                   $quizobj = quiz_settings::create($slot->quizid);
  | 
        
        
            | 
            | 
           1112 | 
              | 
        
        
            | 
            | 
           1113 | 
                   // Validate the context, and check the required capability.
  | 
        
        
            | 
            | 
           1114 | 
                   $context = $quizobj->get_context();
  | 
        
        
            | 
            | 
           1115 | 
                   \core_external\external_api::validate_context($context);
  | 
        
        
            | 
            | 
           1116 | 
                   require_capability('mod/quiz:manage', $context);
  | 
        
        
            | 
            | 
           1117 | 
              | 
        
        
            | 
            | 
           1118 | 
                   // Update the value - truncating the size of the DB column.
  | 
        
        
            | 
            | 
           1119 | 
                   $structure = $quizobj->get_structure();
  | 
        
        
            | 
            | 
           1120 | 
                   $structure->update_slot_display_number($itemid, core_text::substr($newvalue, 0, 16));
  | 
        
        
            | 
            | 
           1121 | 
              | 
        
        
            | 
            | 
           1122 | 
                   // Prepare the element for the output.
  | 
        
        
            | 
            | 
           1123 | 
                   return $structure->make_slot_display_number_in_place_editable($itemid, $context);
  | 
        
        
            | 
            | 
           1124 | 
               }
  | 
        
        
            | 
            | 
           1125 | 
           }
  | 
        
        
            | 
            | 
           1126 | 
              | 
        
        
            | 
            | 
           1127 | 
           /**
  | 
        
        
            | 
            | 
           1128 | 
            * This function is called at the end of quiz_add_instance
  | 
        
        
            | 
            | 
           1129 | 
            * and quiz_update_instance, to do the common processing.
  | 
        
        
            | 
            | 
           1130 | 
            *
  | 
        
        
            | 
            | 
           1131 | 
            * @param stdClass $quiz the quiz object.
  | 
        
        
            | 
            | 
           1132 | 
            */
  | 
        
        
            | 
            | 
           1133 | 
           function quiz_after_add_or_update($quiz) {
  | 
        
        
            | 
            | 
           1134 | 
               global $DB;
  | 
        
        
            | 
            | 
           1135 | 
               $cmid = $quiz->coursemodule;
  | 
        
        
            | 
            | 
           1136 | 
              | 
        
        
            | 
            | 
           1137 | 
               // We need to use context now, so we need to make sure all needed info is already in db.
  | 
        
        
            | 
            | 
           1138 | 
               $DB->set_field('course_modules', 'instance', $quiz->id, ['id' => $cmid]);
  | 
        
        
            | 
            | 
           1139 | 
               $context = context_module::instance($cmid);
  | 
        
        
            | 
            | 
           1140 | 
              | 
        
        
            | 
            | 
           1141 | 
               // Save the feedback.
  | 
        
        
            | 
            | 
           1142 | 
               $DB->delete_records('quiz_feedback', ['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           1143 | 
              | 
        
        
            | 
            | 
           1144 | 
               for ($i = 0; $i <= $quiz->feedbackboundarycount; $i++) {
  | 
        
        
            | 
            | 
           1145 | 
                   $feedback = new stdClass();
  | 
        
        
            | 
            | 
           1146 | 
                   $feedback->quizid = $quiz->id;
  | 
        
        
            | 
            | 
           1147 | 
                   $feedback->feedbacktext = $quiz->feedbacktext[$i]['text'];
  | 
        
        
            | 
            | 
           1148 | 
                   $feedback->feedbacktextformat = $quiz->feedbacktext[$i]['format'];
  | 
        
        
            | 
            | 
           1149 | 
                   $feedback->mingrade = $quiz->feedbackboundaries[$i];
  | 
        
        
            | 
            | 
           1150 | 
                   $feedback->maxgrade = $quiz->feedbackboundaries[$i - 1];
  | 
        
        
            | 
            | 
           1151 | 
                   $feedback->id = $DB->insert_record('quiz_feedback', $feedback);
  | 
        
        
            | 
            | 
           1152 | 
                   $feedbacktext = file_save_draft_area_files((int)$quiz->feedbacktext[$i]['itemid'],
  | 
        
        
            | 
            | 
           1153 | 
                           $context->id, 'mod_quiz', 'feedback', $feedback->id,
  | 
        
        
            | 
            | 
           1154 | 
                           ['subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0],
  | 
        
        
            | 
            | 
           1155 | 
                           $quiz->feedbacktext[$i]['text']);
  | 
        
        
            | 
            | 
           1156 | 
                   $DB->set_field('quiz_feedback', 'feedbacktext', $feedbacktext,
  | 
        
        
            | 
            | 
           1157 | 
                           ['id' => $feedback->id]);
  | 
        
        
            | 
            | 
           1158 | 
               }
  | 
        
        
            | 
            | 
           1159 | 
              | 
        
        
            | 
            | 
           1160 | 
               // Store any settings belonging to the access rules.
  | 
        
        
            | 
            | 
           1161 | 
               access_manager::save_settings($quiz);
  | 
        
        
            | 
            | 
           1162 | 
              | 
        
        
            | 
            | 
           1163 | 
               // Update the events relating to this quiz.
  | 
        
        
            | 
            | 
           1164 | 
               quiz_update_events($quiz);
  | 
        
        
            | 
            | 
           1165 | 
               $completionexpected = (!empty($quiz->completionexpected)) ? $quiz->completionexpected : null;
  | 
        
        
            | 
            | 
           1166 | 
               \core_completion\api::update_completion_date_event($quiz->coursemodule, 'quiz', $quiz->id, $completionexpected);
  | 
        
        
            | 
            | 
           1167 | 
              | 
        
        
            | 
            | 
           1168 | 
               // Update related grade item.
  | 
        
        
            | 
            | 
           1169 | 
               quiz_grade_item_update($quiz);
  | 
        
        
            | 
            | 
           1170 | 
           }
  | 
        
        
            | 
            | 
           1171 | 
              | 
        
        
            | 
            | 
           1172 | 
           /**
  | 
        
        
            | 
            | 
           1173 | 
            * This function updates the events associated to the quiz.
  | 
        
        
            | 
            | 
           1174 | 
            * If $override is non-zero, then it updates only the events
  | 
        
        
            | 
            | 
           1175 | 
            * associated with the specified override.
  | 
        
        
            | 
            | 
           1176 | 
            *
  | 
        
        
            | 
            | 
           1177 | 
            * @param stdClass $quiz the quiz object.
  | 
        
        
            | 
            | 
           1178 | 
            * @param stdClass|null $override limit to a specific override
  | 
        
        
            | 
            | 
           1179 | 
            */
  | 
        
        
            | 
            | 
           1180 | 
           function quiz_update_events($quiz, $override = null) {
  | 
        
        
            | 
            | 
           1181 | 
               global $DB;
  | 
        
        
            | 
            | 
           1182 | 
              | 
        
        
            | 
            | 
           1183 | 
               // Load the old events relating to this quiz.
  | 
        
        
            | 
            | 
           1184 | 
               $conds = ['modulename' => 'quiz',
  | 
        
        
            | 
            | 
           1185 | 
                              'instance' => $quiz->id];
  | 
        
        
            | 
            | 
           1186 | 
               if (!empty($override)) {
  | 
        
        
            | 
            | 
           1187 | 
                   // Only load events for this override.
  | 
        
        
            | 
            | 
           1188 | 
                   if (isset($override->userid)) {
  | 
        
        
            | 
            | 
           1189 | 
                       $conds['userid'] = $override->userid;
  | 
        
        
            | 
            | 
           1190 | 
                   } else {
  | 
        
        
            | 
            | 
           1191 | 
                       $conds['groupid'] = $override->groupid;
  | 
        
        
            | 
            | 
           1192 | 
                   }
  | 
        
        
            | 
            | 
           1193 | 
               }
  | 
        
        
            | 
            | 
           1194 | 
               $oldevents = $DB->get_records('event', $conds, 'id ASC');
  | 
        
        
            | 
            | 
           1195 | 
              | 
        
        
            | 
            | 
           1196 | 
               // Now make a to-do list of all that needs to be updated.
  | 
        
        
            | 
            | 
           1197 | 
               if (empty($override)) {
  | 
        
        
            | 
            | 
           1198 | 
                   // We are updating the primary settings for the quiz, so we need to add all the overrides.
  | 
        
        
            | 
            | 
           1199 | 
                   $overrides = $DB->get_records('quiz_overrides', ['quiz' => $quiz->id], 'id ASC');
  | 
        
        
            | 
            | 
           1200 | 
                   // It is necessary to add an empty stdClass to the beginning of the array as the $oldevents
  | 
        
        
            | 
            | 
           1201 | 
                   // list contains the original (non-override) event for the module. If this is not included
  | 
        
        
            | 
            | 
           1202 | 
                   // the logic below will end up updating the wrong row when we try to reconcile this $overrides
  | 
        
        
            | 
            | 
           1203 | 
                   // list against the $oldevents list.
  | 
        
        
            | 
            | 
           1204 | 
                   array_unshift($overrides, new stdClass());
  | 
        
        
            | 
            | 
           1205 | 
               } else {
  | 
        
        
            | 
            | 
           1206 | 
                   // Just do the one override.
  | 
        
        
            | 
            | 
           1207 | 
                   $overrides = [$override];
  | 
        
        
            | 
            | 
           1208 | 
               }
  | 
        
        
            | 
            | 
           1209 | 
              | 
        
        
            | 
            | 
           1210 | 
               // Get group override priorities.
  | 
        
        
            | 
            | 
           1211 | 
               $grouppriorities = quiz_get_group_override_priorities($quiz->id);
  | 
        
        
            | 
            | 
           1212 | 
              | 
        
        
            | 
            | 
           1213 | 
               foreach ($overrides as $current) {
  | 
        
        
            | 
            | 
           1214 | 
                   $groupid   = isset($current->groupid)?  $current->groupid : 0;
  | 
        
        
            | 
            | 
           1215 | 
                   $userid    = isset($current->userid)? $current->userid : 0;
  | 
        
        
            | 
            | 
           1216 | 
                   $timeopen  = isset($current->timeopen)?  $current->timeopen : $quiz->timeopen;
  | 
        
        
            | 
            | 
           1217 | 
                   $timeclose = isset($current->timeclose)? $current->timeclose : $quiz->timeclose;
  | 
        
        
            | 
            | 
           1218 | 
              | 
        
        
            | 
            | 
           1219 | 
                   // Only add open/close events for an override if they differ from the quiz default.
  | 
        
        
            | 
            | 
           1220 | 
                   $addopen  = empty($current->id) || !empty($current->timeopen);
  | 
        
        
            | 
            | 
           1221 | 
                   $addclose = empty($current->id) || !empty($current->timeclose);
  | 
        
        
            | 
            | 
           1222 | 
              | 
        
        
            | 
            | 
           1223 | 
                   if (!empty($quiz->coursemodule)) {
  | 
        
        
            | 
            | 
           1224 | 
                       $cmid = $quiz->coursemodule;
  | 
        
        
            | 
            | 
           1225 | 
                   } else {
  | 
        
        
            | 
            | 
           1226 | 
                       $cmid = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course)->id;
  | 
        
        
            | 
            | 
           1227 | 
                   }
  | 
        
        
            | 
            | 
           1228 | 
              | 
        
        
            | 
            | 
           1229 | 
                   $event = new stdClass();
  | 
        
        
            | 
            | 
           1230 | 
                   $event->type = !$timeclose ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
  | 
        
        
            | 
            | 
           1231 | 
                   $event->description = format_module_intro('quiz', $quiz, $cmid, false);
  | 
        
        
            | 
            | 
           1232 | 
                   $event->format = FORMAT_HTML;
  | 
        
        
            | 
            | 
           1233 | 
                   // Events module won't show user events when the courseid is nonzero.
  | 
        
        
            | 
            | 
           1234 | 
                   $event->courseid    = ($userid) ? 0 : $quiz->course;
  | 
        
        
            | 
            | 
           1235 | 
                   $event->groupid     = $groupid;
  | 
        
        
            | 
            | 
           1236 | 
                   $event->userid      = $userid;
  | 
        
        
            | 
            | 
           1237 | 
                   $event->modulename  = 'quiz';
  | 
        
        
            | 
            | 
           1238 | 
                   $event->instance    = $quiz->id;
  | 
        
        
            | 
            | 
           1239 | 
                   $event->timestart   = $timeopen;
  | 
        
        
            | 
            | 
           1240 | 
                   $event->timeduration = max($timeclose - $timeopen, 0);
  | 
        
        
            | 
            | 
           1241 | 
                   $event->timesort    = $timeopen;
  | 
        
        
            | 
            | 
           1242 | 
                   $event->visible     = instance_is_visible('quiz', $quiz);
  | 
        
        
            | 
            | 
           1243 | 
                   $event->eventtype   = QUIZ_EVENT_TYPE_OPEN;
  | 
        
        
            | 
            | 
           1244 | 
                   $event->priority    = null;
  | 
        
        
            | 
            | 
           1245 | 
              | 
        
        
            | 
            | 
           1246 | 
                   // Determine the event name and priority.
  | 
        
        
            | 
            | 
           1247 | 
                   if ($groupid) {
  | 
        
        
            | 
            | 
           1248 | 
                       // Group override event.
  | 
        
        
            | 
            | 
           1249 | 
                       $params = new stdClass();
  | 
        
        
            | 
            | 
           1250 | 
                       $params->quiz = $quiz->name;
  | 
        
        
            | 
            | 
           1251 | 
                       $params->group = groups_get_group_name($groupid);
  | 
        
        
            | 
            | 
           1252 | 
                       if ($params->group === false) {
  | 
        
        
            | 
            | 
           1253 | 
                           // Group doesn't exist, just skip it.
  | 
        
        
            | 
            | 
           1254 | 
                           continue;
  | 
        
        
            | 
            | 
           1255 | 
                       }
  | 
        
        
            | 
            | 
           1256 | 
                       $eventname = get_string('overridegroupeventname', 'quiz', $params);
  | 
        
        
            | 
            | 
           1257 | 
                       // Set group override priority.
  | 
        
        
            | 
            | 
           1258 | 
                       if ($grouppriorities !== null) {
  | 
        
        
            | 
            | 
           1259 | 
                           $openpriorities = $grouppriorities['open'];
  | 
        
        
            | 
            | 
           1260 | 
                           if (isset($openpriorities[$timeopen])) {
  | 
        
        
            | 
            | 
           1261 | 
                               $event->priority = $openpriorities[$timeopen];
  | 
        
        
            | 
            | 
           1262 | 
                           }
  | 
        
        
            | 
            | 
           1263 | 
                       }
  | 
        
        
            | 
            | 
           1264 | 
                   } else if ($userid) {
  | 
        
        
            | 
            | 
           1265 | 
                       // User override event.
  | 
        
        
            | 
            | 
           1266 | 
                       $params = new stdClass();
  | 
        
        
            | 
            | 
           1267 | 
                       $params->quiz = $quiz->name;
  | 
        
        
            | 
            | 
           1268 | 
                       $eventname = get_string('overrideusereventname', 'quiz', $params);
  | 
        
        
            | 
            | 
           1269 | 
                       // Set user override priority.
  | 
        
        
            | 
            | 
           1270 | 
                       $event->priority = CALENDAR_EVENT_USER_OVERRIDE_PRIORITY;
  | 
        
        
            | 
            | 
           1271 | 
                   } else {
  | 
        
        
            | 
            | 
           1272 | 
                       // The parent event.
  | 
        
        
            | 
            | 
           1273 | 
                       $eventname = $quiz->name;
  | 
        
        
            | 
            | 
           1274 | 
                   }
  | 
        
        
            | 
            | 
           1275 | 
              | 
        
        
            | 
            | 
           1276 | 
                   if ($addopen or $addclose) {
  | 
        
        
            | 
            | 
           1277 | 
                       // Separate start and end events.
  | 
        
        
            | 
            | 
           1278 | 
                       $event->timeduration  = 0;
  | 
        
        
            | 
            | 
           1279 | 
                       if ($timeopen && $addopen) {
  | 
        
        
            | 
            | 
           1280 | 
                           if ($oldevent = array_shift($oldevents)) {
  | 
        
        
            | 
            | 
           1281 | 
                               $event->id = $oldevent->id;
  | 
        
        
            | 
            | 
           1282 | 
                           } else {
  | 
        
        
            | 
            | 
           1283 | 
                               unset($event->id);
  | 
        
        
            | 
            | 
           1284 | 
                           }
  | 
        
        
            | 
            | 
           1285 | 
                           $event->name = get_string('quizeventopens', 'quiz', $eventname);
  | 
        
        
            | 
            | 
           1286 | 
                           // The method calendar_event::create will reuse a db record if the id field is set.
  | 
        
        
            | 
            | 
           1287 | 
                           calendar_event::create($event, false);
  | 
        
        
            | 
            | 
           1288 | 
                       }
  | 
        
        
            | 
            | 
           1289 | 
                       if ($timeclose && $addclose) {
  | 
        
        
            | 
            | 
           1290 | 
                           if ($oldevent = array_shift($oldevents)) {
  | 
        
        
            | 
            | 
           1291 | 
                               $event->id = $oldevent->id;
  | 
        
        
            | 
            | 
           1292 | 
                           } else {
  | 
        
        
            | 
            | 
           1293 | 
                               unset($event->id);
  | 
        
        
            | 
            | 
           1294 | 
                           }
  | 
        
        
            | 
            | 
           1295 | 
                           $event->type      = CALENDAR_EVENT_TYPE_ACTION;
  | 
        
        
            | 
            | 
           1296 | 
                           $event->name      = get_string('quizeventcloses', 'quiz', $eventname);
  | 
        
        
            | 
            | 
           1297 | 
                           $event->timestart = $timeclose;
  | 
        
        
            | 
            | 
           1298 | 
                           $event->timesort  = $timeclose;
  | 
        
        
            | 
            | 
           1299 | 
                           $event->eventtype = QUIZ_EVENT_TYPE_CLOSE;
  | 
        
        
            | 
            | 
           1300 | 
                           if ($groupid && $grouppriorities !== null) {
  | 
        
        
            | 
            | 
           1301 | 
                               $closepriorities = $grouppriorities['close'];
  | 
        
        
            | 
            | 
           1302 | 
                               if (isset($closepriorities[$timeclose])) {
  | 
        
        
            | 
            | 
           1303 | 
                                   $event->priority = $closepriorities[$timeclose];
  | 
        
        
            | 
            | 
           1304 | 
                               }
  | 
        
        
            | 
            | 
           1305 | 
                           }
  | 
        
        
            | 
            | 
           1306 | 
                           calendar_event::create($event, false);
  | 
        
        
            | 
            | 
           1307 | 
                       }
  | 
        
        
            | 
            | 
           1308 | 
                   }
  | 
        
        
            | 
            | 
           1309 | 
               }
  | 
        
        
            | 
            | 
           1310 | 
              | 
        
        
            | 
            | 
           1311 | 
               // Delete any leftover events.
  | 
        
        
            | 
            | 
           1312 | 
               foreach ($oldevents as $badevent) {
  | 
        
        
            | 
            | 
           1313 | 
                   $badevent = calendar_event::load($badevent);
  | 
        
        
            | 
            | 
           1314 | 
                   $badevent->delete();
  | 
        
        
            | 
            | 
           1315 | 
               }
  | 
        
        
            | 
            | 
           1316 | 
           }
  | 
        
        
            | 
            | 
           1317 | 
              | 
        
        
            | 
            | 
           1318 | 
           /**
  | 
        
        
            | 
            | 
           1319 | 
            * Calculates the priorities of timeopen and timeclose values for group overrides for a quiz.
  | 
        
        
            | 
            | 
           1320 | 
            *
  | 
        
        
            | 
            | 
           1321 | 
            * @param int $quizid The quiz ID.
  | 
        
        
            | 
            | 
           1322 | 
            * @return array|null Array of group override priorities for open and close times. Null if there are no group overrides.
  | 
        
        
            | 
            | 
           1323 | 
            */
  | 
        
        
            | 
            | 
           1324 | 
           function quiz_get_group_override_priorities($quizid) {
  | 
        
        
            | 
            | 
           1325 | 
               global $DB;
  | 
        
        
            | 
            | 
           1326 | 
              | 
        
        
            | 
            | 
           1327 | 
               // Fetch group overrides.
  | 
        
        
            | 
            | 
           1328 | 
               $where = 'quiz = :quiz AND groupid IS NOT NULL';
  | 
        
        
            | 
            | 
           1329 | 
               $params = ['quiz' => $quizid];
  | 
        
        
            | 
            | 
           1330 | 
               $overrides = $DB->get_records_select('quiz_overrides', $where, $params, '', 'id, timeopen, timeclose');
  | 
        
        
            | 
            | 
           1331 | 
               if (!$overrides) {
  | 
        
        
            | 
            | 
           1332 | 
                   return null;
  | 
        
        
            | 
            | 
           1333 | 
               }
  | 
        
        
            | 
            | 
           1334 | 
              | 
        
        
            | 
            | 
           1335 | 
               $grouptimeopen = [];
  | 
        
        
            | 
            | 
           1336 | 
               $grouptimeclose = [];
  | 
        
        
            | 
            | 
           1337 | 
               foreach ($overrides as $override) {
  | 
        
        
            | 
            | 
           1338 | 
                   if ($override->timeopen !== null && !in_array($override->timeopen, $grouptimeopen)) {
  | 
        
        
            | 
            | 
           1339 | 
                       $grouptimeopen[] = $override->timeopen;
  | 
        
        
            | 
            | 
           1340 | 
                   }
  | 
        
        
            | 
            | 
           1341 | 
                   if ($override->timeclose !== null && !in_array($override->timeclose, $grouptimeclose)) {
  | 
        
        
            | 
            | 
           1342 | 
                       $grouptimeclose[] = $override->timeclose;
  | 
        
        
            | 
            | 
           1343 | 
                   }
  | 
        
        
            | 
            | 
           1344 | 
               }
  | 
        
        
            | 
            | 
           1345 | 
              | 
        
        
            | 
            | 
           1346 | 
               // Sort open times in ascending manner. The earlier open time gets higher priority.
  | 
        
        
            | 
            | 
           1347 | 
               sort($grouptimeopen);
  | 
        
        
            | 
            | 
           1348 | 
               // Set priorities.
  | 
        
        
            | 
            | 
           1349 | 
               $opengrouppriorities = [];
  | 
        
        
            | 
            | 
           1350 | 
               $openpriority = 1;
  | 
        
        
            | 
            | 
           1351 | 
               foreach ($grouptimeopen as $timeopen) {
  | 
        
        
            | 
            | 
           1352 | 
                   $opengrouppriorities[$timeopen] = $openpriority++;
  | 
        
        
            | 
            | 
           1353 | 
               }
  | 
        
        
            | 
            | 
           1354 | 
              | 
        
        
            | 
            | 
           1355 | 
               // Sort close times in descending manner. The later close time gets higher priority.
  | 
        
        
            | 
            | 
           1356 | 
               rsort($grouptimeclose);
  | 
        
        
            | 
            | 
           1357 | 
               // Set priorities.
  | 
        
        
            | 
            | 
           1358 | 
               $closegrouppriorities = [];
  | 
        
        
            | 
            | 
           1359 | 
               $closepriority = 1;
  | 
        
        
            | 
            | 
           1360 | 
               foreach ($grouptimeclose as $timeclose) {
  | 
        
        
            | 
            | 
           1361 | 
                   $closegrouppriorities[$timeclose] = $closepriority++;
  | 
        
        
            | 
            | 
           1362 | 
               }
  | 
        
        
            | 
            | 
           1363 | 
              | 
        
        
            | 
            | 
           1364 | 
               return [
  | 
        
        
            | 
            | 
           1365 | 
                   'open' => $opengrouppriorities,
  | 
        
        
            | 
            | 
           1366 | 
                   'close' => $closegrouppriorities
  | 
        
        
            | 
            | 
           1367 | 
               ];
  | 
        
        
            | 
            | 
           1368 | 
           }
  | 
        
        
            | 
            | 
           1369 | 
              | 
        
        
            | 
            | 
           1370 | 
           /**
  | 
        
        
            | 
            | 
           1371 | 
            * List the actions that correspond to a view of this module.
  | 
        
        
            | 
            | 
           1372 | 
            * This is used by the participation report.
  | 
        
        
            | 
            | 
           1373 | 
            *
  | 
        
        
            | 
            | 
           1374 | 
            * Note: This is not used by new logging system. Event with
  | 
        
        
            | 
            | 
           1375 | 
            *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  | 
        
        
            | 
            | 
           1376 | 
            *       be considered as view action.
  | 
        
        
            | 
            | 
           1377 | 
            *
  | 
        
        
            | 
            | 
           1378 | 
            * @return array
  | 
        
        
            | 
            | 
           1379 | 
            */
  | 
        
        
            | 
            | 
           1380 | 
           function quiz_get_view_actions() {
  | 
        
        
            | 
            | 
           1381 | 
               return ['view', 'view all', 'report', 'review'];
  | 
        
        
            | 
            | 
           1382 | 
           }
  | 
        
        
            | 
            | 
           1383 | 
              | 
        
        
            | 
            | 
           1384 | 
           /**
  | 
        
        
            | 
            | 
           1385 | 
            * List the actions that correspond to a post of this module.
  | 
        
        
            | 
            | 
           1386 | 
            * This is used by the participation report.
  | 
        
        
            | 
            | 
           1387 | 
            *
  | 
        
        
            | 
            | 
           1388 | 
            * Note: This is not used by new logging system. Event with
  | 
        
        
            | 
            | 
           1389 | 
            *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  | 
        
        
            | 
            | 
           1390 | 
            *       will be considered as post action.
  | 
        
        
            | 
            | 
           1391 | 
            *
  | 
        
        
            | 
            | 
           1392 | 
            * @return array
  | 
        
        
            | 
            | 
           1393 | 
            */
  | 
        
        
            | 
            | 
           1394 | 
           function quiz_get_post_actions() {
  | 
        
        
            | 
            | 
           1395 | 
               return ['attempt', 'close attempt', 'preview', 'editquestions',
  | 
        
        
            | 
            | 
           1396 | 
                       'delete attempt', 'manualgrade'];
  | 
        
        
            | 
            | 
           1397 | 
           }
  | 
        
        
            | 
            | 
           1398 | 
              | 
        
        
            | 
            | 
           1399 | 
           /**
  | 
        
        
            | 
            | 
           1400 | 
            * Standard callback used by questions_in_use.
  | 
        
        
            | 
            | 
           1401 | 
            *
  | 
        
        
            | 
            | 
           1402 | 
            * @param array $questionids of question ids.
  | 
        
        
            | 
            | 
           1403 | 
            * @return bool whether any of these questions are used by any instance of this module.
  | 
        
        
            | 
            | 
           1404 | 
            */
  | 
        
        
            | 
            | 
           1405 | 
           function quiz_questions_in_use($questionids) {
  | 
        
        
            | 
            | 
           1406 | 
               return question_engine::questions_in_use($questionids,
  | 
        
        
            | 
            | 
           1407 | 
                       new qubaid_join('{quiz_attempts} quiza', 'quiza.uniqueid',
  | 
        
        
            | 
            | 
           1408 | 
                           'quiza.preview = 0'));
  | 
        
        
            | 
            | 
           1409 | 
           }
  | 
        
        
            | 
            | 
           1410 | 
              | 
        
        
            | 
            | 
           1411 | 
           /**
  | 
        
        
            | 
            | 
           1412 | 
            * Implementation of the function for printing the form elements that control
  | 
        
        
            | 
            | 
           1413 | 
            * whether the course reset functionality affects the quiz.
  | 
        
        
            | 
            | 
           1414 | 
            *
  | 
        
        
            | 
            | 
           1415 | 
            * @param MoodleQuickForm $mform the course reset form that is being built.
  | 
        
        
            | 
            | 
           1416 | 
            */
  | 
        
        
            | 
            | 
           1417 | 
           function quiz_reset_course_form_definition($mform) {
  | 
        
        
            | 
            | 
           1418 | 
               $mform->addElement('header', 'quizheader', get_string('modulenameplural', 'quiz'));
  | 
        
        
           | 1441 | 
           ariadna | 
           1419 | 
               $mform->addElement('static', 'quizdelete', get_string('delete'));
  | 
        
        
           | 1 | 
           efrain | 
           1420 | 
               $mform->addElement('advcheckbox', 'reset_quiz_attempts',
  | 
        
        
            | 
            | 
           1421 | 
                       get_string('removeallquizattempts', 'quiz'));
  | 
        
        
            | 
            | 
           1422 | 
               $mform->addElement('advcheckbox', 'reset_quiz_user_overrides',
  | 
        
        
            | 
            | 
           1423 | 
                       get_string('removealluseroverrides', 'quiz'));
  | 
        
        
            | 
            | 
           1424 | 
               $mform->addElement('advcheckbox', 'reset_quiz_group_overrides',
  | 
        
        
            | 
            | 
           1425 | 
                       get_string('removeallgroupoverrides', 'quiz'));
  | 
        
        
            | 
            | 
           1426 | 
           }
  | 
        
        
            | 
            | 
           1427 | 
              | 
        
        
            | 
            | 
           1428 | 
           /**
  | 
        
        
            | 
            | 
           1429 | 
            * Course reset form defaults.
  | 
        
        
            | 
            | 
           1430 | 
            * @return array the defaults.
  | 
        
        
            | 
            | 
           1431 | 
            */
  | 
        
        
            | 
            | 
           1432 | 
           function quiz_reset_course_form_defaults($course) {
  | 
        
        
            | 
            | 
           1433 | 
               return ['reset_quiz_attempts' => 1,
  | 
        
        
            | 
            | 
           1434 | 
                            'reset_quiz_group_overrides' => 1,
  | 
        
        
            | 
            | 
           1435 | 
                            'reset_quiz_user_overrides' => 1];
  | 
        
        
            | 
            | 
           1436 | 
           }
  | 
        
        
            | 
            | 
           1437 | 
              | 
        
        
            | 
            | 
           1438 | 
           /**
  | 
        
        
            | 
            | 
           1439 | 
            * Removes all grades from gradebook
  | 
        
        
            | 
            | 
           1440 | 
            *
  | 
        
        
            | 
            | 
           1441 | 
            * @param int $courseid
  | 
        
        
            | 
            | 
           1442 | 
            * @param string optional type
  | 
        
        
            | 
            | 
           1443 | 
            */
  | 
        
        
            | 
            | 
           1444 | 
           function quiz_reset_gradebook($courseid, $type='') {
  | 
        
        
            | 
            | 
           1445 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           1446 | 
              | 
        
        
            | 
            | 
           1447 | 
               $quizzes = $DB->get_records_sql("
  | 
        
        
            | 
            | 
           1448 | 
                       SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
  | 
        
        
            | 
            | 
           1449 | 
                       FROM {modules} m
  | 
        
        
            | 
            | 
           1450 | 
                       JOIN {course_modules} cm ON m.id = cm.module
  | 
        
        
            | 
            | 
           1451 | 
                       JOIN {quiz} q ON cm.instance = q.id
  | 
        
        
            | 
            | 
           1452 | 
                       WHERE m.name = 'quiz' AND cm.course = ?", [$courseid]);
  | 
        
        
            | 
            | 
           1453 | 
              | 
        
        
            | 
            | 
           1454 | 
               foreach ($quizzes as $quiz) {
  | 
        
        
            | 
            | 
           1455 | 
                   quiz_grade_item_update($quiz, 'reset');
  | 
        
        
            | 
            | 
           1456 | 
               }
  | 
        
        
            | 
            | 
           1457 | 
           }
  | 
        
        
            | 
            | 
           1458 | 
              | 
        
        
            | 
            | 
           1459 | 
           /**
  | 
        
        
            | 
            | 
           1460 | 
            * Actual implementation of the reset course functionality, delete all the
  | 
        
        
            | 
            | 
           1461 | 
            * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
  | 
        
        
            | 
            | 
           1462 | 
            * set and true.
  | 
        
        
            | 
            | 
           1463 | 
            *
  | 
        
        
            | 
            | 
           1464 | 
            * Also, move the quiz open and close dates, if the course start date is changing.
  | 
        
        
            | 
            | 
           1465 | 
            *
  | 
        
        
            | 
            | 
           1466 | 
            * @param stdClass $data the data submitted from the reset course.
  | 
        
        
            | 
            | 
           1467 | 
            * @return array status array
  | 
        
        
            | 
            | 
           1468 | 
            */
  | 
        
        
            | 
            | 
           1469 | 
           function quiz_reset_userdata($data) {
  | 
        
        
            | 
            | 
           1470 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           1471 | 
               require_once($CFG->libdir . '/questionlib.php');
  | 
        
        
            | 
            | 
           1472 | 
              | 
        
        
            | 
            | 
           1473 | 
               $componentstr = get_string('modulenameplural', 'quiz');
  | 
        
        
            | 
            | 
           1474 | 
               $status = [];
  | 
        
        
            | 
            | 
           1475 | 
              | 
        
        
            | 
            | 
           1476 | 
               // Delete attempts.
  | 
        
        
            | 
            | 
           1477 | 
               if (!empty($data->reset_quiz_attempts)) {
  | 
        
        
            | 
            | 
           1478 | 
                   question_engine::delete_questions_usage_by_activities(new qubaid_join(
  | 
        
        
            | 
            | 
           1479 | 
                           '{quiz_attempts} quiza JOIN {quiz} quiz ON quiza.quiz = quiz.id',
  | 
        
        
            | 
            | 
           1480 | 
                           'quiza.uniqueid', 'quiz.course = :quizcourseid',
  | 
        
        
            | 
            | 
           1481 | 
                           ['quizcourseid' => $data->courseid]));
  | 
        
        
            | 
            | 
           1482 | 
              | 
        
        
            | 
            | 
           1483 | 
                   $DB->delete_records_select('quiz_attempts',
  | 
        
        
            | 
            | 
           1484 | 
                           'quiz IN (SELECT id FROM {quiz} WHERE course = ?)', [$data->courseid]);
  | 
        
        
            | 
            | 
           1485 | 
                   $status[] = [
  | 
        
        
            | 
            | 
           1486 | 
                       'component' => $componentstr,
  | 
        
        
           | 1441 | 
           ariadna | 
           1487 | 
                       'item' => get_string('removeallquizattempts', 'quiz'),
  | 
        
        
           | 1 | 
           efrain | 
           1488 | 
                       'error' => false];
  | 
        
        
            | 
            | 
           1489 | 
              | 
        
        
            | 
            | 
           1490 | 
                   // Remove all grades from gradebook.
  | 
        
        
            | 
            | 
           1491 | 
                   $DB->delete_records_select('quiz_grades',
  | 
        
        
            | 
            | 
           1492 | 
                           'quiz IN (SELECT id FROM {quiz} WHERE course = ?)', [$data->courseid]);
  | 
        
        
            | 
            | 
           1493 | 
                   if (empty($data->reset_gradebook_grades)) {
  | 
        
        
            | 
            | 
           1494 | 
                       quiz_reset_gradebook($data->courseid);
  | 
        
        
            | 
            | 
           1495 | 
                   }
  | 
        
        
            | 
            | 
           1496 | 
                   $status[] = [
  | 
        
        
            | 
            | 
           1497 | 
                       'component' => $componentstr,
  | 
        
        
           | 1441 | 
           ariadna | 
           1498 | 
                       'item' => get_string('grades'),
  | 
        
        
           | 1 | 
           efrain | 
           1499 | 
                       'error' => false];
  | 
        
        
            | 
            | 
           1500 | 
               }
  | 
        
        
            | 
            | 
           1501 | 
              | 
        
        
            | 
            | 
           1502 | 
               $purgeoverrides = false;
  | 
        
        
            | 
            | 
           1503 | 
              | 
        
        
            | 
            | 
           1504 | 
               // Remove user overrides.
  | 
        
        
            | 
            | 
           1505 | 
               if (!empty($data->reset_quiz_user_overrides)) {
  | 
        
        
            | 
            | 
           1506 | 
                   $DB->delete_records_select('quiz_overrides',
  | 
        
        
            | 
            | 
           1507 | 
                           'quiz IN (SELECT id FROM {quiz} WHERE course = ?) AND userid IS NOT NULL', [$data->courseid]);
  | 
        
        
            | 
            | 
           1508 | 
                   $status[] = [
  | 
        
        
            | 
            | 
           1509 | 
                       'component' => $componentstr,
  | 
        
        
           | 1441 | 
           ariadna | 
           1510 | 
                       'item' => get_string('useroverrides', 'quiz'),
  | 
        
        
           | 1 | 
           efrain | 
           1511 | 
                       'error' => false];
  | 
        
        
            | 
            | 
           1512 | 
                   $purgeoverrides = true;
  | 
        
        
            | 
            | 
           1513 | 
               }
  | 
        
        
            | 
            | 
           1514 | 
               // Remove group overrides.
  | 
        
        
            | 
            | 
           1515 | 
               if (!empty($data->reset_quiz_group_overrides)) {
  | 
        
        
            | 
            | 
           1516 | 
                   $DB->delete_records_select('quiz_overrides',
  | 
        
        
            | 
            | 
           1517 | 
                           'quiz IN (SELECT id FROM {quiz} WHERE course = ?) AND groupid IS NOT NULL', [$data->courseid]);
  | 
        
        
            | 
            | 
           1518 | 
                   $status[] = [
  | 
        
        
            | 
            | 
           1519 | 
                       'component' => $componentstr,
  | 
        
        
           | 1441 | 
           ariadna | 
           1520 | 
                       'item' => get_string('groupoverrides', 'quiz'),
  | 
        
        
           | 1 | 
           efrain | 
           1521 | 
                       'error' => false];
  | 
        
        
            | 
            | 
           1522 | 
                   $purgeoverrides = true;
  | 
        
        
            | 
            | 
           1523 | 
               }
  | 
        
        
            | 
            | 
           1524 | 
              | 
        
        
            | 
            | 
           1525 | 
               // Updating dates - shift may be negative too.
  | 
        
        
            | 
            | 
           1526 | 
               if ($data->timeshift) {
  | 
        
        
            | 
            | 
           1527 | 
                   $DB->execute("UPDATE {quiz_overrides}
  | 
        
        
            | 
            | 
           1528 | 
                                    SET timeopen = timeopen + ?
  | 
        
        
            | 
            | 
           1529 | 
                                  WHERE quiz IN (SELECT id FROM {quiz} WHERE course = ?)
  | 
        
        
            | 
            | 
           1530 | 
                                    AND timeopen <> 0", [$data->timeshift, $data->courseid]);
  | 
        
        
            | 
            | 
           1531 | 
                   $DB->execute("UPDATE {quiz_overrides}
  | 
        
        
            | 
            | 
           1532 | 
                                    SET timeclose = timeclose + ?
  | 
        
        
            | 
            | 
           1533 | 
                                  WHERE quiz IN (SELECT id FROM {quiz} WHERE course = ?)
  | 
        
        
            | 
            | 
           1534 | 
                                    AND timeclose <> 0", [$data->timeshift, $data->courseid]);
  | 
        
        
            | 
            | 
           1535 | 
              | 
        
        
            | 
            | 
           1536 | 
                   $purgeoverrides = true;
  | 
        
        
            | 
            | 
           1537 | 
              | 
        
        
            | 
            | 
           1538 | 
                   // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  | 
        
        
            | 
            | 
           1539 | 
                   // See MDL-9367.
  | 
        
        
            | 
            | 
           1540 | 
                   shift_course_mod_dates('quiz', ['timeopen', 'timeclose'],
  | 
        
        
            | 
            | 
           1541 | 
                           $data->timeshift, $data->courseid);
  | 
        
        
            | 
            | 
           1542 | 
              | 
        
        
            | 
            | 
           1543 | 
                   $status[] = [
  | 
        
        
            | 
            | 
           1544 | 
                       'component' => $componentstr,
  | 
        
        
            | 
            | 
           1545 | 
                       'item' => get_string('openclosedatesupdated', 'quiz'),
  | 
        
        
            | 
            | 
           1546 | 
                       'error' => false];
  | 
        
        
            | 
            | 
           1547 | 
               }
  | 
        
        
            | 
            | 
           1548 | 
              | 
        
        
            | 
            | 
           1549 | 
               if ($purgeoverrides) {
  | 
        
        
            | 
            | 
           1550 | 
                   \cache_helper::purge_by_event(\mod_quiz\local\override_cache::INVALIDATION_USERDATARESET);
  | 
        
        
            | 
            | 
           1551 | 
               }
  | 
        
        
            | 
            | 
           1552 | 
              | 
        
        
            | 
            | 
           1553 | 
               return $status;
  | 
        
        
            | 
            | 
           1554 | 
           }
  | 
        
        
            | 
            | 
           1555 | 
              | 
        
        
            | 
            | 
           1556 | 
           /**
  | 
        
        
            | 
            | 
           1557 | 
            * Return a textual summary of the number of attempts that have been made at a particular quiz,
  | 
        
        
            | 
            | 
           1558 | 
            * returns '' if no attempts have been made yet, unless $returnzero is passed as true.
  | 
        
        
            | 
            | 
           1559 | 
            *
  | 
        
        
            | 
            | 
           1560 | 
            * @param stdClass $quiz the quiz object. Only $quiz->id is used at the moment.
  | 
        
        
            | 
            | 
           1561 | 
            * @param stdClass $cm the cm object. Only $cm->course, $cm->groupmode and
  | 
        
        
            | 
            | 
           1562 | 
            *      $cm->groupingid fields are used at the moment.
  | 
        
        
            | 
            | 
           1563 | 
            * @param bool $returnzero if false (default), when no attempts have been
  | 
        
        
            | 
            | 
           1564 | 
            *      made '' is returned instead of 'Attempts: 0'.
  | 
        
        
            | 
            | 
           1565 | 
            * @param int $currentgroup if there is a concept of current group where this method is being called
  | 
        
        
            | 
            | 
           1566 | 
            *         (e.g. a report) pass it in here. Default 0 which means no current group.
  | 
        
        
            | 
            | 
           1567 | 
            * @return string a string like "Attempts: 123", "Attemtps 123 (45 from your groups)" or
  | 
        
        
            | 
            | 
           1568 | 
            *          "Attemtps 123 (45 from this group)".
  | 
        
        
            | 
            | 
           1569 | 
            */
  | 
        
        
            | 
            | 
           1570 | 
           function quiz_num_attempt_summary($quiz, $cm, $returnzero = false, $currentgroup = 0) {
  | 
        
        
            | 
            | 
           1571 | 
               global $DB, $USER;
  | 
        
        
            | 
            | 
           1572 | 
               $numattempts = $DB->count_records('quiz_attempts', ['quiz' => $quiz->id, 'preview' => 0]);
  | 
        
        
            | 
            | 
           1573 | 
               if ($numattempts || $returnzero) {
  | 
        
        
            | 
            | 
           1574 | 
                   if (groups_get_activity_groupmode($cm)) {
  | 
        
        
            | 
            | 
           1575 | 
                       $a = new stdClass();
  | 
        
        
            | 
            | 
           1576 | 
                       $a->total = $numattempts;
  | 
        
        
            | 
            | 
           1577 | 
                       if ($currentgroup) {
  | 
        
        
            | 
            | 
           1578 | 
                           $a->group = $DB->count_records_sql('SELECT COUNT(DISTINCT qa.id) FROM ' .
  | 
        
        
            | 
            | 
           1579 | 
                                   '{quiz_attempts} qa JOIN ' .
  | 
        
        
            | 
            | 
           1580 | 
                                   '{groups_members} gm ON qa.userid = gm.userid ' .
  | 
        
        
            | 
            | 
           1581 | 
                                   'WHERE quiz = ? AND preview = 0 AND groupid = ?',
  | 
        
        
            | 
            | 
           1582 | 
                                   [$quiz->id, $currentgroup]);
  | 
        
        
            | 
            | 
           1583 | 
                           return get_string('attemptsnumthisgroup', 'quiz', $a);
  | 
        
        
            | 
            | 
           1584 | 
                       } else if ($groups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
  | 
        
        
           | 1441 | 
           ariadna | 
           1585 | 
                           [$usql, $params] = $DB->get_in_or_equal(array_keys($groups));
  | 
        
        
           | 1 | 
           efrain | 
           1586 | 
                           $a->group = $DB->count_records_sql('SELECT COUNT(DISTINCT qa.id) FROM ' .
  | 
        
        
            | 
            | 
           1587 | 
                                   '{quiz_attempts} qa JOIN ' .
  | 
        
        
            | 
            | 
           1588 | 
                                   '{groups_members} gm ON qa.userid = gm.userid ' .
  | 
        
        
            | 
            | 
           1589 | 
                                   'WHERE quiz = ? AND preview = 0 AND ' .
  | 
        
        
            | 
            | 
           1590 | 
                                   "groupid $usql", array_merge([$quiz->id], $params));
  | 
        
        
            | 
            | 
           1591 | 
                           return get_string('attemptsnumyourgroups', 'quiz', $a);
  | 
        
        
            | 
            | 
           1592 | 
                       }
  | 
        
        
            | 
            | 
           1593 | 
                   }
  | 
        
        
            | 
            | 
           1594 | 
                   return get_string('attemptsnum', 'quiz', $numattempts);
  | 
        
        
            | 
            | 
           1595 | 
               }
  | 
        
        
            | 
            | 
           1596 | 
               return '';
  | 
        
        
            | 
            | 
           1597 | 
           }
  | 
        
        
            | 
            | 
           1598 | 
              | 
        
        
            | 
            | 
           1599 | 
           /**
  | 
        
        
            | 
            | 
           1600 | 
            * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
  | 
        
        
            | 
            | 
           1601 | 
            * to the quiz reports.
  | 
        
        
            | 
            | 
           1602 | 
            *
  | 
        
        
            | 
            | 
           1603 | 
            * @param stdClass $quiz the quiz object. Only $quiz->id is used at the moment.
  | 
        
        
            | 
            | 
           1604 | 
            * @param stdClass $cm the cm object. Only $cm->course, $cm->groupmode and
  | 
        
        
            | 
            | 
           1605 | 
            *      $cm->groupingid fields are used at the moment.
  | 
        
        
            | 
            | 
           1606 | 
            * @param stdClass $context the quiz context.
  | 
        
        
            | 
            | 
           1607 | 
            * @param bool $returnzero if false (default), when no attempts have been made
  | 
        
        
            | 
            | 
           1608 | 
            *      '' is returned instead of 'Attempts: 0'.
  | 
        
        
            | 
            | 
           1609 | 
            * @param int $currentgroup if there is a concept of current group where this method is being called
  | 
        
        
            | 
            | 
           1610 | 
            *         (e.g. a report) pass it in here. Default 0 which means no current group.
  | 
        
        
            | 
            | 
           1611 | 
            * @return string HTML fragment for the link.
  | 
        
        
            | 
            | 
           1612 | 
            */
  | 
        
        
            | 
            | 
           1613 | 
           function quiz_attempt_summary_link_to_reports($quiz, $cm, $context, $returnzero = false,
  | 
        
        
            | 
            | 
           1614 | 
                   $currentgroup = 0) {
  | 
        
        
            | 
            | 
           1615 | 
               global $PAGE;
  | 
        
        
            | 
            | 
           1616 | 
              | 
        
        
            | 
            | 
           1617 | 
               return $PAGE->get_renderer('mod_quiz')->quiz_attempt_summary_link_to_reports(
  | 
        
        
            | 
            | 
           1618 | 
                       $quiz, $cm, $context, $returnzero, $currentgroup);
  | 
        
        
            | 
            | 
           1619 | 
           }
  | 
        
        
            | 
            | 
           1620 | 
              | 
        
        
            | 
            | 
           1621 | 
           /**
  | 
        
        
            | 
            | 
           1622 | 
            * @param string $feature FEATURE_xx constant for requested feature
  | 
        
        
            | 
            | 
           1623 | 
            * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
  | 
        
        
            | 
            | 
           1624 | 
            */
  | 
        
        
            | 
            | 
           1625 | 
           function quiz_supports($feature) {
  | 
        
        
            | 
            | 
           1626 | 
               switch($feature) {
  | 
        
        
            | 
            | 
           1627 | 
                   case FEATURE_GROUPS:                    return true;
  | 
        
        
            | 
            | 
           1628 | 
                   case FEATURE_GROUPINGS:                 return true;
  | 
        
        
            | 
            | 
           1629 | 
                   case FEATURE_MOD_INTRO:                 return true;
  | 
        
        
            | 
            | 
           1630 | 
                   case FEATURE_COMPLETION_TRACKS_VIEWS:   return true;
  | 
        
        
            | 
            | 
           1631 | 
                   case FEATURE_COMPLETION_HAS_RULES:      return true;
  | 
        
        
            | 
            | 
           1632 | 
                   case FEATURE_GRADE_HAS_GRADE:           return true;
  | 
        
        
            | 
            | 
           1633 | 
                   case FEATURE_GRADE_OUTCOMES:            return true;
  | 
        
        
            | 
            | 
           1634 | 
                   case FEATURE_BACKUP_MOODLE2:            return true;
  | 
        
        
            | 
            | 
           1635 | 
                   case FEATURE_SHOW_DESCRIPTION:          return true;
  | 
        
        
            | 
            | 
           1636 | 
                   case FEATURE_CONTROLS_GRADE_VISIBILITY: return true;
  | 
        
        
            | 
            | 
           1637 | 
                   case FEATURE_USES_QUESTIONS:            return true;
  | 
        
        
            | 
            | 
           1638 | 
                   case FEATURE_PLAGIARISM:                return true;
  | 
        
        
            | 
            | 
           1639 | 
                   case FEATURE_MOD_PURPOSE:               return MOD_PURPOSE_ASSESSMENT;
  | 
        
        
            | 
            | 
           1640 | 
              | 
        
        
            | 
            | 
           1641 | 
                   default: return null;
  | 
        
        
            | 
            | 
           1642 | 
               }
  | 
        
        
            | 
            | 
           1643 | 
           }
  | 
        
        
            | 
            | 
           1644 | 
              | 
        
        
            | 
            | 
           1645 | 
           /**
  | 
        
        
            | 
            | 
           1646 | 
            * @return array all other caps used in module
  | 
        
        
            | 
            | 
           1647 | 
            */
  | 
        
        
            | 
            | 
           1648 | 
           function quiz_get_extra_capabilities() {
  | 
        
        
            | 
            | 
           1649 | 
               global $CFG;
  | 
        
        
            | 
            | 
           1650 | 
               require_once($CFG->libdir . '/questionlib.php');
  | 
        
        
            | 
            | 
           1651 | 
               return question_get_all_capabilities();
  | 
        
        
            | 
            | 
           1652 | 
           }
  | 
        
        
            | 
            | 
           1653 | 
              | 
        
        
            | 
            | 
           1654 | 
           /**
  | 
        
        
            | 
            | 
           1655 | 
            * This function extends the settings navigation block for the site.
  | 
        
        
            | 
            | 
           1656 | 
            *
  | 
        
        
            | 
            | 
           1657 | 
            * It is safe to rely on PAGE here as we will only ever be within the module
  | 
        
        
            | 
            | 
           1658 | 
            * context when this is called
  | 
        
        
            | 
            | 
           1659 | 
            *
  | 
        
        
            | 
            | 
           1660 | 
            * @param settings_navigation $settings
  | 
        
        
            | 
            | 
           1661 | 
            * @param navigation_node $quiznode
  | 
        
        
            | 
            | 
           1662 | 
            * @return void
  | 
        
        
            | 
            | 
           1663 | 
            */
  | 
        
        
            | 
            | 
           1664 | 
           function quiz_extend_settings_navigation(settings_navigation $settings, navigation_node $quiznode) {
  | 
        
        
            | 
            | 
           1665 | 
               global $CFG;
  | 
        
        
            | 
            | 
           1666 | 
              | 
        
        
            | 
            | 
           1667 | 
               // Require {@link questionlib.php}
  | 
        
        
            | 
            | 
           1668 | 
               // Included here as we only ever want to include this file if we really need to.
  | 
        
        
            | 
            | 
           1669 | 
               require_once($CFG->libdir . '/questionlib.php');
  | 
        
        
            | 
            | 
           1670 | 
              | 
        
        
            | 
            | 
           1671 | 
               // We want to add these new nodes after the Edit settings node, and before the
  | 
        
        
            | 
            | 
           1672 | 
               // Locally assigned roles node. Of course, both of those are controlled by capabilities.
  | 
        
        
            | 
            | 
           1673 | 
               $keys = $quiznode->get_children_key_list();
  | 
        
        
            | 
            | 
           1674 | 
               $beforekey = null;
  | 
        
        
            | 
            | 
           1675 | 
               $i = array_search('modedit', $keys);
  | 
        
        
            | 
            | 
           1676 | 
               if ($i === false and array_key_exists(0, $keys)) {
  | 
        
        
            | 
            | 
           1677 | 
                   $beforekey = $keys[0];
  | 
        
        
            | 
            | 
           1678 | 
               } else if (array_key_exists($i + 1, $keys)) {
  | 
        
        
            | 
            | 
           1679 | 
                   $beforekey = $keys[$i + 1];
  | 
        
        
            | 
            | 
           1680 | 
               }
  | 
        
        
            | 
            | 
           1681 | 
              | 
        
        
            | 
            | 
           1682 | 
               if (has_any_capability(['mod/quiz:manageoverrides', 'mod/quiz:viewoverrides'], $settings->get_page()->cm->context)) {
  | 
        
        
            | 
            | 
           1683 | 
                   $url = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $settings->get_page()->cm->id, 'mode' => 'user']);
  | 
        
        
            | 
            | 
           1684 | 
                   $node = navigation_node::create(get_string('overrides', 'quiz'),
  | 
        
        
            | 
            | 
           1685 | 
                               $url, navigation_node::TYPE_SETTING, null, 'mod_quiz_useroverrides');
  | 
        
        
            | 
            | 
           1686 | 
                   $settingsoverride = $quiznode->add_node($node, $beforekey);
  | 
        
        
            | 
            | 
           1687 | 
               }
  | 
        
        
            | 
            | 
           1688 | 
              | 
        
        
            | 
            | 
           1689 | 
               if (has_capability('mod/quiz:manage', $settings->get_page()->cm->context)) {
  | 
        
        
            | 
            | 
           1690 | 
                   $node = navigation_node::create(get_string('questions', 'quiz'),
  | 
        
        
            | 
            | 
           1691 | 
                       new moodle_url('/mod/quiz/edit.php', ['cmid' => $settings->get_page()->cm->id]),
  | 
        
        
            | 
            | 
           1692 | 
                       navigation_node::TYPE_SETTING, null, 'mod_quiz_edit', new pix_icon('t/edit', ''));
  | 
        
        
            | 
            | 
           1693 | 
                   $quiznode->add_node($node, $beforekey);
  | 
        
        
            | 
            | 
           1694 | 
               }
  | 
        
        
            | 
            | 
           1695 | 
              | 
        
        
            | 
            | 
           1696 | 
               if (has_capability('mod/quiz:preview', $settings->get_page()->cm->context)) {
  | 
        
        
            | 
            | 
           1697 | 
                   $url = new moodle_url('/mod/quiz/startattempt.php',
  | 
        
        
            | 
            | 
           1698 | 
                           ['cmid' => $settings->get_page()->cm->id, 'sesskey' => sesskey()]);
  | 
        
        
            | 
            | 
           1699 | 
                   $node = navigation_node::create(get_string('preview', 'quiz'), $url,
  | 
        
        
            | 
            | 
           1700 | 
                           navigation_node::TYPE_SETTING, null, 'mod_quiz_preview',
  | 
        
        
            | 
            | 
           1701 | 
                           new pix_icon('i/preview', ''));
  | 
        
        
            | 
            | 
           1702 | 
                   $previewnode = $quiznode->add_node($node, $beforekey);
  | 
        
        
            | 
            | 
           1703 | 
                   $previewnode->set_show_in_secondary_navigation(false);
  | 
        
        
            | 
            | 
           1704 | 
               }
  | 
        
        
            | 
            | 
           1705 | 
              | 
        
        
            | 
            | 
           1706 | 
               question_extend_settings_navigation($quiznode, $settings->get_page()->cm->context)->trim_if_empty();
  | 
        
        
            | 
            | 
           1707 | 
              | 
        
        
            | 
            | 
           1708 | 
               if (has_any_capability(['mod/quiz:viewreports', 'mod/quiz:grade'], $settings->get_page()->cm->context)) {
  | 
        
        
            | 
            | 
           1709 | 
                   require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  | 
        
        
            | 
            | 
           1710 | 
                   $reportlist = quiz_report_list($settings->get_page()->cm->context);
  | 
        
        
            | 
            | 
           1711 | 
              | 
        
        
            | 
            | 
           1712 | 
                   $url = new moodle_url('/mod/quiz/report.php',
  | 
        
        
            | 
            | 
           1713 | 
                           ['id' => $settings->get_page()->cm->id, 'mode' => reset($reportlist)]);
  | 
        
        
            | 
            | 
           1714 | 
                   $reportnode = $quiznode->add_node(navigation_node::create(get_string('results', 'quiz'), $url,
  | 
        
        
            | 
            | 
           1715 | 
                           navigation_node::TYPE_SETTING,
  | 
        
        
            | 
            | 
           1716 | 
                           null, 'quiz_report', new pix_icon('i/report', '')));
  | 
        
        
            | 
            | 
           1717 | 
              | 
        
        
            | 
            | 
           1718 | 
                   foreach ($reportlist as $report) {
  | 
        
        
            | 
            | 
           1719 | 
                       $url = new moodle_url('/mod/quiz/report.php', ['id' => $settings->get_page()->cm->id, 'mode' => $report]);
  | 
        
        
            | 
            | 
           1720 | 
                       $reportnode->add_node(navigation_node::create(get_string($report, 'quiz_'.$report), $url,
  | 
        
        
            | 
            | 
           1721 | 
                               navigation_node::TYPE_SETTING,
  | 
        
        
            | 
            | 
           1722 | 
                               null, 'quiz_report_' . $report, new pix_icon('i/item', '')));
  | 
        
        
            | 
            | 
           1723 | 
                   }
  | 
        
        
            | 
            | 
           1724 | 
               }
  | 
        
        
            | 
            | 
           1725 | 
           }
  | 
        
        
            | 
            | 
           1726 | 
              | 
        
        
            | 
            | 
           1727 | 
           /**
  | 
        
        
            | 
            | 
           1728 | 
            * Serves the quiz files.
  | 
        
        
            | 
            | 
           1729 | 
            *
  | 
        
        
            | 
            | 
           1730 | 
            * @package  mod_quiz
  | 
        
        
            | 
            | 
           1731 | 
            * @category files
  | 
        
        
            | 
            | 
           1732 | 
            * @param stdClass $course course object
  | 
        
        
            | 
            | 
           1733 | 
            * @param stdClass $cm course module object
  | 
        
        
            | 
            | 
           1734 | 
            * @param stdClass $context context object
  | 
        
        
            | 
            | 
           1735 | 
            * @param string $filearea file area
  | 
        
        
            | 
            | 
           1736 | 
            * @param array $args extra arguments
  | 
        
        
            | 
            | 
           1737 | 
            * @param bool $forcedownload whether or not force download
  | 
        
        
            | 
            | 
           1738 | 
            * @param array $options additional options affecting the file serving
  | 
        
        
            | 
            | 
           1739 | 
            * @return bool false if file not found, does not return if found - justsend the file
  | 
        
        
            | 
            | 
           1740 | 
            */
  | 
        
        
            | 
            | 
           1741 | 
           function quiz_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options= []) {
  | 
        
        
            | 
            | 
           1742 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           1743 | 
              | 
        
        
            | 
            | 
           1744 | 
               if ($context->contextlevel != CONTEXT_MODULE) {
  | 
        
        
            | 
            | 
           1745 | 
                   return false;
  | 
        
        
            | 
            | 
           1746 | 
               }
  | 
        
        
            | 
            | 
           1747 | 
              | 
        
        
            | 
            | 
           1748 | 
               require_login($course, false, $cm);
  | 
        
        
            | 
            | 
           1749 | 
              | 
        
        
            | 
            | 
           1750 | 
               if (!$quiz = $DB->get_record('quiz', ['id' => $cm->instance])) {
  | 
        
        
            | 
            | 
           1751 | 
                   return false;
  | 
        
        
            | 
            | 
           1752 | 
               }
  | 
        
        
            | 
            | 
           1753 | 
              | 
        
        
            | 
            | 
           1754 | 
               // The 'intro' area is served by pluginfile.php.
  | 
        
        
            | 
            | 
           1755 | 
               $fileareas = ['feedback'];
  | 
        
        
            | 
            | 
           1756 | 
               if (!in_array($filearea, $fileareas)) {
  | 
        
        
            | 
            | 
           1757 | 
                   return false;
  | 
        
        
            | 
            | 
           1758 | 
               }
  | 
        
        
            | 
            | 
           1759 | 
              | 
        
        
            | 
            | 
           1760 | 
               $feedbackid = (int)array_shift($args);
  | 
        
        
            | 
            | 
           1761 | 
               if (!$feedback = $DB->get_record('quiz_feedback', ['id' => $feedbackid])) {
  | 
        
        
            | 
            | 
           1762 | 
                   return false;
  | 
        
        
            | 
            | 
           1763 | 
               }
  | 
        
        
            | 
            | 
           1764 | 
              | 
        
        
            | 
            | 
           1765 | 
               $fs = get_file_storage();
  | 
        
        
            | 
            | 
           1766 | 
               $relativepath = implode('/', $args);
  | 
        
        
            | 
            | 
           1767 | 
               $fullpath = "/$context->id/mod_quiz/$filearea/$feedbackid/$relativepath";
  | 
        
        
            | 
            | 
           1768 | 
               if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  | 
        
        
            | 
            | 
           1769 | 
                   return false;
  | 
        
        
            | 
            | 
           1770 | 
               }
  | 
        
        
            | 
            | 
           1771 | 
               send_stored_file($file, 0, 0, true, $options);
  | 
        
        
            | 
            | 
           1772 | 
           }
  | 
        
        
            | 
            | 
           1773 | 
              | 
        
        
            | 
            | 
           1774 | 
           /**
  | 
        
        
            | 
            | 
           1775 | 
            * Called via pluginfile.php -> question_pluginfile to serve files belonging to
  | 
        
        
            | 
            | 
           1776 | 
            * a question in a question_attempt when that attempt is a quiz attempt.
  | 
        
        
            | 
            | 
           1777 | 
            *
  | 
        
        
            | 
            | 
           1778 | 
            * @package  mod_quiz
  | 
        
        
            | 
            | 
           1779 | 
            * @category files
  | 
        
        
            | 
            | 
           1780 | 
            * @param stdClass $course course settings object
  | 
        
        
            | 
            | 
           1781 | 
            * @param stdClass $context context object
  | 
        
        
            | 
            | 
           1782 | 
            * @param string $component the name of the component we are serving files for.
  | 
        
        
            | 
            | 
           1783 | 
            * @param string $filearea the name of the file area.
  | 
        
        
            | 
            | 
           1784 | 
            * @param int $qubaid the attempt usage id.
  | 
        
        
            | 
            | 
           1785 | 
            * @param int $slot the id of a question in this quiz attempt.
  | 
        
        
            | 
            | 
           1786 | 
            * @param array $args the remaining bits of the file path.
  | 
        
        
            | 
            | 
           1787 | 
            * @param bool $forcedownload whether the user must be forced to download the file.
  | 
        
        
            | 
            | 
           1788 | 
            * @param array $options additional options affecting the file serving
  | 
        
        
            | 
            | 
           1789 | 
            * @return bool false if file not found, does not return if found - justsend the file
  | 
        
        
            | 
            | 
           1790 | 
            */
  | 
        
        
            | 
            | 
           1791 | 
           function quiz_question_pluginfile($course, $context, $component,
  | 
        
        
            | 
            | 
           1792 | 
                   $filearea, $qubaid, $slot, $args, $forcedownload, array $options= []) {
  | 
        
        
            | 
            | 
           1793 | 
               global $CFG;
  | 
        
        
            | 
            | 
           1794 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           1795 | 
              | 
        
        
            | 
            | 
           1796 | 
               $attemptobj = quiz_attempt::create_from_usage_id($qubaid);
  | 
        
        
            | 
            | 
           1797 | 
               require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  | 
        
        
            | 
            | 
           1798 | 
              | 
        
        
            | 
            | 
           1799 | 
               if ($attemptobj->is_own_attempt() && !$attemptobj->is_finished()) {
  | 
        
        
            | 
            | 
           1800 | 
                   // In the middle of an attempt.
  | 
        
        
            | 
            | 
           1801 | 
                   if (!$attemptobj->is_preview_user()) {
  | 
        
        
            | 
            | 
           1802 | 
                       $attemptobj->require_capability('mod/quiz:attempt');
  | 
        
        
            | 
            | 
           1803 | 
                   }
  | 
        
        
            | 
            | 
           1804 | 
                   $isreviewing = false;
  | 
        
        
            | 
            | 
           1805 | 
              | 
        
        
            | 
            | 
           1806 | 
               } else {
  | 
        
        
            | 
            | 
           1807 | 
                   // Reviewing an attempt.
  | 
        
        
            | 
            | 
           1808 | 
                   $attemptobj->check_review_capability();
  | 
        
        
            | 
            | 
           1809 | 
                   $isreviewing = true;
  | 
        
        
            | 
            | 
           1810 | 
               }
  | 
        
        
            | 
            | 
           1811 | 
              | 
        
        
            | 
            | 
           1812 | 
               if (!$attemptobj->check_file_access($slot, $isreviewing, $context->id,
  | 
        
        
            | 
            | 
           1813 | 
                       $component, $filearea, $args, $forcedownload)) {
  | 
        
        
            | 
            | 
           1814 | 
                   send_file_not_found();
  | 
        
        
            | 
            | 
           1815 | 
               }
  | 
        
        
            | 
            | 
           1816 | 
              | 
        
        
            | 
            | 
           1817 | 
               $fs = get_file_storage();
  | 
        
        
            | 
            | 
           1818 | 
               $relativepath = implode('/', $args);
  | 
        
        
            | 
            | 
           1819 | 
               $fullpath = "/$context->id/$component/$filearea/$relativepath";
  | 
        
        
            | 
            | 
           1820 | 
               if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  | 
        
        
            | 
            | 
           1821 | 
                   send_file_not_found();
  | 
        
        
            | 
            | 
           1822 | 
               }
  | 
        
        
            | 
            | 
           1823 | 
              | 
        
        
            | 
            | 
           1824 | 
               send_stored_file($file, 0, 0, $forcedownload, $options);
  | 
        
        
            | 
            | 
           1825 | 
           }
  | 
        
        
            | 
            | 
           1826 | 
              | 
        
        
            | 
            | 
           1827 | 
           /**
  | 
        
        
            | 
            | 
           1828 | 
            * Return a list of page types
  | 
        
        
            | 
            | 
           1829 | 
            * @param string $pagetype current page type
  | 
        
        
            | 
            | 
           1830 | 
            * @param stdClass $parentcontext Block's parent context
  | 
        
        
            | 
            | 
           1831 | 
            * @param stdClass $currentcontext Current context of block
  | 
        
        
            | 
            | 
           1832 | 
            */
  | 
        
        
            | 
            | 
           1833 | 
           function quiz_page_type_list($pagetype, $parentcontext, $currentcontext) {
  | 
        
        
            | 
            | 
           1834 | 
               $modulepagetype = [
  | 
        
        
            | 
            | 
           1835 | 
                   'mod-quiz-*'       => get_string('page-mod-quiz-x', 'quiz'),
  | 
        
        
            | 
            | 
           1836 | 
                   'mod-quiz-view'    => get_string('page-mod-quiz-view', 'quiz'),
  | 
        
        
            | 
            | 
           1837 | 
                   'mod-quiz-attempt' => get_string('page-mod-quiz-attempt', 'quiz'),
  | 
        
        
            | 
            | 
           1838 | 
                   'mod-quiz-summary' => get_string('page-mod-quiz-summary', 'quiz'),
  | 
        
        
            | 
            | 
           1839 | 
                   'mod-quiz-review'  => get_string('page-mod-quiz-review', 'quiz'),
  | 
        
        
            | 
            | 
           1840 | 
                   'mod-quiz-edit'    => get_string('page-mod-quiz-edit', 'quiz'),
  | 
        
        
            | 
            | 
           1841 | 
                   'mod-quiz-report'  => get_string('page-mod-quiz-report', 'quiz'),
  | 
        
        
            | 
            | 
           1842 | 
               ];
  | 
        
        
            | 
            | 
           1843 | 
               return $modulepagetype;
  | 
        
        
            | 
            | 
           1844 | 
           }
  | 
        
        
            | 
            | 
           1845 | 
              | 
        
        
            | 
            | 
           1846 | 
           /**
  | 
        
        
            | 
            | 
           1847 | 
            * @return the options for quiz navigation.
  | 
        
        
            | 
            | 
           1848 | 
            */
  | 
        
        
            | 
            | 
           1849 | 
           function quiz_get_navigation_options() {
  | 
        
        
            | 
            | 
           1850 | 
               return [
  | 
        
        
            | 
            | 
           1851 | 
                   QUIZ_NAVMETHOD_FREE => get_string('navmethod_free', 'quiz'),
  | 
        
        
            | 
            | 
           1852 | 
                   QUIZ_NAVMETHOD_SEQ  => get_string('navmethod_seq', 'quiz')
  | 
        
        
            | 
            | 
           1853 | 
               ];
  | 
        
        
            | 
            | 
           1854 | 
           }
  | 
        
        
            | 
            | 
           1855 | 
              | 
        
        
            | 
            | 
           1856 | 
           /**
  | 
        
        
            | 
            | 
           1857 | 
            * Check if the module has any update that affects the current user since a given time.
  | 
        
        
            | 
            | 
           1858 | 
            *
  | 
        
        
            | 
            | 
           1859 | 
            * @param  cm_info $cm course module data
  | 
        
        
            | 
            | 
           1860 | 
            * @param  int $from the time to check updates from
  | 
        
        
            | 
            | 
           1861 | 
            * @param  array $filter  if we need to check only specific updates
  | 
        
        
            | 
            | 
           1862 | 
            * @return stdClass an object with the different type of areas indicating if they were updated or not
  | 
        
        
            | 
            | 
           1863 | 
            * @since Moodle 3.2
  | 
        
        
            | 
            | 
           1864 | 
            */
  | 
        
        
            | 
            | 
           1865 | 
           function quiz_check_updates_since(cm_info $cm, $from, $filter = []) {
  | 
        
        
            | 
            | 
           1866 | 
               global $DB, $USER, $CFG;
  | 
        
        
            | 
            | 
           1867 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           1868 | 
              | 
        
        
            | 
            | 
           1869 | 
               $updates = course_check_module_updates_since($cm, $from, [], $filter);
  | 
        
        
            | 
            | 
           1870 | 
              | 
        
        
            | 
            | 
           1871 | 
               // Check if questions were updated.
  | 
        
        
            | 
            | 
           1872 | 
               $updates->questions = (object) ['updated' => false];
  | 
        
        
            | 
            | 
           1873 | 
               $quizobj = quiz_settings::create($cm->instance, $USER->id);
  | 
        
        
            | 
            | 
           1874 | 
               $quizobj->preload_questions();
  | 
        
        
            | 
            | 
           1875 | 
               $questionids = array_keys($quizobj->get_questions(null, false));
  | 
        
        
            | 
            | 
           1876 | 
               if (!empty($questionids)) {
  | 
        
        
           | 1441 | 
           ariadna | 
           1877 | 
                   [$questionsql, $params] = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED);
  | 
        
        
           | 1 | 
           efrain | 
           1878 | 
                   $select = 'id ' . $questionsql . ' AND (timemodified > :time1 OR timecreated > :time2)';
  | 
        
        
            | 
            | 
           1879 | 
                   $params['time1'] = $from;
  | 
        
        
            | 
            | 
           1880 | 
                   $params['time2'] = $from;
  | 
        
        
            | 
            | 
           1881 | 
                   $questions = $DB->get_records_select('question', $select, $params, '', 'id');
  | 
        
        
            | 
            | 
           1882 | 
                   if (!empty($questions)) {
  | 
        
        
            | 
            | 
           1883 | 
                       $updates->questions->updated = true;
  | 
        
        
            | 
            | 
           1884 | 
                       $updates->questions->itemids = array_keys($questions);
  | 
        
        
            | 
            | 
           1885 | 
                   }
  | 
        
        
            | 
            | 
           1886 | 
               }
  | 
        
        
            | 
            | 
           1887 | 
              | 
        
        
            | 
            | 
           1888 | 
               // Check for new attempts or grades.
  | 
        
        
            | 
            | 
           1889 | 
               $updates->attempts = (object) ['updated' => false];
  | 
        
        
            | 
            | 
           1890 | 
               $updates->grades = (object) ['updated' => false];
  | 
        
        
            | 
            | 
           1891 | 
               $select = 'quiz = ? AND userid = ? AND timemodified > ?';
  | 
        
        
            | 
            | 
           1892 | 
               $params = [$cm->instance, $USER->id, $from];
  | 
        
        
            | 
            | 
           1893 | 
              | 
        
        
            | 
            | 
           1894 | 
               $attempts = $DB->get_records_select('quiz_attempts', $select, $params, '', 'id');
  | 
        
        
            | 
            | 
           1895 | 
               if (!empty($attempts)) {
  | 
        
        
            | 
            | 
           1896 | 
                   $updates->attempts->updated = true;
  | 
        
        
            | 
            | 
           1897 | 
                   $updates->attempts->itemids = array_keys($attempts);
  | 
        
        
            | 
            | 
           1898 | 
               }
  | 
        
        
            | 
            | 
           1899 | 
               $grades = $DB->get_records_select('quiz_grades', $select, $params, '', 'id');
  | 
        
        
            | 
            | 
           1900 | 
               if (!empty($grades)) {
  | 
        
        
            | 
            | 
           1901 | 
                   $updates->grades->updated = true;
  | 
        
        
            | 
            | 
           1902 | 
                   $updates->grades->itemids = array_keys($grades);
  | 
        
        
            | 
            | 
           1903 | 
               }
  | 
        
        
            | 
            | 
           1904 | 
              | 
        
        
            | 
            | 
           1905 | 
               // Now, teachers should see other students updates.
  | 
        
        
            | 
            | 
           1906 | 
               if (has_capability('mod/quiz:viewreports', $cm->context)) {
  | 
        
        
            | 
            | 
           1907 | 
                   $select = 'quiz = ? AND timemodified > ?';
  | 
        
        
            | 
            | 
           1908 | 
                   $params = [$cm->instance, $from];
  | 
        
        
            | 
            | 
           1909 | 
              | 
        
        
            | 
            | 
           1910 | 
                   if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
  | 
        
        
            | 
            | 
           1911 | 
                       $groupusers = array_keys(groups_get_activity_shared_group_members($cm));
  | 
        
        
            | 
            | 
           1912 | 
                       if (empty($groupusers)) {
  | 
        
        
            | 
            | 
           1913 | 
                           return $updates;
  | 
        
        
            | 
            | 
           1914 | 
                       }
  | 
        
        
           | 1441 | 
           ariadna | 
           1915 | 
                       [$insql, $inparams] = $DB->get_in_or_equal($groupusers);
  | 
        
        
           | 1 | 
           efrain | 
           1916 | 
                       $select .= ' AND userid ' . $insql;
  | 
        
        
            | 
            | 
           1917 | 
                       $params = array_merge($params, $inparams);
  | 
        
        
            | 
            | 
           1918 | 
                   }
  | 
        
        
            | 
            | 
           1919 | 
              | 
        
        
            | 
            | 
           1920 | 
                   $updates->userattempts = (object) ['updated' => false];
  | 
        
        
            | 
            | 
           1921 | 
                   $attempts = $DB->get_records_select('quiz_attempts', $select, $params, '', 'id');
  | 
        
        
            | 
            | 
           1922 | 
                   if (!empty($attempts)) {
  | 
        
        
            | 
            | 
           1923 | 
                       $updates->userattempts->updated = true;
  | 
        
        
            | 
            | 
           1924 | 
                       $updates->userattempts->itemids = array_keys($attempts);
  | 
        
        
            | 
            | 
           1925 | 
                   }
  | 
        
        
            | 
            | 
           1926 | 
              | 
        
        
            | 
            | 
           1927 | 
                   $updates->usergrades = (object) ['updated' => false];
  | 
        
        
            | 
            | 
           1928 | 
                   $grades = $DB->get_records_select('quiz_grades', $select, $params, '', 'id');
  | 
        
        
            | 
            | 
           1929 | 
                   if (!empty($grades)) {
  | 
        
        
            | 
            | 
           1930 | 
                       $updates->usergrades->updated = true;
  | 
        
        
            | 
            | 
           1931 | 
                       $updates->usergrades->itemids = array_keys($grades);
  | 
        
        
            | 
            | 
           1932 | 
                   }
  | 
        
        
            | 
            | 
           1933 | 
               }
  | 
        
        
            | 
            | 
           1934 | 
               return $updates;
  | 
        
        
            | 
            | 
           1935 | 
           }
  | 
        
        
            | 
            | 
           1936 | 
              | 
        
        
            | 
            | 
           1937 | 
           /**
  | 
        
        
            | 
            | 
           1938 | 
            * Get icon mapping for font-awesome.
  | 
        
        
            | 
            | 
           1939 | 
            */
  | 
        
        
            | 
            | 
           1940 | 
           function mod_quiz_get_fontawesome_icon_map() {
  | 
        
        
            | 
            | 
           1941 | 
               return [
  | 
        
        
            | 
            | 
           1942 | 
                   'mod_quiz:navflagged' => 'fa-flag',
  | 
        
        
            | 
            | 
           1943 | 
               ];
  | 
        
        
            | 
            | 
           1944 | 
           }
  | 
        
        
            | 
            | 
           1945 | 
              | 
        
        
            | 
            | 
           1946 | 
           /**
  | 
        
        
            | 
            | 
           1947 | 
            * This function receives a calendar event and returns the action associated with it, or null if there is none.
  | 
        
        
            | 
            | 
           1948 | 
            *
  | 
        
        
            | 
            | 
           1949 | 
            * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
  | 
        
        
            | 
            | 
           1950 | 
            * is not displayed on the block.
  | 
        
        
            | 
            | 
           1951 | 
            *
  | 
        
        
            | 
            | 
           1952 | 
            * @param calendar_event $event
  | 
        
        
            | 
            | 
           1953 | 
            * @param \core_calendar\action_factory $factory
  | 
        
        
            | 
            | 
           1954 | 
            * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
  | 
        
        
            | 
            | 
           1955 | 
            * @return \core_calendar\local\event\entities\action_interface|null
  | 
        
        
            | 
            | 
           1956 | 
            */
  | 
        
        
            | 
            | 
           1957 | 
           function mod_quiz_core_calendar_provide_event_action(calendar_event $event,
  | 
        
        
            | 
            | 
           1958 | 
                                                                \core_calendar\action_factory $factory,
  | 
        
        
            | 
            | 
           1959 | 
                                                                int $userid = 0) {
  | 
        
        
            | 
            | 
           1960 | 
               global $CFG, $USER;
  | 
        
        
            | 
            | 
           1961 | 
              | 
        
        
            | 
            | 
           1962 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           1963 | 
              | 
        
        
            | 
            | 
           1964 | 
               if (empty($userid)) {
  | 
        
        
            | 
            | 
           1965 | 
                   $userid = $USER->id;
  | 
        
        
            | 
            | 
           1966 | 
               }
  | 
        
        
            | 
            | 
           1967 | 
              | 
        
        
            | 
            | 
           1968 | 
               $cm = get_fast_modinfo($event->courseid, $userid)->instances['quiz'][$event->instance];
  | 
        
        
            | 
            | 
           1969 | 
               $quizobj = quiz_settings::create($cm->instance, $userid);
  | 
        
        
            | 
            | 
           1970 | 
               $quiz = $quizobj->get_quiz();
  | 
        
        
            | 
            | 
           1971 | 
              | 
        
        
            | 
            | 
           1972 | 
               // Check they have capabilities allowing them to view the quiz.
  | 
        
        
            | 
            | 
           1973 | 
               if (!has_any_capability(['mod/quiz:reviewmyattempts', 'mod/quiz:attempt'], $quizobj->get_context(), $userid)) {
  | 
        
        
            | 
            | 
           1974 | 
                   return null;
  | 
        
        
            | 
            | 
           1975 | 
               }
  | 
        
        
            | 
            | 
           1976 | 
              | 
        
        
            | 
            | 
           1977 | 
               $completion = new \completion_info($cm->get_course());
  | 
        
        
            | 
            | 
           1978 | 
              | 
        
        
            | 
            | 
           1979 | 
               $completiondata = $completion->get_data($cm, false, $userid);
  | 
        
        
            | 
            | 
           1980 | 
              | 
        
        
            | 
            | 
           1981 | 
               if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
  | 
        
        
            | 
            | 
           1982 | 
                   return null;
  | 
        
        
            | 
            | 
           1983 | 
               }
  | 
        
        
            | 
            | 
           1984 | 
              | 
        
        
            | 
            | 
           1985 | 
               quiz_update_effective_access($quiz, $userid);
  | 
        
        
            | 
            | 
           1986 | 
              | 
        
        
            | 
            | 
           1987 | 
               // Check if quiz is closed, if so don't display it.
  | 
        
        
            | 
            | 
           1988 | 
               if (!empty($quiz->timeclose) && $quiz->timeclose <= time()) {
  | 
        
        
            | 
            | 
           1989 | 
                   return null;
  | 
        
        
            | 
            | 
           1990 | 
               }
  | 
        
        
            | 
            | 
           1991 | 
              | 
        
        
            | 
            | 
           1992 | 
               if (!$quizobj->is_participant($userid)) {
  | 
        
        
            | 
            | 
           1993 | 
                   // If the user is not a participant then they have
  | 
        
        
            | 
            | 
           1994 | 
                   // no action to take. This will filter out the events for teachers.
  | 
        
        
            | 
            | 
           1995 | 
                   return null;
  | 
        
        
            | 
            | 
           1996 | 
               }
  | 
        
        
            | 
            | 
           1997 | 
              | 
        
        
            | 
            | 
           1998 | 
               $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $userid);
  | 
        
        
            | 
            | 
           1999 | 
               if (!empty($attempts)) {
  | 
        
        
            | 
            | 
           2000 | 
                   // The student's last attempt is finished.
  | 
        
        
            | 
            | 
           2001 | 
                   return null;
  | 
        
        
            | 
            | 
           2002 | 
               }
  | 
        
        
            | 
            | 
           2003 | 
              | 
        
        
            | 
            | 
           2004 | 
               $name = get_string('attemptquiznow', 'quiz');
  | 
        
        
            | 
            | 
           2005 | 
               $url = new \moodle_url('/mod/quiz/view.php', [
  | 
        
        
            | 
            | 
           2006 | 
                   'id' => $cm->id
  | 
        
        
            | 
            | 
           2007 | 
               ]);
  | 
        
        
            | 
            | 
           2008 | 
               $itemcount = 1;
  | 
        
        
            | 
            | 
           2009 | 
               $actionable = true;
  | 
        
        
            | 
            | 
           2010 | 
              | 
        
        
            | 
            | 
           2011 | 
               // Check if the quiz is not currently actionable.
  | 
        
        
            | 
            | 
           2012 | 
               if (!empty($quiz->timeopen) && $quiz->timeopen > time()) {
  | 
        
        
            | 
            | 
           2013 | 
                   $actionable = false;
  | 
        
        
            | 
            | 
           2014 | 
               }
  | 
        
        
            | 
            | 
           2015 | 
              | 
        
        
            | 
            | 
           2016 | 
               return $factory->create_instance(
  | 
        
        
            | 
            | 
           2017 | 
                   $name,
  | 
        
        
            | 
            | 
           2018 | 
                   $url,
  | 
        
        
            | 
            | 
           2019 | 
                   $itemcount,
  | 
        
        
            | 
            | 
           2020 | 
                   $actionable
  | 
        
        
            | 
            | 
           2021 | 
               );
  | 
        
        
            | 
            | 
           2022 | 
           }
  | 
        
        
            | 
            | 
           2023 | 
              | 
        
        
            | 
            | 
           2024 | 
           /**
  | 
        
        
            | 
            | 
           2025 | 
            * Add a get_coursemodule_info function in case any quiz type wants to add 'extra' information
  | 
        
        
            | 
            | 
           2026 | 
            * for the course (see resource).
  | 
        
        
            | 
            | 
           2027 | 
            *
  | 
        
        
            | 
            | 
           2028 | 
            * Given a course_module object, this function returns any "extra" information that may be needed
  | 
        
        
            | 
            | 
           2029 | 
            * when printing this activity in a course listing.  See get_array_of_activities() in course/lib.php.
  | 
        
        
            | 
            | 
           2030 | 
            *
  | 
        
        
            | 
            | 
           2031 | 
            * @param stdClass $coursemodule The coursemodule object (record).
  | 
        
        
            | 
            | 
           2032 | 
            * @return cached_cm_info|false An object on information that the courses
  | 
        
        
            | 
            | 
           2033 | 
            *                        will know about (most noticeably, an icon).
  | 
        
        
            | 
            | 
           2034 | 
            */
  | 
        
        
            | 
            | 
           2035 | 
           function quiz_get_coursemodule_info($coursemodule) {
  | 
        
        
            | 
            | 
           2036 | 
               global $DB;
  | 
        
        
            | 
            | 
           2037 | 
              | 
        
        
            | 
            | 
           2038 | 
               $dbparams = ['id' => $coursemodule->instance];
  | 
        
        
            | 
            | 
           2039 | 
               $fields = 'id, name, intro, introformat, completionattemptsexhausted, completionminattempts,
  | 
        
        
            | 
            | 
           2040 | 
                   timeopen, timeclose';
  | 
        
        
            | 
            | 
           2041 | 
               if (!$quiz = $DB->get_record('quiz', $dbparams, $fields)) {
  | 
        
        
            | 
            | 
           2042 | 
                   return false;
  | 
        
        
            | 
            | 
           2043 | 
               }
  | 
        
        
            | 
            | 
           2044 | 
              | 
        
        
            | 
            | 
           2045 | 
               $result = new cached_cm_info();
  | 
        
        
            | 
            | 
           2046 | 
               $result->name = $quiz->name;
  | 
        
        
            | 
            | 
           2047 | 
              | 
        
        
            | 
            | 
           2048 | 
               if ($coursemodule->showdescription) {
  | 
        
        
            | 
            | 
           2049 | 
                   // Convert intro to html. Do not filter cached version, filters run at display time.
  | 
        
        
            | 
            | 
           2050 | 
                   $result->content = format_module_intro('quiz', $quiz, $coursemodule->id, false);
  | 
        
        
            | 
            | 
           2051 | 
               }
  | 
        
        
            | 
            | 
           2052 | 
              | 
        
        
            | 
            | 
           2053 | 
               // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
  | 
        
        
            | 
            | 
           2054 | 
               if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
  | 
        
        
            | 
            | 
           2055 | 
                   if ($quiz->completionattemptsexhausted) {
  | 
        
        
            | 
            | 
           2056 | 
                       $result->customdata['customcompletionrules']['completionpassorattemptsexhausted'] = [
  | 
        
        
            | 
            | 
           2057 | 
                           'completionpassgrade' => $coursemodule->completionpassgrade,
  | 
        
        
            | 
            | 
           2058 | 
                           'completionattemptsexhausted' => $quiz->completionattemptsexhausted,
  | 
        
        
            | 
            | 
           2059 | 
                       ];
  | 
        
        
            | 
            | 
           2060 | 
                   } else {
  | 
        
        
            | 
            | 
           2061 | 
                       $result->customdata['customcompletionrules']['completionpassorattemptsexhausted'] = [];
  | 
        
        
            | 
            | 
           2062 | 
                   }
  | 
        
        
            | 
            | 
           2063 | 
              | 
        
        
            | 
            | 
           2064 | 
                   $result->customdata['customcompletionrules']['completionminattempts'] = $quiz->completionminattempts;
  | 
        
        
            | 
            | 
           2065 | 
               }
  | 
        
        
            | 
            | 
           2066 | 
              | 
        
        
            | 
            | 
           2067 | 
               // Populate some other values that can be used in calendar or on dashboard.
  | 
        
        
            | 
            | 
           2068 | 
               if ($quiz->timeopen) {
  | 
        
        
            | 
            | 
           2069 | 
                   $result->customdata['timeopen'] = $quiz->timeopen;
  | 
        
        
            | 
            | 
           2070 | 
               }
  | 
        
        
            | 
            | 
           2071 | 
               if ($quiz->timeclose) {
  | 
        
        
            | 
            | 
           2072 | 
                   $result->customdata['timeclose'] = $quiz->timeclose;
  | 
        
        
            | 
            | 
           2073 | 
               }
  | 
        
        
            | 
            | 
           2074 | 
              | 
        
        
            | 
            | 
           2075 | 
               return $result;
  | 
        
        
            | 
            | 
           2076 | 
           }
  | 
        
        
            | 
            | 
           2077 | 
              | 
        
        
            | 
            | 
           2078 | 
           /**
  | 
        
        
            | 
            | 
           2079 | 
            * Sets dynamic information about a course module
  | 
        
        
            | 
            | 
           2080 | 
            *
  | 
        
        
            | 
            | 
           2081 | 
            * This function is called from cm_info when displaying the module
  | 
        
        
            | 
            | 
           2082 | 
            *
  | 
        
        
            | 
            | 
           2083 | 
            * @param cm_info $cm
  | 
        
        
            | 
            | 
           2084 | 
            */
  | 
        
        
            | 
            | 
           2085 | 
           function mod_quiz_cm_info_dynamic(cm_info $cm) {
  | 
        
        
            | 
            | 
           2086 | 
               global $USER;
  | 
        
        
            | 
            | 
           2087 | 
              | 
        
        
            | 
            | 
           2088 | 
               $cache = new override_cache($cm->instance);
  | 
        
        
            | 
            | 
           2089 | 
               $override = $cache->get_cached_user_override($USER->id);
  | 
        
        
            | 
            | 
           2090 | 
              | 
        
        
            | 
            | 
           2091 | 
               if (!$override) {
  | 
        
        
            | 
            | 
           2092 | 
                   $override = (object) [
  | 
        
        
            | 
            | 
           2093 | 
                       'timeopen' => null,
  | 
        
        
            | 
            | 
           2094 | 
                       'timeclose' => null,
  | 
        
        
            | 
            | 
           2095 | 
                   ];
  | 
        
        
            | 
            | 
           2096 | 
               }
  | 
        
        
            | 
            | 
           2097 | 
              | 
        
        
            | 
            | 
           2098 | 
               // No need to look for group overrides if there are user overrides for both timeopen and timeclose.
  | 
        
        
            | 
            | 
           2099 | 
               if (is_null($override->timeopen) || is_null($override->timeclose)) {
  | 
        
        
            | 
            | 
           2100 | 
                   $opens = [];
  | 
        
        
            | 
            | 
           2101 | 
                   $closes = [];
  | 
        
        
            | 
            | 
           2102 | 
                   $groupings = groups_get_user_groups($cm->course, $USER->id);
  | 
        
        
            | 
            | 
           2103 | 
                   foreach ($groupings[0] as $groupid) {
  | 
        
        
            | 
            | 
           2104 | 
                       $groupoverride = $cache->get_cached_group_override($groupid);
  | 
        
        
            | 
            | 
           2105 | 
                       if (isset($groupoverride->timeopen)) {
  | 
        
        
            | 
            | 
           2106 | 
                           $opens[] = $groupoverride->timeopen;
  | 
        
        
            | 
            | 
           2107 | 
                       }
  | 
        
        
            | 
            | 
           2108 | 
                       if (isset($groupoverride->timeclose)) {
  | 
        
        
            | 
            | 
           2109 | 
                           $closes[] = $groupoverride->timeclose;
  | 
        
        
            | 
            | 
           2110 | 
                       }
  | 
        
        
            | 
            | 
           2111 | 
                   }
  | 
        
        
            | 
            | 
           2112 | 
                   // If there is a user override for a setting, ignore the group override.
  | 
        
        
            | 
            | 
           2113 | 
                   if (is_null($override->timeopen) && count($opens)) {
  | 
        
        
            | 
            | 
           2114 | 
                       $override->timeopen = min($opens);
  | 
        
        
            | 
            | 
           2115 | 
                   }
  | 
        
        
            | 
            | 
           2116 | 
                   if (is_null($override->timeclose) && count($closes)) {
  | 
        
        
            | 
            | 
           2117 | 
                       if (in_array(0, $closes)) {
  | 
        
        
            | 
            | 
           2118 | 
                           $override->timeclose = 0;
  | 
        
        
            | 
            | 
           2119 | 
                       } else {
  | 
        
        
            | 
            | 
           2120 | 
                           $override->timeclose = max($closes);
  | 
        
        
            | 
            | 
           2121 | 
                       }
  | 
        
        
            | 
            | 
           2122 | 
                   }
  | 
        
        
            | 
            | 
           2123 | 
               }
  | 
        
        
            | 
            | 
           2124 | 
              | 
        
        
            | 
            | 
           2125 | 
               // Populate some other values that can be used in calendar or on dashboard.
  | 
        
        
            | 
            | 
           2126 | 
               if (!is_null($override->timeopen)) {
  | 
        
        
            | 
            | 
           2127 | 
                   $cm->override_customdata('timeopen', $override->timeopen);
  | 
        
        
            | 
            | 
           2128 | 
               }
  | 
        
        
            | 
            | 
           2129 | 
               if (!is_null($override->timeclose)) {
  | 
        
        
            | 
            | 
           2130 | 
                   $cm->override_customdata('timeclose', $override->timeclose);
  | 
        
        
            | 
            | 
           2131 | 
               }
  | 
        
        
            | 
            | 
           2132 | 
           }
  | 
        
        
            | 
            | 
           2133 | 
              | 
        
        
            | 
            | 
           2134 | 
           /**
  | 
        
        
            | 
            | 
           2135 | 
            * Callback which returns human-readable strings describing the active completion custom rules for the module instance.
  | 
        
        
            | 
            | 
           2136 | 
            *
  | 
        
        
            | 
            | 
           2137 | 
            * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
  | 
        
        
            | 
            | 
           2138 | 
            * @return array $descriptions the array of descriptions for the custom rules.
  | 
        
        
            | 
            | 
           2139 | 
            */
  | 
        
        
            | 
            | 
           2140 | 
           function mod_quiz_get_completion_active_rule_descriptions($cm) {
  | 
        
        
            | 
            | 
           2141 | 
               // Values will be present in cm_info, and we assume these are up to date.
  | 
        
        
            | 
            | 
           2142 | 
               if (empty($cm->customdata['customcompletionrules'])
  | 
        
        
            | 
            | 
           2143 | 
                   || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
  | 
        
        
            | 
            | 
           2144 | 
                   return [];
  | 
        
        
            | 
            | 
           2145 | 
               }
  | 
        
        
            | 
            | 
           2146 | 
              | 
        
        
            | 
            | 
           2147 | 
               $descriptions = [];
  | 
        
        
            | 
            | 
           2148 | 
               $rules = $cm->customdata['customcompletionrules'];
  | 
        
        
            | 
            | 
           2149 | 
              | 
        
        
            | 
            | 
           2150 | 
               if (!empty($rules['completionpassorattemptsexhausted'])) {
  | 
        
        
            | 
            | 
           2151 | 
                   if (!empty($rules['completionpassorattemptsexhausted']['completionattemptsexhausted'])) {
  | 
        
        
            | 
            | 
           2152 | 
                       $descriptions[] = get_string('completionpassorattemptsexhausteddesc', 'quiz');
  | 
        
        
            | 
            | 
           2153 | 
                   }
  | 
        
        
            | 
            | 
           2154 | 
               } else {
  | 
        
        
            | 
            | 
           2155 | 
                   // Fallback.
  | 
        
        
            | 
            | 
           2156 | 
                   if (!empty($rules['completionattemptsexhausted'])) {
  | 
        
        
            | 
            | 
           2157 | 
                       $descriptions[] = get_string('completionpassorattemptsexhausteddesc', 'quiz');
  | 
        
        
            | 
            | 
           2158 | 
                   }
  | 
        
        
            | 
            | 
           2159 | 
               }
  | 
        
        
            | 
            | 
           2160 | 
              | 
        
        
            | 
            | 
           2161 | 
               if (!empty($rules['completionminattempts'])) {
  | 
        
        
            | 
            | 
           2162 | 
                   $descriptions[] = get_string('completionminattemptsdesc', 'quiz', $rules['completionminattempts']);
  | 
        
        
            | 
            | 
           2163 | 
               }
  | 
        
        
            | 
            | 
           2164 | 
              | 
        
        
            | 
            | 
           2165 | 
               return $descriptions;
  | 
        
        
            | 
            | 
           2166 | 
           }
  | 
        
        
            | 
            | 
           2167 | 
              | 
        
        
            | 
            | 
           2168 | 
           /**
  | 
        
        
            | 
            | 
           2169 | 
            * Returns the min and max values for the timestart property of a quiz
  | 
        
        
            | 
            | 
           2170 | 
            * activity event.
  | 
        
        
            | 
            | 
           2171 | 
            *
  | 
        
        
            | 
            | 
           2172 | 
            * The min and max values will be the timeopen and timeclose properties
  | 
        
        
            | 
            | 
           2173 | 
            * of the quiz, respectively, if they are set.
  | 
        
        
            | 
            | 
           2174 | 
            *
  | 
        
        
            | 
            | 
           2175 | 
            * If either value isn't set then null will be returned instead to
  | 
        
        
            | 
            | 
           2176 | 
            * indicate that there is no cutoff for that value.
  | 
        
        
            | 
            | 
           2177 | 
            *
  | 
        
        
            | 
            | 
           2178 | 
            * If the vent has no valid timestart range then [false, false] will
  | 
        
        
            | 
            | 
           2179 | 
            * be returned. This is the case for overriden events.
  | 
        
        
            | 
            | 
           2180 | 
            *
  | 
        
        
            | 
            | 
           2181 | 
            * A minimum and maximum cutoff return value will look like:
  | 
        
        
            | 
            | 
           2182 | 
            * [
  | 
        
        
            | 
            | 
           2183 | 
            *     [1505704373, 'The date must be after this date'],
  | 
        
        
            | 
            | 
           2184 | 
            *     [1506741172, 'The date must be before this date']
  | 
        
        
            | 
            | 
           2185 | 
            * ]
  | 
        
        
            | 
            | 
           2186 | 
            *
  | 
        
        
            | 
            | 
           2187 | 
            * @throws \moodle_exception
  | 
        
        
            | 
            | 
           2188 | 
            * @param \calendar_event $event The calendar event to get the time range for
  | 
        
        
            | 
            | 
           2189 | 
            * @param stdClass $quiz The module instance to get the range from
  | 
        
        
            | 
            | 
           2190 | 
            * @return array
  | 
        
        
            | 
            | 
           2191 | 
            */
  | 
        
        
            | 
            | 
           2192 | 
           function mod_quiz_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $quiz) {
  | 
        
        
            | 
            | 
           2193 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           2194 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           2195 | 
              | 
        
        
            | 
            | 
           2196 | 
               // Overrides do not have a valid timestart range.
  | 
        
        
            | 
            | 
           2197 | 
               if (quiz_is_overriden_calendar_event($event)) {
  | 
        
        
            | 
            | 
           2198 | 
                   return [false, false];
  | 
        
        
            | 
            | 
           2199 | 
               }
  | 
        
        
            | 
            | 
           2200 | 
              | 
        
        
            | 
            | 
           2201 | 
               $mindate = null;
  | 
        
        
            | 
            | 
           2202 | 
               $maxdate = null;
  | 
        
        
            | 
            | 
           2203 | 
              | 
        
        
            | 
            | 
           2204 | 
               if ($event->eventtype == QUIZ_EVENT_TYPE_OPEN) {
  | 
        
        
            | 
            | 
           2205 | 
                   if (!empty($quiz->timeclose)) {
  | 
        
        
            | 
            | 
           2206 | 
                       $maxdate = [
  | 
        
        
            | 
            | 
           2207 | 
                           $quiz->timeclose,
  | 
        
        
            | 
            | 
           2208 | 
                           get_string('openafterclose', 'quiz')
  | 
        
        
            | 
            | 
           2209 | 
                       ];
  | 
        
        
            | 
            | 
           2210 | 
                   }
  | 
        
        
            | 
            | 
           2211 | 
               } else if ($event->eventtype == QUIZ_EVENT_TYPE_CLOSE) {
  | 
        
        
            | 
            | 
           2212 | 
                   if (!empty($quiz->timeopen)) {
  | 
        
        
            | 
            | 
           2213 | 
                       $mindate = [
  | 
        
        
            | 
            | 
           2214 | 
                           $quiz->timeopen,
  | 
        
        
            | 
            | 
           2215 | 
                           get_string('closebeforeopen', 'quiz')
  | 
        
        
            | 
            | 
           2216 | 
                       ];
  | 
        
        
            | 
            | 
           2217 | 
                   }
  | 
        
        
            | 
            | 
           2218 | 
               }
  | 
        
        
            | 
            | 
           2219 | 
              | 
        
        
            | 
            | 
           2220 | 
               return [$mindate, $maxdate];
  | 
        
        
            | 
            | 
           2221 | 
           }
  | 
        
        
            | 
            | 
           2222 | 
              | 
        
        
            | 
            | 
           2223 | 
           /**
  | 
        
        
            | 
            | 
           2224 | 
            * This function will update the quiz module according to the
  | 
        
        
            | 
            | 
           2225 | 
            * event that has been modified.
  | 
        
        
            | 
            | 
           2226 | 
            *
  | 
        
        
            | 
            | 
           2227 | 
            * It will set the timeopen or timeclose value of the quiz instance
  | 
        
        
            | 
            | 
           2228 | 
            * according to the type of event provided.
  | 
        
        
            | 
            | 
           2229 | 
            *
  | 
        
        
            | 
            | 
           2230 | 
            * @throws \moodle_exception
  | 
        
        
            | 
            | 
           2231 | 
            * @param \calendar_event $event A quiz activity calendar event
  | 
        
        
            | 
            | 
           2232 | 
            * @param \stdClass $quiz A quiz activity instance
  | 
        
        
            | 
            | 
           2233 | 
            */
  | 
        
        
            | 
            | 
           2234 | 
           function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $quiz) {
  | 
        
        
            | 
            | 
           2235 | 
               global $CFG, $DB;
  | 
        
        
            | 
            | 
           2236 | 
               require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           2237 | 
              | 
        
        
            | 
            | 
           2238 | 
               if (!in_array($event->eventtype, [QUIZ_EVENT_TYPE_OPEN, QUIZ_EVENT_TYPE_CLOSE])) {
  | 
        
        
            | 
            | 
           2239 | 
                   // This isn't an event that we care about so we can ignore it.
  | 
        
        
            | 
            | 
           2240 | 
                   return;
  | 
        
        
            | 
            | 
           2241 | 
               }
  | 
        
        
            | 
            | 
           2242 | 
              | 
        
        
            | 
            | 
           2243 | 
               $courseid = $event->courseid;
  | 
        
        
            | 
            | 
           2244 | 
               $modulename = $event->modulename;
  | 
        
        
            | 
            | 
           2245 | 
               $instanceid = $event->instance;
  | 
        
        
            | 
            | 
           2246 | 
               $modified = false;
  | 
        
        
            | 
            | 
           2247 | 
               $closedatechanged = false;
  | 
        
        
            | 
            | 
           2248 | 
              | 
        
        
            | 
            | 
           2249 | 
               // Something weird going on. The event is for a different module so
  | 
        
        
            | 
            | 
           2250 | 
               // we should ignore it.
  | 
        
        
            | 
            | 
           2251 | 
               if ($modulename != 'quiz') {
  | 
        
        
            | 
            | 
           2252 | 
                   return;
  | 
        
        
            | 
            | 
           2253 | 
               }
  | 
        
        
            | 
            | 
           2254 | 
              | 
        
        
            | 
            | 
           2255 | 
               if ($quiz->id != $instanceid) {
  | 
        
        
            | 
            | 
           2256 | 
                   // The provided quiz instance doesn't match the event so
  | 
        
        
            | 
            | 
           2257 | 
                   // there is nothing to do here.
  | 
        
        
            | 
            | 
           2258 | 
                   return;
  | 
        
        
            | 
            | 
           2259 | 
               }
  | 
        
        
            | 
            | 
           2260 | 
              | 
        
        
            | 
            | 
           2261 | 
               // We don't update the activity if it's an override event that has
  | 
        
        
            | 
            | 
           2262 | 
               // been modified.
  | 
        
        
            | 
            | 
           2263 | 
               if (quiz_is_overriden_calendar_event($event)) {
  | 
        
        
            | 
            | 
           2264 | 
                   return;
  | 
        
        
            | 
            | 
           2265 | 
               }
  | 
        
        
            | 
            | 
           2266 | 
              | 
        
        
            | 
            | 
           2267 | 
               $coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
  | 
        
        
            | 
            | 
           2268 | 
               $context = context_module::instance($coursemodule->id);
  | 
        
        
            | 
            | 
           2269 | 
              | 
        
        
            | 
            | 
           2270 | 
               // The user does not have the capability to modify this activity.
  | 
        
        
            | 
            | 
           2271 | 
               if (!has_capability('moodle/course:manageactivities', $context)) {
  | 
        
        
            | 
            | 
           2272 | 
                   return;
  | 
        
        
            | 
            | 
           2273 | 
               }
  | 
        
        
            | 
            | 
           2274 | 
              | 
        
        
            | 
            | 
           2275 | 
               if ($event->eventtype == QUIZ_EVENT_TYPE_OPEN) {
  | 
        
        
            | 
            | 
           2276 | 
                   // If the event is for the quiz activity opening then we should
  | 
        
        
            | 
            | 
           2277 | 
                   // set the start time of the quiz activity to be the new start
  | 
        
        
            | 
            | 
           2278 | 
                   // time of the event.
  | 
        
        
            | 
            | 
           2279 | 
                   if ($quiz->timeopen != $event->timestart) {
  | 
        
        
            | 
            | 
           2280 | 
                       $quiz->timeopen = $event->timestart;
  | 
        
        
            | 
            | 
           2281 | 
                       $modified = true;
  | 
        
        
            | 
            | 
           2282 | 
                   }
  | 
        
        
            | 
            | 
           2283 | 
               } else if ($event->eventtype == QUIZ_EVENT_TYPE_CLOSE) {
  | 
        
        
            | 
            | 
           2284 | 
                   // If the event is for the quiz activity closing then we should
  | 
        
        
            | 
            | 
           2285 | 
                   // set the end time of the quiz activity to be the new start
  | 
        
        
            | 
            | 
           2286 | 
                   // time of the event.
  | 
        
        
            | 
            | 
           2287 | 
                   if ($quiz->timeclose != $event->timestart) {
  | 
        
        
            | 
            | 
           2288 | 
                       $quiz->timeclose = $event->timestart;
  | 
        
        
            | 
            | 
           2289 | 
                       $modified = true;
  | 
        
        
            | 
            | 
           2290 | 
                       $closedatechanged = true;
  | 
        
        
            | 
            | 
           2291 | 
                   }
  | 
        
        
            | 
            | 
           2292 | 
               }
  | 
        
        
            | 
            | 
           2293 | 
              | 
        
        
            | 
            | 
           2294 | 
               if ($modified) {
  | 
        
        
            | 
            | 
           2295 | 
                   $quiz->timemodified = time();
  | 
        
        
            | 
            | 
           2296 | 
                   $DB->update_record('quiz', $quiz);
  | 
        
        
            | 
            | 
           2297 | 
              | 
        
        
            | 
            | 
           2298 | 
                   if ($closedatechanged) {
  | 
        
        
            | 
            | 
           2299 | 
                       quiz_update_open_attempts(['quizid' => $quiz->id]);
  | 
        
        
            | 
            | 
           2300 | 
                   }
  | 
        
        
            | 
            | 
           2301 | 
              | 
        
        
            | 
            | 
           2302 | 
                   // Delete any previous preview attempts.
  | 
        
        
            | 
            | 
           2303 | 
                   quiz_delete_previews($quiz);
  | 
        
        
            | 
            | 
           2304 | 
                   quiz_update_events($quiz);
  | 
        
        
            | 
            | 
           2305 | 
                   $event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
  | 
        
        
            | 
            | 
           2306 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           2307 | 
               }
  | 
        
        
            | 
            | 
           2308 | 
           }
  | 
        
        
            | 
            | 
           2309 | 
              | 
        
        
            | 
            | 
           2310 | 
           /**
  | 
        
        
            | 
            | 
           2311 | 
            * Generates the question bank in a fragment output. This allows
  | 
        
        
            | 
            | 
           2312 | 
            * the question bank to be displayed in a modal.
  | 
        
        
            | 
            | 
           2313 | 
            *
  | 
        
        
            | 
            | 
           2314 | 
            * The only expected argument provided in the $args array is
  | 
        
        
            | 
            | 
           2315 | 
            * 'querystring'. The value should be the list of parameters
  | 
        
        
            | 
            | 
           2316 | 
            * URL encoded and used to build the question bank page.
  | 
        
        
            | 
            | 
           2317 | 
            *
  | 
        
        
            | 
            | 
           2318 | 
            * The individual list of parameters expected can be found in
  | 
        
        
            | 
            | 
           2319 | 
            * question_build_edit_resources.
  | 
        
        
            | 
            | 
           2320 | 
            *
  | 
        
        
            | 
            | 
           2321 | 
            * @param array $args The fragment arguments.
  | 
        
        
            | 
            | 
           2322 | 
            * @return string The rendered mform fragment.
  | 
        
        
            | 
            | 
           2323 | 
            */
  | 
        
        
            | 
            | 
           2324 | 
           function mod_quiz_output_fragment_quiz_question_bank($args): string {
  | 
        
        
            | 
            | 
           2325 | 
               global $PAGE;
  | 
        
        
            | 
            | 
           2326 | 
              | 
        
        
            | 
            | 
           2327 | 
               // Retrieve params.
  | 
        
        
            | 
            | 
           2328 | 
               $params = [];
  | 
        
        
            | 
            | 
           2329 | 
               $extraparams = [];
  | 
        
        
            | 
            | 
           2330 | 
               $querystring = parse_url($args['querystring'], PHP_URL_QUERY);
  | 
        
        
            | 
            | 
           2331 | 
               parse_str($querystring, $params);
  | 
        
        
            | 
            | 
           2332 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           2333 | 
               // Load the bank we are looking at rather than always the quiz module itself.
  | 
        
        
            | 
            | 
           2334 | 
               $params['cmid'] = clean_param($args['bankcmid'], PARAM_INT);
  | 
        
        
            | 
            | 
           2335 | 
              | 
        
        
           | 1 | 
           efrain | 
           2336 | 
               $viewclass = \mod_quiz\question\bank\custom_view::class;
  | 
        
        
            | 
            | 
           2337 | 
               $extraparams['view'] = $viewclass;
  | 
        
        
            | 
            | 
           2338 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           2339 | 
               // We need the quiz modid to POST back to.
  | 
        
        
            | 
            | 
           2340 | 
               $extraparams['quizcmid'] = clean_param($args['quizcmid'], PARAM_INT);
  | 
        
        
            | 
            | 
           2341 | 
              | 
        
        
           | 1 | 
           efrain | 
           2342 | 
               // Build required parameters.
  | 
        
        
            | 
            | 
           2343 | 
               [$contexts, $thispageurl, $cm, $pagevars, $extraparams] =
  | 
        
        
            | 
            | 
           2344 | 
                       build_required_parameters_for_custom_view($params, $extraparams);
  | 
        
        
            | 
            | 
           2345 | 
              | 
        
        
            | 
            | 
           2346 | 
               $course = get_course($cm->course);
  | 
        
        
           | 1441 | 
           ariadna | 
           2347 | 
               if (!has_capability('moodle/question:useall', $contexts->lowest())) {
  | 
        
        
            | 
            | 
           2348 | 
                   require_capability('moodle/question:usemine', $contexts->lowest());
  | 
        
        
            | 
            | 
           2349 | 
               }
  | 
        
        
           | 1 | 
           efrain | 
           2350 | 
              | 
        
        
            | 
            | 
           2351 | 
               // Custom View.
  | 
        
        
            | 
            | 
           2352 | 
               $questionbank = new $viewclass($contexts, $thispageurl, $course, $cm, $pagevars, $extraparams);
  | 
        
        
            | 
            | 
           2353 | 
              | 
        
        
            | 
            | 
           2354 | 
               // Output.
  | 
        
        
            | 
            | 
           2355 | 
               $renderer = $PAGE->get_renderer('mod_quiz', 'edit');
  | 
        
        
            | 
            | 
           2356 | 
               return $renderer->question_bank_contents($questionbank, $pagevars);
  | 
        
        
            | 
            | 
           2357 | 
           }
  | 
        
        
            | 
            | 
           2358 | 
              | 
        
        
            | 
            | 
           2359 | 
           /**
  | 
        
        
           | 1441 | 
           ariadna | 
           2360 | 
            * Build and return the output for the question bank and category chooser.
  | 
        
        
            | 
            | 
           2361 | 
            *
  | 
        
        
            | 
            | 
           2362 | 
            * @param array $args provided by the AJAX request.
  | 
        
        
            | 
            | 
           2363 | 
            * @return string html to render to the modal.
  | 
        
        
            | 
            | 
           2364 | 
            */
  | 
        
        
            | 
            | 
           2365 | 
           function mod_quiz_output_fragment_switch_question_bank($args): string {
  | 
        
        
            | 
            | 
           2366 | 
               global $USER, $COURSE, $OUTPUT;
  | 
        
        
            | 
            | 
           2367 | 
              | 
        
        
            | 
            | 
           2368 | 
               $quizcmid = clean_param($args['quizcmid'], PARAM_INT);
  | 
        
        
            | 
            | 
           2369 | 
              | 
        
        
            | 
            | 
           2370 | 
               $switchbankwidget = new \core_question\output\switch_question_bank($quizcmid, $COURSE->id, $USER->id);
  | 
        
        
            | 
            | 
           2371 | 
              | 
        
        
            | 
            | 
           2372 | 
               return $OUTPUT->render($switchbankwidget);
  | 
        
        
            | 
            | 
           2373 | 
           }
  | 
        
        
            | 
            | 
           2374 | 
              | 
        
        
            | 
            | 
           2375 | 
           /**
  | 
        
        
           | 1 | 
           efrain | 
           2376 | 
            * Generates the add random question in a fragment output. This allows the
  | 
        
        
            | 
            | 
           2377 | 
            * form to be rendered in javascript, for example inside a modal.
  | 
        
        
            | 
            | 
           2378 | 
            *
  | 
        
        
            | 
            | 
           2379 | 
            * The required arguments as keys in the $args array are:
  | 
        
        
            | 
            | 
           2380 | 
            *      addonpage {int} The page id to add this question to.
  | 
        
        
            | 
            | 
           2381 | 
            *      returnurl {string} URL to return to after form submission.
  | 
        
        
           | 1441 | 
           ariadna | 
           2382 | 
            *      quizcmid {int} The quiz course module id the questions are being added to.
  | 
        
        
            | 
            | 
           2383 | 
            *      bankcmid {int} The question bank course module id the questions are being added from.
  | 
        
        
           | 1 | 
           efrain | 
           2384 | 
            *
  | 
        
        
            | 
            | 
           2385 | 
            * @param array $args The fragment arguments.
  | 
        
        
            | 
            | 
           2386 | 
            * @return string The rendered mform fragment.
  | 
        
        
            | 
            | 
           2387 | 
            */
  | 
        
        
            | 
            | 
           2388 | 
           function mod_quiz_output_fragment_add_random_question_form($args) {
  | 
        
        
           | 1441 | 
           ariadna | 
           2389 | 
               global $PAGE, $OUTPUT, $DB;
  | 
        
        
           | 1 | 
           efrain | 
           2390 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           2391 | 
               $slotid = clean_param($args['slotid'] ?? 0, PARAM_INT);
  | 
        
        
            | 
            | 
           2392 | 
               if (empty($slotid)) {
  | 
        
        
            | 
            | 
           2393 | 
                   $params = $args;
  | 
        
        
            | 
            | 
           2394 | 
               } else {
  | 
        
        
            | 
            | 
           2395 | 
                   // Load the stored filters for the current slot.
  | 
        
        
            | 
            | 
           2396 | 
                   $setreference = $DB->get_record('question_set_references',
  | 
        
        
            | 
            | 
           2397 | 
                       ['itemid' => $slotid, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
  | 
        
        
            | 
            | 
           2398 | 
                   $filterconditions = json_decode($setreference->filtercondition, true);
  | 
        
        
            | 
            | 
           2399 | 
                   $filterconditions = \core_question\question_reference_manager::convert_legacy_set_reference_filter_condition(
  | 
        
        
            | 
            | 
           2400 | 
                       $filterconditions,
  | 
        
        
            | 
            | 
           2401 | 
                   );
  | 
        
        
            | 
            | 
           2402 | 
                   $params = \core_question\local\bank\filter_condition_manager::filter_invalid_values($filterconditions);
  | 
        
        
            | 
            | 
           2403 | 
               }
  | 
        
        
            | 
            | 
           2404 | 
              | 
        
        
            | 
            | 
           2405 | 
               if (!empty($args['savedfiltercondition'])) {
  | 
        
        
            | 
            | 
           2406 | 
                   $filtercondition = json_decode($args['savedfiltercondition'], true);
  | 
        
        
            | 
            | 
           2407 | 
                   $params['filter'] = $filtercondition['filter'];
  | 
        
        
            | 
            | 
           2408 | 
               }
  | 
        
        
            | 
            | 
           2409 | 
              | 
        
        
           | 1 | 
           efrain | 
           2410 | 
               $extraparams = [];
  | 
        
        
           | 1441 | 
           ariadna | 
           2411 | 
               $extraparams['quizcmid'] = clean_param($args['quizcmid'], PARAM_INT);
  | 
        
        
            | 
            | 
           2412 | 
               $extraparams['cmid'] = clean_param($args['bankcmid'], PARAM_INT);
  | 
        
        
           | 1 | 
           efrain | 
           2413 | 
              | 
        
        
            | 
            | 
           2414 | 
               // Build required parameters.
  | 
        
        
            | 
            | 
           2415 | 
               [$contexts, $thispageurl, $cm, $pagevars, $extraparams] =
  | 
        
        
           | 1441 | 
           ariadna | 
           2416 | 
                       build_required_parameters_for_custom_view($params, $extraparams);
  | 
        
        
           | 1 | 
           efrain | 
           2417 | 
              | 
        
        
            | 
            | 
           2418 | 
               // Additional param to differentiate with other question bank view.
  | 
        
        
            | 
            | 
           2419 | 
               $extraparams['view'] = mod_quiz\question\bank\random_question_view::class;
  | 
        
        
            | 
            | 
           2420 | 
              | 
        
        
            | 
            | 
           2421 | 
               $course = get_course($cm->course);
  | 
        
        
            | 
            | 
           2422 | 
               require_capability('mod/quiz:manage', $contexts->lowest());
  | 
        
        
            | 
            | 
           2423 | 
              | 
        
        
            | 
            | 
           2424 | 
               // Custom View.
  | 
        
        
            | 
            | 
           2425 | 
               $questionbank = new mod_quiz\question\bank\random_question_view($contexts, $thispageurl, $course, $cm, $pagevars, $extraparams);
  | 
        
        
            | 
            | 
           2426 | 
              | 
        
        
            | 
            | 
           2427 | 
               $renderer = $PAGE->get_renderer('mod_quiz', 'edit');
  | 
        
        
            | 
            | 
           2428 | 
               $questionbankoutput = $renderer->question_bank_contents($questionbank, $pagevars);
  | 
        
        
            | 
            | 
           2429 | 
              | 
        
        
            | 
            | 
           2430 | 
               $maxrand = 100;
  | 
        
        
            | 
            | 
           2431 | 
               for ($i = 1; $i <= min(100, $maxrand); $i++) {
  | 
        
        
            | 
            | 
           2432 | 
                   $randomcount[] = ['value' => $i, 'name' => $i];
  | 
        
        
            | 
            | 
           2433 | 
               }
  | 
        
        
            | 
            | 
           2434 | 
              | 
        
        
            | 
            | 
           2435 | 
               // Parent category select.
  | 
        
        
            | 
            | 
           2436 | 
               $usablecontexts = $contexts->having_cap('moodle/question:useall');
  | 
        
        
            | 
            | 
           2437 | 
               $categoriesarray = helper::question_category_options($usablecontexts);
  | 
        
        
            | 
            | 
           2438 | 
               $catoptions = [];
  | 
        
        
            | 
            | 
           2439 | 
               foreach ($categoriesarray as $group => $opts) {
  | 
        
        
            | 
            | 
           2440 | 
                   // Options for each category group.
  | 
        
        
            | 
            | 
           2441 | 
                   $categories = [];
  | 
        
        
            | 
            | 
           2442 | 
                   foreach ($opts as $context => $name) {
  | 
        
        
            | 
            | 
           2443 | 
                       $categories[] = ['value' => $context, 'name' => $name];
  | 
        
        
            | 
            | 
           2444 | 
                   }
  | 
        
        
            | 
            | 
           2445 | 
                   $catoptions[] = ['label' => $group, 'options' => $categories];
  | 
        
        
            | 
            | 
           2446 | 
               }
  | 
        
        
            | 
            | 
           2447 | 
              | 
        
        
            | 
            | 
           2448 | 
               // Template data.
  | 
        
        
            | 
            | 
           2449 | 
               $data = [
  | 
        
        
            | 
            | 
           2450 | 
                   'questionbank' => $questionbankoutput,
  | 
        
        
            | 
            | 
           2451 | 
                   'randomoptions' => $randomcount,
  | 
        
        
            | 
            | 
           2452 | 
                   'questioncategoryoptions' => $catoptions,
  | 
        
        
            | 
            | 
           2453 | 
               ];
  | 
        
        
           | 1441 | 
           ariadna | 
           2454 | 
               if (!empty($slotid)) {
  | 
        
        
            | 
            | 
           2455 | 
                   $data['slotid'] = $slotid;
  | 
        
        
            | 
            | 
           2456 | 
               }
  | 
        
        
           | 1 | 
           efrain | 
           2457 | 
              | 
        
        
            | 
            | 
           2458 | 
               $helpicon = new \help_icon('parentcategory', 'question');
  | 
        
        
            | 
            | 
           2459 | 
               $data['questioncategoryhelp'] = $helpicon->export_for_template($renderer);
  | 
        
        
            | 
            | 
           2460 | 
              | 
        
        
            | 
            | 
           2461 | 
               $result = $OUTPUT->render_from_template('mod_quiz/add_random_question_form', $data);
  | 
        
        
            | 
            | 
           2462 | 
              | 
        
        
            | 
            | 
           2463 | 
               return $result;
  | 
        
        
            | 
            | 
           2464 | 
           }
  | 
        
        
            | 
            | 
           2465 | 
              | 
        
        
            | 
            | 
           2466 | 
           /**
  | 
        
        
            | 
            | 
           2467 | 
            * Callback to fetch the activity event type lang string.
  | 
        
        
            | 
            | 
           2468 | 
            *
  | 
        
        
            | 
            | 
           2469 | 
            * @param string $eventtype The event type.
  | 
        
        
            | 
            | 
           2470 | 
            * @return lang_string The event type lang string.
  | 
        
        
            | 
            | 
           2471 | 
            */
  | 
        
        
            | 
            | 
           2472 | 
           function mod_quiz_core_calendar_get_event_action_string(string $eventtype): string {
  | 
        
        
            | 
            | 
           2473 | 
               $modulename = get_string('modulename', 'quiz');
  | 
        
        
            | 
            | 
           2474 | 
              | 
        
        
            | 
            | 
           2475 | 
               switch ($eventtype) {
  | 
        
        
            | 
            | 
           2476 | 
                   case QUIZ_EVENT_TYPE_OPEN:
  | 
        
        
            | 
            | 
           2477 | 
                       $identifier = 'quizeventopens';
  | 
        
        
            | 
            | 
           2478 | 
                       break;
  | 
        
        
            | 
            | 
           2479 | 
                   case QUIZ_EVENT_TYPE_CLOSE:
  | 
        
        
            | 
            | 
           2480 | 
                       $identifier = 'quizeventcloses';
  | 
        
        
            | 
            | 
           2481 | 
                       break;
  | 
        
        
            | 
            | 
           2482 | 
                   default:
  | 
        
        
            | 
            | 
           2483 | 
                       return get_string('requiresaction', 'calendar', $modulename);
  | 
        
        
            | 
            | 
           2484 | 
               }
  | 
        
        
            | 
            | 
           2485 | 
              | 
        
        
            | 
            | 
           2486 | 
               return get_string($identifier, 'quiz', $modulename);
  | 
        
        
            | 
            | 
           2487 | 
           }
  | 
        
        
            | 
            | 
           2488 | 
              | 
        
        
            | 
            | 
           2489 | 
           /**
  | 
        
        
            | 
            | 
           2490 | 
            * Delete all question references for a quiz.
  | 
        
        
            | 
            | 
           2491 | 
            *
  | 
        
        
            | 
            | 
           2492 | 
            * @param int $quizid The id of quiz.
  | 
        
        
            | 
            | 
           2493 | 
            */
  | 
        
        
            | 
            | 
           2494 | 
           function quiz_delete_references($quizid): void {
  | 
        
        
            | 
            | 
           2495 | 
               global $DB;
  | 
        
        
            | 
            | 
           2496 | 
              | 
        
        
            | 
            | 
           2497 | 
               $cm = get_coursemodule_from_instance('quiz', $quizid);
  | 
        
        
            | 
            | 
           2498 | 
               $context = context_module::instance($cm->id);
  | 
        
        
            | 
            | 
           2499 | 
              | 
        
        
            | 
            | 
           2500 | 
               $conditions = [
  | 
        
        
            | 
            | 
           2501 | 
                   'usingcontextid' => $context->id,
  | 
        
        
            | 
            | 
           2502 | 
                   'component' => 'mod_quiz',
  | 
        
        
            | 
            | 
           2503 | 
                   'questionarea' => 'slot',
  | 
        
        
            | 
            | 
           2504 | 
               ];
  | 
        
        
            | 
            | 
           2505 | 
              | 
        
        
            | 
            | 
           2506 | 
               $DB->delete_records('question_references', $conditions);
  | 
        
        
            | 
            | 
           2507 | 
               $DB->delete_records('question_set_references', $conditions);
  | 
        
        
            | 
            | 
           2508 | 
           }
  | 
        
        
            | 
            | 
           2509 | 
              | 
        
        
            | 
            | 
           2510 | 
           /**
  | 
        
        
            | 
            | 
           2511 | 
            * Question data fragment to get the question html via ajax call.
  | 
        
        
            | 
            | 
           2512 | 
            *
  | 
        
        
            | 
            | 
           2513 | 
            * @param array $args
  | 
        
        
            | 
            | 
           2514 | 
            * @return string
  | 
        
        
            | 
            | 
           2515 | 
            */
  | 
        
        
            | 
            | 
           2516 | 
           function mod_quiz_output_fragment_question_data(array $args): string {
  | 
        
        
            | 
            | 
           2517 | 
               // Return if there is no args.
  | 
        
        
            | 
            | 
           2518 | 
               if (empty($args)) {
  | 
        
        
            | 
            | 
           2519 | 
                   return '';
  | 
        
        
            | 
            | 
           2520 | 
               }
  | 
        
        
            | 
            | 
           2521 | 
              | 
        
        
            | 
            | 
           2522 | 
               // Retrieve params from query string.
  | 
        
        
            | 
            | 
           2523 | 
               [$params, $extraparams] = \core_question\local\bank\filter_condition_manager::extract_parameters_from_fragment_args($args);
  | 
        
        
            | 
            | 
           2524 | 
              | 
        
        
            | 
            | 
           2525 | 
               // Build required parameters.
  | 
        
        
            | 
            | 
           2526 | 
               $cmid = clean_param($args['cmid'], PARAM_INT);
  | 
        
        
            | 
            | 
           2527 | 
               $thispageurl = new \moodle_url('/mod/quiz/edit.php', ['cmid' => $cmid]);
  | 
        
        
            | 
            | 
           2528 | 
               $thiscontext = \context_module::instance($cmid);
  | 
        
        
            | 
            | 
           2529 | 
               $contexts = new \core_question\local\bank\question_edit_contexts($thiscontext);
  | 
        
        
           | 1441 | 
           ariadna | 
           2530 | 
               $defaultcategory = question_get_default_category($contexts->lowest()->id, true);
  | 
        
        
           | 1 | 
           efrain | 
           2531 | 
               $params['cat'] = implode(',', [$defaultcategory->id, $defaultcategory->contextid]);
  | 
        
        
            | 
            | 
           2532 | 
              | 
        
        
            | 
            | 
           2533 | 
               $course = get_course($params['courseid']);
  | 
        
        
           | 1441 | 
           ariadna | 
           2534 | 
               // The viewing bank mod id.
  | 
        
        
            | 
            | 
           2535 | 
               [, $cm] = get_module_from_cmid(clean_param($args['cmid'], PARAM_INT));
  | 
        
        
           | 1 | 
           efrain | 
           2536 | 
               $params['tabname'] = 'questions';
  | 
        
        
           | 1441 | 
           ariadna | 
           2537 | 
               $extraparams['quizcmid'] = clean_param($args['quizcmid'], PARAM_INT);
  | 
        
        
           | 1 | 
           efrain | 
           2538 | 
              | 
        
        
            | 
            | 
           2539 | 
               // Custom question bank View.
  | 
        
        
            | 
            | 
           2540 | 
               $viewclass = clean_param($args['view'], PARAM_NOTAGS);
  | 
        
        
            | 
            | 
           2541 | 
               $questionbank = new $viewclass($contexts, $thispageurl, $course, $cm, $params, $extraparams);
  | 
        
        
            | 
            | 
           2542 | 
              | 
        
        
            | 
            | 
           2543 | 
               // Question table.
  | 
        
        
            | 
            | 
           2544 | 
               $questionbank->add_standard_search_conditions();
  | 
        
        
            | 
            | 
           2545 | 
               ob_start();
  | 
        
        
            | 
            | 
           2546 | 
               $questionbank->display_question_list();
  | 
        
        
            | 
            | 
           2547 | 
               return ob_get_clean();
  | 
        
        
            | 
            | 
           2548 | 
           }
  | 
        
        
            | 
            | 
           2549 | 
              | 
        
        
            | 
            | 
           2550 | 
           /**
  | 
        
        
            | 
            | 
           2551 | 
            * Build required parameters for question bank custom view
  | 
        
        
            | 
            | 
           2552 | 
            *
  | 
        
        
            | 
            | 
           2553 | 
            * @param array $params the page parameters
  | 
        
        
            | 
            | 
           2554 | 
            * @param array $extraparams additional parameters
  | 
        
        
            | 
            | 
           2555 | 
            * @return array
  | 
        
        
            | 
            | 
           2556 | 
            */
  | 
        
        
            | 
            | 
           2557 | 
           function build_required_parameters_for_custom_view(array $params, array $extraparams): array {
  | 
        
        
            | 
            | 
           2558 | 
               // Retrieve questions per page.
  | 
        
        
            | 
            | 
           2559 | 
               $viewclass = $extraparams['view'] ?? null;
  | 
        
        
            | 
            | 
           2560 | 
               $defaultpagesize = $viewclass ? $viewclass::DEFAULT_PAGE_SIZE : DEFAULT_QUESTIONS_PER_PAGE;
  | 
        
        
            | 
            | 
           2561 | 
               // Build the required params.
  | 
        
        
            | 
            | 
           2562 | 
               [$thispageurl, $contexts, $cmid, $cm, , $pagevars] = question_build_edit_resources(
  | 
        
        
            | 
            | 
           2563 | 
                       'editq',
  | 
        
        
            | 
            | 
           2564 | 
                       '/mod/quiz/edit.php',
  | 
        
        
            | 
            | 
           2565 | 
                       array_merge($params, $extraparams),
  | 
        
        
            | 
            | 
           2566 | 
                       $defaultpagesize);
  | 
        
        
            | 
            | 
           2567 | 
              | 
        
        
            | 
            | 
           2568 | 
               // Add cmid so we can retrieve later in extra params.
  | 
        
        
            | 
            | 
           2569 | 
               $extraparams['cmid'] = $cmid;
  | 
        
        
            | 
            | 
           2570 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           2571 | 
               $extraparams['requirebankswitch'] = !empty(question_bank_helper::get_activity_types_with_shareable_questions());
  | 
        
        
            | 
            | 
           2572 | 
              | 
        
        
           | 1 | 
           efrain | 
           2573 | 
               return [$contexts, $thispageurl, $cm, $pagevars, $extraparams];
  | 
        
        
            | 
            | 
           2574 | 
           }
  | 
        
        
            | 
            | 
           2575 | 
              | 
        
        
            | 
            | 
           2576 | 
           /**
  | 
        
        
            | 
            | 
           2577 | 
            * Implement the calculate_question_stats callback.
  | 
        
        
            | 
            | 
           2578 | 
            *
  | 
        
        
            | 
            | 
           2579 | 
            * This enables quiz statistics to be shown in statistics columns in the database.
  | 
        
        
            | 
            | 
           2580 | 
            *
  | 
        
        
            | 
            | 
           2581 | 
            * @param context $context return the statistics related to this context (which will be a quiz context).
  | 
        
        
            | 
            | 
           2582 | 
            * @return all_calculated_for_qubaid_condition|null The statistics for this quiz, if available, else null.
  | 
        
        
            | 
            | 
           2583 | 
            */
  | 
        
        
            | 
            | 
           2584 | 
           function mod_quiz_calculate_question_stats(context $context): ?all_calculated_for_qubaid_condition {
  | 
        
        
            | 
            | 
           2585 | 
               global $CFG;
  | 
        
        
            | 
            | 
           2586 | 
               require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
  | 
        
        
            | 
            | 
           2587 | 
               $cm = get_coursemodule_from_id('quiz', $context->instanceid);
  | 
        
        
            | 
            | 
           2588 | 
               $report = new quiz_statistics_report();
  | 
        
        
            | 
            | 
           2589 | 
               return $report->calculate_questions_stats_for_question_bank($cm->instance, false, false);
  | 
        
        
            | 
            | 
           2590 | 
           }
  | 
        
        
            | 
            | 
           2591 | 
              | 
        
        
            | 
            | 
           2592 | 
           /**
  | 
        
        
            | 
            | 
           2593 | 
            * Return a list of all the user preferences used by mod_quiz.
  | 
        
        
            | 
            | 
           2594 | 
            *
  | 
        
        
            | 
            | 
           2595 | 
            * @uses core_user::is_current_user
  | 
        
        
            | 
            | 
           2596 | 
            *
  | 
        
        
            | 
            | 
           2597 | 
            * @return array[]
  | 
        
        
            | 
            | 
           2598 | 
            */
  | 
        
        
            | 
            | 
           2599 | 
           function mod_quiz_user_preferences(): array {
  | 
        
        
            | 
            | 
           2600 | 
               $preferences = [];
  | 
        
        
            | 
            | 
           2601 | 
               $preferences['quiz_timerhidden'] = [
  | 
        
        
            | 
            | 
           2602 | 
                   'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           2603 | 
                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           2604 | 
                   'default' => '0',
  | 
        
        
            | 
            | 
           2605 | 
                   'permissioncallback' => [core_user::class, 'is_current_user'],
  | 
        
        
            | 
            | 
           2606 | 
               ];
  | 
        
        
            | 
            | 
           2607 | 
               return $preferences;
  | 
        
        
            | 
            | 
           2608 | 
           }
  |