More readable coding. Now, I understand everything. :)
[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
4fbfd971 18/**
19* Function to read all questions for category into big array
20*
21* @param int $category category number
22* @param bool @noparent if true only questions with NO parent will be selected
23* @author added by Howard Miller June 2004
24*/
25function get_questions_category( $category, $noparent=false ) {
26
27 global $QTYPES;
28
29 // questions will be added to an array
30 $qresults = array();
31
32 // build sql bit for $noparent
33 $npsql = '';
34 if ($noparent) {
35 $npsql = " and parent='0' ";
36 }
37
38 // get the list of questions for the category
39 if ($questions = get_records_select("question","category={$category->id} $npsql", "qtype, name ASC")) {
40
41 // iterate through questions, getting stuff we need
42 foreach($questions as $question) {
43 $questiontype = $QTYPES[$question->qtype];
44 $questiontype->get_question_options( $question );
45 $qresults[] = $question;
46 }
47 }
48
49 return $qresults;
50}
51
52/**
53* Gets the default category in a course
54*
55* It returns the first category with no parent category. If no categories
56* exist yet then one is created.
57* @return object The default category
58* @param integer $courseid The id of the course whose default category is wanted
59*/
60function get_default_question_category($courseid) {
61
62 if ($categories = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", "id")) {
63 foreach ($categories as $category) {
64 return $category; // Return the first one (lowest id)
65 }
66 }
67
68 // Otherwise, we need to make one
69 $category->name = get_string("default", "quiz");
70 $category->info = get_string("defaultinfo", "quiz");
71 $category->course = $courseid;
72 $category->parent = 0;
73 // TODO: Figure out why we use 999 below
74 $category->sortorder = 999;
75 $category->publish = 0;
76 $category->stamp = make_unique_id_code();
77
78 if (!$category->id = insert_record("question_categories", $category)) {
79 notify("Error creating a default category!");
80 return false;
81 }
82 return $category;
83}
84
85/**
86 * Return a list of categories nicely formatted
87 * @param int courseid id of course
88 * @param bool published true=include all published categories
89 * @return array formatted category names
90 */
91function question_category_menu($courseid, $published=false) {
92/// Returns the list of categories
93 $publish = "";
94 if ($published) {
95 $publish = "OR publish = '1'";
96 }
97
98 if (!isadmin()) {
99 $categories = get_records_select("question_categories", "course = '$courseid' $publish", 'parent, sortorder, name ASC');
100 } else {
101 $categories = get_records_select("question_categories", '', 'parent, sortorder, name ASC');
102 }
103 if (!$categories) {
104 return false;
105 }
106 $categories = add_indented_names($categories);
516cf3eb 107
4fbfd971 108 foreach ($categories as $category) {
109 if ($catcourse = get_record("course", "id", $category->course)) {
110 if ($category->publish && ($category->course != $courseid)) {
111 $category->indentedname .= " ($catcourse->shortname)";
112 }
113 $catmenu[$category->id] = $category->indentedname;
114 }
115 }
116 return $catmenu;
117}
4fbfd971 118
119/**
120 * prints a form to choose categories
121 */
e586cfb4 122function question_category_form($course, $current, $recurse=1, $showhidden=false) {
123 global $CFG;
516cf3eb 124
125/// Make sure the default category exists for this course
dc1f00de 126 if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
4f48fb42 127 if (!$category = get_default_question_category($course->id)) {
516cf3eb 128 notify("Error creating a default category!");
129 }
130 }
131
132/// Get all the existing categories now
dc1f00de 133 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
516cf3eb 134 notify("Could not find any question categories!");
135 return false; // Something is really wrong
136 }
dcd51df2 137
138 $categories = add_indented_names( $categories );
516cf3eb 139 foreach ($categories as $key => $category) {
140 if ($catcourse = get_record("course", "id", $category->course)) {
141 if ($category->publish && $category->course != $course->id) {
142 $category->indentedname .= " ($catcourse->shortname)";
143 }
144 $catmenu[$category->id] = $category->indentedname;
145 }
146 }
147 $strcategory = get_string("category", "quiz");
148 $strshow = get_string("show", "quiz");
149 $streditcats = get_string("editcategories", "quiz");
150
151 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
152 echo "<b>$strcategory:</b>&nbsp;";
153 echo "</td><td>";
e92d8ccf 154 popup_form ("edit.php?courseid=$course->id&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
516cf3eb 155 echo "</td><td align=\"right\">";
e586cfb4 156 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
516cf3eb 157 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
158 echo "<input type=\"submit\" value=\"$streditcats\" />";
159 echo "</form>";
160 echo '</td></tr></table>';
161 echo '<form method="post" action="edit.php" name="displayoptions">';
162 echo '<table><tr><td>';
fd14913e 163 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
516cf3eb 164 echo '<input type="hidden" name="recurse" value="0" />';
165 echo '<input type="checkbox" name="recurse" value="1"';
166 if ($recurse) {
167 echo ' checked="checked"';
168 }
169 echo ' onchange="document.displayoptions.submit(); return true;" />';
170 print_string('recurse', 'quiz');
171 // hide-feature
172 echo '<br />';
173 echo '<input type="hidden" name="showhidden" value="0" />';
174 echo '<input type="checkbox" name="showhidden"';
175 if ($showhidden) {
176 echo ' checked="checked"';
177 }
178 echo ' onchange="document.displayoptions.submit(); return true;" />';
179 print_string('showhidden', 'quiz');
180 echo '</td><noscript><td valign="center">';
181 echo ' <input type="submit" value="'. get_string('go') .'" />';
182 echo '</td></noscript></tr></table></form>';
183}
184
185
186/**
187* Prints the table of questions in a category with interactions
188*
189* @param object $course The course object
190* @param int $categoryid The id of the question category to be displayed
191* @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
192* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
193* @param int $page The number of the page to be displayed
194* @param int $perpage Number of questions to show per page
195* @param boolean $showhidden True if also hidden questions should be displayed
196*/
e49a8d09 197function question_list($course, $categoryid, $quizid=0,
198 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
dc1f00de 199 global $QTYPE_MENU, $USER, $CFG;
516cf3eb 200
50530eb7 201 $qtypemenu = $QTYPE_MENU;
202 if ($rqp_types = get_records('question_rqp_types')) {
203 foreach($rqp_types as $type) {
204 $qtypemenu['rqp_'.$type->id] = $type->name;
205 }
206 }
207
516cf3eb 208 $strcategory = get_string("category", "quiz");
209 $strquestion = get_string("question", "quiz");
210 $straddquestions = get_string("addquestions", "quiz");
211 $strimportquestions = get_string("importquestions", "quiz");
212 $strexportquestions = get_string("exportquestions", "quiz");
213 $strnoquestions = get_string("noquestions", "quiz");
214 $strselect = get_string("select", "quiz");
215 $strselectall = get_string("selectall", "quiz");
216 $strselectnone = get_string("selectnone", "quiz");
217 $strcreatenewquestion = get_string("createnewquestion", "quiz");
218 $strquestionname = get_string("questionname", "quiz");
219 $strdelete = get_string("delete");
220 $stredit = get_string("edit");
221 $straction = get_string("action");
222 $strrestore = get_string('restore');
223
224 $straddtoquiz = get_string("addtoquiz", "quiz");
225 $strtype = get_string("type", "quiz");
226 $strcreatemultiple = get_string("createmultiple", "quiz");
227 $strpreview = get_string("preview","quiz");
228
229 if (!$categoryid) {
230 echo "<p align=\"center\"><b>";
231 print_string("selectcategoryabove", "quiz");
232 echo "</b></p>";
233 if ($quizid) {
234 echo "<p>";
235 print_string("addingquestions", "quiz");
236 echo "</p>";
237 }
238 return;
239 }
240
dc1f00de 241 if (!$category = get_record("question_categories", "id", "$categoryid")) {
516cf3eb 242 notify("Category not found!");
243 return;
244 }
245 echo "<center>";
7347c60b 246 $formatoptions->noclean = true;
247 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
516cf3eb 248
249 echo '<table><tr>';
250
251 // check if editing of this category is allowed
252 if (isteacheredit($category->course)) {
253 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
254 echo '<td valign="top" align="right">';
50530eb7 255 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
516cf3eb 256 "", "choose", "", "", false, "self");
257 echo '</td><td width="10" valign="top" align="right">';
258 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
259 echo '</td></tr>';
260 }
261 else {
262 echo '<tr><td>';
263 print_string("publishedit","quiz");
264 echo '</td></tr>';
265 }
266
267 echo '<tr><td colspan="3" align="right"><font size="2">';
268 if (isteacheredit($category->course)) {
5ab6af12 269 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 270 helpbutton("import", $strimportquestions, "quiz");
271 echo ' | ';
272 }
5ab6af12 273 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
516cf3eb 274 helpbutton("export", $strexportquestions, "quiz");
275 echo '</font></td></tr>';
276
277 echo '</table>';
278
279 echo '</center>';
280
dc1f00de 281 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 282
283 // hide-feature
284 $showhidden = $showhidden ? '' : " AND hidden = '0'";
285
4f48fb42 286 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
516cf3eb 287 echo "<p align=\"center\">";
288 print_string("noquestions", "quiz");
289 echo "</p>";
290 return;
291 }
292
4f48fb42 293 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 294 // There are no questions on the requested page.
295 $page = 0;
4f48fb42 296 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 297 // There are no questions at all
298 echo "<p align=\"center\">";
299 print_string("noquestions", "quiz");
300 echo "</p>";
301 return;
302 }
303 }
304
305 print_paging_bar($totalnumber, $page, $perpage,
a0e512fe 306 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 307
308 $canedit = isteacheredit($category->course);
309
310 echo '<form method="post" action="edit.php">';
311 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
dcd51df2 312 echo "<input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
516cf3eb 313 print_simple_box_start('center', '100%', '#ffffff', 0);
314 echo '<table id="categoryquestions" cellspacing="0"><tr>';
315 $actionwidth = $canedit ? 95 : 70;
316 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
317
318 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
319 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
320 'id ASC' => get_string("sortage", "quiz"));
321 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
322 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
323 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
324 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
325 echo "</tr>\n";
326 foreach ($questions as $question) {
516cf3eb 327 echo "<tr>\n<td nowrap=\"nowrap\">\n";
328 if ($quizid) {
329 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
330 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
331 }
e586cfb4 332 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
516cf3eb 333 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
334 if ($canedit) {
e586cfb4 335 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
516cf3eb 336 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
337 // hide-feature
338 if($question->hidden) {
54b3626b 339 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
516cf3eb 340 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
341 } else {
54b3626b 342 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
516cf3eb 343 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
344 }
345 }
346 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
347 echo "</td>\n";
348
349 if ($question->hidden) {
350 echo '<td class="dimmed_text">'.$question->name."</td>\n";
351 } else {
352 echo "<td>".$question->name."</td>\n";
353 }
354 echo "<td align=\"center\">\n";
4f48fb42 355 print_question_icon($question, $canedit);
516cf3eb 356 echo "</td>\n";
357 echo "</tr>\n";
358 }
3abfa3a0 359 echo '<tr><td align="center" colspan="3">';
a0e512fe 360 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
3abfa3a0 361 if ($totalnumber > $perpage) {
362 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
363 }
516cf3eb 364 echo "</td></tr></table>\n";
365 print_simple_box_end();
366
367 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
368 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
369 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
370 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
371 if ($quizid) {
372 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
373 echo '</td><td>';
374 }
375 if ($canedit) {
376 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
377 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 378 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 379 }
380 echo "</td></tr></table>";
381
382 if ($quizid) {
383 for ($i=1;$i<=10; $i++) {
384 $randomcount[$i] = $i;
385 }
386 echo '<br />';
387 print_string('addrandom', 'quiz',
388 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
389 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
390 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
391 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
392 helpbutton('random', get_string('random', 'quiz'), 'quiz');
393 }
394
395 echo "</form>\n";
396}
397
398?>