From: jamiesensei Date: Thu, 3 May 2007 10:10:01 +0000 (+0000) Subject: moved question sort order and paging params to be passed from page to page as get... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b72ff476856a4496d9dee0a78fc08b5b64ffed36;p=moodle.git moved question sort order and paging params to be passed from page to page as get params instead of being saved in session var. --- diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index c25eec2468..47d904168e 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -29,71 +29,37 @@ require_once("../../config.php"); require_once($CFG->dirroot.'/mod/quiz/editlib.php'); - require_login(); - $quizid = optional_param('quizid', 0, PARAM_INT); + list($thispageurl, $courseid, $cmid, $cm, $quiz, $pagevars) = question_edit_setup(true); + $strquizzes = get_string('modulenameplural', 'quiz'); $strquiz = get_string('modulename', 'quiz'); $streditingquestions = get_string('editquestions', "quiz"); - $streditingquiz = get_string("editinga", "moodle", $strquiz); - - if ($modform = data_submitted() and !empty($modform->course)) { // data submitted + $streditingquiz = get_string('editinga', 'moodle', $strquiz); + - $SESSION->modform = $modform; // Save the form in the current session - } else if ($quizid) { - if (isset($SESSION->modform->id) and $SESSION->modform->id == $quizid) { - // modform for this quiz already exists, use it - $modform = $SESSION->modform; - } else { - // create new modform from database - 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 - } - - } else if (!empty($sortorder)) { - // no quiz or course was specified so we need to use the stored modform - if (isset($SESSION->modform)) { - $modform = $SESSION->modform; - } else { - error('cmunknown'); - } - } else { - // no quiz or course was specified so we need to use the stored modform - if (isset($SESSION->modform)) { - $modform = $SESSION->modform; - } else { - print_error('cmunknown'); - } - } // Get the course object and related bits. - if (! $course = get_record("course", "id", $modform->course)) { + if (! $course = get_record("course", "id", $quiz->course)) { error("This course doesn't exist"); } - $coursecontext = get_context_instance(CONTEXT_COURSE, $modform->course); + $coursecontext = get_context_instance(CONTEXT_COURSE, $quiz->course); + $quizcontext = get_context_instance(CONTEXT_MODULE, $quiz->cmid); require_login($course->id, false); - // Get the module and related bits. - $cm = get_coursemodule_from_instance('quiz', $modform->instance); - $modform->cmid = $cm->id; - $context = get_context_instance(CONTEXT_MODULE, $cm->id); // Log this visit. add_to_log($cm->course, 'quiz', 'editquestions', - "view.php?id=$cm->id", "$quizid", $cm->id); + "view.php?id=$cm->id", "$quiz->id", $cm->id); - require_capability('mod/quiz:manage', $context); + require_capability('mod/quiz:manage', $quizcontext); - if (isset($modform->instance) - && empty($modform->grades)) // Construct an array to hold all the grades. - { - $modform->grades = quiz_get_all_question_grades($modform); + if (isset($quiz->instance) + && empty($quiz->grades)){ // Construct an array to hold all the grades. + $quiz->grades = quiz_get_all_question_grades($quiz); } $SESSION->returnurl = $FULLME; @@ -102,18 +68,18 @@ if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot $up = optional_param('up', 0, PARAM_INT); - $questions = explode(",", $modform->questions); + $questions = explode(",", $quiz->questions); if ($up > 0 and isset($questions[$up])) { $prevkey = ($questions[$up-1] == 0) ? $up-2 : $up-1; $swap = $questions[$prevkey]; $questions[$prevkey] = $questions[$up]; $questions[$up] = $swap; - $modform->questions = implode(",", $questions); + $quiz->questions = implode(",", $questions); // Always have a page break at the end - $modform->questions = $modform->questions . ',0'; + $quiz->questions = $quiz->questions . ',0'; // Avoid duplicate page breaks - $modform->questions = str_replace(',0,0', ',0', $modform->questions); - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + $quiz->questions = str_replace(',0,0', ',0', $quiz->questions); + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) { error('Could not save question list'); } } @@ -121,29 +87,30 @@ if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot $down = optional_param('down', 0, PARAM_INT); - $questions = explode(",", $modform->questions); + $questions = explode(",", $quiz->questions); if ($down < count($questions)) { $nextkey = ($questions[$down+1] == 0) ? $down+2 : $down+1; $swap = $questions[$nextkey]; $questions[$nextkey] = $questions[$down]; $questions[$down] = $swap; - $modform->questions = implode(",", $questions); + $quiz->questions = implode(",", $questions); // Avoid duplicate page breaks - $modform->questions = str_replace(',0,0', ',0', $modform->questions); - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + $quiz->questions = str_replace(',0,0', ',0', $quiz->questions); + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) { error('Could not save question list'); } } } if (isset($_REQUEST['addquestion']) and confirm_sesskey()) { /// Add a single question to the current quiz - quiz_add_quiz_question($_REQUEST['addquestion'], $modform); + quiz_add_quiz_question($_REQUEST['addquestion'], $quiz); } if (isset($_REQUEST['add']) and confirm_sesskey()) { /// Add selected questions to the current quiz foreach ($_POST as $key => $value) { // Parse input for question ids - if (substr($key, 0, 1) == "q") { - quiz_add_quiz_question(substr($key,1), $modform); + if (preg_match('!q([0-9]+)!', $key, $matches)) { + $key = $matches[1]; + quiz_add_quiz_question($key, $quiz); } } } @@ -161,7 +128,7 @@ $random = RANDOM; if ($existingquestions = get_records_select('question', "qtype = '$random' AND category = '$category->id'")) { // now remove the ones that are already used in this quiz - if ($questionids = explode(',', $modform->questions)) { + if ($questionids = explode(',', $quiz->questions)) { foreach ($questionids as $questionid) { unset($existingquestions[$questionid]); } @@ -171,7 +138,7 @@ while (($existingquestion = array_pop($existingquestions)) and ($i < $randomcount)) { if ($existingquestion->questiontext == $recurse) { // this question has the right recurse property, so use it - quiz_add_quiz_question($existingquestion->id, $modform); + quiz_add_quiz_question($existingquestion->id, $quiz); $i++; } } @@ -198,45 +165,41 @@ if(!isset($question->id)) { error('Could not insert new random question!'); } - quiz_add_quiz_question($question->id, $modform); + quiz_add_quiz_question($question->id, $quiz); } } } if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz if (isset($_REQUEST['questionsperpage'])) { - $modform->questionsperpage = required_param('questionsperpage', PARAM_INT); - if (!set_field('quiz', 'questionsperpage', $modform->questionsperpage, 'id', $modform->id)) { + $quiz->questionsperpage = required_param('questionsperpage', PARAM_INT); + if (!set_field('quiz', 'questionsperpage', $quiz->questionsperpage, 'id', $quiz->id)) { error('Could not save number of questions per page'); } } - $modform->questions = quiz_repaginate($modform->questions, $modform->questionsperpage); - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->id)) { + $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage); + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) { error('Could not save layout'); } } if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz - quiz_delete_quiz_question($_REQUEST['delete'], $modform); + quiz_delete_quiz_question($_REQUEST['delete'], $quiz); } if (isset($_REQUEST['savechanges']) and confirm_sesskey()) { - $savequizid = required_param('savequizid', PARAM_INT); - if ($modform->id != $savequizid) { - error("Error saving quiz settings, please do not change two quizes from the same browser", $CFG->wwwroot.'/mod/quiz/edit.php?quizid='.$savequizid); - } /// We need to save the new ordering (if given) and the new grades - $oldquestions = explode(",", $modform->questions); // the questions in the old order + $oldquestions = explode(",", $quiz->questions); // the questions in the old order $questions = array(); // for questions in the new order $rawgrades = $_POST; - unset($modform->grades); + unset($quiz->grades); foreach ($rawgrades as $key => $value) { // Parse input for question -> grades - if (substr($key, 0, 1) == "q") { - $key = substr($key,1); - $modform->grades[$key] = $value; - quiz_update_question_instance($modform->grades[$key], $key, $modform->instance); - } elseif (substr($key, 0, 1) == "o") { // Parse input for ordering info - $key = substr($key,1); + if (preg_match('!q([0-9]+)!', $key, $matches)) { + $key = $matches[1]; + $quiz->grades[$key] = $value; + quiz_update_question_instance($quiz->grades[$key], $key, $quiz->instance); + } elseif (preg_match('!q([0-9]+)!', $key, $matches)) { // Parse input for ordering info + $key = $matches[1]; $questions[$value] = $oldquestions[$key]; } } @@ -244,21 +207,21 @@ // If ordering info was given, reorder the questions if ($questions) { ksort($questions); - $modform->questions = implode(",", $questions); + $quiz->questions = implode(",", $questions); // Always have a page break at the end - $modform->questions = $modform->questions . ',0'; + $quiz->questions = $quiz->questions . ',0'; // Avoid duplicate page breaks - while (strpos($modform->questions, ',0,0')) { - $modform->questions = str_replace(',0,0', ',0', $modform->questions); + while (strpos($quiz->questions, ',0,0')) { + $quiz->questions = str_replace(',0,0', ',0', $quiz->questions); } - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) { error('Could not save question list'); } } // If rescaling is required save the new maximum if (isset($_REQUEST['maxgrade'])) { - if (!quiz_set_grade(optional_param('maxgrade', 0), $modform)) { + if (!quiz_set_grade(optional_param('maxgrade', 0), $quiz)) { error('Could not set a new maximum grade for the quiz'); } } @@ -271,14 +234,14 @@ /// Delete any teacher preview attempts if the quiz has been modified if (isset($_REQUEST['savechanges']) or isset($_REQUEST['delete']) or isset($_REQUEST['repaginate']) or isset($_REQUEST['addrandom']) or isset($_REQUEST['addquestion']) or isset($_REQUEST['up']) or isset($_REQUEST['down']) or isset($_REQUEST['add'])) { - delete_records('quiz_attempts', 'preview', '1', 'quiz', $modform->id); + delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id); } /// all commands have been dealt with, now print the page - if (empty($modform->category) or !record_exists('question_categories', 'id', $modform->category)) { + if (empty($quiz->category) or !record_exists('question_categories', 'id', $quiz->category)) { $category = get_default_question_category($course->id); - $modform->category = $category->id; + $quiz->category = $category->id; } if (!isset($SESSION->quiz_showbreaks)) { $SESSION->quiz_showbreaks = ($CFG->quiz_questionsperpage < 2) ? 0 : 1; @@ -287,17 +250,16 @@ $SESSION->quiz_reordertool = 0; } - $SESSION->modform = $modform; // Print basic page layout. - if (isset($modform->instance) and record_exists_select('quiz_attempts', "quiz = '$modform->instance' AND preview = '0'")){ + if (isset($quiz->instance) and record_exists_select('quiz_attempts', "quiz = '$quiz->instance' AND preview = '0'")){ // one column layout with table of questions used in this quiz $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext) - ? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz')) + ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz')) : ""; $crumbs[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity'); - $crumbs[] = array('name' => format_string($modform->name), 'link' => "view.php?q=$modform->instance", 'type' => 'activityinstance'); + $crumbs[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance'); $crumbs[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title'); $navigation = build_navigation($crumbs); @@ -306,7 +268,7 @@ $currenttab = 'edit'; $mode = 'editq'; - $quiz = &$modform; + include('tabs.php'); print_box_start(); @@ -314,15 +276,15 @@ $a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0); $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)'); $a->studentstring = $course->students; - if (! $cm = get_coursemodule_from_instance("quiz", $modform->instance, $course->id)) { + if (! $cm = get_coursemodule_from_instance("quiz", $quiz->instance, $course->id)) { error("Course Module ID was incorrect"); } echo "
\n"; echo "id\">".get_string('numattempts', 'quiz', $a)."
".get_string("attemptsexist","quiz"); echo "

\n"; - $sumgrades = quiz_print_question_list($modform, false, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); - if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) { + $sumgrades = quiz_print_question_list($quiz, $thispageurl, false, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); + if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) { error('Failed to set sumgrades'); } @@ -333,11 +295,11 @@ // two column layout with quiz info in left column $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext) - ? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz')) + ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz')) : ""; $crumbs[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity'); - $crumbs[] = array('name' => format_string($modform->name), 'link' => "view.php?q=$modform->instance", 'type' => 'activityinstance'); + $crumbs[] = array('name' => format_string($quiz->name), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance'); $crumbs[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title'); $navigation = build_navigation($crumbs); @@ -345,7 +307,7 @@ $currenttab = 'edit'; $mode = 'editq'; - $quiz = &$modform; + include('tabs.php'); echo ''; @@ -353,8 +315,8 @@ print_box_start('generalbox quizquestions'); print_heading(get_string('questionsinthisquiz', 'quiz'), '', 2); - $sumgrades = quiz_print_question_list($modform, true, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); - if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) { + $sumgrades = quiz_print_question_list($quiz, $thispageurl, true, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); + if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance)) { error('Failed to set sumgrades'); } @@ -362,7 +324,7 @@ echo ''; echo '
'; - require($CFG->dirroot.'/question/showbank.php'); + question_showbank($thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder']); echo '
'; diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index bbc3cc6c70..c23156bd5c 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -15,17 +15,17 @@ require_once("locallib.php"); /** * Delete a question from a quiz * -* Deletes a question or a pagebreak from a quiz by updating $modform +* Deletes a question or a pagebreak from a quiz by updating $quiz * as well as the quiz, quiz_question_instances * @return boolean false if the question was not in the quiz * @param int $id The id of the question to be deleted -* @param object $modform The extended quiz object as used by edit.php +* @param object $quiz The extended quiz object as used by edit.php * This is updated by this function */ -function quiz_delete_quiz_question($id, &$modform) { +function quiz_delete_quiz_question($id, &$quiz) { // TODO: For the sake of safety check that this question can be deleted // safely, i.e., that it is not already in use. - $questions = explode(",", $modform->questions); + $questions = explode(",", $quiz->questions); // only do something if this question exists if (!isset($questions[$id])) { @@ -39,14 +39,14 @@ function quiz_delete_quiz_question($id, &$modform) { if ($id == 0 && count($questions) > 1 && $questions[1] == 0) { unset($questions[1]); } - $modform->questions = implode(",", $questions); + $quiz->questions = implode(",", $questions); // Avoid duplicate page breaks - $modform->questions = str_replace(',0,0', ',0', $modform->questions); + $quiz->questions = str_replace(',0,0', ',0', $quiz->questions); // save new questionlist in database - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) { error('Could not save question list'); } - delete_records('quiz_question_instances', 'quiz', $modform->instance, 'question', $question); + delete_records('quiz_question_instances', 'quiz', $quiz->instance, 'question', $question); return true; } @@ -54,16 +54,16 @@ function quiz_delete_quiz_question($id, &$modform) { /** * Add a question to a quiz * -* Adds a question to a quiz by updating $modform as well as the +* Adds a question to a quiz by updating $quiz as well as the * quiz and quiz_question_instances tables. It also adds a page break * if required. * @return boolean false if the question was already in the quiz * @param int $id The id of the question to be added -* @param object $modform The extended quiz object as used by edit.php +* @param object $quiz The extended quiz object as used by edit.php * This is updated by this function */ -function quiz_add_quiz_question($id, &$modform) { - $questions = explode(",", $modform->questions); +function quiz_add_quiz_question($id, &$quiz) { + $questions = explode(",", $quiz->questions); if (in_array($id, $questions)) { return false; @@ -75,7 +75,7 @@ function quiz_add_quiz_question($id, &$modform) { $end = end($breaks); $last = prev($breaks); $last = $last ? $last : -1; - if (!$modform->questionsperpage or (($end - $last -1) < $modform->questionsperpage)) { + if (!$quiz->questionsperpage or (($end - $last -1) < $quiz->questionsperpage)) { array_pop($questions); } } @@ -85,15 +85,16 @@ function quiz_add_quiz_question($id, &$modform) { $questions[] = 0; // Save new questionslist in database - $modform->questions = implode(",", $questions); - if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->id)) { + $quiz->questions = implode(",", $questions); + if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) { error('Could not save question list'); } // update question grades $questionrecord = get_record("question", "id", $id); - $modform->grades[$id] = $questionrecord->defaultgrade; - quiz_update_question_instance($modform->grades[$id], $id, $modform->instance); + $quiz->grades[$id] + = $questionrecord->defaultgrade; + quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance); return true; } @@ -132,7 +133,7 @@ function quiz_update_question_instance($grade, $questionid, $quizid) { * @param boolean $showbreaks Indicates whether the page breaks should be displayed * @param boolean $showbreaks Indicates whether the reorder tool should be displayed */ -function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $reordertool=false) { +function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $showbreaks=true, $reordertool=false) { global $USER, $CFG, $QTYPES; $strorder = get_string("order"); @@ -178,6 +179,7 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r echo "
"; echo '
'; echo "sesskey\" />"; + echo $pageurl->hidden_params_out(); echo "\n"; echo ""; @@ -218,15 +220,15 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r echo ''; echo '
$strorder
'; if ($count > 1) { - echo "id&sesskey=$USER->sesskey\">out_action(array('up'=>$count))."\">pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" />"; } echo ' '; if ($count < $lastindex) { - echo "id&sesskey=$USER->sesskey\">out_action(array('down'=>$count))."\">pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" />"; - echo "id&sesskey=$USER->sesskey\"> + echo "out_action(array('delete'=>$count))."\"> pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strremove\" />"; } echo '
'; @@ -242,13 +244,13 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r echo ""; if ($count != 0) { - echo "id&sesskey=$USER->sesskey\">out_action(array('up'=>$count))."\">pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" />"; } echo ""; echo ""; if ($count < $lastindex-1) { - echo "id&sesskey=$USER->sesskey\">out_action(array('down'=>$count))."\">pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" />"; } echo ""; @@ -283,7 +285,7 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" />"; } if ($allowdelete) { - echo "id&sesskey=$USER->sesskey\"> + echo "out_action(array('delete'=>$count))."\"> pixpath/t/removeright.gif\" class=\"iconsmall\" alt=\"$strremove\" />"; } @@ -311,7 +313,6 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r echo '
'; echo '
'; - echo ''; // ugly hack to prevent modform session "mistakes" echo '
'; echo "
\n"; @@ -319,6 +320,7 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r /// Form to choose to show pagebreaks and to repaginate quiz echo '
'; echo '
'; + echo $pageurl->hidden_params_out(); echo ''; echo ''; echo 'page = optional_param('page', 1, PARAM_INT); + $thispageurl->param('page', $param->page); $param->moveup = optional_param('moveup', 0, PARAM_INT); $param->movedown = optional_param('movedown', 0, PARAM_INT); @@ -33,35 +38,48 @@ $param->edit = optional_param('edit', 0, PARAM_INT); $param->updateid = optional_param('updateid', 0, PARAM_INT); - if (! $course = get_record("course", "id", $id)) { + if (! $course = get_record("course", "id", $courseid)) { error("Course ID is incorrect"); } - $context = get_context_instance(CONTEXT_COURSE, $id); + $context = get_context_instance(CONTEXT_COURSE, $courseid); require_login($course->id, false); require_capability('moodle/question:managecategory', $context); - $qcobject = new question_category_object($param->page); + $qcobject = new question_category_object($param->page, $thispageurl); + $streditingcategories = get_string('editcategories', 'quiz'); if ($qcobject->editlist->process_actions($param->left, $param->right, $param->moveup, $param->movedown)) { //processing of these actions is handled in the method and page redirects. - } else if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { + } else if ($cm!==null) { // Page header - // TODO: generalise this to any activity $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) - ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz')) + ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname)) : ""; - print_header_simple(get_string('editcategories', 'quiz'), '', - "wwwroot/mod/quiz/index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').''. - " -> wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).''. - ' -> '.get_string('editcategories', 'quiz'), - "", "", true, $strupdatemodule); + $crumbs = array(); + $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname), + 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", + 'type' => 'activity'); + $crumbs[] = array('name' => format_string($module->name), + 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", + 'type' => 'title'); + $crumbs[] = array('name' => $streditingcategories, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + print_header_simple($streditingcategories, '', $navigation, "", "", true, $strupdatemodule); + + $currenttab = 'edit'; $mode = 'categories'; - include($CFG->dirroot.'/mod/quiz/tabs.php'); + ${$cm->modname} = $module; + include($CFG->dirroot."/mod/{$cm->modname}/tabs.php"); } else { - print_header_simple(get_string('editcategories', 'quiz'), '', get_string('editcategories', 'quiz')); + // Print basic page layout. + $crumbs = array(); + $crumbs[] = array('name' => $streditingcategories, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + + print_header_simple($streditingcategories, '', $navigation); // print tabs $currenttab = 'categories'; diff --git a/question/edit.php b/question/edit.php index 3ea031a9bb..e8d14f37aa 100644 --- a/question/edit.php +++ b/question/edit.php @@ -13,15 +13,7 @@ require_once("../config.php"); require_once("editlib.php"); - require_login(); - - $courseid = required_param('courseid', PARAM_INT); - - // The optional parameter 'clean' allows us to clear module information, - // guaranteeing a module-independent question bank editing interface - if (optional_param('clean', false, PARAM_BOOL)) { - unset($SESSION->modform); - } + list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(); if (! $course = get_record("course", "id", $courseid)) { error("This course doesn't exist"); @@ -31,39 +23,40 @@ $SESSION->returnurl = $FULLME; - // Print basic page layout. - $streditingquestions = get_string('editquestions', "quiz"); - - // TODO: generalise this to any activity - $strquizzes = get_string('modulenameplural', 'quiz'); $streditingquestions = get_string('editquestions', "quiz"); - if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { + if ($cm!==null) { $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) - ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz')) + ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname)) : ""; - print_header_simple($streditingquestions, '', - "wwwroot/mod/quiz/index.php?id=$course->id\">$strquizzes". - " -> wwwroot/mod/quiz/view.php?q={$SESSION->modform->instance}\">".format_string($SESSION->modform->name).''. - " -> $streditingquestions", - "", "", true, $strupdatemodule); + $crumbs = array(); + $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity'); + $crumbs[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title'); + $crumbs[] = array('name' => $streditingquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + print_header_simple($streditingquestions, '', $navigation, "", "", true, $strupdatemodule); $currenttab = 'edit'; $mode = 'questions'; - $quiz = &$SESSION->modform; - include($CFG->dirroot.'/mod/quiz/tabs.php'); + ${$cm->modname} = $module; + include($CFG->dirroot."/mod/$cm->modname/tabs.php"); } else { - print_header_simple($streditingquestions, '', - "$streditingquestions"); - + // Print basic page layout. + $crumbs = array(); + $crumbs[] = array('name' => $streditingquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + + print_header_simple($streditingquestions, '', $navigation); + // print tabs $currenttab = 'questions'; include('tabs.php'); } - + + echo ''; echo ''; echo '
'; - include($CFG->dirroot.'/question/showbank.php'); + question_showbank($thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder']); echo '
'; diff --git a/question/editlib.php b/question/editlib.php index f6dddcc0ea..909a012c81 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -1,6 +1,6 @@ id}&perpage=$perpage&"); + print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage'); echo ''; echo '
'; echo ''; - echo $pageurl->hidden_params_out(); + echo $pageurl->hidden_params_out(array('qsortorder')); echo ''; echo ""; $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"), 'qtype, name ASC' => get_string("sorttypealpha", "quiz"), 'id ASC' => get_string("sortage", "quiz")); - $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true); + $orderselect = choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true); $orderselect .= ''; echo ""; @@ -350,14 +349,13 @@ function question_list($course, $pageurl, $categoryid, $cm = null, } echo "
$straction$strquestionname $orderselect $strtype
\n"; - $paging = print_paging_bar($totalnumber, $page, $perpage, - "edit.php?".$pageurl->get_query_string()."&perpage=$perpage&", 'page', + $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true); if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) { if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) { - $showall = ''.get_string('showall', 'moodle', $totalnumber).''; + $showall = ''.get_string('showall', 'moodle', $totalnumber).''; } else { - $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; + $showall = ''.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).''; } if ($paging) { $paging = substr($paging, 0, strrpos($paging, '')); @@ -420,32 +418,9 @@ function question_list($course, $pageurl, $categoryid, $cm = null, * {@link http://maths.york.ac.uk/serving_maths} * @param moodle_url $pageurl object representing this pages url. */ -function showbank($pageurl, $cm){ +function question_showbank($pageurl, $cm, $page, $perpage, $sortorder){ global $SESSION, $COURSE; - $page = optional_param('page', -1, PARAM_INT); - $perpage = optional_param('perpage', -1, PARAM_INT); - $sortorder = optional_param('sortorder', ''); - if (preg_match("/[';]/", $sortorder)) { - error("Incorrect use of the parameter 'sortorder'"); - } - - if ($page > -1) { - $SESSION->questionpage = $page; - } else { - $page = isset($SESSION->questionpage) ? $SESSION->questionpage : 0; - } - if ($perpage > -1) { - $SESSION->questionperpage = $perpage; - } else { - $perpage = isset($SESSION->questionperpage) ? $SESSION->questionperpage : DEFAULT_QUESTIONS_PER_PAGE; - } - - if ($sortorder) { - $SESSION->questionsortorder = $sortorder; - } else { - $sortorder = isset($SESSION->questionsortorder) ? $SESSION->questionsortorder : 'qtype, name ASC'; - } $SESSION->fromurl = $pageurl->out(); /// Now, check for commands on this page and modify variables as necessary @@ -458,8 +433,8 @@ function showbank($pageurl, $cm){ error(get_string('categorynoedit', 'quiz', $tocategory->name), $pageurl->out()); } foreach ($_POST as $key => $value) { // Parse input for question ids - if (substr($key, 0, 1) == "q") { - $key = substr($key,1); + if (preg_match('!q([0-9]+)!', $key, $matches)) { + $key = $matches[1]; if (!set_field('question', 'category', $tocategory->id, 'id', $key)) { error('Could not update category field'); } @@ -501,9 +476,8 @@ function showbank($pageurl, $cm){ // an asterix in front of those that are in use $inuse = false; // set to true if at least one of the questions is in use foreach ($rawquestions as $key => $value) { // Parse input for question ids - if (substr($key, 0, 1) == "q") { - $key = substr($key,1); - $questionlist .= $key.','; + if (preg_match('!q([0-9]+)!', $key, $matches)) { + $key = $matches[1]; $questionlist .= $key.','; if (record_exists('quiz_question_instances', 'question', $key) or record_exists('question_states', 'originalquestion', $key)) { $questionnames .= '* '; @@ -586,4 +560,62 @@ function showbank($pageurl, $cm){ print_box_end(); } +/** + * Common setup for all pages for editing questions. + * @param boolean $requirecmid require cmid? default false + * @param boolean $requirecourseid require courseid, if cmid is not given? default true + * @return array $thispageurl, $courseid, $cmid, $cm, $module, $pagevars + */ +function question_edit_setup($requirecmid = false, $requirecourseid = true){ + $thispageurl = new moodle_url(); + if ($requirecmid){ + $cmid =required_param('cmid', PARAM_INT); + } else { + $cmid = optional_param('cmid', 0, PARAM_INT); + } + if ($cmid){ + list($module, $cm) = get_module_from_cmid($cmid); + $courseid = $cm->course; + $thispageurl->params(compact('cmid')); + } else { + $module = null; + $cm = null; + if ($requirecourseid){ + $courseid = required_param('courseid', PARAM_INT); + } else { + $courseid = optional_param('courseid', 0, PARAM_INT); + } + if ($courseid){ + $thispageurl->params(compact('courseid')); + } + } + + $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT); + $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT); + $pagevars['qsortorder'] = optional_param('qsortorder', ''); + + if (preg_match("/[';]/", $pagevars['qsortorder'])) { + error("Incorrect use of the parameter 'qsortorder'"); + } + + if ($pagevars['qpage'] > -1) { + $thispageurl->param('qpage', $pagevars['qpage']); + } else { + $pagevars['qpage'] = 0; + } + + if ($pagevars['qperpage'] > -1) { + $thispageurl->param('qperpage', $pagevars['qperpage']); + } else { + $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE; + } + + if ($pagevars['qsortorder']) { + $thispageurl->param('qsortorder', $pagevars['qsortorder']); + } else { + $pagevars['qsortorder'] = 'qtype, name ASC'; + } + return array($thispageurl, $courseid, $cmid, $cm, $module, $pagevars); + +} ?> diff --git a/question/export.php b/question/export.php index 6c0d19ae98..32b1e22f2e 100644 --- a/question/export.php +++ b/question/export.php @@ -12,10 +12,11 @@ require_once("../config.php"); require_once( "editlib.php" ); - // get parameters + list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(); + $categoryid = optional_param('category',0, PARAM_INT); $cattofile = optional_param('cattofile',0, PARAM_BOOL); - $courseid = required_param('courseid',PARAM_INT); + $exportfilename = optional_param('exportfilename','',PARAM_FILE ); $format = optional_param('format','', PARAM_FILE ); @@ -41,7 +42,7 @@ if ($categoryid) { // update category in session variable $SESSION->questioncat = $categoryid; - } else { // try to get category from modform + } else { // try to get category from session if (isset($SESSION->questioncat)) { $categoryid = $SESSION->questioncat; } @@ -73,20 +74,28 @@ } /// Header - if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { - $strupdatemodule = has_capability('moodle/course:manageactivities', $context) - ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename ) + if ($cm!==null) { + $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) + ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname)) : ""; - print_header_simple($txt->exportquestions, '', - "wwwroot/mod/quiz/index.php?id=$course->id\">$txt->modulenameplural". - " -> wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).''. - ' -> '.$txt->exportquestions, - "", "", true, $strupdatemodule); + $crumbs = array(); + $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity'); + $crumbs[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title'); + $crumbs[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + print_header_simple($txt->exportquestions, '', $navigation, "", "", true, $strupdatemodule); + $currenttab = 'edit'; $mode = 'export'; - include($CFG->dirroot.'/mod/quiz/tabs.php'); + ${$cm->modname} = $module; + include($CFG->dirroot."/mod/$cm->modname/tabs.php"); } else { - print_header_simple($txt->exportquestions, '', $txt->exportquestions); + // Print basic page layout. + $crumbs = array(); + $crumbs[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + + print_header_simple($txt->exportquestions, '', $navigation); // print tabs $currenttab = 'export'; include('tabs.php'); @@ -116,15 +125,15 @@ $qformat->setCattofile( $cattofile ); if (! $qformat->exportpreprocess()) { // Do anything before that we need to - error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id"); + error( $txt->exporterror, $thispageurl->out(false, array('category'=>$category->id))); } if (! $qformat->exportprocess()) { // Process the export data - error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id"); + error( $txt->exporterror, $thispageurl->out(false, array('category'=>$category->id))); } if (! $qformat->exportpostprocess()) { // In case anything needs to be done after - error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id"); + error( $txt->exporterror, $thispageurl->out(false, array('category'=>$category->id))); } echo "
"; @@ -139,7 +148,7 @@ echo "

"; echo "

$txt->downloadextra

"; - print_continue("edit.php?courseid=$course->id"); + print_continue("edit.php?".$thispageurl->get_query_string()); print_footer($course); exit; } @@ -161,8 +170,7 @@
- - + hidden_params_out(array(), 3); ?> diff --git a/question/import.php b/question/import.php index 3c5241416a..1d2115d54b 100644 --- a/question/import.php +++ b/question/import.php @@ -14,12 +14,13 @@ require_once($CFG->libdir . '/uploadlib.php'); require_once($CFG->libdir . '/questionlib.php'); + list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(false, false); + // get parameters $params = new stdClass; $params->choosefile = optional_param('choosefile','',PARAM_PATH); $categoryid = optional_param('category', 0, PARAM_INT); $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL ); - $courseid = optional_param('courseid', 0, PARAM_INT); $format = optional_param('format','',PARAM_FILE); $params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA); $params->stoponerror = optional_param('stoponerror', 0, PARAM_BOOL); @@ -28,7 +29,6 @@ $txt = new stdClass(); $txt->category = get_string('category','quiz'); $txt->choosefile = get_string('choosefile','quiz'); - $txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz"); $txt->file = get_string('file'); $txt->fileformat = get_string('fileformat','quiz'); $txt->fromfile = get_string('fromfile','quiz'); @@ -58,7 +58,7 @@ if ($categoryid) { // update category in session variable $SESSION->questioncat = $categoryid; - } else { // try to get category from modform + } else { // try to get category from session if (isset($SESSION->questioncat)) { $categoryid = $SESSION->questioncat; } @@ -94,20 +94,28 @@ // PAGE HEADER //========== - if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { - $strupdatemodule = has_capability('moodle/course:manageactivities', $context) - ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename) + if ($cm!==null) { + $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) + ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname)) : ""; - print_header_simple($txt->importquestions, '', - "wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural.''. - " -> wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).''. - ' -> '.$txt->importquestions, - "", "", true, $strupdatemodule); + $crumbs = array(); + $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity'); + $crumbs[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title'); + $crumbs[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule); + $currenttab = 'edit'; $mode = 'import'; - include($CFG->dirroot.'/mod/quiz/tabs.php'); + ${$cm->modname} = $module; + include($CFG->dirroot."/mod/$cm->modname/tabs.php"); } else { - print_header_simple($txt->importquestions, '', $txt->importquestions); + // Print basic page layout. + $crumbs = array(); + $crumbs[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title'); + $navigation = build_navigation($crumbs); + + print_header_simple($txt->importquestions, '', $navigation); // print tabs $currenttab = 'import'; include('tabs.php'); @@ -130,8 +138,7 @@ else { notify($txt->uploadproblem); } - } - else { + } else { // must be upload file if (empty($_FILES['newfile'])) { notify( $txt->uploadproblem ); @@ -168,24 +175,21 @@ // Do anything before that we need to if (! $qformat->importpreprocess()) { - error( $txt->importerror , - "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); + error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id))); } // Process the uploaded file if (! $qformat->importprocess() ) { - error( $txt->importerror , - "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); + error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id))); } // In case anything needs to be done after if (! $qformat->importpostprocess()) { - error( $txt->importerror , - "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); + error( $txt->importerror, $thispageurl->out(false, array('category'=>$category->id))); } echo "
"; - print_continue("edit.php?courseid=$course->id"); + print_continue("edit.php?".$thispageurl->get_query_string()); print_footer($course); exit; } @@ -210,6 +214,7 @@
+ hidden_params_out(array(), 3); ?>
category; ?>: