Automatic installer.php lang files by installer_builder (20061110)
[moodle.git] / question / editlib.php
blob543028cbb9b6594eac761545b1ebfaeadf1e2bb4
1 <?php // $Id$
2 /**
3 * Functions used by showbank.php to show question editing interface
5 * TODO: currently the function question_list still provides controls specific
6 * to the quiz module. This needs to be generalised.
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
13 * @package question
16 require_once($CFG->libdir.'/questionlib.php');
18 define('DEFAULT_QUESTIONS_PER_PAGE', 20);
20 /**
21 * Function to read all questions for category into big array
23 * @param int $category category number
24 * @param bool $noparent if true only questions with NO parent will be selected
25 * @param bool $recurse include subdirectories
26 * @author added by Howard Miller June 2004
28 function get_questions_category( $category, $noparent=false, $recurse=true ) {
30 global $QTYPES;
32 // questions will be added to an array
33 $qresults = array();
35 // build sql bit for $noparent
36 $npsql = '';
37 if ($noparent) {
38 $npsql = " and parent='0' ";
41 // get (list) of categories
42 if ($recurse) {
43 $categorylist = question_categorylist( $category->id );
45 else {
46 $categorylist = $category->id;
49 // get the list of questions for the category
50 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
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;
60 return $qresults;
63 /**
64 * Gets the default category in a course
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
71 function get_default_question_category($courseid) {
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)
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();
89 if (!$category->id = insert_record("question_categories", $category)) {
90 notify("Error creating a default category!");
91 return false;
93 return $category;
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
102 function question_category_menu($courseid, $published=false) {
103 /// Returns the list of categories
104 $publish = "";
105 if ($published) {
106 $publish = "OR publish = '1'";
109 if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
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');
114 if (!$categories) {
115 return false;
117 $categories = add_indented_names($categories);
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)";
124 $catmenu[$category->id] = $category->indentedname;
127 return $catmenu;
131 * prints a form to choose categories
133 function question_category_form($course, $current, $recurse=1, $showhidden=false) {
134 global $CFG;
136 /// Make sure the default category exists for this course
137 if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
138 if (!$category = get_default_question_category($course->id)) {
139 notify("Error creating a default category!");
143 /// Get all the existing categories now
144 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
145 notify("Could not find any question categories!");
146 return false; // Something is really wrong
149 $categories = add_indented_names( $categories );
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)";
155 $catmenu[$category->id] = $category->indentedname;
158 $strcategory = get_string("category", "quiz");
159 $strshow = get_string("show", "quiz");
160 $streditcats = get_string("editcategories", "quiz");
162 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
163 echo "<b>$strcategory:</b>&nbsp;";
164 echo "</td><td>";
165 popup_form ("edit.php?courseid=$course->id&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
166 echo "</td><td align=\"right\">";
167 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
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>';
174 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
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"';
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"';
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>';
198 * Prints the table of questions in a category with interactions
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
208 function question_list($course, $categoryid, $quizid=0,
209 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
210 global $QTYPE_MENU, $USER, $CFG;
212 $context = get_context_instance(CONTEXT_COURSE, $course->id);
214 $qtypemenu = $QTYPE_MENU;
215 if ($rqp_types = get_records('question_rqp_types')) {
216 foreach($rqp_types as $type) {
217 $qtypemenu['rqp_'.$type->id] = $type->name;
221 $strcategory = get_string("category", "quiz");
222 $strquestion = get_string("question", "quiz");
223 $straddquestions = get_string("addquestions", "quiz");
224 $strimportquestions = get_string("importquestions", "quiz");
225 $strexportquestions = get_string("exportquestions", "quiz");
226 $strnoquestions = get_string("noquestions", "quiz");
227 $strselect = get_string("select", "quiz");
228 $strselectall = get_string("selectall", "quiz");
229 $strselectnone = get_string("selectnone", "quiz");
230 $strcreatenewquestion = get_string("createnewquestion", "quiz");
231 $strquestionname = get_string("questionname", "quiz");
232 $strdelete = get_string("delete");
233 $stredit = get_string("edit");
234 $straction = get_string("action");
235 $strrestore = get_string('restore');
237 $straddtoquiz = get_string("addtoquiz", "quiz");
238 $strtype = get_string("type", "quiz");
239 $strcreatemultiple = get_string("createmultiple", "quiz");
240 $strpreview = get_string("preview","quiz");
242 if (!$categoryid) {
243 echo "<p align=\"center\"><b>";
244 print_string("selectcategoryabove", "quiz");
245 echo "</b></p>";
246 if ($quizid) {
247 echo "<p>";
248 print_string("addingquestions", "quiz");
249 echo "</p>";
251 return;
254 if (!$category = get_record("question_categories", "id", "$categoryid")) {
255 notify("Category not found!");
256 return;
258 echo "<center>";
259 $formatoptions = new stdClass;
260 $formatoptions->noclean = true;
261 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
263 echo '<table><tr>';
265 // check if editing of this category is allowed
266 if (has_capability('moodle/question:managecategory', $context)) {
267 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
268 echo '<td valign="top" align="right">';
269 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
270 "", "choose", "", "", false, "self");
271 echo '</td><td width="10" valign="top" align="right">';
272 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
273 echo '</td></tr>';
275 else {
276 echo '<tr><td>';
277 print_string("publishedit","quiz");
278 echo '</td></tr>';
281 echo '<tr><td colspan="3" align="right"><font size="2">';
283 if (has_capability('moodle/question:import', $context)) {
284 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
285 helpbutton("import", $strimportquestions, "quiz");
286 echo ' | ';
289 if (has_capability('moodle/question:export', $context)) {
290 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
291 helpbutton("export", $strexportquestions, "quiz");
293 echo '</font></td></tr>';
295 echo '</table>';
297 echo '</center>';
299 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
301 // hide-feature
302 $showhidden = $showhidden ? '' : " AND hidden = '0'";
304 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
305 echo "<p align=\"center\">";
306 print_string("noquestions", "quiz");
307 echo "</p>";
308 return;
311 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
312 // There are no questions on the requested page.
313 $page = 0;
314 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
315 // There are no questions at all
316 echo "<p align=\"center\">";
317 print_string("noquestions", "quiz");
318 echo "</p>";
319 return;
323 print_paging_bar($totalnumber, $page, $perpage,
324 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
326 $canedit = has_capability('moodle/question:manage', $context);
328 echo '<form method="post" action="edit.php?courseid='.$course->id.'">';
329 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
330 print_simple_box_start('center', '100%', '#ffffff', 0);
331 echo '<table id="categoryquestions" cellspacing="0"><tr>';
332 $actionwidth = $canedit ? 95 : 70;
333 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
335 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
336 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
337 'id ASC' => get_string("sortage", "quiz"));
338 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
339 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
340 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
341 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
342 echo "</tr>\n";
343 foreach ($questions as $question) {
344 echo "<tr>\n<td nowrap=\"nowrap\">\n";
346 // add to quiz
347 if ($quizid && has_capability('mod/quiz:manage', $context)) {
348 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;quizid=$quizid&amp;sesskey=$USER->sesskey\"><img
349 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
352 // preview
353 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview', " .
354 QUESTION_PREVIEW_POPUP_OPTIONS . ", false)\"><img
355 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
357 // edit, hide, delete question, using question capabilities, not quiz capabilieies
358 if (has_capability('moodle/question:manage', $context)) {
359 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
360 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
361 // hide-feature
362 if($question->hidden) {
363 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
364 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
365 } else {
366 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
367 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
370 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
371 echo "</td>\n";
373 if ($question->hidden) {
374 echo '<td class="dimmed_text">'.$question->name."</td>\n";
375 } else {
376 echo "<td>".$question->name."</td>\n";
378 echo "<td align=\"center\">\n";
379 print_question_icon($question, $canedit);
380 echo "</td>\n";
381 echo "</tr>\n";
383 echo '<tr><td align="center" colspan="3">';
384 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
385 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
386 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
387 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
388 } else {
389 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=' . DEFAULT_QUESTIONS_PER_PAGE . '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
392 echo "</td></tr></table>\n";
393 print_simple_box_end();
395 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
396 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
397 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
398 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
399 if ($quizid && has_capability('mod/quiz:manage', $context)) {
400 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
401 echo '</td><td>';
403 // print delete and move selected question
404 if (has_capability('moodle/question:manage', $context)) {
405 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
406 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
407 question_category_select_menu($course->id, false, true, $category->id);
409 echo "</td></tr></table>";
411 // add random question
412 if ($quizid && has_capability('mod/quiz:manage', $context)) {
413 for ($i=1;$i<=10; $i++) {
414 $randomcount[$i] = $i;
416 echo '<br />';
417 print_string('addrandom', 'quiz',
418 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
419 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
420 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
421 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
422 helpbutton('random', get_string('random', 'quiz'), 'quiz');
425 echo "</form>\n";