I incorrectly had renamed quiz_ to question_ in some places.
[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
e586cfb4 16 require_once($CFG->libdir.'/questionlib.php');
516cf3eb 17
516cf3eb 18
e586cfb4 19function question_category_form($course, $current, $recurse=1, $showhidden=false) {
20 global $CFG;
516cf3eb 21/// Prints a form to choose categories
22
23/// Make sure the default category exists for this course
dc1f00de 24 if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
4f48fb42 25 if (!$category = get_default_question_category($course->id)) {
516cf3eb 26 notify("Error creating a default category!");
27 }
28 }
29
30/// Get all the existing categories now
dc1f00de 31 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
516cf3eb 32 notify("Could not find any question categories!");
33 return false; // Something is really wrong
34 }
dcd51df2 35
36 $categories = add_indented_names( $categories );
516cf3eb 37 foreach ($categories as $key => $category) {
38 if ($catcourse = get_record("course", "id", $category->course)) {
39 if ($category->publish && $category->course != $course->id) {
40 $category->indentedname .= " ($catcourse->shortname)";
41 }
42 $catmenu[$category->id] = $category->indentedname;
43 }
44 }
45 $strcategory = get_string("category", "quiz");
46 $strshow = get_string("show", "quiz");
47 $streditcats = get_string("editcategories", "quiz");
48
49 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
50 echo "<b>$strcategory:</b>&nbsp;";
51 echo "</td><td>";
e92d8ccf 52 popup_form ("edit.php?courseid=$course->id&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
516cf3eb 53 echo "</td><td align=\"right\">";
e586cfb4 54 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
516cf3eb 55 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
56 echo "<input type=\"submit\" value=\"$streditcats\" />";
57 echo "</form>";
58 echo '</td></tr></table>';
59 echo '<form method="post" action="edit.php" name="displayoptions">';
60 echo '<table><tr><td>';
fd14913e 61 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
516cf3eb 62 echo '<input type="hidden" name="recurse" value="0" />';
63 echo '<input type="checkbox" name="recurse" value="1"';
64 if ($recurse) {
65 echo ' checked="checked"';
66 }
67 echo ' onchange="document.displayoptions.submit(); return true;" />';
68 print_string('recurse', 'quiz');
69 // hide-feature
70 echo '<br />';
71 echo '<input type="hidden" name="showhidden" value="0" />';
72 echo '<input type="checkbox" name="showhidden"';
73 if ($showhidden) {
74 echo ' checked="checked"';
75 }
76 echo ' onchange="document.displayoptions.submit(); return true;" />';
77 print_string('showhidden', 'quiz');
78 echo '</td><noscript><td valign="center">';
79 echo ' <input type="submit" value="'. get_string('go') .'" />';
80 echo '</td></noscript></tr></table></form>';
81}
82
83
84/**
85* Prints the table of questions in a category with interactions
86*
87* @param object $course The course object
88* @param int $categoryid The id of the question category to be displayed
89* @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
90* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
91* @param int $page The number of the page to be displayed
92* @param int $perpage Number of questions to show per page
93* @param boolean $showhidden True if also hidden questions should be displayed
94*/
e49a8d09 95function question_list($course, $categoryid, $quizid=0,
96 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
dc1f00de 97 global $QTYPE_MENU, $USER, $CFG;
516cf3eb 98
99 $strcategory = get_string("category", "quiz");
100 $strquestion = get_string("question", "quiz");
101 $straddquestions = get_string("addquestions", "quiz");
102 $strimportquestions = get_string("importquestions", "quiz");
103 $strexportquestions = get_string("exportquestions", "quiz");
104 $strnoquestions = get_string("noquestions", "quiz");
105 $strselect = get_string("select", "quiz");
106 $strselectall = get_string("selectall", "quiz");
107 $strselectnone = get_string("selectnone", "quiz");
108 $strcreatenewquestion = get_string("createnewquestion", "quiz");
109 $strquestionname = get_string("questionname", "quiz");
110 $strdelete = get_string("delete");
111 $stredit = get_string("edit");
112 $straction = get_string("action");
113 $strrestore = get_string('restore');
114
115 $straddtoquiz = get_string("addtoquiz", "quiz");
116 $strtype = get_string("type", "quiz");
117 $strcreatemultiple = get_string("createmultiple", "quiz");
118 $strpreview = get_string("preview","quiz");
119
120 if (!$categoryid) {
121 echo "<p align=\"center\"><b>";
122 print_string("selectcategoryabove", "quiz");
123 echo "</b></p>";
124 if ($quizid) {
125 echo "<p>";
126 print_string("addingquestions", "quiz");
127 echo "</p>";
128 }
129 return;
130 }
131
dc1f00de 132 if (!$category = get_record("question_categories", "id", "$categoryid")) {
516cf3eb 133 notify("Category not found!");
134 return;
135 }
136 echo "<center>";
7347c60b 137 $formatoptions->noclean = true;
138 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
516cf3eb 139
140 echo '<table><tr>';
141
142 // check if editing of this category is allowed
143 if (isteacheredit($category->course)) {
144 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
145 echo '<td valign="top" align="right">';
dc1f00de 146 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $QTYPE_MENU, "addquestion",
516cf3eb 147 "", "choose", "", "", false, "self");
148 echo '</td><td width="10" valign="top" align="right">';
149 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
150 echo '</td></tr>';
151 }
152 else {
153 echo '<tr><td>';
154 print_string("publishedit","quiz");
155 echo '</td></tr>';
156 }
157
158 echo '<tr><td colspan="3" align="right"><font size="2">';
159 if (isteacheredit($category->course)) {
5ab6af12 160 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 161 helpbutton("import", $strimportquestions, "quiz");
162 echo ' | ';
163 }
5ab6af12 164 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
516cf3eb 165 helpbutton("export", $strexportquestions, "quiz");
166 echo '</font></td></tr>';
167
168 echo '</table>';
169
170 echo '</center>';
171
dc1f00de 172 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 173
174 // hide-feature
175 $showhidden = $showhidden ? '' : " AND hidden = '0'";
176
4f48fb42 177 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
516cf3eb 178 echo "<p align=\"center\">";
179 print_string("noquestions", "quiz");
180 echo "</p>";
181 return;
182 }
183
4f48fb42 184 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 185 // There are no questions on the requested page.
186 $page = 0;
4f48fb42 187 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 188 // There are no questions at all
189 echo "<p align=\"center\">";
190 print_string("noquestions", "quiz");
191 echo "</p>";
192 return;
193 }
194 }
195
196 print_paging_bar($totalnumber, $page, $perpage,
a0e512fe 197 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 198
199 $canedit = isteacheredit($category->course);
200
201 echo '<form method="post" action="edit.php">';
202 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
dcd51df2 203 echo "<input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
516cf3eb 204 print_simple_box_start('center', '100%', '#ffffff', 0);
205 echo '<table id="categoryquestions" cellspacing="0"><tr>';
206 $actionwidth = $canedit ? 95 : 70;
207 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
208
209 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
210 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
211 'id ASC' => get_string("sortage", "quiz"));
212 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
213 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
214 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
215 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
216 echo "</tr>\n";
217 foreach ($questions as $question) {
516cf3eb 218 echo "<tr>\n<td nowrap=\"nowrap\">\n";
219 if ($quizid) {
220 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
221 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
222 }
e586cfb4 223 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 224 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
225 if ($canedit) {
e586cfb4 226 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
516cf3eb 227 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
228 // hide-feature
229 if($question->hidden) {
54b3626b 230 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
516cf3eb 231 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
232 } else {
54b3626b 233 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
516cf3eb 234 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
235 }
236 }
237 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
238 echo "</td>\n";
239
240 if ($question->hidden) {
241 echo '<td class="dimmed_text">'.$question->name."</td>\n";
242 } else {
243 echo "<td>".$question->name."</td>\n";
244 }
245 echo "<td align=\"center\">\n";
4f48fb42 246 print_question_icon($question, $canedit);
516cf3eb 247 echo "</td>\n";
248 echo "</tr>\n";
249 }
3abfa3a0 250 echo '<tr><td align="center" colspan="3">';
a0e512fe 251 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
3abfa3a0 252 if ($totalnumber > $perpage) {
253 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
254 }
516cf3eb 255 echo "</td></tr></table>\n";
256 print_simple_box_end();
257
258 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
259 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
260 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
261 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
262 if ($quizid) {
263 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
264 echo '</td><td>';
265 }
266 if ($canedit) {
267 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
268 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 269 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 270 }
271 echo "</td></tr></table>";
272
273 if ($quizid) {
274 for ($i=1;$i<=10; $i++) {
275 $randomcount[$i] = $i;
276 }
277 echo '<br />';
278 print_string('addrandom', 'quiz',
279 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
280 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
281 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
282 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
283 helpbutton('random', get_string('random', 'quiz'), 'quiz');
284 }
285
286 echo "</form>\n";
287}
288
289?>