Skip to content

Commit

Permalink
quiz edit.php now saves changes to the quiz in the database as soon a…
Browse files Browse the repository at this point in the history
…s the teacher makes them, see http://moodle.org/mod/forum/discuss.php?d=17167
  • Loading branch information
gustav_delius committed Jan 22, 2005
1 parent 446d876 commit 4ac04ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 82 deletions.
68 changes: 31 additions & 37 deletions mod/quiz/edit.php
Expand Up @@ -34,24 +34,30 @@
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
// following the edit link on the review page. Perhaps that should be fixed.
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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}


Expand All @@ -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.
Expand All @@ -234,10 +242,6 @@
print_simple_box_start("center", "100%", $THEME->cellcontent2);
print_heading($modform->name);
quiz_print_question_list($modform->questions, $modform->grades);
?>
<center>
<p>&nbsp;</p>
<?php

if ($attemptcount = count_records_select("quiz_attempts", "quiz = '$modform->instance' AND timefinish > 0")) {
$strviewallanswers = get_string("viewallanswers","quiz",$attemptcount);
Expand All @@ -249,19 +253,9 @@
}
notify("$strattemptsexist<br /><a href=\"report.php?id=$cm->id\">$strviewallanswers ($usercount $strusers)</a>");
}

$SESSION->sesskey = !empty($USER->id) ? $USER->sesskey : '';
?>
<form method="post" action="edit.php">
<input type="hidden" name="sesskey" value="save" />
<input type="submit" name="save" value="<?php print_string("savequiz", "quiz") ?>" />
<input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
</form>
</center>
<?php


print_simple_box_end();
print_continue('view.php?q='.$modform->instance);
echo '</td><td valign="top" width="50%">';
}
print_simple_box_start("center", "100%", $THEME->cellcontent2);
Expand Down
45 changes: 0 additions & 45 deletions mod/quiz/lib.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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'")) {
Expand Down
24 changes: 24 additions & 0 deletions mod/quiz/locallib.php
Expand Up @@ -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
Expand Down

0 comments on commit 4ac04ef

Please sign in to comment.