MDL-8100, defaultgrade not imported.\rQuestion obect initialisation and value import...
[moodle-linuxchix.git] / question / editlib.php
blob7fd276a3ad16115971e28ec90f18e346f0c09294
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 style=\"width:100%;\"><tr><td style=\"width:20px; white-space:nowrap;\">";
163 echo "<strong>$strcategory:</strong>&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 "<fieldset class='invisiblefieldset'>";
169 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
170 echo "<input type=\"submit\" value=\"$streditcats\" />";
171 echo '</fieldset>';
172 echo "</form>";
173 echo '</td></tr></table>';
175 echo '<form method="post" action="edit.php" id="displayoptions">';
176 echo "<fieldset class='invisiblefieldset'>";
177 echo '<table><tr><td>';
178 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />";
179 echo '<input type="hidden" name="recurse" value="0" />';
180 echo '<input type="checkbox" name="recurse" value="1"';
181 if ($recurse) {
182 echo ' checked="checked"';
184 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
185 print_string('recurse', 'quiz');
186 // hide-feature
187 echo '<br />';
188 echo '<input type="hidden" name="showhidden" value="0" />';
189 echo '<input type="checkbox" name="showhidden"';
190 if ($showhidden) {
191 echo ' checked="checked"';
193 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
194 print_string('showhidden', 'quiz');
195 echo '</td><td valign="middle"><noscript><div>';
196 echo ' <input type="submit" value="'. get_string('go') .'" />';
197 echo '</div></noscript></td></tr></table></fieldset></form>';
202 * Prints the table of questions in a category with interactions
204 * @param object $course The course object
205 * @param int $categoryid The id of the question category to be displayed
206 * @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
207 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
208 * @param int $page The number of the page to be displayed
209 * @param int $perpage Number of questions to show per page
210 * @param boolean $showhidden True if also hidden questions should be displayed
212 function question_list($course, $categoryid, $quizid=0,
213 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC') {
214 global $QTYPE_MENU, $USER, $CFG, $THEME;
216 $context = get_context_instance(CONTEXT_COURSE, $course->id);
218 $qtypemenu = $QTYPE_MENU;
219 if ($rqp_types = get_records('question_rqp_types')) {
220 foreach($rqp_types as $type) {
221 $qtypemenu['rqp_'.$type->id] = $type->name;
225 $strcategory = get_string("category", "quiz");
226 $strquestion = get_string("question", "quiz");
227 $straddquestions = get_string("addquestions", "quiz");
228 $strimportquestions = get_string("importquestions", "quiz");
229 $strexportquestions = get_string("exportquestions", "quiz");
230 $strnoquestions = get_string("noquestions", "quiz");
231 $strselect = get_string("select", "quiz");
232 $strselectall = get_string("selectall", "quiz");
233 $strselectnone = get_string("selectnone", "quiz");
234 $strcreatenewquestion = get_string("createnewquestion", "quiz");
235 $strquestionname = get_string("questionname", "quiz");
236 $strdelete = get_string("delete");
237 $stredit = get_string("edit");
238 $straction = get_string("action");
239 $strrestore = get_string('restore');
241 $straddtoquiz = get_string("addtoquiz", "quiz");
242 $strtype = get_string("type", "quiz");
243 $strcreatemultiple = get_string("createmultiple", "quiz");
244 $strpreview = get_string("preview","quiz");
246 if (!$categoryid) {
247 echo "<p style=\"text-align:center;\"><b>";
248 print_string("selectcategoryabove", "quiz");
249 echo "</b></p>";
250 if ($quizid) {
251 echo "<p>";
252 print_string("addingquestions", "quiz");
253 echo "</p>";
255 return;
258 if (!$category = get_record("question_categories", "id", "$categoryid")) {
259 notify("Category not found!");
260 return;
262 echo '<div class="boxaligncenter">';
263 $formatoptions = new stdClass;
264 $formatoptions->noclean = true;
265 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
267 echo '<table><tr>';
269 // check if editing of this category is allowed
270 if (has_capability('moodle/question:managecategory', $context)) {
271 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
272 echo '<td valign="top" align="right">';
273 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
274 "", "choose", "", "", false, "self");
275 echo '</td><td style="width:10px;" valign="top" align="right">';
276 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
277 echo '</td></tr>';
279 else {
280 echo '<tr><td>';
281 print_string("publishedit","quiz");
282 echo '</td></tr>';
285 echo '<tr><td colspan="3" align="right" style="font-size:0.8em;">';
287 if (has_capability('moodle/question:import', $context)) {
288 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
289 helpbutton("import", $strimportquestions, "quiz");
290 echo ' | ';
293 if (has_capability('moodle/question:export', $context)) {
294 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
295 helpbutton("export", $strexportquestions, "quiz");
297 echo '</td></tr>';
299 echo '</table>';
301 echo '</div>';
303 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
305 // hide-feature
306 $showhidden = $showhidden ? '' : " AND hidden = '0'";
308 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
309 echo "<p style=\"text-align:center;\">";
310 print_string("noquestions", "quiz");
311 echo "</p>";
312 return;
315 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
316 // There are no questions on the requested page.
317 $page = 0;
318 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
319 // There are no questions at all
320 echo "<p style=\"text-align:center;\">";
321 print_string("noquestions", "quiz");
322 echo "</p>";
323 return;
327 print_paging_bar($totalnumber, $page, $perpage,
328 "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
330 $canedit = has_capability('moodle/question:manage', $context);
332 echo '<form method="post" action="edit.php?courseid='.$course->id.'">';
333 echo '<fieldset class="invisiblefieldset">';
334 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
335 print_simple_box_start('center', '100%', '#ffffff', 0);
336 echo '<table id="categoryquestions" cellspacing="0"><tr>';
337 $actionwidth = $canedit ? 95 : 70;
338 echo "<th style=\"width:$actionwidth; white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
340 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
341 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
342 'id ASC' => get_string("sortage", "quiz"));
343 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
344 $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
345 echo "<th align=\"left\" style=\"width:100%; white-space:nowrap;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
346 <th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strtype</th>";
347 echo "</tr>\n";
348 foreach ($questions as $question) {
349 echo "<tr>\n<td style=\"white-space:nowrap;\">\n";
351 // add to quiz
352 if ($quizid && has_capability('mod/quiz:manage', $context)) {
353 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;quizid=$quizid&amp;sesskey=$USER->sesskey\"><img
354 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
357 // preview
358 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onclick=\"openpopup('/question/preview.php?id=$question->id&amp;quizid=$quizid','$strpreview', " .
359 QUESTION_PREVIEW_POPUP_OPTIONS . ", false)\"><img
360 src=\"$CFG->pixpath/t/preview.gif\" alt=\"$strpreview\" /></a>&nbsp;";
362 // edit, hide, delete question, using question capabilities, not quiz capabilieies
363 if (has_capability('moodle/question:manage', $context)) {
364 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
365 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
366 // hide-feature
367 if($question->hidden) {
368 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
369 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
370 } else {
371 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
372 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
375 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
376 echo "</td>\n";
378 if ($question->hidden) {
379 echo '<td class="dimmed_text">'.$question->name."</td>\n";
380 } else {
381 echo "<td>".$question->name."</td>\n";
383 echo "<td align=\"center\">\n";
384 print_question_icon($question, $canedit);
385 echo "</td>\n";
386 echo "</tr>\n";
388 echo '<tr><td align="center" colspan="3">';
389 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
390 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
391 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
392 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
393 } else {
394 echo '<a href="edit.php?courseid='.$course->id.'&amp;perpage=' . DEFAULT_QUESTIONS_PER_PAGE . '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
397 echo "</td></tr></table>\n";
398 print_simple_box_end();
400 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
401 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
402 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
403 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
405 if ($quizid && has_capability('mod/quiz:manage', $context)) {
406 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
407 echo '</td><td>';
409 // print delete and move selected question
410 if (has_capability('moodle/question:manage', $context)) {
411 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
412 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
413 question_category_select_menu($course->id, false, true, $category->id);
415 echo "</td></tr></table>";
417 // add random question
418 if ($quizid && has_capability('mod/quiz:manage', $context)) {
419 for ($i=1;$i<=10; $i++) {
420 $randomcount[$i] = $i;
422 echo '<br />';
423 print_string('addrandom', 'quiz',
424 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
425 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
426 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
427 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
428 helpbutton('random', get_string('random', 'quiz'), 'quiz');
430 echo '</fieldset>';
431 echo "</form>\n";