From 4ac04efaf79bf2fc041442602f5b4c0b4a00c9d6 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sat, 22 Jan 2005 15:42:22 +0000 Subject: [PATCH] quiz edit.php now saves changes to the quiz in the database as soon as the teacher makes them, see http://moodle.org/mod/forum/discuss.php?d=17167 --- mod/quiz/edit.php | 68 ++++++++++++++++++++----------------------- mod/quiz/lib.php | 45 ---------------------------- mod/quiz/locallib.php | 24 +++++++++++++++ 3 files changed, 55 insertions(+), 82 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 596f9a5be7..3bee558dc0 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -34,17 +34,21 @@ if (! $modform = get_record('quiz', 'id', $quizid)) { error("The required quiz doesn't exist"); } - $modform->instance = $modform->id; - $SESSION->modform = $modform; // Save the form in the current session + + $cm = get_coursemodule_from_instance('quiz', $modform->instance); + add_to_log($cm->course, 'quiz', 'editquestions', + "view.php?id=$modform->instance", + "$modform->name", $cm->id); } else if ($courseid) { // Page retrieve through "Edit Questions" link - no quiz selected $modform->course = $courseid; unset($modform->instance); - $SESSION->modform = $modform; // Save the form in the current session - + + add_to_log($courseid, 'quiz', 'editquestions', "index.php?id=$courseid"); + } else { if (!isset($SESSION->modform)) { // We currently also get here after editing a question by @@ -52,6 +56,8 @@ error(''); } + // The data is obtained from a $SESSION variable. This is mostly for historic reasons. + // With the way things work now it would be just as possible to get the data from the database. $modform = $SESSION->modform; } @@ -96,6 +102,9 @@ } $modform->questions = implode(",", $questions); } + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); + } } if (!empty($down)) { /// Move the given question down a slot @@ -111,6 +120,9 @@ } $modform->questions = implode(",", $questions); } + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); + } } if (!empty($add)) { /// Add a question to the current quiz @@ -146,6 +158,10 @@ } else { $modform->questions = ""; } + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); + } + quiz_questiongrades_update($modform->grades, $modform->instance); } if (!empty($delete)) { /// Delete a question from the list @@ -154,9 +170,15 @@ if ($question == $delete) { unset($questions[$key]); unset($modform->grades[$question]); + if (!delete_records('quiz_question_grades', 'quiz', $modform->instance, 'question', $question)) { + error("Could not delete question grade"); + } } } $modform->questions = implode(",", $questions); + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); + } } if (!empty($setgrades)) { /// The grades have been updated, so update our internal list @@ -168,24 +190,10 @@ $modform->grades[$key] = $value; } } - } - - if (!empty($save)) { // Save the list of questions and grades in the database and return - - //If caller is correct, $SESSION->sesskey must exist and coincide - if (empty($SESSION->sesskey) or !confirm_sesskey($SESSION->sesskey)) { - error(get_string('confirmsesskeybad', 'error')); + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); } - //Unset this, check done - unset($SESSION->sesskey); - - quiz_update_instance($modform); - $coursemodule = get_coursemodule_from_instance('quiz', $modform->instance); - add_to_log($course->id, 'quiz', 'editquestions', - "view.php?id=$coursemodule", - "$modform->instance", $coursemodule); - redirect('view.php?q='.$modform->instance); - die; + quiz_questiongrades_update($modform->grades, $modform->instance); } @@ -212,7 +220,7 @@ $strname = get_string('name'); $strquizzes = get_string('modulenameplural', 'quiz'); - $strediting = get_string(isset($modform->instance) ? "editingquiz" : "editquestions", "quiz"); + $strediting = get_string('editquestions', "quiz"); $strheading = empty($modform->name) ? $strediting : $modform->name; // Print basic page layout. @@ -234,10 +242,6 @@ print_simple_box_start("center", "100%", $THEME->cellcontent2); print_heading($modform->name); quiz_print_question_list($modform->questions, $modform->grades); - ?> -
-

 

- instance' AND timefinish > 0")) { $strviewallanswers = get_string("viewallanswers","quiz",$attemptcount); @@ -249,19 +253,9 @@ } notify("$strattemptsexist
id\">$strviewallanswers ($usercount $strusers)"); } - - $SESSION->sesskey = !empty($USER->id) ? $USER->sesskey : ''; - ?> -
- - " /> - " /> -
-
- instance); echo ''; } print_simple_box_start("center", "100%", $THEME->cellcontent2); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index ecb53d03c6..290f10cdf1 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -33,23 +33,6 @@ function quiz_add_instance($quiz) { set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref); } - // The grades for every question in this quiz are stored in an array - // (because this is currently only called from mod.html there are not - // going to be any grades, but we will leave this code here just in case) - if (isset($quiz->grades)) { - foreach ($quiz->grades as $question => $grade) { - if ($question) { - unset($questiongrade); - $questiongrade->quiz = $quiz->id; - $questiongrade->question = $question; - $questiongrade->grade = $grade; - if (!insert_record("quiz_question_grades", $questiongrade)) { - return false; - } - } - } - } - delete_records('event', 'modulename', 'quiz', 'instance', $quiz->id); // Just in case $event = NULL; @@ -111,34 +94,6 @@ function quiz_update_instance($quiz) { set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref); } - // The grades for every question in this quiz are stored in an array - // Insert or update records as appropriate - - $existing = get_records("quiz_question_grades", "quiz", $quiz->id, "", "question,grade,id"); - - if (isset($quiz->grades)) { // this will not be set if we come from mod.html - foreach ($quiz->grades as $question => $grade) { - if ($question) { - unset($questiongrade); - $questiongrade->quiz = $quiz->id; - $questiongrade->question = $question; - $questiongrade->grade = $grade; - if (isset($existing[$question])) { - if ($existing[$question]->grade != $grade) { - $questiongrade->id = $existing[$question]->id; - if (!update_record("quiz_question_grades", $questiongrade)) { - return false; - } - } - } else { - if (!insert_record("quiz_question_grades", $questiongrade)) { - return false; - } - } - } - } - } - // currently this code deletes all existing events and adds new ones // this should be improved to update existing events only if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) { diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 64e927cc12..af0bd671a1 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -352,6 +352,30 @@ function quiz_get_question_grades($quizid, $questionlist) { AND question IN ($questionlist)"); } +function quiz_questiongrades_update($grades, $quizid) { + $existing = get_records("quiz_question_grades", "quiz", $quizid, "", "question,grade,id"); + foreach ($grades as $question => $grade) { + if ($question) { + unset($questiongrade); + $questiongrade->quiz = $quizid; + $questiongrade->question = $question; + $questiongrade->grade = $grade; + if (isset($existing[$question])) { + if ($existing[$question]->grade != $grade) { + $questiongrade->id = $existing[$question]->id; + if (!update_record("quiz_question_grades", $questiongrade)) { + return false; + } + } + } else { + if (!insert_record("quiz_question_grades", $questiongrade)) { + return false; + } + } + } + } +} + function quiz_get_grade_records($quiz) { /// Gets all info required to display the table of quiz results /// for report.php -- 2.39.5