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.
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
16 require_once($CFG->libdir
.'/questionlib.php');
18 define('DEFAULT_QUESTIONS_PER_PAGE', 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 ) {
32 // questions will be added to an array
35 // build sql bit for $noparent
38 $npsql = " and parent='0' ";
41 // get (list) of categories
43 $categorylist = question_categorylist( $category->id
);
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;
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) {
72 // If it already exists, just return it.
73 if ($category = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", 'id', '*', '', 1)) {
74 return reset($category);
77 // Otherwise, we need to make one
78 $category = new stdClass
;
79 $category->name
= get_string("default", "quiz");
80 $category->info
= get_string("defaultinfo", "quiz");
81 $category->course
= $courseid;
82 $category->parent
= 0;
83 $category->sortorder
= 999; // By default, all categories get this number, and are sorted alphabetically.
84 $category->publish
= 0;
85 $category->stamp
= make_unique_id_code();
87 if (!$category->id
= insert_record("question_categories", $category)) {
88 notify("Error creating a default category!");
95 * prints a form to choose categories
97 function question_category_form($course, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
100 /// Make sure the default category exists for this course
101 get_default_question_category($course->id
);
103 /// Get all the existing categories now
104 $catmenu = question_category_options($course->id
, true);
106 $strcategory = get_string("category", "quiz");
107 $strshow = get_string("show", "quiz");
108 $streditcats = get_string("editcategories", "quiz");
110 echo "<table><tr><td style=\"white-space:nowrap;\">";
111 echo "<strong>$strcategory:</strong> ";
113 popup_form ("edit.php?courseid=$course->id&cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
114 echo "</td><td align=\"right\">";
115 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
117 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
118 echo "<input type=\"submit\" value=\"$streditcats\" />";
121 echo '</td></tr></table>';
123 echo '<form method="post" action="edit.php" id="displayoptions">';
124 echo "<fieldset class='invisiblefieldset'>";
125 echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />\n";
126 question_category_form_checkbox('recurse', $recurse);
127 question_category_form_checkbox('showhidden', $showhidden);
128 question_category_form_checkbox('showquestiontext', $showquestiontext);
129 echo '<noscript><div class="centerpara"><input type="submit" value="'. get_string('go') .'" />';
130 echo '</div></noscript></fieldset></form>';
134 * Private funciton to help the preceeding function.
136 function question_category_form_checkbox($name, $checked) {
137 echo '<div><input type="hidden" id="' . $name . '_off" name="' . $name . '" value="0" />';
138 echo '<input type="checkbox" id="' . $name . '_on" name="' . $name . '" value="1"';
140 echo ' checked="checked"';
142 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
143 echo '<label for="' . $name . '_on">';
144 print_string($name, 'quiz');
145 echo "</label></div>\n";
149 * Prints the table of questions in a category with interactions
151 * @param object $course The course object
152 * @param int $categoryid The id of the question category to be displayed
153 * @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
154 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
155 * @param int $page The number of the page to be displayed
156 * @param int $perpage Number of questions to show per page
157 * @param boolean $showhidden True if also hidden questions should be displayed
159 function question_list($course, $categoryid, $quizid=0,
160 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC',
161 $showquestiontext = false) {
162 global $QTYPE_MENU, $USER, $CFG, $THEME;
164 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
166 $qtypemenu = $QTYPE_MENU;
167 if ($rqp_types = get_records('question_rqp_types')) {
168 foreach($rqp_types as $type) {
169 $qtypemenu['rqp_'.$type->id
] = $type->name
;
173 $strcategory = get_string("category", "quiz");
174 $strquestion = get_string("question", "quiz");
175 $straddquestions = get_string("addquestions", "quiz");
176 $strimportquestions = get_string("importquestions", "quiz");
177 $strexportquestions = get_string("exportquestions", "quiz");
178 $strnoquestions = get_string("noquestions", "quiz");
179 $strselect = get_string("select", "quiz");
180 $strselectall = get_string("selectall", "quiz");
181 $strselectnone = get_string("selectnone", "quiz");
182 $strcreatenewquestion = get_string("createnewquestion", "quiz");
183 $strquestionname = get_string("questionname", "quiz");
184 $strdelete = get_string("delete");
185 $stredit = get_string("edit");
186 $straction = get_string("action");
187 $strrestore = get_string('restore');
189 $straddtoquiz = get_string("addtoquiz", "quiz");
190 $strtype = get_string("type", "quiz");
191 $strcreatemultiple = get_string("createmultiple", "quiz");
192 $strpreview = get_string("preview","quiz");
195 echo "<p style=\"text-align:center;\"><b>";
196 print_string("selectcategoryabove", "quiz");
200 print_string("addingquestions", "quiz");
206 if (!$category = get_record("question_categories", "id", "$categoryid")) {
207 notify("Category not found!");
210 echo '<div class="boxaligncenter">';
211 $formatoptions = new stdClass
;
212 $formatoptions->noclean
= true;
213 echo format_text($category->info
, FORMAT_MOODLE
, $formatoptions, $course->id
);
217 // check if editing of this category is allowed
218 if (has_capability('moodle/question:managecategory', $context)) {
219 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
220 echo '<td valign="top" align="right">';
221 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $qtypemenu, "addquestion",
222 "", "choose", "", "", false, "self");
223 echo '</td><td valign="top" align="right">';
224 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
229 print_string("publishedit","quiz");
236 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
239 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
241 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
242 echo "<p style=\"text-align:center;\">";
243 print_string("noquestions", "quiz");
248 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
249 // There are no questions on the requested page.
251 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
252 // There are no questions at all
253 echo "<p style=\"text-align:center;\">";
254 print_string("noquestions", "quiz");
260 print_paging_bar($totalnumber, $page, $perpage,
261 "edit.php?courseid={$course->id}&perpage=$perpage&");
263 $canedit = has_capability('moodle/question:manage', $context);
265 echo '<form method="post" action="edit.php?courseid='.$course->id
.'">';
266 echo '<fieldset class="invisiblefieldset" style="display: block;">';
267 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
269 echo '<table id="categoryquestions" style="width: 100%"><tr>';
270 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
272 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
273 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
274 'id ASC' => get_string("sortage", "quiz"));
275 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
276 $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
277 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
278 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
280 foreach ($questions as $question) {
283 if ($question->hidden
) {
284 $nameclass = 'dimmed_text';
285 $textclass = 'dimmed_text';
287 if ($showquestiontext) {
288 $nameclass .= ' header';
291 $nameclass = 'class="' . $nameclass . '"';
294 $textclass = 'class="' . $textclass . '"';
297 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
300 if ($quizid && has_capability('mod/quiz:manage', $context)) {
301 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&quizid=$quizid&sesskey=$USER->sesskey\"><img
302 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a> ";
306 echo "<a title=\"$strpreview\" href=\"javascript:void();\" onclick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview', " .
307 QUESTION_PREVIEW_POPUP_OPTIONS
. ", false)\"><img
308 src=\"$CFG->pixpath/t/preview.gif\" alt=\"$strpreview\" /></a> ";
310 // edit, hide, delete question, using question capabilities, not quiz capabilieies
311 if (has_capability('moodle/question:manage', $context)) {
312 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
313 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a> ";
315 if($question->hidden
) {
316 echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&unhide=$question->id&sesskey=$USER->sesskey\"><img
317 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
319 echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&deleteselected=$question->id&q$question->id=1\"><img
320 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
323 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
326 echo "<td $nameclass>" . $question->name
. "</td>\n";
327 echo "<td $nameclass style='text-align: right'>\n";
328 print_question_icon($question, $canedit);
331 if($showquestiontext){
332 echo '<tr><td colspan="3" ' . $textclass . '>';
333 $formatoptions = new stdClass
;
334 $formatoptions->noclean
= true;
335 $formatoptions->para
= false;
336 echo format_text($question->questiontext
, $question->questiontextformat
,
337 $formatoptions, $course->id
);
343 $paging = print_paging_bar($totalnumber, $page, $perpage,
344 "edit.php?courseid={$course->id}&perpage=$perpage&", 'page',
346 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE
) {
347 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE
) {
348 $showall = '<a href="edit.php?courseid='.$course->id
.'&perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
350 $showall = '<a href="edit.php?courseid='.$course->id
.'&perpage=' . DEFAULT_QUESTIONS_PER_PAGE
. '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE
).'</a>';
353 $paging = substr($paging, 0, strrpos($paging, '</div>'));
354 $paging .= "<br />$showall</div>";
356 $paging = "<div class='paging'>$showall</div>";
361 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
362 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
363 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
364 '</td><td align="right"><b> '.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
366 if ($quizid && has_capability('mod/quiz:manage', $context)) {
367 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
370 // print delete and move selected question
371 if (has_capability('moodle/question:manage', $context)) {
372 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
373 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
374 question_category_select_menu($course->id
, false, true, $category->id
);
376 echo "</td></tr></table>";
378 // add random question
379 if ($quizid && has_capability('mod/quiz:manage', $context)) {
380 for ($i = 1;$i <= min(10, $totalnumber); $i++
) {
381 $randomcount[$i] = $i;
383 for ($i = 20;$i <= min(100, $totalnumber); $i +
= 10) {
384 $randomcount[$i] = $i;
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');