merged, apply nick's patch for MDL-8534, typo in restorelib.php
[moodle.git] / question / editlib.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
e586cfb4 3* Functions used by showbank.php to show question editing interface
4*
5* TODO: currently the function question_list still provides controls specific
6* to the quiz module. This needs to be generalised.
516cf3eb 7*
8* @version $Id$
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
e586cfb4 13* @package question
516cf3eb 14*/
15
4fbfd971 16require_once($CFG->libdir.'/questionlib.php');
516cf3eb 17
f92cf442 18define('DEFAULT_QUESTIONS_PER_PAGE', 20);
19
4fbfd971 20/**
21* Function to read all questions for category into big array
22*
23* @param int $category category number
36e2232e 24* @param bool $noparent if true only questions with NO parent will be selected
25* @param bool $recurse include subdirectories
4fbfd971 26* @author added by Howard Miller June 2004
27*/
36e2232e 28function get_questions_category( $category, $noparent=false, $recurse=true ) {
4fbfd971 29
30 global $QTYPES;
31
32 // questions will be added to an array
33 $qresults = array();
34
35 // build sql bit for $noparent
36 $npsql = '';
37 if ($noparent) {
38 $npsql = " and parent='0' ";
39 }
40
36e2232e 41 // get (list) of categories
42 if ($recurse) {
43 $categorylist = question_categorylist( $category->id );
44 }
45 else {
46 $categorylist = $category->id;
47 }
48
4fbfd971 49 // get the list of questions for the category
36e2232e 50 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
4fbfd971 51
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;
57 }
58 }
59
60 return $qresults;
61}
62
63/**
64* Gets the default category in a course
65*
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
70*/
71function get_default_question_category($courseid) {
72
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)
76 }
77 }
78
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();
88
89 if (!$category->id = insert_record("question_categories", $category)) {
90 notify("Error creating a default category!");
91 return false;
92 }
93 return $category;
94}
95
96/**
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
101 */
102function question_category_menu($courseid, $published=false) {
103/// Returns the list of categories
104 $publish = "";
105 if ($published) {
106 $publish = "OR publish = '1'";
107 }
108
2d14e8a8 109 if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
4fbfd971 110 $categories = get_records_select("question_categories", "course = '$courseid' $publish", 'parent, sortorder, name ASC');
111 } else {
112 $categories = get_records_select("question_categories", '', 'parent, sortorder, name ASC');
113 }
114 if (!$categories) {
115 return false;
116 }
117 $categories = add_indented_names($categories);
516cf3eb 118
4fbfd971 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)";
123 }
124 $catmenu[$category->id] = $category->indentedname;
125 }
126 }
127 return $catmenu;
128}
4fbfd971 129
130/**
131 * prints a form to choose categories
132 */
e586cfb4 133function question_category_form($course, $current, $recurse=1, $showhidden=false) {
134 global $CFG;
516cf3eb 135
136/// Make sure the default category exists for this course
dc1f00de 137 if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
4f48fb42 138 if (!$category = get_default_question_category($course->id)) {
516cf3eb 139 notify("Error creating a default category!");
140 }
141 }
142
143/// Get all the existing categories now
dc1f00de 144 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
516cf3eb 145 notify("Could not find any question categories!");
146 return false; // Something is really wrong
147 }
dcd51df2 148
149 $categories = add_indented_names( $categories );
516cf3eb 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)";
154 }
155 $catmenu[$category->id] = $category->indentedname;
156 }
157 }
158 $strcategory = get_string("category", "quiz");
159 $strshow = get_string("show", "quiz");
160 $streditcats = get_string("editcategories", "quiz");
161
0fd5feef 162 echo "<table style=\"width:100%;\"><tr><td style=\"width:20px; white-space:nowrap;\">";
163 echo "<strong>$strcategory:</strong>&nbsp;";
516cf3eb 164 echo "</td><td>";
e92d8ccf 165 popup_form ("edit.php?courseid=$course->id&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
516cf3eb 166 echo "</td><td align=\"right\">";
e586cfb4 167 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
0fd5feef 168 echo "<fieldset class='invisiblefieldset'>";
516cf3eb 169 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
170 echo "<input type=\"submit\" value=\"$streditcats\" />";
0fd5feef 171 echo '</fieldset>';
516cf3eb 172 echo "</form>";
173 echo '</td></tr></table>';
0fd5feef 174
d2ce367f 175 echo '<form method="post" action="edit.php" id="displayoptions">';
0fd5feef 176 echo "<fieldset class='invisiblefieldset'>";
516cf3eb 177 echo '<table><tr><td>';
fd14913e 178 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
516cf3eb 179 echo '<input type="hidden" name="recurse" value="0" />';
180 echo '<input type="checkbox" name="recurse" value="1"';
181 if ($recurse) {
182 echo ' checked="checked"';
183 }
d2ce367f 184 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
516cf3eb 185 print_string('recurse', 'quiz');
186 // hide-feature
187 echo '<br />';
188 echo '<input type="hidden" name="showhidden" value="0" />';
189 echo '<input type="checkbox" name="showhidden"';
190 if ($showhidden) {
191 echo ' checked="checked"';
192 }
d2ce367f 193 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
516cf3eb 194 print_string('showhidden', 'quiz');
09275894 195 echo '</td><td valign="middle"><noscript><div>';
516cf3eb 196 echo ' <input type="submit" value="'. get_string('go') .'" />';
09275894 197 echo '</div></noscript></td></tr></table></fieldset></form>';
516cf3eb 198}
199
200
201/**
202* Prints the table of questions in a category with interactions
203*
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
211*/
e49a8d09 212function question_list($course, $categoryid, $quizid=0,
213 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
95947ac9 214 global $QTYPE_MENU, $USER, $CFG, $THEME;
3765fd53 215
216 $context = get_context_instance(CONTEXT_COURSE, $course->id);
516cf3eb 217
50530eb7 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;
222 }
223 }
224
516cf3eb 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');
240
241 $straddtoquiz = get_string("addtoquiz", "quiz");
242 $strtype = get_string("type", "quiz");
243 $strcreatemultiple = get_string("createmultiple", "quiz");
244 $strpreview = get_string("preview","quiz");
245
246 if (!$categoryid) {
0fd5feef 247 echo "<p style=\"text-align:center;\"><b>";
516cf3eb 248 print_string("selectcategoryabove", "quiz");
249 echo "</b></p>";
250 if ($quizid) {
251 echo "<p>";
252 print_string("addingquestions", "quiz");
253 echo "</p>";
254 }
255 return;
256 }
257
dc1f00de 258 if (!$category = get_record("question_categories", "id", "$categoryid")) {
516cf3eb 259 notify("Category not found!");
260 return;
261 }
09275894 262 echo '<div class="boxaligncenter">';
4abc23b2 263 $formatoptions = new stdClass;
7347c60b 264 $formatoptions->noclean = true;
265 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
516cf3eb 266
267 echo '<table><tr>';
268
269 // check if editing of this category is allowed
f3fb11f3 270 if (has_capability('moodle/question:managecategory', $context)) {
516cf3eb 271 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
272 echo '<td valign="top" align="right">';
50530eb7 273 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
516cf3eb 274 "", "choose", "", "", false, "self");
0fd5feef 275 echo '</td><td style="width:10px;" valign="top" align="right">';
516cf3eb 276 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
277 echo '</td></tr>';
278 }
279 else {
280 echo '<tr><td>';
281 print_string("publishedit","quiz");
282 echo '</td></tr>';
283 }
284
0fd5feef 285 echo '<tr><td colspan="3" align="right" style="font-size:0.8em;">';
3765fd53 286
287 if (has_capability('moodle/question:import', $context)) {
5ab6af12 288 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 289 helpbutton("import", $strimportquestions, "quiz");
290 echo ' | ';
291 }
3765fd53 292
8cd402f9 293 if (has_capability('moodle/question:export', $context)) {
3765fd53 294 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
295 helpbutton("export", $strexportquestions, "quiz");
296 }
0fd5feef 297 echo '</td></tr>';
516cf3eb 298
299 echo '</table>';
300
09275894 301 echo '</div>';
516cf3eb 302
dc1f00de 303 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 304
305 // hide-feature
306 $showhidden = $showhidden ? '' : " AND hidden = '0'";
307
4f48fb42 308 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
0fd5feef 309 echo "<p style=\"text-align:center;\">";
516cf3eb 310 print_string("noquestions", "quiz");
311 echo "</p>";
312 return;
313 }
314
4f48fb42 315 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 316 // There are no questions on the requested page.
317 $page = 0;
4f48fb42 318 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 319 // There are no questions at all
0fd5feef 320 echo "<p style=\"text-align:center;\">";
516cf3eb 321 print_string("noquestions", "quiz");
322 echo "</p>";
323 return;
324 }
325 }
326
327 print_paging_bar($totalnumber, $page, $perpage,
a0e512fe 328 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 329
17d6a25e 330 $canedit = has_capability('moodle/question:manage', $context);
516cf3eb 331
5bbca3a4 332 echo '<form method="post" action="edit.php?courseid='.$course->id.'">';
09275894 333 echo '<fieldset class="invisiblefieldset">';
516cf3eb 334 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
901011fd 335 print_simple_box_start();
516cf3eb 336 echo '<table id="categoryquestions" cellspacing="0"><tr>';
337 $actionwidth = $canedit ? 95 : 70;
09275894 338 echo "<th style=\"width:$actionwidth; white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
516cf3eb 339
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);
09275894 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>
0fd5feef 346 <th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strtype</th>";
516cf3eb 347 echo "</tr>\n";
348 foreach ($questions as $question) {
0fd5feef 349 echo "<tr>\n<td style=\"white-space:nowrap;\">\n";
3765fd53 350
351 // add to quiz
352 if ($quizid && has_capability('mod/quiz:manage', $context)) {
b2ec1095 353 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;quizid=$quizid&amp;sesskey=$USER->sesskey\"><img
0fd5feef 354 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
516cf3eb 355 }
3765fd53 356
357 // preview
09275894 358 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onclick=\"openpopup('/question/preview.php?id=$question->id&amp;quizid=$quizid','$strpreview', " .
1b8a7434 359 QUESTION_PREVIEW_POPUP_OPTIONS . ", false)\"><img
0fd5feef 360 src=\"$CFG->pixpath/t/preview.gif\" alt=\"$strpreview\" /></a>&nbsp;";
3765fd53 361
362 // edit, hide, delete question, using question capabilities, not quiz capabilieies
363 if (has_capability('moodle/question:manage', $context)) {
e586cfb4 364 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
0fd5feef 365 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
516cf3eb 366 // hide-feature
367 if($question->hidden) {
54b3626b 368 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 369 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
516cf3eb 370 } else {
54b3626b 371 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
0fd5feef 372 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
516cf3eb 373 }
374 }
375 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
376 echo "</td>\n";
377
378 if ($question->hidden) {
379 echo '<td class="dimmed_text">'.$question->name."</td>\n";
380 } else {
381 echo "<td>".$question->name."</td>\n";
382 }
383 echo "<td align=\"center\">\n";
4f48fb42 384 print_question_icon($question, $canedit);
516cf3eb 385 echo "</td>\n";
386 echo "</tr>\n";
387 }
3abfa3a0 388 echo '<tr><td align="center" colspan="3">';
a0e512fe 389 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
f92cf442 390 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
391 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
392 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
393 } else {
394 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=' . DEFAULT_QUESTIONS_PER_PAGE . '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
395 }
3abfa3a0 396 }
516cf3eb 397 echo "</td></tr></table>\n";
398 print_simple_box_end();
399
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>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
95947ac9 404
3765fd53 405 if ($quizid && has_capability('mod/quiz:manage', $context)) {
95947ac9 406 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
516cf3eb 407 echo '</td><td>';
408 }
3765fd53 409 // print delete and move selected question
410 if (has_capability('moodle/question:manage', $context)) {
516cf3eb 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";
dc1f00de 413 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 414 }
415 echo "</td></tr></table>";
416
3765fd53 417 // add random question
418 if ($quizid && has_capability('mod/quiz:manage', $context)) {
b28e2577 419 for ($i = 1;$i <= min(10, $totalnumber); $i++) {
420 $randomcount[$i] = $i;
421 }
422 for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
516cf3eb 423 $randomcount[$i] = $i;
424 }
425 echo '<br />';
426 print_string('addrandom', 'quiz',
427 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
428 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
429 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
430 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
431 helpbutton('random', get_string('random', 'quiz'), 'quiz');
432 }
09275894 433 echo '</fieldset>';
434 echo "</form>\n";
516cf3eb 435}
436
09275894 437?>