3 * Functions used by showbank.php to show question editing interface
5 * TODO: currently the function question_list still provides controls specific
6 * to the quiz module. This needs to be generalised.
9 * @author Martin Dougiamas and many others. This has recently been extensively
10 * rewritten by members of the Serving Mathematics project
11 * {@link http://maths.york.ac.uk/serving_maths}
12 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
16 require_once($CFG->libdir
.'/questionlib.php');
18 define('DEFAULT_QUESTIONS_PER_PAGE', 20);
21 * Function to read all questions for category into big array
23 * @param int $category category number
24 * @param bool $noparent if true only questions with NO parent will be selected
25 * @param bool $recurse include subdirectories
26 * @author added by Howard Miller June 2004
28 function get_questions_category( $category, $noparent=false, $recurse=true ) {
32 // questions will be added to an array
35 // build sql bit for $noparent
38 $npsql = " and parent='0' ";
41 // get (list) of categories
43 $categorylist = question_categorylist( $category->id
);
46 $categorylist = $category->id
;
49 // get the list of questions for the category
50 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
52 // iterate through questions, getting stuff we need
53 foreach($questions as $question) {
54 $questiontype = $QTYPES[$question->qtype
];
55 $questiontype->get_question_options( $question );
56 $qresults[] = $question;
64 * Gets the default category in a course
66 * It returns the first category with no parent category. If no categories
67 * exist yet then one is created.
68 * @return object The default category
69 * @param integer $courseid The id of the course whose default category is wanted
71 function get_default_question_category($courseid) {
73 if ($categories = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", "id")) {
74 foreach ($categories as $category) {
75 return $category; // Return the first one (lowest id)
79 // Otherwise, we need to make one
80 $category->name
= get_string("default", "quiz");
81 $category->info
= get_string("defaultinfo", "quiz");
82 $category->course
= $courseid;
83 $category->parent
= 0;
84 // TODO: Figure out why we use 999 below
85 $category->sortorder
= 999;
86 $category->publish
= 0;
87 $category->stamp
= make_unique_id_code();
89 if (!$category->id
= insert_record("question_categories", $category)) {
90 notify("Error creating a default category!");
97 * Return a list of categories nicely formatted
98 * @param int courseid id of course
99 * @param bool published true=include all published categories
100 * @return array formatted category names
102 function question_category_menu($courseid, $published=false) {
103 /// Returns the list of categories
106 $publish = "OR publish = '1'";
110 $categories = get_records_select("question_categories", "course = '$courseid' $publish", 'parent, sortorder, name ASC');
112 $categories = get_records_select("question_categories", '', 'parent, sortorder, name ASC');
117 $categories = add_indented_names($categories);
119 foreach ($categories as $category) {
120 if ($catcourse = get_record("course", "id", $category->course
)) {
121 if ($category->publish
&& ($category->course
!= $courseid)) {
122 $category->indentedname
.= " ($catcourse->shortname)";
124 $catmenu[$category->id
] = $category->indentedname
;
131 * prints a form to choose categories
133 function question_category_form($course, $current, $recurse=1, $showhidden=false) {
136 /// Make sure the default category exists for this course
137 if (!$categories = get_records("question_categories", "course", $course->id
, "id ASC")) {
138 if (!$category = get_default_question_category($course->id
)) {
139 notify("Error creating a default category!");
143 /// Get all the existing categories now
144 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
145 notify("Could not find any question categories!");
146 return false; // Something is really wrong
149 $categories = add_indented_names( $categories );
150 foreach ($categories as $key => $category) {
151 if ($catcourse = get_record("course", "id", $category->course
)) {
152 if ($category->publish
&& $category->course
!= $course->id
) {
153 $category->indentedname
.= " ($catcourse->shortname)";
155 $catmenu[$category->id
] = $category->indentedname
;
158 $strcategory = get_string("category", "quiz");
159 $strshow = get_string("show", "quiz");
160 $streditcats = get_string("editcategories", "quiz");
162 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
163 echo "<b>$strcategory:</b> ";
165 popup_form ("edit.php?courseid=$course->id&cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
166 echo "</td><td align=\"right\">";
167 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
168 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
169 echo "<input type=\"submit\" value=\"$streditcats\" />";
171 echo '</td></tr></table>';
172 echo '<form method="post" action="edit.php" name="displayoptions">';
173 echo '<table><tr><td>';
174 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
175 echo '<input type="hidden" name="recurse" value="0" />';
176 echo '<input type="checkbox" name="recurse" value="1"';
178 echo ' checked="checked"';
180 echo ' onchange="document.displayoptions.submit(); return true;" />';
181 print_string('recurse', 'quiz');
184 echo '<input type="hidden" name="showhidden" value="0" />';
185 echo '<input type="checkbox" name="showhidden"';
187 echo ' checked="checked"';
189 echo ' onchange="document.displayoptions.submit(); return true;" />';
190 print_string('showhidden', 'quiz');
191 echo '</td><noscript><td valign="center">';
192 echo ' <input type="submit" value="'. get_string('go') .'" />';
193 echo '</td></noscript></tr></table></form>';
198 * Prints the table of questions in a category with interactions
200 * @param object $course The course object
201 * @param int $categoryid The id of the question category to be displayed
202 * @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
203 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
204 * @param int $page The number of the page to be displayed
205 * @param int $perpage Number of questions to show per page
206 * @param boolean $showhidden True if also hidden questions should be displayed
208 function question_list($course, $categoryid, $quizid=0,
209 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
210 global $QTYPE_MENU, $USER, $CFG;
212 $qtypemenu = $QTYPE_MENU;
213 if ($rqp_types = get_records('question_rqp_types')) {
214 foreach($rqp_types as $type) {
215 $qtypemenu['rqp_'.$type->id
] = $type->name
;
219 $strcategory = get_string("category", "quiz");
220 $strquestion = get_string("question", "quiz");
221 $straddquestions = get_string("addquestions", "quiz");
222 $strimportquestions = get_string("importquestions", "quiz");
223 $strexportquestions = get_string("exportquestions", "quiz");
224 $strnoquestions = get_string("noquestions", "quiz");
225 $strselect = get_string("select", "quiz");
226 $strselectall = get_string("selectall", "quiz");
227 $strselectnone = get_string("selectnone", "quiz");
228 $strcreatenewquestion = get_string("createnewquestion", "quiz");
229 $strquestionname = get_string("questionname", "quiz");
230 $strdelete = get_string("delete");
231 $stredit = get_string("edit");
232 $straction = get_string("action");
233 $strrestore = get_string('restore');
235 $straddtoquiz = get_string("addtoquiz", "quiz");
236 $strtype = get_string("type", "quiz");
237 $strcreatemultiple = get_string("createmultiple", "quiz");
238 $strpreview = get_string("preview","quiz");
241 echo "<p align=\"center\"><b>";
242 print_string("selectcategoryabove", "quiz");
246 print_string("addingquestions", "quiz");
252 if (!$category = get_record("question_categories", "id", "$categoryid")) {
253 notify("Category not found!");
257 $formatoptions = new stdClass
;
258 $formatoptions->noclean
= true;
259 echo format_text($category->info
, FORMAT_MOODLE
, $formatoptions, $course->id
);
263 // check if editing of this category is allowed
264 if (isteacheredit($category->course
)) {
265 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
266 echo '<td valign="top" align="right">';
267 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $qtypemenu, "addquestion",
268 "", "choose", "", "", false, "self");
269 echo '</td><td width="10" valign="top" align="right">';
270 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
275 print_string("publishedit","quiz");
279 echo '<tr><td colspan="3" align="right"><font size="2">';
280 if (isteacheredit($category->course
)) {
281 echo '<a href="'.$CFG->wwwroot
.'/question/import.php?category='.$category->id
.'">'.$strimportquestions.'</a>';
282 helpbutton("import", $strimportquestions, "quiz");
285 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&courseid={$course->id}\">$strexportquestions</a>";
286 helpbutton("export", $strexportquestions, "quiz");
287 echo '</font></td></tr>';
293 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
296 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
298 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
299 echo "<p align=\"center\">";
300 print_string("noquestions", "quiz");
305 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
306 // There are no questions on the requested page.
308 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
309 // There are no questions at all
310 echo "<p align=\"center\">";
311 print_string("noquestions", "quiz");
317 print_paging_bar($totalnumber, $page, $perpage,
318 "edit.php?courseid={$course->id}&perpage=$perpage&");
320 $canedit = isteacheredit($category->course
);
322 echo '<form method="post" action="edit.php?courseid='.$course->id
.'">';
323 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
324 print_simple_box_start('center', '100%', '#ffffff', 0);
325 echo '<table id="categoryquestions" cellspacing="0"><tr>';
326 $actionwidth = $canedit ?
95 : 70;
327 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
329 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
330 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
331 'id ASC' => get_string("sortage", "quiz"));
332 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
333 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
334 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
335 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
337 foreach ($questions as $question) {
338 echo "<tr>\n<td nowrap=\"nowrap\">\n";
340 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&quizid=$quizid&sesskey=$USER->sesskey\"><img
341 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a> ";
343 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview','scrollbars=yes,resizable=yes,width=700,height=480', false)\"><img
344 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a> ";
346 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
347 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a> ";
349 if($question->hidden
) {
350 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&unhide=$question->id&sesskey=$USER->sesskey\"><img
351 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
353 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&deleteselected=$question->id&q$question->id=1\"><img
354 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
357 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
360 if ($question->hidden
) {
361 echo '<td class="dimmed_text">'.$question->name
."</td>\n";
363 echo "<td>".$question->name
."</td>\n";
365 echo "<td align=\"center\">\n";
366 print_question_icon($question, $canedit);
370 echo '<tr><td align="center" colspan="3">';
371 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&perpage=$perpage&");
372 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE
) {
373 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE
) {
374 echo '<a href="edit.php?courseid='.$course->id
.'&perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
376 echo '<a href="edit.php?courseid='.$course->id
.'&perpage=' . DEFAULT_QUESTIONS_PER_PAGE
. '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE
).'</a>';
379 echo "</td></tr></table>\n";
380 print_simple_box_end();
382 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
383 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
384 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
385 '</td><td align="right"><b> '.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
387 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
391 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
392 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
393 question_category_select_menu($course->id
, false, true, $category->id
);
395 echo "</td></tr></table>";
398 for ($i = 1;$i <= min(10, $totalnumber); $i++
) {
399 $randomcount[$i] = $i;
401 for ($i = 20;$i <= min(100, $totalnumber); $i +
= 10) {
402 $randomcount[$i] = $i;
405 print_string('addrandom', 'quiz',
406 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
407 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
408 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
409 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
410 helpbutton('random', get_string('random', 'quiz'), 'quiz');