Removed a notice
[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*/
e586cfb4 95function question_list($course, $categoryid, $quizid,
516cf3eb 96 $recurse=1, $page, $perpage, $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>";
137 echo format_text($category->info, FORMAT_MOODLE);
138
139 echo '<table><tr>';
140
141 // check if editing of this category is allowed
142 if (isteacheredit($category->course)) {
143 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
144 echo '<td valign="top" align="right">';
dc1f00de 145 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $QTYPE_MENU, "addquestion",
516cf3eb 146 "", "choose", "", "", false, "self");
147 echo '</td><td width="10" valign="top" align="right">';
148 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
149 echo '</td></tr>';
150 }
151 else {
152 echo '<tr><td>';
153 print_string("publishedit","quiz");
154 echo '</td></tr>';
155 }
156
157 echo '<tr><td colspan="3" align="right"><font size="2">';
158 if (isteacheredit($category->course)) {
5ab6af12 159 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 160 helpbutton("import", $strimportquestions, "quiz");
161 echo ' | ';
162 }
5ab6af12 163 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
516cf3eb 164 helpbutton("export", $strexportquestions, "quiz");
165 echo '</font></td></tr>';
166
167 echo '</table>';
168
169 echo '</center>';
170
dc1f00de 171 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 172
173 // hide-feature
174 $showhidden = $showhidden ? '' : " AND hidden = '0'";
175
4f48fb42 176 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
516cf3eb 177 echo "<p align=\"center\">";
178 print_string("noquestions", "quiz");
179 echo "</p>";
180 return;
181 }
182
4f48fb42 183 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 184 // There are no questions on the requested page.
185 $page = 0;
4f48fb42 186 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 187 // There are no questions at all
188 echo "<p align=\"center\">";
189 print_string("noquestions", "quiz");
190 echo "</p>";
191 return;
192 }
193 }
194
195 print_paging_bar($totalnumber, $page, $perpage,
a0e512fe 196 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 197
198 $canedit = isteacheredit($category->course);
199
200 echo '<form method="post" action="edit.php">';
201 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
dcd51df2 202 echo "<input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
516cf3eb 203 print_simple_box_start('center', '100%', '#ffffff', 0);
204 echo '<table id="categoryquestions" cellspacing="0"><tr>';
205 $actionwidth = $canedit ? 95 : 70;
206 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
207
208 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
209 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
210 'id ASC' => get_string("sortage", "quiz"));
211 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
212 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
213 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
214 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
215 echo "</tr>\n";
216 foreach ($questions as $question) {
217 if ($question->qtype == RANDOM) {
218 //continue;
219 }
220 echo "<tr>\n<td nowrap=\"nowrap\">\n";
221 if ($quizid) {
222 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
223 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
224 }
e586cfb4 225 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 226 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
227 if ($canedit) {
e586cfb4 228 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
516cf3eb 229 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
230 // hide-feature
231 if($question->hidden) {
e586cfb4 232 echo "<a title=\"$strrestore\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&amp;hide=0&amp;sesskey=$USER->sesskey\"><img
516cf3eb 233 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
234 } else {
e586cfb4 235 echo "<a title=\"$strdelete\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&amp;delete=$question->id\"><img
516cf3eb 236 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
237 }
238 }
239 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
240 echo "</td>\n";
241
242 if ($question->hidden) {
243 echo '<td class="dimmed_text">'.$question->name."</td>\n";
244 } else {
245 echo "<td>".$question->name."</td>\n";
246 }
247 echo "<td align=\"center\">\n";
4f48fb42 248 print_question_icon($question, $canedit);
516cf3eb 249 echo "</td>\n";
250 echo "</tr>\n";
251 }
252 echo '<tr><td colspan="3">';
a0e512fe 253 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
516cf3eb 254 echo "</td></tr></table>\n";
255 print_simple_box_end();
256
257 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
258 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
259 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
260 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
261 if ($quizid) {
262 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
263 echo '</td><td>';
264 }
265 if ($canedit) {
266 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
267 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 268 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 269 }
270 echo "</td></tr></table>";
271
272 if ($quizid) {
273 for ($i=1;$i<=10; $i++) {
274 $randomcount[$i] = $i;
275 }
276 echo '<br />';
277 print_string('addrandom', 'quiz',
278 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
279 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
280 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
281 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
282 helpbutton('random', get_string('random', 'quiz'), 'quiz');
283 }
284
285 echo "</form>\n";
286}
287
288?>