printing user picture for comments
[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
18/**
19* Array of question types names translated to the user's language
20*
21* The $QUIZ_QUESTION_TYPE 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.
f02c6f01 23* The complete list of question types can be found in {@link $QTYPES}.
516cf3eb 24*/
25
26$QUIZ_QUESTION_TYPE = 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")
36 );
37// add remote question types
38if ($rqp_types = get_records('quiz_rqp_types')) {
39 foreach($rqp_types as $type) {
40 $QUIZ_QUESTION_TYPE[100+$type->id] = $type->name;
41 }
42}
43
e586cfb4 44function question_category_form($course, $current, $recurse=1, $showhidden=false) {
45 global $CFG;
516cf3eb 46/// Prints a form to choose categories
47
48/// Make sure the default category exists for this course
49 if (!$categories = get_records("quiz_categories", "course", $course->id, "id ASC")) {
4f48fb42 50 if (!$category = get_default_question_category($course->id)) {
516cf3eb 51 notify("Error creating a default category!");
52 }
53 }
54
55/// Get all the existing categories now
56 if (!$categories = get_records_select("quiz_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
59 }
60 $categories = add_indented_names($categories);
61 foreach ($categories as $key => $category) {
62 if ($catcourse = get_record("course", "id", $category->course)) {
63 if ($category->publish && $category->course != $course->id) {
64 $category->indentedname .= " ($catcourse->shortname)";
65 }
66 $catmenu[$category->id] = $category->indentedname;
67 }
68 }
69 $strcategory = get_string("category", "quiz");
70 $strshow = get_string("show", "quiz");
71 $streditcats = get_string("editcategories", "quiz");
72
73 echo "<table width=\"100%\"><tr><td width=\"20\" nowrap=\"nowrap\">";
74 echo "<b>$strcategory:</b>&nbsp;";
75 echo "</td><td>";
76 popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
77 echo "</td><td align=\"right\">";
e586cfb4 78 echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
516cf3eb 79 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
80 echo "<input type=\"submit\" value=\"$streditcats\" />";
81 echo "</form>";
82 echo '</td></tr></table>';
83 echo '<form method="post" action="edit.php" name="displayoptions">';
84 echo '<table><tr><td>';
85 echo '<input type="hidden" name="recurse" value="0" />';
86 echo '<input type="checkbox" name="recurse" value="1"';
87 if ($recurse) {
88 echo ' checked="checked"';
89 }
90 echo ' onchange="document.displayoptions.submit(); return true;" />';
91 print_string('recurse', 'quiz');
92 // hide-feature
93 echo '<br />';
94 echo '<input type="hidden" name="showhidden" value="0" />';
95 echo '<input type="checkbox" name="showhidden"';
96 if ($showhidden) {
97 echo ' checked="checked"';
98 }
99 echo ' onchange="document.displayoptions.submit(); return true;" />';
100 print_string('showhidden', 'quiz');
101 echo '</td><noscript><td valign="center">';
102 echo ' <input type="submit" value="'. get_string('go') .'" />';
103 echo '</td></noscript></tr></table></form>';
104}
105
106
107/**
108* Prints the table of questions in a category with interactions
109*
110* @param object $course The course object
111* @param int $categoryid The id of the question category to be displayed
112* @param int $quizid The quiz id if we are in the context of a particular quiz, 0 otherwise
113* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
114* @param int $page The number of the page to be displayed
115* @param int $perpage Number of questions to show per page
116* @param boolean $showhidden True if also hidden questions should be displayed
117*/
e586cfb4 118function question_list($course, $categoryid, $quizid,
516cf3eb 119 $recurse=1, $page, $perpage, $showhidden=false, $sortorder='qtype, name ASC') {
120 global $QUIZ_QUESTION_TYPE, $USER, $CFG;
121
122 $strcategory = get_string("category", "quiz");
123 $strquestion = get_string("question", "quiz");
124 $straddquestions = get_string("addquestions", "quiz");
125 $strimportquestions = get_string("importquestions", "quiz");
126 $strexportquestions = get_string("exportquestions", "quiz");
127 $strnoquestions = get_string("noquestions", "quiz");
128 $strselect = get_string("select", "quiz");
129 $strselectall = get_string("selectall", "quiz");
130 $strselectnone = get_string("selectnone", "quiz");
131 $strcreatenewquestion = get_string("createnewquestion", "quiz");
132 $strquestionname = get_string("questionname", "quiz");
133 $strdelete = get_string("delete");
134 $stredit = get_string("edit");
135 $straction = get_string("action");
136 $strrestore = get_string('restore');
137
138 $straddtoquiz = get_string("addtoquiz", "quiz");
139 $strtype = get_string("type", "quiz");
140 $strcreatemultiple = get_string("createmultiple", "quiz");
141 $strpreview = get_string("preview","quiz");
142
143 if (!$categoryid) {
144 echo "<p align=\"center\"><b>";
145 print_string("selectcategoryabove", "quiz");
146 echo "</b></p>";
147 if ($quizid) {
148 echo "<p>";
149 print_string("addingquestions", "quiz");
150 echo "</p>";
151 }
152 return;
153 }
154
155 if (!$category = get_record("quiz_categories", "id", "$categoryid")) {
156 notify("Category not found!");
157 return;
158 }
159 echo "<center>";
160 echo format_text($category->info, FORMAT_MOODLE);
161
162 echo '<table><tr>';
163
164 // check if editing of this category is allowed
165 if (isteacheredit($category->course)) {
166 echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
167 echo '<td valign="top" align="right">';
e586cfb4 168 popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $QUIZ_QUESTION_TYPE, "addquestion",
516cf3eb 169 "", "choose", "", "", false, "self");
170 echo '</td><td width="10" valign="top" align="right">';
171 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
172 echo '</td></tr>';
173 }
174 else {
175 echo '<tr><td>';
176 print_string("publishedit","quiz");
177 echo '</td></tr>';
178 }
179
180 echo '<tr><td colspan="3" align="right"><font size="2">';
181 if (isteacheredit($category->course)) {
5ab6af12 182 echo '<a href="'.$CFG->wwwroot.'/question/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
516cf3eb 183 helpbutton("import", $strimportquestions, "quiz");
184 echo ' | ';
185 }
5ab6af12 186 echo "<a href=\"$CFG->wwwroot/question/export.php?category={$category->id}&amp;courseid={$course->id}\">$strexportquestions</a>";
516cf3eb 187 helpbutton("export", $strexportquestions, "quiz");
188 echo '</font></td></tr>';
189
190 echo '</table>';
191
192 echo '</center>';
193
194 $categorylist = ($recurse) ? quiz_categorylist($category->id) : $category->id;
195
196 // hide-feature
197 $showhidden = $showhidden ? '' : " AND hidden = '0'";
198
4f48fb42 199 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
516cf3eb 200 echo "<p align=\"center\">";
201 print_string("noquestions", "quiz");
202 echo "</p>";
203 return;
204 }
205
4f48fb42 206 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 207 // There are no questions on the requested page.
208 $page = 0;
4f48fb42 209 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 210 // There are no questions at all
211 echo "<p align=\"center\">";
212 print_string("noquestions", "quiz");
213 echo "</p>";
214 return;
215 }
216 }
217
218 print_paging_bar($totalnumber, $page, $perpage,
219 "edit.php?perpage=$perpage&amp;");
220
221 $canedit = isteacheredit($category->course);
222
223 echo '<form method="post" action="edit.php">';
224 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
225 print_simple_box_start('center', '100%', '#ffffff', 0);
226 echo '<table id="categoryquestions" cellspacing="0"><tr>';
227 $actionwidth = $canedit ? 95 : 70;
228 echo "<th width=\"$actionwidth\" nowrap=\"nowrap\" class=\"header\">$straction</th>";
229
230 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
231 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
232 'id ASC' => get_string("sortage", "quiz"));
233 $orderselect = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
234 $orderselect .= '<noscript><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></noscript>';
235 echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\" class=\"header\">$strquestionname $orderselect</th>
236 <th nowrap=\"nowrap\" class=\"header\">$strtype</th>";
237 echo "</tr>\n";
238 foreach ($questions as $question) {
239 if ($question->qtype == RANDOM) {
240 //continue;
241 }
242 echo "<tr>\n<td nowrap=\"nowrap\">\n";
243 if ($quizid) {
244 echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
245 src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a>&nbsp;";
246 }
e586cfb4 247 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 248 src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>&nbsp;";
249 if ($canedit) {
e586cfb4 250 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
516cf3eb 251 src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
252 // hide-feature
253 if($question->hidden) {
e586cfb4 254 echo "<a title=\"$strrestore\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&amp;hide=0&amp;sesskey=$USER->sesskey\"><img
516cf3eb 255 src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
256 } else {
e586cfb4 257 echo "<a title=\"$strdelete\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&amp;delete=$question->id\"><img
516cf3eb 258 src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
259 }
260 }
261 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
262 echo "</td>\n";
263
264 if ($question->hidden) {
265 echo '<td class="dimmed_text">'.$question->name."</td>\n";
266 } else {
267 echo "<td>".$question->name."</td>\n";
268 }
269 echo "<td align=\"center\">\n";
4f48fb42 270 print_question_icon($question, $canedit);
516cf3eb 271 echo "</td>\n";
272 echo "</tr>\n";
273 }
274 echo '<tr><td colspan="3">';
275 print_paging_bar($totalnumber, $page, $perpage, "edit.php?perpage=$perpage&amp;");
276 echo "</td></tr></table>\n";
277 print_simple_box_end();
278
279 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
280 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
281 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
282 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
283 if ($quizid) {
284 echo "<input type=\"submit\" name=\"add\" value=\"<< $straddtoquiz\" />\n";
285 echo '</td><td>';
286 }
287 if ($canedit) {
288 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
289 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
290 quiz_category_select_menu($course->id, false, true, $category->id);
291 }
292 echo "</td></tr></table>";
293
294 if ($quizid) {
295 for ($i=1;$i<=10; $i++) {
296 $randomcount[$i] = $i;
297 }
298 echo '<br />';
299 print_string('addrandom', 'quiz',
300 choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
301 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
302 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
303 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
304 helpbutton('random', get_string('random', 'quiz'), 'quiz');
305 }
306
307 echo "</form>\n";
308}
309
310?>