From 1da1f5a8906817295754ae7d40ba4d86d1a55e87 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Wed, 5 Jan 2005 17:27:23 +0000 Subject: [PATCH] It is now possible to have also questions in all subcategories listed on edit.php. Because there can be a large number of questions the list of questions is now paged. The random questions have another option specifying whether they can choose their questions also from subcategories or not. This option is stored in the unused questiontext field. --- mod/quiz/category.php | 29 +++-------- mod/quiz/edit.php | 19 +++++-- mod/quiz/lib.php | 50 +++++++++++++++---- mod/quiz/multiple.php | 18 +++---- .../questiontypes/random/questiontype.php | 12 +++-- mod/quiz/questiontypes/random/random.html | 19 ++++++- 6 files changed, 94 insertions(+), 53 deletions(-) diff --git a/mod/quiz/category.php b/mod/quiz/category.php index 6f97ec93a6..432b9fbd49 100644 --- a/mod/quiz/category.php +++ b/mod/quiz/category.php @@ -171,25 +171,12 @@ class quiz_category_object { function initialize() { /// Get the existing categories - if (!$this->categories = $this->get_quiz_categories(null, "parent, sortorder, name ASC")) { - unset($this->categories); - $defaultcategory = quiz_get_default_category($this->course->id); - if (!$defaultcategory) { - error("Error: Could not find or make a category!"); - } else { - $this->categories[$defaultcategory->id] = $defaultcategory; - $this->defaultcategory = $defaultcategory->id; - } - } else { - /// Find lowest ID category for this course - this is the default category - $this->defaultcategory = 99999; - foreach ($this->categories as $category) { - if ($category->course == $this->course->id && $category->id < $this->defaultcategory) { - $this->defaultcategory = $category->id; - } - } - } - + if (!$this->defaultcategory = quiz_get_default_category($this->course->id)) { + error("Error: Could not find or make a category!"); + } + + $this->categories = $this->get_quiz_categories(null, "parent, sortorder, name ASC"); + $this->categories = $this->arrange_categories($this->categories); // create the array of id=>full_name strings @@ -440,8 +427,7 @@ class quiz_category_object { $edittable->data[] = array(); $edittable->tablealign = 'center'; - /// Each section below adds a data cell to the table row - + /// Each section below adds a data cell to the table row $viableparents = $this->categorystrings; $this->set_viable_parents($viableparents, $category); @@ -570,6 +556,7 @@ class quiz_category_object { } } } + /** * Gets quiz categories * diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index ab05c34efe..8ce36301da 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -7,6 +7,8 @@ optional_variable($courseid); optional_variable($quizid); + optional_variable($page, 0); + optional_variable($perpage, "20"); if (empty($destination)) { $destination = ""; @@ -45,8 +47,8 @@ } 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. + // We currently also get here after editing a question by + // following the edit link on the review page. Perhaps that should be fixed. error(''); } @@ -76,6 +78,10 @@ if (isset($cancel)) { redirect('view.php?q='.$modform->instance); } + + if (isset($recurse)) { + $modform->recurse = $recurse; + } if (!empty($up)) { /// Move the given question up a slot $questions = explode(",", $modform->questions); @@ -188,7 +194,10 @@ } if (empty($modform->category)) { - $modform->category = ""; + $modform->category = quiz_get_default_category($modform->course)->id; + } + if (!isset($modform->recurse)) { + $modform->recurse = 1; } $modform->sumgrades = 0; @@ -255,14 +264,14 @@ echo ''; } print_simple_box_start("center", "100%", $THEME->cellcontent2); - quiz_print_category_form($course, $modform->category); + quiz_print_category_form($course, $modform->category, $modform->recurse); print_simple_box_end(); print_spacer(5,1); print_simple_box_start("center", "100%", $THEME->cellcontent2); quiz_print_cat_question_list($modform->category, - isset($modform->instance)); + isset($modform->instance), $modform->recurse, $page, $perpage); print_simple_box_end(); echo ''; diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 7146103ff4..46a3721dc4 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -1247,7 +1247,7 @@ function quiz_print_quiz_questions($quiz, $questions, $results=NULL, function quiz_get_default_category($courseid) { /// Returns the current category - if ($categories = get_records("quiz_categories", "course", $courseid, "id")) { + if ($categories = get_records_select("quiz_categories", "course = '$courseid' AND parent = '0'", "id")) { foreach ($categories as $category) { return $category; // Return the first one (lowest id) } @@ -1297,7 +1297,7 @@ function quiz_get_category_menu($courseid, $published=false) { return $catmenu; } -function quiz_print_category_form($course, $current) { +function quiz_print_category_form($course, $current, $recurse=1) { /// Prints a form to choose categories /// Make sure the default category exists for this course @@ -1328,20 +1328,29 @@ function quiz_print_category_form($course, $current) { echo "
"; echo "$strcategory: "; echo ""; - popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "choose", "", "", false, "self"); + popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "", "", "", false, "self"); echo ""; echo "
"; echo "id\" />"; echo ""; echo "
"; - echo "
"; + echo ''; + echo '
'; + print_string('recurse', 'quiz'); + echo ''; + echo ''; + echo '
'; } function add_indented_names(&$categories, $id = 0, $indent = 0) { // returns the categories with their names indented to show parent-child relationships $fillstr = '   '; - $fill = str_repeat($fillstr, $indent); + $fill = str_repeat($fillstr, $indent); $children = array(); $keys = array_keys($categories); @@ -1547,8 +1556,8 @@ function quiz_print_question_list($questionlist, $grades) { } -function quiz_print_cat_question_list($categoryid, $quizselected=true) { -// Prints a form to choose categories +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; @@ -1620,7 +1629,9 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true) { echo ''; - if (!$questions = get_records("quiz_questions", "category", $category->id, "qtype ASC, name ASC")) { + $categorylist = ($recurse) ? quiz_categorylist($category->id) : $category->id; + + if (!$questions = get_records_select('quiz_questions', "category IN ($categorylist) AND qtype != '".RANDOM."'", 'qtype, name ASC', '*', $page*$perpage, $perpage)) { echo "

"; print_string("noquestions", "quiz"); echo "

"; @@ -1642,7 +1653,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true) { echo "\n"; foreach ($questions as $question) { if ($question->qtype == RANDOM) { - continue; + //continue; } echo "cellcontent\">\n"; if ($quizselected) { @@ -1662,10 +1673,16 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true) { src=\"../../pix/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /> "; echo "id\">\"$stredit\""; - echo "\n";// deleted jm + echo "\n"; } echo "\n"; } + $numquestions = count_records_select('quiz_questions', "category IN ($categorylist) AND qtype != '".RANDOM."'"); + echo ''; + print_paging_bar($numquestions, $page, $perpage, + "edit.php?perpage=$perpage&"); + echo ''; + if ($quizselected) { echo "\n"; echo "\n"; @@ -1685,7 +1702,8 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true) { print_string('addrandom2', 'quiz'); // Don't offer the option to change the grade //choose_from_menu($randomcount, 'randomgrade', '1', ''); - echo ''; + echo ''; + echo ''; echo "id\" />"; echo ' '; helpbutton('random', get_string('random', 'quiz'), 'quiz'); @@ -2270,4 +2288,14 @@ function get_exp_answers( $question_num ) { return $answers; } +function quiz_categorylist($categoryid) { + // returns a comma separated list of ids of the category and all subcategories + $categorylist = $categoryid; + if ($subcategories = get_records('quiz_categories', 'parent', $categoryid, 'sortorder ASC', 'id, id')) { + foreach ($subcategories as $subcategory) { + $categorylist .= ','. quiz_categorylist($subcategory->id); + } + } + return $categorylist; +} ?> diff --git a/mod/quiz/multiple.php b/mod/quiz/multiple.php index 3c715c6ad3..20251f61ef 100644 --- a/mod/quiz/multiple.php +++ b/mod/quiz/multiple.php @@ -39,22 +39,19 @@ // 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 any + // now remove the ones that are already used in this quiz if ($questionids = explode(',', $modform->questions)) { foreach ($questionids as $questionid) { - foreach ($existingquestions as $key => $existingquestion) { - if ($existingquestion->id == $questionid) { - unset($existingquestions[$key]); - break; - } - } + unset($existingquestions[$key]); } } // now take as many of these as needed $i = 0; while (($existingquestion = array_pop($existingquestions)) and ($i < $form->randomcreate)) { - $newquestionids[] = $existingquestion->id; - $i++; + if ($existingquestion->questiontext == "$form->recurse") { + // this question has the right recurse property, so use it + $newquestionids[] = $existingquestion->id; + $i++; } $randomcreate = $form->randomcreate - $i; // the number of additional random questions needed. } else { @@ -66,7 +63,8 @@ $question->qtype = RANDOM; $question->category = $category->id; $question->name = get_string('random', 'quiz') .' ('. $category->name .')'; - $question->questiontext = '---'; + $question->questiontext = "$form->recurse"; // we use the questiontext field to store the info + // on whether to include questions in subcategories $question->image = ''; $question->defaultgrade = $form->randomgrade; for ($i=0; $i<$randomcreate; $i++) { diff --git a/mod/quiz/questiontypes/random/questiontype.php b/mod/quiz/questiontypes/random/questiontype.php index 1a7790f5ec..a269a0d09b 100644 --- a/mod/quiz/questiontypes/random/questiontype.php +++ b/mod/quiz/questiontypes/random/questiontype.php @@ -75,9 +75,15 @@ class quiz_random_qtype extends quiz_default_questiontype { $possiblerandomqtypes = "'" . implode("','", $this->possiblerandomqtypes) . "'"; + if ($question->questiontext == "1") { + // recurse into subcategories + $categorylist = quiz_categorylist($question->category); + } else { + $categorylist = $question->category; + } $this->catrandoms[$question->category] = get_records_sql ("SELECT * FROM {$CFG->prefix}quiz_questions - WHERE category = '$question->category' + WHERE category IN ($categorylist) AND id NOT IN ($questionsinuse) AND qtype IN ($possiblerandomqtypes)"); $this->catrandoms[$question->category] = @@ -215,9 +221,7 @@ class quiz_random_qtype extends quiz_default_questiontype { function get_wrapped_question($question, $nameprefix) { if (!empty($question->response[$nameprefix]) and $actualquestion = get_record('quiz_questions', - 'id', $question->response[$nameprefix], - // The category check is a security check - 'category', $question->category)) { + 'id', $question->response[$nameprefix])) { $actualquestion->response = $question->response; unset($actualquestion->response[$nameprefix]); $actualquestion->maxgrade = $question->maxgrade; diff --git a/mod/quiz/questiontypes/random/random.html b/mod/quiz/questiontypes/random/random.html index 61e13b7eb3..36d529d463 100644 --- a/mod/quiz/questiontypes/random/random.html +++ b/mod/quiz/questiontypes/random/random.html @@ -38,11 +38,26 @@ - + + + : + + + questiontext)) { + + $question->questiontext = "0"; + } ?> + + questiontext == "1") ? 'checked="checked"' : '' ?> /> + + + + + + - -- 2.39.5