Removed $forum->open, $forum->assesspublic from code. Fixed bug in upgrade
[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
109 if (!isadmin()) {
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
162 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
163 echo "<b>$strcategory:</b>&nbsp;";
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\">";
516cf3eb 168 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
169 echo "<input type=\"submit\" value=\"$streditcats\" />";
170 echo "</form>";
171 echo '</td></tr></table>';
172 echo '<form method="post" action="edit.php" name="displayoptions">';
173 echo '<table><tr><td>';
fd14913e 174 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
516cf3eb 175 echo '<input type="hidden" name="recurse" value="0" />';
176 echo '<input type="checkbox" name="recurse" value="1"';
177 if ($recurse) {
178 echo ' checked="checked"';
179 }
180 echo ' onchange="document.displayoptions.submit(); return true;" />';
181 print_string('recurse', 'quiz');
182 // hide-feature
183 echo '<br />';
184 echo '<input type="hidden" name="showhidden" value="0" />';
185 echo '<input type="checkbox" name="showhidden"';
186 if ($showhidden) {
187 echo ' checked="checked"';
188 }
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>';
194}
195
196
197/**
198* Prints the table of questions in a category with interactions
199*
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
207*/
e49a8d09 208function question_list($course, $categoryid, $quizid=0,
209 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
dc1f00de 210 global $QTYPE_MENU, $USER, $CFG;
516cf3eb 211
50530eb7 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;
216 }
217 }
218
516cf3eb 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');
234
235 $straddtoquiz = get_string("addtoquiz", "quiz");
236 $strtype = get_string("type", "quiz");
237 $strcreatemultiple = get_string("createmultiple", "quiz");
238 $strpreview = get_string("preview","quiz");
239
240 if (!$categoryid) {
241 echo "<p align=\"center\"><b>";
242 print_string("selectcategoryabove", "quiz");
243 echo "</b></p>";
244 if ($quizid) {
245 echo "<p>";
246 print_string("addingquestions", "quiz");
247 echo "</p>";
248 }
249 return;
250 }
251
dc1f00de 252 if (!$category = get_record("question_categories", "id", "$categoryid")) {
516cf3eb 253 notify("Category not found!");
254 return;
255 }
256 echo "<center>";
4abc23b2 257 $formatoptions = new stdClass;
7347c60b 258 $formatoptions->noclean = true;
259 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
516cf3eb 260
261 echo '<table><tr>';
262
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">';
50530eb7 267 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
516cf3eb 268 "", "choose", "", "", false, "self");
269 echo '</td><td width="10" valign="top" align="right">';
270 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
271 echo '</td></tr>';
272 }
273 else {
274 echo '<tr><td>';
275 print_string("publishedit","quiz");
276 echo '</td></tr>';
277 }
278
279 echo '<tr><td colspan="3" align="right"><font size="2">';
280 if (isteacheredit($category->course)) {
5ab6af12 281 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 282 helpbutton("import", $strimportquestions, "quiz");
283 echo ' | ';
284 }
5ab6af12 285 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
516cf3eb 286 helpbutton("export", $strexportquestions, "quiz");
287 echo '</font></td></tr>';
288
289 echo '</table>';
290
291 echo '</center>';
292
dc1f00de 293 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 294
295 // hide-feature
296 $showhidden = $showhidden ? '' : " AND hidden = '0'";
297
4f48fb42 298 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
516cf3eb 299 echo "<p align=\"center\">";
300 print_string("noquestions", "quiz");
301 echo "</p>";
302 return;
303 }
304
4f48fb42 305 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 306 // There are no questions on the requested page.
307 $page = 0;
4f48fb42 308 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 309 // There are no questions at all
310 echo "<p align=\"center\">";
311 print_string("noquestions", "quiz");
312 echo "</p>";
313 return;
314 }
315 }
316
317 print_paging_bar($totalnumber, $page, $perpage,
a0e512fe 318 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 319
320 $canedit = isteacheredit($category->course);
321
5bbca3a4 322 echo '<form method="post" action="edit.php?courseid='.$course->id.'">';
516cf3eb 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>";
328
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>";
336 echo "</tr>\n";
337 foreach ($questions as $question) {
516cf3eb 338 echo "<tr>\n<td nowrap=\"nowrap\">\n";
339 if ($quizid) {
b2ec1095 340 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;quizid=$quizid&amp;sesskey=$USER->sesskey\"><img
516cf3eb 341 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
342 }
1b8a7434 343 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview', " .
344 QUESTION_PREVIEW_POPUP_OPTIONS . ", false)\"><img
345 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
516cf3eb 346 if ($canedit) {
e586cfb4 347 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
1b8a7434 348 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
516cf3eb 349 // hide-feature
350 if($question->hidden) {
54b3626b 351 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
1b8a7434 352 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
516cf3eb 353 } else {
54b3626b 354 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
1b8a7434 355 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
516cf3eb 356 }
357 }
358 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
359 echo "</td>\n";
360
361 if ($question->hidden) {
362 echo '<td class="dimmed_text">'.$question->name."</td>\n";
363 } else {
364 echo "<td>".$question->name."</td>\n";
365 }
366 echo "<td align=\"center\">\n";
4f48fb42 367 print_question_icon($question, $canedit);
516cf3eb 368 echo "</td>\n";
369 echo "</tr>\n";
370 }
3abfa3a0 371 echo '<tr><td align="center" colspan="3">';
a0e512fe 372 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
f92cf442 373 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
374 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
375 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
376 } else {
377 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=' . DEFAULT_QUESTIONS_PER_PAGE . '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
378 }
3abfa3a0 379 }
516cf3eb 380 echo "</td></tr></table>\n";
381 print_simple_box_end();
382
383 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
384 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
385 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
386 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
387 if ($quizid) {
388 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
389 echo '</td><td>';
390 }
391 if ($canedit) {
392 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
393 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 394 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 395 }
396 echo "</td></tr></table>";
397
398 if ($quizid) {
399 for ($i=1;$i<=10; $i++) {
400 $randomcount[$i] = $i;
401 }
402 echo '<br />';
403 print_string('addrandom', 'quiz',
404 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
405 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
406 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
407 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
408 helpbutton('random', get_string('random', 'quiz'), 'quiz');
409 }
410
411 echo "</form>\n";
412}
413
414?>