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');
19 * Array of question types names translated to the user's language
21 * The $QTYPE_MENU array holds the names of all the question types that the user should
22 * be able to create directly. Some internal question types like random questions are excluded.
23 * The complete list of question types can be found in {@link $QTYPES}.
26 $QTYPE_MENU = array ( MULTICHOICE
=> get_string("multichoice", "quiz"),
27 TRUEFALSE
=> get_string("truefalse", "quiz"),
28 SHORTANSWER
=> get_string("shortanswer", "quiz"),
29 NUMERICAL
=> get_string("numerical", "quiz"),
30 CALCULATED
=> get_string("calculated", "quiz"),
31 MATCH
=> get_string("match", "quiz"),
32 DESCRIPTION
=> get_string("description", "quiz"),
33 RANDOMSAMATCH
=> get_string("randomsamatch", "quiz"),
34 MULTIANSWER
=> get_string("multianswer", "quiz"),
35 ESSAY
=> get_string("essay", "quiz")
37 // add remote question types
38 if ($rqp_types = get_records('quiz_rqp_types')) {
39 foreach($rqp_types as $type) {
40 $QTYPE_MENU[100+
$type->id
] = $type->name
;
44 function question_category_form($course, $current, $recurse=1, $showhidden=false) {
46 /// Prints a form to choose categories
48 /// Make sure the default category exists for this course
49 if (!$categories = get_records("question_categories", "course", $course->id
, "id ASC")) {
50 if (!$category = get_default_question_category($course->id
)) {
51 notify("Error creating a default category!");
55 /// Get all the existing categories now
56 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
57 notify("Could not find any question categories!");
58 return false; // Something is really wrong
61 $categories = add_indented_names( $categories );
62 foreach ($categories as $key => $category) {
63 if ($catcourse = get_record("course", "id", $category->course
)) {
64 if ($category->publish
&& $category->course
!= $course->id
) {
65 $category->indentedname
.= " ($catcourse->shortname)";
67 $catmenu[$category->id
] = $category->indentedname
;
70 $strcategory = get_string("category", "quiz");
71 $strshow = get_string("show", "quiz");
72 $streditcats = get_string("editcategories", "quiz");
74 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
75 echo "<b>$strcategory:</b> ";
77 popup_form ("edit.php?courseid=$course->id&cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
78 echo "</td><td align=\"right\">";
79 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
80 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
81 echo "<input type=\"submit\" value=\"$streditcats\" />";
83 echo '</td></tr></table>';
84 echo '<form method="post" action="edit.php" name="displayoptions">';
85 echo '<table><tr><td>';
86 echo '<input type="hidden" name="recurse" value="0" />';
87 echo '<input type="checkbox" name="recurse" value="1"';
89 echo ' checked="checked"';
91 echo ' onchange="document.displayoptions.submit(); return true;" />';
92 print_string('recurse', 'quiz');
95 echo '<input type="hidden" name="showhidden" value="0" />';
96 echo '<input type="checkbox" name="showhidden"';
98 echo ' checked="checked"';
100 echo ' onchange="document.displayoptions.submit(); return true;" />';
101 print_string('showhidden', 'quiz');
102 echo '</td><noscript><td valign="center">';
103 echo ' <input type="submit" value="'. get_string('go') .'" />';
104 echo '</td></noscript></tr></table></form>';
109 * Prints the table of questions in a category with interactions
111 * @param object $course The course object
112 * @param int $categoryid The id of the question category to be displayed
113 * @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
114 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
115 * @param int $page The number of the page to be displayed
116 * @param int $perpage Number of questions to show per page
117 * @param boolean $showhidden True if also hidden questions should be displayed
119 function question_list($course, $categoryid, $quizid,
120 $recurse=1, $page, $perpage, $showhidden=false, $sortorder='qtype, name ASC') {
121 global $QTYPE_MENU, $USER, $CFG;
123 $strcategory = get_string("category", "quiz");
124 $strquestion = get_string("question", "quiz");
125 $straddquestions = get_string("addquestions", "quiz");
126 $strimportquestions = get_string("importquestions", "quiz");
127 $strexportquestions = get_string("exportquestions", "quiz");
128 $strnoquestions = get_string("noquestions", "quiz");
129 $strselect = get_string("select", "quiz");
130 $strselectall = get_string("selectall", "quiz");
131 $strselectnone = get_string("selectnone", "quiz");
132 $strcreatenewquestion = get_string("createnewquestion", "quiz");
133 $strquestionname = get_string("questionname", "quiz");
134 $strdelete = get_string("delete");
135 $stredit = get_string("edit");
136 $straction = get_string("action");
137 $strrestore = get_string('restore');
139 $straddtoquiz = get_string("addtoquiz", "quiz");
140 $strtype = get_string("type", "quiz");
141 $strcreatemultiple = get_string("createmultiple", "quiz");
142 $strpreview = get_string("preview","quiz");
145 echo "<p align=\"center\"><b>";
146 print_string("selectcategoryabove", "quiz");
150 print_string("addingquestions", "quiz");
156 if (!$category = get_record("question_categories", "id", "$categoryid")) {
157 notify("Category not found!");
161 echo format_text($category->info
, FORMAT_MOODLE
);
165 // check if editing of this category is allowed
166 if (isteacheredit($category->course
)) {
167 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
168 echo '<td valign="top" align="right">';
169 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $QTYPE_MENU, "addquestion",
170 "", "choose", "", "", false, "self");
171 echo '</td><td width="10" valign="top" align="right">';
172 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
177 print_string("publishedit","quiz");
181 echo '<tr><td colspan="3" align="right"><font size="2">';
182 if (isteacheredit($category->course
)) {
183 echo '<a href="'.$CFG->wwwroot
.'/question/import.php?category='.$category->id
.'">'.$strimportquestions.'</a>';
184 helpbutton("import", $strimportquestions, "quiz");
187 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&courseid={$course->id}\">$strexportquestions</a>";
188 helpbutton("export", $strexportquestions, "quiz");
189 echo '</font></td></tr>';
195 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
198 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
200 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
201 echo "<p align=\"center\">";
202 print_string("noquestions", "quiz");
207 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
208 // There are no questions on the requested page.
210 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
211 // There are no questions at all
212 echo "<p align=\"center\">";
213 print_string("noquestions", "quiz");
219 print_paging_bar($totalnumber, $page, $perpage,
220 "edit.php?courseid={$course->id}&perpage=$perpage&");
222 $canedit = isteacheredit($category->course
);
224 echo '<form method="post" action="edit.php">';
225 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
226 echo "<input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
227 print_simple_box_start('center', '100%', '#ffffff', 0);
228 echo '<table id="categoryquestions" cellspacing="0"><tr>';
229 $actionwidth = $canedit ?
95 : 70;
230 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
232 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
233 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
234 'id ASC' => get_string("sortage", "quiz"));
235 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
236 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
237 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
238 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
240 foreach ($questions as $question) {
241 if ($question->qtype
== RANDOM
) {
244 echo "<tr>\n<td nowrap=\"nowrap\">\n";
246 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&sesskey=$USER->sesskey\"><img
247 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a> ";
249 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
250 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a> ";
252 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
253 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a> ";
255 if($question->hidden
) {
256 echo "<a title=\"$strrestore\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&hide=0&sesskey=$USER->sesskey\"><img
257 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
259 echo "<a title=\"$strdelete\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&delete=$question->id\"><img
260 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
263 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
266 if ($question->hidden
) {
267 echo '<td class="dimmed_text">'.$question->name
."</td>\n";
269 echo "<td>".$question->name
."</td>\n";
271 echo "<td align=\"center\">\n";
272 print_question_icon($question, $canedit);
276 echo '<tr><td colspan="3">';
277 print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&perpage=$perpage&");
278 echo "</td></tr></table>\n";
279 print_simple_box_end();
281 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
282 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
283 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
284 '</td><td align="right"><b> '.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
286 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
290 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
291 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
292 question_category_select_menu($course->id
, false, true, $category->id
);
294 echo "</td></tr></table>";
297 for ($i=1;$i<=10; $i++
) {
298 $randomcount[$i] = $i;
301 print_string('addrandom', 'quiz',
302 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
303 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
304 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
305 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
306 helpbutton('random', get_string('random', 'quiz'), 'quiz');