|
| 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 | +defined('MOODLE_INTERNAL') || die(); |
| 19 | + |
| 20 | +require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php'); |
| 21 | + |
| 22 | +/* |
| 23 | + * @package quizaccess_reattemptchecker |
| 24 | + * @copyright 2015 Amir Shurrab |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * A rule requiring the students have not achieved a pass grade |
| 30 | + */ |
| 31 | +class quizaccess_reattemptchecker extends quiz_access_rule_base { |
| 32 | + |
| 33 | + |
| 34 | + public static function make(quiz $quizobj, $timenow, $canignoretimelimits) { |
| 35 | + |
| 36 | + if (empty($quizobj->get_quiz()->reattemptchecker)) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + return new self($quizobj, $timenow); |
| 41 | + } |
| 42 | + |
| 43 | + public static function add_settings_form_fields( |
| 44 | + mod_quiz_mod_form $quizform, MoodleQuickForm $mform) { |
| 45 | + |
| 46 | + $mform->addElement('text', 'reattemptchecker', get_string("preventpassed", "quizaccess_reattemptchecker"), 'maxlength="3" size="3"'); |
| 47 | + $mform->setType('reattemptchecker', PARAM_INT); |
| 48 | + $mform->addHelpButton('reattemptchecker', |
| 49 | + 'preventpassed', 'quizaccess_reattemptchecker'); |
| 50 | + } |
| 51 | + |
| 52 | + public static function save_settings($quiz) { |
| 53 | + global $DB; |
| 54 | + if (empty($quiz->reattemptchecker)) { |
| 55 | + $DB->delete_records('quizaccess_reattemptchecker', array('quizid' => $quiz->id)); |
| 56 | + } else { |
| 57 | + if ($record = $DB->get_record('quizaccess_reattemptchecker', array('quizid' => $quiz->id))) { |
| 58 | + $record->reattemptchecker = $quiz->reattemptchecker; |
| 59 | + $DB->update_record('quizaccess_reattemptchecker', $record); |
| 60 | + } else { |
| 61 | + $record = new stdClass(); |
| 62 | + $record->quizid = $quiz->id; |
| 63 | + $record->reattemptchecker = $quiz->reattemptchecker; |
| 64 | + $DB->insert_record('quizaccess_reattemptchecker', $record); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static function get_settings_sql($quizid) { |
| 70 | + return array( |
| 71 | + 'reattemptchecker', |
| 72 | + 'LEFT JOIN {quizaccess_reattemptchecker} reattemptchecker ON reattemptchecker.quizid = quiz.id', |
| 73 | + array()); |
| 74 | + } |
| 75 | + |
| 76 | + public function prevent_new_attempt($numattempts, $lastattempt) { |
| 77 | + global $DB; |
| 78 | + |
| 79 | + if ($numattempts == 0) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + // Check if preventonpass is set, and whether the student has passed the minimum passing grade. |
| 84 | + $previousattempts = $DB->get_records_select('quiz_attempts', |
| 85 | + "quiz = :quizid AND userid = :userid AND timefinish > 0 and preview != 1", |
| 86 | + array('quizid' => $this->quiz->id, 'userid' => $lastattempt->userid)); |
| 87 | + |
| 88 | + if (quiz_calculate_best_grade($this->quiz, $previousattempts) >= $this->quiz->reattemptchecker) { |
| 89 | + return get_string('accessprevented', 'quizaccess_reattemptchecker'); |
| 90 | + } |
| 91 | + |
| 92 | + return false; |
| 93 | + } |
| 94 | +} |
0 commit comments