Make a library function for deleting quiz attempts, rather than having duplicated code. Merged from OU Moodle.

This commit is contained in:
tjhunt
2007-06-18 16:19:00 +00:00
parent 95affb8a35
commit ff51d64621
3 changed files with 38 additions and 27 deletions
+5 -11
View File
@@ -172,17 +172,11 @@
$newattempt = false;
if (!$attempt) {
// Check if this is a preview request from a teacher
// in which case the previous previews should be deleted
if ($ispreviewing) {
if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
AND userid = '$USER->id'")) {
delete_records('quiz_attempts', 'quiz', $quiz->id, 'userid', $USER->id);
delete_records('quiz_grades', 'quiz', $quiz->id, 'userid', $USER->id);
foreach ($oldattempts as $oldattempt) {
// there should only be one but we loop just in case
delete_attempt($oldattempt->uniqueid);
}
// Delete any previous preview attempts belonging to this user.
if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
AND userid = '$USER->id'")) {
foreach ($oldattempts as $oldattempt) {
quiz_delete_attempt($oldattempt, $quiz);
}
}
$newattempt = true;
+31
View File
@@ -117,6 +117,37 @@ function quiz_get_user_attempts($quizid, $userid, $status = 'finished') {
}
}
/**
* Delete a quiz attempt.
*/
function quiz_delete_attempt($attempt, $quiz) {
if (is_numeric($attempt)) {
if (!$attempt = get_record('quiz_attempts', 'id', $attempt)) {
return;
}
}
if ($attempt->quiz != $quiz->id) {
debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " .
"but was passed quiz $quiz->id.");
return;
}
delete_records('quiz_attempts', 'id', $attempt->id);
delete_attempt($attempt->uniqueid);
// Search quiz_attempts for other instances by this user.
// If none, then delete record for this quiz, this user from quiz_grades
// else recalculate best grade
$userid = $attempt->userid;
if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) {
delete_records('quiz_grades', 'userid', $userid,'quiz', $quiz->id);
} else {
quiz_save_best_grade($quiz, $userid);
}
}
/// Functions to do with quiz layout and pages ////////////////////////////////
/**
+2 -16
View File
@@ -36,22 +36,8 @@ class quiz_report extends quiz_default_report {
$attemptids = optional_param('attemptid', array(), PARAM_INT);
foreach($attemptids as $attemptid) {
if ($attemptid && $todelete = get_record('quiz_attempts', 'id', $attemptid)) {
delete_records('quiz_attempts', 'id', $attemptid);
delete_attempt($todelete->uniqueid);
// Search quiz_attempts for other instances by this user.
// If none, then delete record for this quiz, this user from quiz_grades
// else recalculate best grade
$userid = $todelete->userid;
if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) {
delete_records('quiz_grades', 'userid', $userid,'quiz', $quiz->id);
} else {
quiz_save_best_grade($quiz, $userid);
}
quiz_update_grades($quiz, $userid);
}
quiz_delete_attempt($attemptid, $quiz);
quiz_update_grades($quiz, $USER->id);
}
break;
}