From: gustav_delius Date: Wed, 22 Mar 2006 18:27:28 +0000 (+0000) Subject: Deleting attempts properly now X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=0429cd86c3f8216e4005cd169f30fa0eb94bcd87;p=moodle.git Deleting attempts properly now --- diff --git a/lib/questionlib.php b/lib/questionlib.php index 1d20529911..e59f87cd32 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -317,6 +317,29 @@ function question_category_isused($categoryid, $recursive = false) { return false; } +/** + * Deletes all data associated to an attempt from the database + * + * @param object $question The question being deleted + */ +function delete_attempt($attemptid) { + global $QTYPES; + + $states = get_records('question_states', 'attempt', $attemptid); + $stateslist = implode(',', array_keys($states)); + + // delete questiontype-specific data + foreach ($QTYPES as $qtype) { + $qtype->delete_states($stateslist); + } + + // delete entries from all other question tables + // It is important that this is done only after calling the questiontype functions + delete_records("question_states", "attempt", $attemptid); + delete_records("question_sessions", "attemptid", $attemptid); + + return; +} /** * Deletes question and all associated data from the database @@ -341,6 +364,14 @@ function delete_question($questionid) { echo "Question with id $questionid does not exist.
"; } + $states = get_records('question_states', 'question', $questionid); + $stateslist = implode(',', array_keys($states)); + + // delete questiontype-specific data + foreach ($QTYPES as $qtype) { + $qtype->delete_states($stateslist); + } + // delete entries from all other question tables // It is important that this is done only after calling the questiontype functions delete_records("question_answers", "question", $questionid); diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 4ead6d79d2..28cc149372 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -207,10 +207,7 @@ 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 - // TODO: the following should become a function in questionlib.php which - // really deletes all records associated to this attempt. - delete_records('question_states', 'attempt', $oldattempt->uniqueid); - delete_records('question_sessions', 'attemptid', $oldattempt->uniqueid); + delete_attempt($oldattempt->uniqueid); } } } diff --git a/question/questiontypes/essay/questiontype.php b/question/questiontypes/essay/questiontype.php index 19fe7b4e8a..cc8ad8a2c1 100644 --- a/question/questiontypes/essay/questiontype.php +++ b/question/questiontypes/essay/questiontype.php @@ -69,9 +69,18 @@ class question_essay_qtype extends default_questiontype { } /** - * Deletes question from the question-type specific tables + * Deletes states from the question-type specific tables + * + * @param string $stateslist Comma separated list of state ids to be deleted + */ + function delete_states($stateslist) { + delete_records_select("question_essay_states", "stateid IN ($stateslist)"); + return true; + } + + /** + * Deletes a question from the question-type specific tables * - * @return boolean Success/Failure * @param object $question The question being deleted */ function delete_question($questionid) { diff --git a/question/questiontypes/questiontype.php b/question/questiontypes/questiontype.php index be65d50c1c..745b3ecf2c 100644 --- a/question/questiontypes/questiontype.php +++ b/question/questiontypes/questiontype.php @@ -178,7 +178,19 @@ class default_questiontype { } /** - * Deletes question from the question-type specific tables + * Deletes states from the question-type specific tables + * + * @param string $stateslist Comma separated list of state ids to be deleted + */ + function delete_states($stateslist) { + /// The default question type does not have any tables of its own + // therefore there is nothing to delete + + return true; + } + + /** + * Deletes a question from the question-type specific tables * * @return boolean Success/Failure * @param object $question The question being deleted diff --git a/question/questiontypes/rqp/questiontype.php b/question/questiontypes/rqp/questiontype.php index f60510368b..3328b97d6e 100644 --- a/question/questiontypes/rqp/questiontype.php +++ b/question/questiontypes/rqp/questiontype.php @@ -123,6 +123,16 @@ class question_rqp_qtype extends default_questiontype { return true; } + /** + * Deletes states from the question-type specific tables + * + * @param string $stateslist Comma separated list of state ids to be deleted + */ + function delete_states($stateslist) { + delete_records_select("question_rqp_states", "stateid IN ($stateslist)"); + return true; + } + /** * Deletes question from the question-type specific tables * @@ -131,11 +141,6 @@ class question_rqp_qtype extends default_questiontype { */ function delete_question($questionid) { delete_records("question_rqp", "question", $questionid); - if ($states = get_records('question_states', 'question', $questionid)) { - foreach ($states as $state) { - delete_records('question_rqp_states', 'stateid', $state->id); - } - } return true; }