public function prevent_new_attempt($numattempts, $lastattempt) { global $DB; if ($numattempts == 0) { return false; } // Check if preventonpass is set, and whether the student has passed the minimum passing grade. $previousattempts = $DB->get_records_select('quiz_attempts', "quiz = :quizid AND userid = :userid AND timefinish > 0 and preview != 1", array('quizid' => $this->quiz->id, 'userid' => $lastattempt->userid)); if (quiz_rescale_grade(quiz_calculate_best_grade($this->quiz, $previousattempts), $this->quiz, false) >= $this->quiz->reattemptchecker) { return get_string('accessprevented', 'quizaccess_reattemptchecker'); } return false; }
/** * Save the overall grade for a user at a quiz in the quiz_grades table * * @param object $quiz The quiz for which the best grade is to be calculated and then saved. * @param integer $userid The userid to calculate the grade for. Defaults to the current user. * @return boolean Indicates success or failure. */ function quiz_save_best_grade($quiz, $userid = null) { global $USER; if (empty($userid)) { $userid = $USER->id; } // Get all the attempts made by the user if (!($attempts = quiz_get_user_attempts($quiz->id, $userid))) { notify('Could not find any user attempts'); return false; } // Calculate the best grade $bestgrade = quiz_calculate_best_grade($quiz, $attempts); $bestgrade = quiz_rescale_grade($bestgrade, $quiz); // Save the best grade in the database if ($grade = get_record('quiz_grades', 'quiz', $quiz->id, 'userid', $userid)) { $grade->grade = $bestgrade; $grade->timemodified = time(); if (!update_record('quiz_grades', $grade)) { notify('Could not update best grade'); return false; } } else { $grade->quiz = $quiz->id; $grade->userid = $userid; $grade->grade = $bestgrade; $grade->timemodified = time(); if (!insert_record('quiz_grades', $grade)) { notify('Could not insert new best grade'); return false; } } quiz_update_grades($quiz, $userid); return true; }
/** * Save the overall grade for a user at a quiz in the quiz_grades table * * @param object $quiz The quiz for which the best grade is to be calculated and then saved. * @param int $userid The userid to calculate the grade for. Defaults to the current user. * @param array $attempts The attempts of this user. Useful if you are * looping through many users. Attempts can be fetched in one master query to * avoid repeated querying. * @return bool Indicates success or failure. */ function quiz_save_best_grade($quiz, $userid = null, $attempts = array()) { global $DB, $OUTPUT, $USER; if (empty($userid)) { $userid = $USER->id; } if (!$attempts) { // Get all the attempts made by the user. $attempts = quiz_get_user_attempts($quiz->id, $userid); } // Calculate the best grade. $bestgrade = quiz_calculate_best_grade($quiz, $attempts); $bestgrade = quiz_rescale_grade($bestgrade, $quiz, false); // Save the best grade in the database. if (is_null($bestgrade)) { $DB->delete_records('quiz_grades', array('quiz' => $quiz->id, 'userid' => $userid)); } else if ($grade = $DB->get_record('quiz_grades', array('quiz' => $quiz->id, 'userid' => $userid))) { $grade->grade = $bestgrade; $grade->timemodified = time(); $DB->update_record('quiz_grades', $grade); } else { $grade = new stdClass(); $grade->quiz = $quiz->id; $grade->userid = $userid; $grade->grade = $bestgrade; $grade->timemodified = time(); $DB->insert_record('quiz_grades', $grade); } quiz_update_grades($quiz, $userid); }
/** * Save the overall grade for a user at a quiz in the quiz_grades table * * @param object $quiz The quiz for which the best grade is to be calculated and then saved. * @param integer $userid The userid to calculate the grade for. Defaults to the current user. * @param array $attempts The attempts of this user. Useful if you are * looping through many users. Attempts can be fetched in one master query to * avoid repeated querying. * @return boolean Indicates success or failure. */ function quiz_save_best_grade($quiz, $userid = null, $attempts = array()) { global $DB; global $USER, $OUTPUT; if (empty($userid)) { $userid = $USER->id; } if (!$attempts) { // Get all the attempts made by the user if (!($attempts = quiz_get_user_attempts($quiz->id, $userid))) { echo $OUTPUT->notification('Could not find any user attempts'); return false; } } // Calculate the best grade $bestgrade = quiz_calculate_best_grade($quiz, $attempts); $bestgrade = quiz_rescale_grade($bestgrade, $quiz, false); // Save the best grade in the database if ($grade = $DB->get_record('quiz_grades', array('quiz' => $quiz->id, 'userid' => $userid))) { $grade->grade = $bestgrade; $grade->timemodified = time(); $DB->update_record('quiz_grades', $grade); } else { $grade->quiz = $quiz->id; $grade->userid = $userid; $grade->grade = $bestgrade; $grade->timemodified = time(); $DB->insert_record('quiz_grades', $grade); } quiz_update_grades($quiz, $userid); return true; }