"MDL-12304, fix double text"
[moodle-linuxchix.git] / mod / quiz / editlib.php
blobc23226f1b92dd8be091d9ab68aeae99069a913f5
1 <?php // $Id$
2 /**
3 * Functions used by edit.php to edit quizzes
5 * @author Martin Dougiamas and many others. This has recently been extensively
6 * rewritten by members of the Serving Mathematics project
7 * {@link http://maths.york.ac.uk/serving_maths}
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package quiz
12 require_once("locallib.php");
14 /**
15 * Delete a question from a quiz
17 * Deletes a question or a pagebreak from a quiz by updating $quiz
18 * as well as the quiz, quiz_question_instances
19 * @return boolean false if the question was not in the quiz
20 * @param int $id The id of the question to be deleted
21 * @param object $quiz The extended quiz object as used by edit.php
22 * This is updated by this function
24 function quiz_delete_quiz_question($id, &$quiz) {
25 // TODO: For the sake of safety check that this question can be deleted
26 // safely, i.e., that it is not already in use.
27 $questions = explode(",", $quiz->questions);
29 // only do something if this question exists
30 if (!isset($questions[$id])) {
31 return false;
34 $question = $questions[$id];
35 unset($questions[$id]);
36 // If we deleted the question at the top and it was followed by
37 // a page break then delete page break as well
38 if ($id == 0 && count($questions) > 1 && $questions[1] == 0) {
39 unset($questions[1]);
41 $quiz->questions = implode(",", $questions);
42 // Avoid duplicate page breaks
43 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
44 // save new questionlist in database
45 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
46 error('Could not save question list');
48 delete_records('quiz_question_instances', 'quiz', $quiz->instance, 'question', $question);
49 return true;
53 /**
54 * Add a question to a quiz
56 * Adds a question to a quiz by updating $quiz as well as the
57 * quiz and quiz_question_instances tables. It also adds a page break
58 * if required.
59 * @return boolean false if the question was already in the quiz
60 * @param int $id The id of the question to be added
61 * @param object $quiz The extended quiz object as used by edit.php
62 * This is updated by this function
64 function quiz_add_quiz_question($id, &$quiz) {
65 $questions = explode(",", $quiz->questions);
67 if (in_array($id, $questions)) {
68 return false;
71 // remove ending page break if it is not needed
72 if ($breaks = array_keys($questions, 0)) {
73 // determine location of the last two page breaks
74 $end = end($breaks);
75 $last = prev($breaks);
76 $last = $last ? $last : -1;
77 if (!$quiz->questionsperpage or (($end - $last -1) < $quiz->questionsperpage)) {
78 array_pop($questions);
81 // add question
82 $questions[] = $id;
83 // add ending page break
84 $questions[] = 0;
86 // Save new questionslist in database
87 $quiz->questions = implode(",", $questions);
88 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) {
89 error('Could not save question list');
92 // update question grades
93 $questionrecord = get_record("question", "id", $id);
94 $quiz->grades[$id]
95 = $questionrecord->defaultgrade;
96 quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance);
98 return true;
102 * Save changes to question instance
104 * Saves changes to the question grades in the quiz_question_instances table.
105 * It does not update 'sumgrades' in the quiz table.
106 * @return boolean Indicates success or failure.
107 * @param integer grade The maximal grade for the question
108 * @param integer $questionid The id of the question
109 * @param integer $quizid The id of the quiz to update / add the instances for.
111 function quiz_update_question_instance($grade, $questionid, $quizid) {
112 if ($instance = get_record("quiz_question_instances", "quiz", $quizid, 'question', $questionid)) {
113 $instance->grade = $grade;
114 return update_record('quiz_question_instances', $instance);
115 } else {
116 unset($instance);
117 $instance->quiz = $quizid;
118 $instance->question = $questionid;
119 $instance->grade = $grade;
120 return insert_record("quiz_question_instances", $instance);
125 * Prints a list of quiz questions in a small layout form with knobs
127 * @return int sum of maximum grades
128 * @param object $quiz This is not the standard quiz object used elsewhere but
129 * it contains the quiz layout in $quiz->questions and the grades in
130 * $quiz->grades
131 * @param boolean $allowdelete Indicates whether the delete icons should be displayed
132 * @param boolean $showbreaks Indicates whether the page breaks should be displayed
133 * @param boolean $showbreaks Indicates whether the reorder tool should be displayed
135 function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $showbreaks=true, $reordertool=false) {
136 global $USER, $CFG, $QTYPES;
138 $strorder = get_string("order");
139 $strquestionname = get_string("questionname", "quiz");
140 $strgrade = get_string("grade");
141 $strremove = get_string('remove', 'quiz');
142 $stredit = get_string("edit");
143 $strview = get_string("view");
144 $straction = get_string("action");
145 $strmoveup = get_string("moveup");
146 $strmovedown = get_string("movedown");
147 $strsavegrades = get_string("savegrades", "quiz");
148 $strtype = get_string("type", "quiz");
149 $strpreview = get_string("preview", "quiz");
151 if (!$quiz->questions) {
152 echo "<p class=\"quizquestionlistcontrols\">";
153 print_string("noquestions", "quiz");
154 echo "</p>";
155 return 0;
158 if (!$questions = get_records_sql("SELECT q.*,c.contextid
159 FROM {$CFG->prefix}question q,
160 {$CFG->prefix}question_categories c
161 WHERE q.id in ($quiz->questions)
162 AND q.category = c.id")) {
163 echo "<p class=\"quizquestionlistcontrols\">";
164 print_string("noquestions", "quiz");
165 echo "</p>";
166 return 0;
169 $count = 0;
170 $qno = 1;
171 $sumgrade = 0;
172 $order = explode(',', $quiz->questions);
173 $lastindex = count($order)-1;
174 // If the list does not end with a pagebreak then add it on.
175 if ($order[$lastindex] != 0) {
176 $order[] = 0;
177 $lastindex++;
179 echo "<form method=\"post\" action=\"edit.php\">";
180 echo '<fieldset class="invisiblefieldset" style="display: block;">';
181 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
182 echo $pageurl->hidden_params_out();
184 echo "<table style=\"width:100%;\">\n";
185 echo "<tr><th colspan=\"3\" style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strorder</th>";
186 echo "<th class=\"header\" scope=\"col\">#</th>";
187 echo "<th align=\"left\" style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strquestionname</th>";
188 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strtype</th>";
189 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$strgrade</th>";
190 echo "<th align=\"center\" style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
191 echo "</tr>\n";
193 // for RTL languages: switch right and left arrows /****/
194 if (right_to_left()) {
195 $movearrow = 'moveleft.gif';
196 } else {
197 $movearrow = 'removeright.gif';
200 foreach ($order as $i => $qnum) {
202 if ($qnum and empty($questions[$qnum])) {
203 continue;
206 // If the questiontype is missing change the question type
207 if ($qnum and !array_key_exists($questions[$qnum]->qtype, $QTYPES)) {
208 $questions[$qnum]->qtype = 'missingtype';
211 // Show the re-ordering field if the tool is turned on.
212 // But don't show it in front of pagebreaks if they are hidden.
213 if ($reordertool) {
214 if ($qnum or $showbreaks) {
215 echo '<tr><td><input type="text" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
216 } else {
217 echo '<tr><td><input type="hidden" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
219 } else {
220 echo '<tr><td></td>';
222 if ($qnum == 0) { // This is a page break
223 if ($showbreaks) {
224 echo '<td colspan ="3">&nbsp;</td>';
225 echo '<td><table style="width:100%; line-height:11px; font-size:9px; margin: -5px -5px;"><tr>';
226 echo '<td><hr /></td>';
227 echo '<td style="width:50px;">Page break</td>';
228 echo '<td><hr /></td>';
229 echo '<td style="width:45px;">';
230 if ($count > 1) {
231 echo "<a title=\"$strmoveup\" href=\"".$pageurl->out_action(array('up'=>$count))."\"><img
232 src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" /></a>";
234 echo '&nbsp;';
235 if ($count < $lastindex) {
236 echo "<a title=\"$strmovedown\" href=\"".$pageurl->out_action(array('down'=>$count))."\"><img
237 src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" /></a>";
239 echo "<a title=\"$strremove\" href=\"".$pageurl->out_action(array('delete'=>$count))."\">
240 <img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strremove\" /></a>";
242 echo '</td></tr></table></td>';
243 echo '<td colspan="2">&nbsp;</td>';
245 $count++;
246 // missing </tr> here, if loop is broken, need to close the </tr>
247 echo "</tr>";
248 continue;
250 $question = $questions[$qnum];
252 echo "<td>";
253 if ($count != 0) {
254 echo "<a title=\"$strmoveup\" href=\"".$pageurl->out_action(array('up'=>$count))."\"><img
255 src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" /></a>";
257 echo "</td>";
258 echo "<td>";
259 if ($count < $lastindex-1) {
260 echo "<a title=\"$strmovedown\" href=\"".$pageurl->out_action(array('down'=>$count))."\"><img
261 src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" /></a>";
263 echo "</td>";
265 if (!$quiz->shufflequestions) {
266 // Print and increment question number
267 echo '<td>'.($question->length ? $qno : '&nbsp;').'</td>';
268 $qno += $question->length;
269 } else {
270 echo '<td>&nbsp;</td>';
273 echo '<td>' . format_string($question->name) . '</td>';
274 echo "<td align=\"center\">";
275 print_question_icon($question);
276 echo "</td>";
277 echo '<td align="left">';
278 if ($question->qtype == 'description') {
279 echo "<input type=\"hidden\" name=\"q$qnum\" value=\"0\" /> \n";
280 } else {
281 echo '<input type="text" name="q'.$qnum.'" size="2" value="'.$quiz->grades[$qnum].
282 '" tabindex="'.($lastindex+$qno).'" />';
284 echo '</td><td align="center">';
286 if (($question->qtype != 'random')){
287 echo quiz_question_preview_button($quiz, $question);
289 $returnurl = $pageurl->out();
290 $questionparams = array('returnurl' => $returnurl, 'cmid'=>$quiz->cmid, 'id' => $question->id);
291 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams);
292 if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) {
293 echo "<a title=\"$stredit\" href=\"".$questionurl->out()."\">
294 <img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a>";
295 } elseif (question_has_capability_on($question, 'view', $question->category)){
296 echo "<a title=\"$strview\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
297 src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a>&nbsp;";
299 if ($allowdelete && question_has_capability_on($question, 'use', $question->category)) { // remove from quiz, not question delete.
300 echo "<a title=\"$strremove\" href=\"".$pageurl->out_action(array('delete'=>$count))."\">
301 <img src=\"$CFG->pixpath/t/$movearrow\" class=\"iconsmall\" alt=\"$strremove\" /></a>";
304 echo "</td></tr>";
305 $count++;
306 $sumgrade += $quiz->grades[$qnum];
309 echo "<tr><td colspan=\"6\" align=\"right\">\n";
310 print_string('total');
311 echo ": </td>";
312 echo "<td align=\"left\">\n";
313 echo "<strong>$sumgrade</strong>";
314 echo "</td><td>&nbsp;\n</td></tr>\n";
316 echo "<tr><td colspan=\"6\" align=\"right\">\n";
317 print_string('maximumgrade');
318 echo ": </td>";
319 echo "<td align=\"left\">\n";
320 echo '<input type="text" name="maxgrade" size="2" tabindex="'.($qno+1)
321 .'" value="'.$quiz->grade.'" />';
322 echo '</td><td align="left">';
323 helpbutton("maxgrade", get_string("maximumgrade"), "quiz");
324 echo "</td></tr></table>\n";
326 echo '<div class="quizquestionlistcontrols"><input type="submit" value="'.get_string('savechanges').'" />';
327 echo '<input type="hidden" name="savechanges" value="save" /></div>';
329 echo '</fieldset>';
330 echo "</form>\n";
332 /// Form to choose to show pagebreaks and to repaginate quiz
333 echo '<form method="post" action="edit.php" id="showbreaks">';
334 echo '<fieldset class="invisiblefieldset">';
335 echo $pageurl->hidden_params_out(array('showbreaks', 'reordertool'));
336 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
337 echo '<input type="hidden" name="showbreaks" value="0" />';
338 echo '<input type="checkbox" name="showbreaks" value="1"';
339 if ($showbreaks) {
340 echo ' checked="checked"';
342 echo ' onclick="form.submit(); return true;" />';
343 print_string('showbreaks', 'quiz');
345 if ($showbreaks) {
346 $perpage= array();
347 for ($i=0; $i<=50; ++$i) {
348 $perpage[$i] = $i;
350 $perpage[0] = get_string('allinone', 'quiz');
351 echo '<br />&nbsp;&nbsp;';
352 print_string('repaginate', 'quiz',
353 choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true));
356 echo '<br /><input type="hidden" name="reordertool" value="0" />';
357 echo '<input type="checkbox" name="reordertool" value="1"';
358 if ($reordertool) {
359 echo ' checked="checked"';
361 echo ' onclick="form.submit(); return true;" />';
362 print_string('reordertool', 'quiz');
363 echo ' ';
364 helpbutton('reorderingtool', get_string('reordertool', 'quiz'), 'quiz');
366 echo '<div class="quizquestionlistcontrols"><input type="submit" name="repaginate" value="'. get_string('go') .'" /></div>';
367 echo '</fieldset>';
368 echo '</form>';
370 return $sumgrade;