]> git.mjollnir.org Git - moodle.git/commitdiff
It is now possible to have also questions in all subcategories listed on edit.php...
authorgustav_delius <gustav_delius>
Wed, 5 Jan 2005 17:27:23 +0000 (17:27 +0000)
committergustav_delius <gustav_delius>
Wed, 5 Jan 2005 17:27:23 +0000 (17:27 +0000)
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
mod/quiz/edit.php
mod/quiz/lib.php
mod/quiz/multiple.php
mod/quiz/questiontypes/random/questiontype.php
mod/quiz/questiontypes/random/random.html

index 6f97ec93a62b6dd1ac1fe9b0c2ec05cd9850f316..432b9fbd491e480fff12f2763baf0b0072f9e94c 100644 (file)
@@ -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
 *
index ab05c34efedcaa1a8f7cf268e5cbb171c1d5fc7e..8ce36301da5f808ba5bfd8ee9ee796241e4fe24d 100644 (file)
@@ -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('');
         }
 
     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);
     }
 
     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;
         echo '</td><td valign="top" width="50%">';
     }
     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 '</td></tr>';
index 7146103ff40923f36086349e99d2102e8d24b0ac..46a3721dc48977aee06501b6515e8527843db4f8 100644 (file)
@@ -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 "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
     echo "<b>$strcategory:</b>&nbsp;";
     echo "</td><td>";
-    popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "choose", "", "", false, "self");
+    popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
     echo "</td><td align=\"right\">";
     echo "<form method=\"get\" action=\"category.php\">";
     echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
     echo "<input type=\"submit\" value=\"$streditcats\" />";
     echo "</form>";
-    echo "</td></tr></table>";
+    echo '</td></tr></table>';
+    echo '<form method="get" action="edit.php" name="recurse">';
+    print_string('recurse', 'quiz');
+    echo '<input type="hidden" name="recurse" value="0">';
+    echo '<input type="checkbox" name="recurse" value="1"';
+    if ($recurse) {
+        echo ' checked="checked"';
+    }
+    echo ' onclick="document.recurse.submit(); return true;">';
+    echo '</form>';
 }
 
 
 function add_indented_names(&$categories, $id = 0, $indent = 0) {
 // returns the categories with their names indented to show parent-child relationships
     $fillstr = '&nbsp;&nbsp;&nbsp;';
- $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 '</center>';
 
-    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 "<p align=\"center\">";
         print_string("noquestions", "quiz");
         echo "</p>";
@@ -1642,7 +1653,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true) {
     echo "</tr>\n";
     foreach ($questions as $question) {
         if ($question->qtype == RANDOM) {
-            continue;
+            //continue;
         }
         echo "<tr bgcolor=\"$THEME->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\" /></a>&nbsp;";
                 echo "<a title=\"$stredit\" href=\"question.php?id=$question->id\"><img
                      src=\"../../pix/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>";
-            echo "</td>\n";// deleted </tr> jm
+            echo "</td>\n";
         }
         echo "</tr>\n";
     }
+    $numquestions = count_records_select('quiz_questions', "category IN ($categorylist) AND qtype != '".RANDOM."'");
+    echo '<tr><td colspan="3">';
+    print_paging_bar($numquestions, $page, $perpage,
+                "edit.php?perpage=$perpage&amp;");
+    echo '</td></tr>';
+
     if ($quizselected) {
         echo "<tr>\n<td colspan=\"3\">";
         echo "<input type=\"submit\" name=\"add\" value=\"<< $straddselectedtoquiz\" />\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 '<input type="hidden" name="randomgrade", value="1" />';
+        echo '<input type="hidden" name="randomgrade" value="1" />';
+        echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
         echo "<input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
         echo ' <input type="submit" name="save" value="'. get_string('add') .'" />';
         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;
+}
 ?>
index 3c715c6ad3c4697ad6b3cc3808b026f1f8be77e1..20251f61efd9531c557fe7afef0b8c2fa82060aa 100644 (file)
             // 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++) {
index 1a7790f5eced37ea68501f2c8e2a50974b10294e..a269a0d09b874e2af8d332fc32579de2c27840b0 100644 (file)
@@ -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;
index 61e13b7eb33f1c4c203808016487fa1f16d8b987..36d529d46300f739bebb71d18bf624d7fe45358f 100644 (file)
 
 </tr>
 
-</table>
+<tr valign="top">
+
+    <td align="right"><b><?php  print_string("recurse", "quiz") ?>:</b></td>
+
+    <td>
+        <?php  if (!isset($question->questiontext)) {
+
+            $question->questiontext = "0";
 
+        } ?>
 
+        <input type="hidden" name="questiontext" value="0" />
+        <input type="checkbox" name="questiontext" value="1" <?php echo ($question->questiontext == "1") ? 'checked="checked"' : '' ?> />
+
+    </td>
+
+</tr>
+
+</table>
 
-<input type="hidden" name="questiontext" value="---" />
 <input type="hidden" name="questiontextformat" value="0" />