Skip to content

Commit

Permalink
It is now possible to have also questions in all subcategories listed…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
gustav_delius committed Jan 5, 2005
1 parent 59c005b commit 1da1f5a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 53 deletions.
29 changes: 8 additions & 21 deletions mod/quiz/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,12 @@ function display_user_interface($modform = null) {
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
Expand Down Expand Up @@ -440,8 +427,7 @@ function output_edit_single_table($category) {
$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);
Expand Down Expand Up @@ -570,6 +556,7 @@ function set_viable_parents(&$parentstrings, $category) {
}
}
}

/**
* Gets quiz categories
*
Expand Down
19 changes: 14 additions & 5 deletions mod/quiz/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

optional_variable($courseid);
optional_variable($quizid);
optional_variable($page, 0);
optional_variable($perpage, "20");

if (empty($destination)) {
$destination = "";
Expand Down Expand Up @@ -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('');
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -255,14 +264,14 @@
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>';
Expand Down
50 changes: 39 additions & 11 deletions mod/quiz/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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>";
Expand All @@ -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) {
Expand All @@ -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";
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}
?>
18 changes: 8 additions & 10 deletions mod/quiz/multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++) {
Expand Down
12 changes: 8 additions & 4 deletions mod/quiz/questiontypes/random/questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ function create_response($question, $nameprefix, $questionsinuse) {

$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] =
Expand Down Expand Up @@ -215,9 +221,7 @@ function print_question_formulation_and_controls($question,
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;
Expand Down
19 changes: 17 additions & 2 deletions mod/quiz/questiontypes/random/random.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,26 @@

</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" />


Expand Down

0 comments on commit 1da1f5a

Please sign in to comment.