Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / editlib.php
blobf75a8e95cf93f00b137c7f25028e7ef5fa1e4a8e
1 <?php // $Id$
2 /**
3 * Functions used by edit.php to edit quizzes
5 * @version $Id$
6 * @author Martin Dougiamas and many others. This has recently been extensively
7 * rewritten by members of the Serving Mathematics project
8 * {@link http://maths.york.ac.uk/serving_maths}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package quiz
13 require_once("locallib.php");
15 /**
16 * Delete a question from a quiz
18 * Deletes a question or a pagebreak from a quiz by updating $quiz
19 * as well as the quiz, quiz_question_instances
20 * @return boolean false if the question was not in the quiz
21 * @param int $id The id of the question to be deleted
22 * @param object $quiz The extended quiz object as used by edit.php
23 * This is updated by this function
25 function quiz_delete_quiz_question($id, &$quiz) {
26 // TODO: For the sake of safety check that this question can be deleted
27 // safely, i.e., that it is not already in use.
28 $questions = explode(",", $quiz->questions);
30 // only do something if this question exists
31 if (!isset($questions[$id])) {
32 return false;
35 $question = $questions[$id];
36 unset($questions[$id]);
37 // If we deleted the question at the top and it was followed by
38 // a page break then delete page break as well
39 if ($id == 0 && count($questions) > 1 && $questions[1] == 0) {
40 unset($questions[1]);
42 $quiz->questions = implode(",", $questions);
43 // Avoid duplicate page breaks
44 $quiz->questions = str_replace(',0,0', ',0', $quiz->questions);
45 // save new questionlist in database
46 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->instance)) {
47 error('Could not save question list');
49 delete_records('quiz_question_instances', 'quiz', $quiz->instance, 'question', $question);
50 return true;
54 /**
55 * Add a question to a quiz
57 * Adds a question to a quiz by updating $quiz as well as the
58 * quiz and quiz_question_instances tables. It also adds a page break
59 * if required.
60 * @return boolean false if the question was already in the quiz
61 * @param int $id The id of the question to be added
62 * @param object $quiz The extended quiz object as used by edit.php
63 * This is updated by this function
65 function quiz_add_quiz_question($id, &$quiz) {
66 $questions = explode(",", $quiz->questions);
68 if (in_array($id, $questions)) {
69 return false;
72 // remove ending page break if it is not needed
73 if ($breaks = array_keys($questions, 0)) {
74 // determine location of the last two page breaks
75 $end = end($breaks);
76 $last = prev($breaks);
77 $last = $last ? $last : -1;
78 if (!$quiz->questionsperpage or (($end - $last -1) < $quiz->questionsperpage)) {
79 array_pop($questions);
82 // add question
83 $questions[] = $id;
84 // add ending page break
85 $questions[] = 0;
87 // Save new questionslist in database
88 $quiz->questions = implode(",", $questions);
89 if (!set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id)) {
90 error('Could not save question list');
93 // update question grades
94 $questionrecord = get_record("question", "id", $id);
95 $quiz->grades[$id]
96 = $questionrecord->defaultgrade;
97 quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance);
99 return true;
103 * Save changes to question instance
105 * Saves changes to the question grades in the quiz_question_instances table.
106 * It does not update 'sumgrades' in the quiz table.
107 * @return boolean Indicates success or failure.
108 * @param integer grade The maximal grade for the question
109 * @param integer $questionid The id of the question
110 * @param integer $quizid The id of the quiz to update / add the instances for.
112 function quiz_update_question_instance($grade, $questionid, $quizid) {
113 if ($instance = get_record("quiz_question_instances", "quiz", $quizid, 'question', $questionid)) {
114 $instance->grade = $grade;
115 return update_record('quiz_question_instances', $instance);
116 } else {
117 unset($instance);
118 $instance->quiz = $quizid;
119 $instance->question = $questionid;
120 $instance->grade = $grade;
121 return insert_record("quiz_question_instances", $instance);
126 * Prints a list of quiz questions in a small layout form with knobs
128 * @return int sum of maximum grades
129 * @param object $quiz This is not the standard quiz object used elsewhere but
130 * it contains the quiz layout in $quiz->questions and the grades in
131 * $quiz->grades
132 * @param boolean $allowdelete Indicates whether the delete icons should be displayed
133 * @param boolean $showbreaks Indicates whether the page breaks should be displayed
134 * @param boolean $showbreaks Indicates whether the reorder tool should be displayed
136 function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $showbreaks=true, $reordertool=false) {
137 global $USER, $CFG, $QTYPES;
139 $strorder = get_string("order");
140 $strquestionname = get_string("questionname", "quiz");
141 $strgrade = get_string("grade");
142 $strremove = get_string('remove', 'quiz');
143 $stredit = get_string("edit");
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.course
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";
192 foreach ($order as $i => $qnum) {
194 if ($qnum and empty($questions[$qnum])) {
195 continue;
198 // If the questiontype is missing change the question type
199 if ($qnum and !array_key_exists($questions[$qnum]->qtype, $QTYPES)) {
200 $questions[$qnum]->qtype = 'missingtype';
203 // Show the re-ordering field if the tool is turned on.
204 // But don't show it in front of pagebreaks if they are hidden.
205 if ($reordertool) {
206 if ($qnum or $showbreaks) {
207 echo '<tr><td><input type="text" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
208 } else {
209 echo '<tr><td><input type="hidden" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
211 } else {
212 echo '<tr><td></td>';
214 if ($qnum == 0) { // This is a page break
215 if ($showbreaks) {
216 echo '<td colspan ="3">&nbsp;</td>';
217 echo '<td><table style="width:100%; line-height:11px; font-size:9px; margin: -5px -5px;"><tr>';
218 echo '<td><hr /></td>';
219 echo '<td style="width:50px;">Page break</td>';
220 echo '<td><hr /></td>';
221 echo '<td style="width:45px;">';
222 if ($count > 1) {
223 echo "<a title=\"$strmoveup\" href=\"".$pageurl->out_action(array('up'=>$count))."\"><img
224 src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" /></a>";
226 echo '&nbsp;';
227 if ($count < $lastindex) {
228 echo "<a title=\"$strmovedown\" href=\"".$pageurl->out_action(array('down'=>$count))."\"><img
229 src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" /></a>";
231 echo "<a title=\"$strremove\" href=\"".$pageurl->out_action(array('delete'=>$count))."\">
232 <img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strremove\" /></a>";
234 echo '</td></tr></table></td>';
235 echo '<td colspan="2">&nbsp;</td>';
237 $count++;
238 // missing </tr> here, if loop is broken, need to close the </tr> from line 199/201
239 echo "</tr>";
240 continue;
242 $question = $questions[$qnum];
243 $canedit = has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $question->course));
245 echo "<td>";
246 if ($count != 0) {
247 echo "<a title=\"$strmoveup\" href=\"".$pageurl->out_action(array('up'=>$count))."\"><img
248 src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strmoveup\" /></a>";
250 echo "</td>";
251 echo "<td>";
252 if ($count < $lastindex-1) {
253 echo "<a title=\"$strmovedown\" href=\"".$pageurl->out_action(array('down'=>$count))."\"><img
254 src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strmovedown\" /></a>";
256 echo "</td>";
258 if (!$quiz->shufflequestions) {
259 // Print and increment question number
260 echo '<td>'.($question->length ? $qno : '&nbsp;').'</td>';
261 $qno += $question->length;
262 } else {
263 echo '<td>&nbsp;</td>';
266 echo '<td>' . format_string($question->name) . '</td>';
267 echo "<td align=\"center\">";
268 print_question_icon($question);
269 echo "</td>";
270 echo '<td align="left">';
271 if ($question->qtype == 'description') {
272 echo "<input type=\"hidden\" name=\"q$qnum\" value=\"0\" /> \n";
273 } else {
274 echo '<input type="text" name="q'.$qnum.'" size="2" value="'.$quiz->grades[$qnum].
275 '" tabindex="'.($lastindex+$qno).'" />';
277 echo '</td><td align="center">';
279 if ($question->qtype != 'random') {
280 quiz_question_preview_button($quiz, $question);
282 if ($canedit) {
283 $returnurl = $pageurl->out();
284 $questionparams = array('returnurl' => $returnurl, 'cmid'=>$quiz->cmid, 'id' => $qnum);
285 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams);
286 echo "<a title=\"$stredit\" href=\"".$questionurl->out()."\">
287 <img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a>";
289 if ($allowdelete) {
290 echo "<a title=\"$strremove\" href=\"".$pageurl->out_action(array('delete'=>$count))."\">
291 <img src=\"$CFG->pixpath/t/removeright.gif\" class=\"iconsmall\" alt=\"$strremove\" /></a>";
294 echo "</td></tr>";
295 $count++;
296 $sumgrade += $quiz->grades[$qnum];
299 echo "<tr><td colspan=\"6\" align=\"right\">\n";
300 print_string('total');
301 echo ": </td>";
302 echo "<td align=\"left\">\n";
303 echo "<strong>$sumgrade</strong>";
304 echo "</td><td>&nbsp;\n</td></tr>\n";
306 echo "<tr><td colspan=\"6\" align=\"right\">\n";
307 print_string('maximumgrade');
308 echo ": </td>";
309 echo "<td align=\"left\">\n";
310 echo '<input type="text" name="maxgrade" size="2" tabindex="'.($qno+1)
311 .'" value="'.$quiz->grade.'" />';
312 echo '</td><td align="left">';
313 helpbutton("maxgrade", get_string("maximumgrade"), "quiz");
314 echo "</td></tr></table>\n";
316 echo '<div class="quizquestionlistcontrols"><input type="submit" value="'.get_string('savechanges').'" />';
317 echo '<input type="hidden" name="savechanges" value="save" /></div>';
319 echo '</fieldset>';
320 echo "</form>\n";
322 /// Form to choose to show pagebreaks and to repaginate quiz
323 echo '<form method="post" action="edit.php" id="showbreaks">';
324 echo '<fieldset class="invisiblefieldset">';
325 echo $pageurl->hidden_params_out(array('showbreaks', 'reordertool'));
326 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
327 echo '<input type="hidden" name="showbreaks" value="0" />';
328 echo '<input type="checkbox" name="showbreaks" value="1"';
329 if ($showbreaks) {
330 echo ' checked="checked"';
332 echo ' onchange="getElementById(\'showbreaks\').submit(); return true;" />';
333 print_string('showbreaks', 'quiz');
335 if ($showbreaks) {
336 $perpage= array();
337 for ($i=0; $i<=50; ++$i) {
338 $perpage[$i] = $i;
340 $perpage[0] = get_string('allinone', 'quiz');
341 echo '<br />&nbsp;&nbsp;';
342 print_string('repaginate', 'quiz',
343 choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true));
346 echo '<br /><input type="hidden" name="reordertool" value="0" />';
347 echo '<input type="checkbox" name="reordertool" value="1"';
348 if ($reordertool) {
349 echo ' checked="checked"';
351 echo ' onchange="getElementById(\'showbreaks\').submit(); return true;" />';
352 print_string('reordertool', 'quiz');
353 echo ' ';
354 helpbutton('reorderingtool', get_string('reorderingtool', 'quiz'), 'quiz');
356 echo '<div class="quizquestionlistcontrols"><input type="submit" name="repaginate" value="'. get_string('go') .'" /></div>';
357 echo '</fieldset>';
358 echo '</form>';
360 return $sumgrade;