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'";
109 if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
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 style=\"width:100%;\"><tr><td style=\"width:20px; white-space:nowrap;\">";
163 echo "<strong>$strcategory:</strong> ";
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 "<fieldset class='invisiblefieldset'>";
169 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
170 echo "<input type=\"submit\" value=\"$streditcats\" />";
173 echo '</td></tr></table>';
175 echo '<form method="post" action="edit.php" id="displayoptions">';
176 echo "<fieldset class='invisiblefieldset'>";
177 echo '<table><tr><td>';
178 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
179 echo '<input type="hidden" name="recurse" value="0" />';
180 echo '<input type="checkbox" name="recurse" value="1"';
182 echo ' checked="checked"';
184 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
185 print_string('recurse', 'quiz');
188 echo '<input type="hidden" name="showhidden" value="0" />';
189 echo '<input type="checkbox" name="showhidden"';
191 echo ' checked="checked"';
193 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
194 print_string('showhidden', 'quiz');
195 echo '</td><td valign="middle"><noscript><div>';
196 echo ' <input type="submit" value="'. get_string('go') .'" />';
197 echo '</div></noscript></td></tr></table></fieldset></form>';
202 * Prints the table of questions in a category with interactions
204 * @param object $course The course object
205 * @param int $categoryid The id of the question category to be displayed
206 * @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
207 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
208 * @param int $page The number of the page to be displayed
209 * @param int $perpage Number of questions to show per page
210 * @param boolean $showhidden True if also hidden questions should be displayed
212 function question_list($course, $categoryid, $quizid=0,
213 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
214 global $QTYPE_MENU, $USER, $CFG, $THEME;
216 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
218 $qtypemenu = $QTYPE_MENU;
219 if ($rqp_types = get_records('question_rqp_types')) {
220 foreach($rqp_types as $type) {
221 $qtypemenu['rqp_'.$type->id
] = $type->name
;
225 $strcategory = get_string("category", "quiz");
226 $strquestion = get_string("question", "quiz");
227 $straddquestions = get_string("addquestions", "quiz");
228 $strimportquestions = get_string("importquestions", "quiz");
229 $strexportquestions = get_string("exportquestions", "quiz");
230 $strnoquestions = get_string("noquestions", "quiz");
231 $strselect = get_string("select", "quiz");
232 $strselectall = get_string("selectall", "quiz");
233 $strselectnone = get_string("selectnone", "quiz");
234 $strcreatenewquestion = get_string("createnewquestion", "quiz");
235 $strquestionname = get_string("questionname", "quiz");
236 $strdelete = get_string("delete");
237 $stredit = get_string("edit");
238 $straction = get_string("action");
239 $strrestore = get_string('restore');
241 $straddtoquiz = get_string("addtoquiz", "quiz");
242 $strtype = get_string("type", "quiz");
243 $strcreatemultiple = get_string("createmultiple", "quiz");
244 $strpreview = get_string("preview","quiz");
247 echo "<p style=\"text-align:center;\"><b>";
248 print_string("selectcategoryabove", "quiz");
252 print_string("addingquestions", "quiz");
258 if (!$category = get_record("question_categories", "id", "$categoryid")) {
259 notify("Category not found!");
262 echo '<div class="boxaligncenter">';
263 $formatoptions = new stdClass
;
264 $formatoptions->noclean
= true;
265 echo format_text($category->info
, FORMAT_MOODLE
, $formatoptions, $course->id
);
269 // check if editing of this category is allowed
270 if (has_capability('moodle/question:managecategory', $context)) {
271 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
272 echo '<td valign="top" align="right">';
273 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $qtypemenu, "addquestion",
274 "", "choose", "", "", false, "self");
275 echo '</td><td style="width:10px;" valign="top" align="right">';
276 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
281 print_string("publishedit","quiz");
285 echo '<tr><td colspan="3" align="right" style="font-size:0.8em;">';
287 if (has_capability('moodle/question:import', $context)) {
288 echo '<a href="'.$CFG->wwwroot
.'/question/import.php?category='.$category->id
.'">'.$strimportquestions.'</a>';
289 helpbutton("import", $strimportquestions, "quiz");
293 if (has_capability('moodle/question:export', $context)) {
294 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&courseid={$course->id}\">$strexportquestions</a>";
295 helpbutton("export", $strexportquestions, "quiz");
303 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
306 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
308 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
309 echo "<p style=\"text-align:center;\">";
310 print_string("noquestions", "quiz");
315 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
316 // There are no questions on the requested page.
318 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
319 // There are no questions at all
320 echo "<p style=\"text-align:center;\">";
321 print_string("noquestions", "quiz");
327 print_paging_bar($totalnumber, $page, $perpage,
328 "edit.php?courseid={$course->id}&perpage=$perpage&");
330 $canedit = has_capability('moodle/question:manage', $context);
332 echo '<form method="post" action="edit.php?courseid='.$course->id
.'">';
333 echo '<fieldset class="invisiblefieldset">';
334 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
335 print_simple_box_start('center', '100%', '#ffffff', 0);
336 echo '<table id="categoryquestions" cellspacing="0"><tr>';
337 $actionwidth = $canedit ?
95 : 70;
338 echo "<th style=\"width:$actionwidth; white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
340 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
341 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
342 'id ASC' => get_string("sortage", "quiz"));
343 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
344 $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
345 echo "<th align=\"left\" style=\"width:100%; white-space:nowrap;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
346 <th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strtype</th>";
348 foreach ($questions as $question) {
349 echo "<tr>\n<td style=\"white-space:nowrap;\">\n";
352 if ($quizid && has_capability('mod/quiz:manage', $context)) {
353 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&quizid=$quizid&sesskey=$USER->sesskey\"><img
354 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a> ";
358 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onclick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview', " .
359 QUESTION_PREVIEW_POPUP_OPTIONS
. ", false)\"><img
360 src=\"$CFG->pixpath/t/preview.gif\" alt=\"$strpreview\" /></a> ";
362 // edit, hide, delete question, using question capabilities, not quiz capabilieies
363 if (has_capability('moodle/question:manage', $context)) {
364 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
365 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a> ";
367 if($question->hidden
) {
368 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&unhide=$question->id&sesskey=$USER->sesskey\"><img
369 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
371 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&deleteselected=$question->id&q$question->id=1\"><img
372 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
375 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
378 if ($question->hidden
) {
379 echo '<td class="dimmed_text">'.$question->name
."</td>\n";
381 echo "<td>".$question->name
."</td>\n";
383 echo "<td align=\"center\">\n";
384 print_question_icon($question, $canedit);
388 echo '<tr><td align="center" colspan="3">';
389 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&perpage=$perpage&");
390 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE
) {
391 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE
) {
392 echo '<a href="edit.php?courseid='.$course->id
.'&perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
394 echo '<a href="edit.php?courseid='.$course->id
.'&perpage=' . DEFAULT_QUESTIONS_PER_PAGE
. '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE
).'</a>';
397 echo "</td></tr></table>\n";
398 print_simple_box_end();
400 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
401 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
402 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
403 '</td><td align="right"><b> '.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
405 if ($quizid && has_capability('mod/quiz:manage', $context)) {
406 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
409 // print delete and move selected question
410 if (has_capability('moodle/question:manage', $context)) {
411 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
412 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
413 question_category_select_menu($course->id
, false, true, $category->id
);
415 echo "</td></tr></table>";
417 // add random question
418 if ($quizid && has_capability('mod/quiz:manage', $context)) {
419 for ($i=1;$i<=10; $i++
) {
420 $randomcount[$i] = $i;
423 print_string('addrandom', 'quiz',
424 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
425 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
426 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
427 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
428 helpbutton('random', get_string('random', 'quiz'), 'quiz');