diff --git a/question/category.php b/question/category.php
index ef2aeebf32a..64cdc7965c5 100644
--- a/question/category.php
+++ b/question/category.php
@@ -6,11 +6,11 @@
* @author Martin Dougiamas and many others.
* {@link http://moodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
-* @package quiz
+* @package question
*/
- require_once("../../config.php");
- require_once("locallib.php");
+ require_once("../config.php");
+ require_once($CFG->libdir.'/questionlib.php');
require_once("category_class.php");
// get values from form
@@ -34,10 +34,6 @@
error("Course ID is incorrect");
}
- if (isset($_REQUEST['backtoquiz'])) {
- redirect("edit.php");
- }
-
require_login($course->id, false);
if (!isteacheredit($course->id)) {
@@ -51,21 +47,21 @@
// PAGE HEADER
//==========
+ // TODO: generalise this to any activity
if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
$strupdatemodule = isteacheredit($course->id)
? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
: "";
print_header_simple(get_string('editcategories', 'quiz'), '',
- "id\">".get_string('modulenameplural', 'quiz').''.
- " -> id\">".format_string($quiz->name).''.
+ "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);
$currenttab = 'edit';
$mode = 'categories';
- include('tabs.php');
+ include($CFG->dirroot.'/mod/quiz/tabs.php');
} else {
print_header_simple(get_string('editcategories', 'quiz'), '',
- "id\">".get_string('modulenameplural', 'quiz').''.
'-> '.get_string('editquestions', 'quiz').''.
' -> '.get_string('editcategories', 'quiz'));
}
diff --git a/question/category_class.php b/question/category_class.php
index e227a60bcd2..af89615230d 100644
--- a/question/category_class.php
+++ b/question/category_class.php
@@ -4,6 +4,10 @@
*
* Used for handling changes to the quiz categories
*
+* @version $Id$
+* @author Martin Dougiamas and many others. {@link http://moodle.org}
+* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+* @package question
*/
// number of categories to display on page
diff --git a/question/edit.php b/question/edit.php
index 2441ae698c8..3e3913ab2b8 100644
--- a/question/edit.php
+++ b/question/edit.php
@@ -1,119 +1,24 @@
course)) { // data submitted
-
- $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
-
- $cm = get_coursemodule_from_instance('quiz', $modform->instance);
- $modform->cmid = $cm->id;
- // We don't log all visits to this page but only those that recreate modform
- add_to_log($cm->course, 'quiz', 'editquestions',
- "view.php?id=$cm->id",
- "$quizid", $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 (!empty($sortorder)) {
- // no quiz or course was specified so we need to use the stored modform
- if (isset($SESSION->modform)) {
- $modform = $SESSION->modform;
- } else {
- exit;
- }
- } else {
- // we might get here after editing a question in
- // a popup window. So close window automatically.
-?>
-
-
-modform)) {
- $modform = $SESSION->modform;
- } else {
- exit;
- }
- }
-
- if (! $course = get_record("course", "id", $modform->course)) {
+ if (! $course = get_record("course", "id", $courseid)) {
error("This course doesn't exist");
}
@@ -123,398 +28,19 @@ if (self.name == 'editquestion') {
error("You can't modify this course!");
}
- if (isset($modform->instance)
- && empty($modform->grades)) // Construct an array to hold all the grades.
- {
- $modform->grades = quiz_get_all_question_grades($modform);
- }
-
- if ($page > -1) {
- $modform->page = $page;
- } else {
- $page = isset($modform->page) ? $modform->page : 0;
- }
-
-/// Now, check for commands on this page and modify variables as necessary
-
- if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot
- $questions = explode(",", $modform->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);
- // Always have a page break at the end
- $modform->questions = $modform->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)) {
- error('Could not save question list');
- }
- }
- }
-
- if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot
- $questions = explode(",", $modform->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);
- // Avoid duplicate page breaks
- $modform->questions = str_replace(',0,0', ',0', $modform->questions);
- if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->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);
- }
-
- 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 (isset($_REQUEST['addrandom']) and confirm_sesskey()) { /// Add random questions to the quiz
- $recurse = optional_param('recurse', 0, PARAM_BOOL);
- $categoryid = required_param('categoryid', PARAM_INT);
- $randomcount = required_param('randomcount', PARAM_INT);
- // load category
- if (! $category = get_record('quiz_categories', 'id', $categoryid)) {
- error('Category ID is incorrect');
- }
- // find existing random questions in this category
- $random = RANDOM;
- if ($existingquestions = get_records_select('quiz_questions', "qtype = '$random' AND category = '$category->id'")) {
- // now remove the ones that are already used in this quiz
- if ($questionids = explode(',', $modform->questions)) {
- foreach ($questionids as $questionid) {
- unset($existingquestions[$questionid]);
- }
- }
- // now take as many of these as needed
- $i = 0;
- 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);
- $i++;
- }
- }
- $randomcreate = $randomcount - $i; // the number of additional random questions needed.
- } else {
- $randomcreate = $randomcount;
- }
-
- if ($randomcreate > 0) {
-
- $form->name = get_string('random', 'quiz') .' ('. $category->name .')';
- $form->questiontext = $recurse; // we use the questiontext field to store the info
- // on whether to include questions in subcategories
- $form->questiontextformat = 0;
- $form->image = '';
- $form->defaultgrade = 1;
- $form->hidden = 1;
- for ($i=0; $i<$randomcreate; $i++) {
- $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
- $question = new stdClass;
- $question->category = $category->id;
- $question->qtype = RANDOM;
- $question = $QUIZ_QTYPES[RANDOM]->save_question($question, $form, $course);
- if(!isset($question->id)) {
- error('Could not insert new random question!');
- }
- quiz_add_quiz_question($question->id, $modform);
- }
- }
- }
-
- if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz
- if (isset($_REQUEST['questionsperpage'])) {
- $modform->questionsperpage = required_param('questionsperpage', 1, PARAM_INT);
- if (!set_field('quiz', 'questionsperpage', $modform->questionsperpage, 'id', $modform->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)) {
- error('Could not save layout');
- }
- }
-
- if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
- if (!$tocategory = get_record('quiz_categories', 'id', $_REQUEST['category'])) {
- error('Invalid category');
- }
- if (!isteacheredit($tocategory->course)) {
- error(get_string('categorynoedit', 'quiz', $tocategory->name), 'edit.php');
- }
- foreach ($_POST as $key => $value) { // Parse input for question ids
- if (substr($key, 0, 1) == "q") {
- $key = substr($key,1);
- if (!set_field('quiz_questions', 'category', $tocategory->id, 'id', $key)) {
- error('Could not update category field');
- }
- }
- }
- }
-
- if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
- quiz_delete_quiz_question($_REQUEST['delete'], $modform);
- }
-
- if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
-
- if (isset($confirm) and confirm_sesskey()) { // teacher has already confirmed the action
- if ($confirm == md5($deleteselected)) {
- if ($questionlist = explode(',', $deleteselected)) {
- // for each question either hide it if it is in use or delete it
- foreach ($questionlist as $questionid) {
- if (record_exists('quiz_question_instances', 'question', $questionid) or
- record_exists('quiz_states', 'originalquestion', $questionid)) {
- if (!set_field('quiz_questions', 'hidden', 1, 'id', $questionid)) {
- error('Was not able to hide question');
- }
- } else {
- delete_records("quiz_questions", "id", $questionid);
- }
- }
- }
- redirect("edit.php");
- } else {
- error("Confirmation string was incorrect");
- }
-
- } else { // teacher still has to confirm
- // make a list of all the questions that are selected
- $rawquestions = $_POST;
- $questionlist = ''; // comma separated list of ids of questions to be deleted
- $questionnames = ''; // string with names of questions separated by
with
- // 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 (record_exists('quiz_question_instances', 'question', $key) or
- record_exists('quiz_states', 'originalquestion', $key)) {
- $questionnames .= '* ';
- $inuse = true;
- }
- $questionnames .= get_field('quiz_questions', 'name', 'id', $key).'
';
- }
- }
- if (!$questionlist) { // no questions were selected
- redirect('edit.php');
- }
- $questionlist = rtrim($questionlist, ',');
-
- // Add an explanation about questions in use
- if ($inuse) {
- $questionnames .= get_string('questionsinuse', 'quiz');
- }
- print_header_simple($streditingquestions, '',
- "$streditingquestions");
- notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
- "edit.php?sesskey=$USER->sesskey&deleteselected=$questionlist&confirm=".md5($questionlist), "edit.php");
- print_footer($course);
- exit;
- }
- }
-
- if (isset($_REQUEST['savechanges']) and confirm_sesskey()) {
- /// We need to save the new ordering (if given) and the new grades
- $oldquestions = explode(",", $modform->questions); // the questions in the old order
- $questions = array(); // for questions in the new order
- $rawgrades = $_POST;
- unset($modform->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);
- $questions[$value] = $oldquestions[$key];
- }
- }
-
- // If ordering info was given, reorder the questions
- if ($questions) {
- ksort($questions);
- $modform->questions = implode(",", $questions);
- // Always have a page break at the end
- $modform->questions = $modform->questions . ',0';
- // Avoid duplicate page breaks
- while (strpos($modform->questions, ',0,0')) {
- $modform->questions = str_replace(',0,0', ',0', $modform->questions);
- }
- if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
- error('Could not save question list');
- }
- }
-
- // If rescaling is required save the new maximum
- if (isset($_REQUEST['maxgrade'])) {
- $modform->grade = optional_param('maxgrade', 0);
- if (!set_field('quiz', 'grade', $modform->grade, 'id', $modform->instance)) {
- error('Could not set new maximal grade for quiz');
- }
- }
- }
-
- if (isset($_REQUEST['cat'])) { /// coming from category selection drop-down menu
- $modform->category = $cat;
- $page = 0;
- $modform->page = 0;
- }
-
- if(isset($_REQUEST['recurse'])) {
- $SESSION->quiz_recurse = optional_param('recurse', 0, PARAM_BOOL);
- }
- if(isset($_REQUEST['showbreaks'])) {
- $SESSION->quiz_showbreaks = optional_param('showbreaks', 0, PARAM_BOOL);
- $SESSION->quiz_reordertool = optional_param('reordertool', 0, PARAM_BOOL);
- }
- if(isset($_REQUEST['showhidden'])) {
- $SESSION->quiz_showhidden = optional_param('showhidden', 0, PARAM_BOOL);
- }
-
-/// Delete any teacher preview attempts if the quiz has been modified
- if (isset($_REQUEST['setgrades']) 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);
- }
-
-/// all commands have been dealt with, now print the page
-
- if (empty($modform->category) or !record_exists('quiz_categories', 'id', $modform->category)) {
- $category = quiz_get_default_category($course->id);
- $modform->category = $category->id;
- }
- if (!isset($SESSION->quiz_recurse)) {
- $SESSION->quiz_recurse = 1;
- }
- if (!isset($SESSION->quiz_showhidden)) {
- $SESSION->quiz_showhidden = false;
- }
- if (!isset($SESSION->quiz_showbreaks)) {
- $SESSION->quiz_showbreaks = ($CFG->quiz_questionsperpage < 2) ? 0 : 1;
- }
- if (!isset($SESSION->quiz_reordertool)) {
- $SESSION->quiz_reordertool = 0;
- }
-
- $SESSION->modform = $modform;
-
// Print basic page layout.
- if (isset($modform->instance) and record_exists_sql("SELECT * FROM {$CFG->prefix}quiz_attempts WHERE quiz = '$modform->instance' AND preview = '0' LIMIT 1")){
- // one column layout with table of questions used in this quiz
- $strupdatemodule = isteacheredit($course->id)
- ? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz'))
- : "";
- print_header_simple($streditingquiz, '',
- "id\">$strquizzes".
- " -> instance\">".format_string($modform->name).''.
- " -> $streditingquiz", "", "",
- true, $strupdatemodule);
+ $streditingquestions = get_string('editquestions', "quiz");
+ print_header_simple($streditingquestions, '',
+ "$streditingquestions");
+ print_heading_with_help(get_string('questions', 'quiz'), 'questionbank');
+ echo '
| ';
- $currenttab = 'edit';
- $mode = 'editq';
- $quiz = &$modform;
- include('tabs.php');
-
- print_simple_box_start("center");
-
- $attemptcount = count_records('quiz_attempts', 'quiz', $modform->instance, 'preview', 0);
-
- $strviewallanswers = get_string("viewallanswers","quiz",$attemptcount);
- $strattemptsexist = get_string("attemptsexist","quiz");
- $usercount = count_records_select('quiz_attempts', "quiz = '$modform->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
- $strusers = $course->students;
- if (! $cm = get_coursemodule_from_instance("quiz", $modform->instance, $course->id)) {
- error("Course Module ID was incorrect");
- }
- echo " id\">$strviewallanswers ($usercount $strusers)"; - echo ""; - echo " \n"; - - $sumgrades = quiz_print_question_list($modform, false, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); - if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) { - error('Failed to set sumgrades'); - } - - print_simple_box_end(); - print_footer($course); - exit; - } - - if (!isset($modform->instance)) { - // one column layout for non-quiz-specific editing page - print_header_simple($streditingquestions, '', - "$streditingquestions"); - print_heading_with_help(get_string('questions', 'quiz'), 'questionbank'); - echo '
| |||||||||||