From 56b2152f697276d50ffcd9c5d1164bb4b420437d Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sat, 22 Jan 2005 18:17:33 +0000 Subject: [PATCH] better parameter validation and general code cleanup for edit.php --- mod/quiz/edit.php | 75 +++++++++++++++---------------------------- mod/quiz/lib.php | 17 +++++----- mod/quiz/locallib.php | 54 ++++++++++++++++--------------- 3 files changed, 63 insertions(+), 83 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 37e1231c95d..4a90136e545 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -5,28 +5,12 @@ require_login(); - optional_variable($courseid); - optional_variable($quizid); - optional_variable($page, 0); - optional_variable($perpage, "20"); + $courseid = optional_param('courseid'); + $quizid = optional_param('quizid'); + $page = optional_param('page', 0); + $perpage = optional_param('perpage', 20); - if (empty($destination)) { - $destination = ""; - } - - $modform = data_submitted($destination); - - if ($modform and !empty($modform->course)) { // data submitted - - $modform->name = trim($modform->name); - - if (empty($modform->name)) { - if (empty($modform->intro)) { - $modform->name = get_string('modulename', 'quiz'); - } else { - $modform->name = strip_tags($modform->intro); - } - } + if ($modform = data_submitted() and !empty($modform->course)) { // data submitted $SESSION->modform = $modform; // Save the form in the current session @@ -79,17 +63,9 @@ } - // Now, check for commands on this page and modify variables as necessary - - if (isset($cancel)) { - redirect('view.php?q='.$modform->instance); - } - - if (isset($recurse)) { - $modform->recurse = $recurse; - } +/// Now, check for commands on this page and modify variables as necessary - if (!empty($up)) { /// Move the given question up a slot + if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot $questions = explode(",", $modform->questions); if ($questions[0] <> $up) { foreach ($questions as $key => $question) { @@ -107,7 +83,7 @@ } } - if (!empty($down)) { /// Move the given question down a slot + if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot $questions = explode(",", $modform->questions); if ($questions[count($questions)-1] <> $down) { foreach ($questions as $key => $question) { @@ -125,7 +101,7 @@ } } - if (!empty($add)) { /// Add a question to the current quiz + if (isset($_REQUEST['add']) and confirm_sesskey()) { /// Add a question to the current quiz $rawquestions = $_POST; if (!empty($modform->questions)) { $questions = explode(",", $modform->questions); @@ -164,7 +140,7 @@ quiz_questiongrades_update($modform->grades, $modform->instance); } - if (!empty($delete)) { /// Delete a question from the list + if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Delete a question from the list $questions = explode(",", $modform->questions); foreach ($questions as $key => $question) { if ($question == $delete) { @@ -181,7 +157,7 @@ } } - if (!empty($setgrades)) { /// The grades have been updated, so update our internal list + if (isset($_REQUEST['setgrades']) and confirm_sesskey()) { /// The grades have been updated, so update our internal list $rawgrades = $_POST; unset($modform->grades); foreach ($rawgrades as $key => $value) { // Parse input for question -> grades @@ -195,11 +171,16 @@ } quiz_questiongrades_update($modform->grades, $modform->instance); } - - - if (!empty($cat)) { //----------------------------------------------------------- + + if (isset($_REQUEST['cat'])) { /// coming from category selection drop-down menu $modform->category = $cat; } + + if (isset($_REQUEST['recurse'])) { /// coming from checkbox below category selection form + $modform->recurse = $recurse; + } + +/// all commands have been dealt with, now print the page if (empty($modform->category)) { $category = quiz_get_default_category($course->id); @@ -209,23 +190,15 @@ $modform->recurse = 1; } - $modform->sumgrades = 0; - if (!empty($modform->grades)) { - foreach ($modform->grades as $grade) { - $modform->sumgrades += $grade; - } - } - $SESSION->modform = $modform; - $strname = get_string('name'); $strquizzes = get_string('modulenameplural', 'quiz'); $strediting = get_string('editquestions', "quiz"); - $strheading = empty($modform->name) ? $strediting : $modform->name; // Print basic page layout. if (!isset($modform->instance)) { + // one column layout for non-quiz-specific editing page print_header_simple($strediting, '', "id\">$strquizzes". " -> $strediting"); @@ -233,6 +206,7 @@ echo ''; } else { + // two column layout with quiz info in left column print_header_simple($strediting, '', "id\">$strquizzes". " -> instance\">$modform->name". @@ -258,15 +232,16 @@ } print_simple_box_end(); - print_continue('view.php?q='.$modform->instance); echo ''; } + // non-quiz-specific column print_simple_box_start("center", "100%", $THEME->cellcontent2); + // starts with category selection form quiz_print_category_form($course, $modform->category, $modform->recurse); print_simple_box_end(); print_spacer(5,1); - + // continues with list of questions print_simple_box_start("center", "100%", $THEME->cellcontent2); quiz_print_cat_question_list($modform->category, isset($modform->instance), $modform->recurse, $page, $perpage); @@ -277,6 +252,8 @@ if (!isset($modform->instance)) { print_continue("index.php?id=$modform->course"); + } else { + print_continue('view.php?q='.$modform->instance); } print_footer($course); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 290f10cdf14..527abf0b79e 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -14,16 +14,21 @@ function quiz_add_instance($quiz) { /// will create a new instance and return the id number /// of the new instance. - global $SESSION; - - unset($SESSION->modform); - $quiz->created = time(); $quiz->timemodified = time(); $quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday, $quiz->openhour, $quiz->openminute, 0); $quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday, $quiz->closehour, $quiz->closeminute, 0); + + if (empty($quiz->name)) { + if (empty($quiz->intro)) { + $quiz->name = get_string('modulename', 'quiz'); + } else { + $quiz->name = strip_tags($quiz->intro); + } + } + $quiz->name = trim($quiz->name); if (!$quiz->id = insert_record("quiz", $quiz)) { return false; // some error occurred @@ -73,10 +78,6 @@ function quiz_update_instance($quiz) { /// (defined by the form in mod.html or edit.php) this function /// will update an existing instance with new data. - global $SESSION; - - unset($SESSION->modform); - $quiz->timemodified = time(); if (isset($quiz->openyear)) { // this would not be set if we come from edit.php $quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday, diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index af0bd671a11..d3c458be2db 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -353,25 +353,25 @@ function quiz_get_question_grades($quizid, $questionlist) { } function quiz_questiongrades_update($grades, $quizid) { + // this is called from edit.php to store changes to the question grades + // in the quiz_question_grades table. It does not update 'sumgrades' in the quiz table. $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)) { + 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; + } } } } @@ -1109,7 +1109,7 @@ function quiz_print_question_list($questionlist, $grades) { // $questionlist is comma-separated list // $grades is an array of corresponding grades - global $THEME; + global $THEME, $USER; if (!$questionlist) { echo "

"; @@ -1143,6 +1143,7 @@ function quiz_print_question_list($questionlist, $grades) { $sumgrade = 0; $total = count($order); echo "

"; + echo "sesskey\">"; echo "\n"; echo "\n"; foreach ($order as $qnum) { @@ -1156,13 +1157,13 @@ function quiz_print_question_list($questionlist, $grades) { echo ""; echo ""; echo ""; @@ -1178,15 +1179,15 @@ function quiz_print_question_list($questionlist, $grades) { "q$qnum", (string)$grades[$qnum], ""); } echo ""; $sumgrade += $grades[$qnum]; @@ -1207,7 +1208,7 @@ function quiz_print_question_list($questionlist, $grades) { function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=1, $page, $perpage) { // Prints the table of questions in a category with interactions - global $THEME, $QUIZ_QUESTION_TYPE; + global $THEME, $QUIZ_QUESTION_TYPE, $USER; $strcategory = get_string("category", "quiz"); $strquestion = get_string("question", "quiz"); @@ -1290,6 +1291,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse= $canedit = isteacheredit($category->course); echo ""; + echo "sesskey\">"; echo "
$strorder$strquestionname$strtype$strgrade$stredit
$count"; if ($count != 1) { - echo "sesskey\">\"$strmoveup\""; } echo ""; if ($count != $total) { - echo "sesskey\">\"$strmovedown\""; } echo ""; - echo "\"$strdelete\" "; - echo "\"$strpreview\" "; - if ($canedit) { - echo "\"$stredit\"\n"; - } + if ($canedit) { + echo " + \"$stredit\" "; + echo "sesskey\"> + \"$strdelete\" "; + echo " + \"$strpreview\""; + } echo "
"; echo ""; if ($quizselected) {