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.
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
$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);
}
}
}
+
/**
* Gets quiz categories
*
optional_variable($courseid);
optional_variable($quizid);
+ optional_variable($page, 0);
+ optional_variable($perpage, "20");
if (empty($destination)) {
$destination = "";
} 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>';
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)
}
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
echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
echo "<b>$strcategory:</b> ";
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 = ' ';
- $fill = str_repeat($fillstr, $indent);
+ $fill = str_repeat($fillstr, $indent);
$children = array();
$keys = array_keys($categories);
}
-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;
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>";
echo "</tr>\n";
foreach ($questions as $question) {
if ($question->qtype == RANDOM) {
- continue;
+ //continue;
}
echo "<tr bgcolor=\"$THEME->cellcontent\">\n";
if ($quizselected) {
src=\"../../pix/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a> ";
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&");
+ echo '</td></tr>';
+
if ($quizselected) {
echo "<tr>\n<td colspan=\"3\">";
echo "<input type=\"submit\" name=\"add\" value=\"<< $straddselectedtoquiz\" />\n";
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');
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;
+}
?>
// 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 {
$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++) {
$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] =
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;
</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" />