Add a warning when trying to restore a pre-1.6 backup on a
[moodle.git] / question / type / questiontype.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
3* The default questiontype class.
4*
5* @version $Id$
6* @author Martin Dougiamas and many others. This has recently been completely
7* rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
8* the Serving Mathematics project
9* {@link http://maths.york.ac.uk/serving_maths}
10* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11* @package quiz
12*/
13
14/// Question type class //////////////////////////////////////////////
15
af3830ee 16class default_questiontype {
516cf3eb 17
18 /**
19 * Name of the question type
20 *
21 * The name returned should coincide with the name of the directory
22 * in which this questiontype is located
23 * @ return string
24 */
25 function name() {
26 return 'default';
27 }
28
29 /**
30 * Saves or updates a question after editing by a teacher
31 *
32 * Given some question info and some data about the answers
33 * this function parses, organises and saves the question
34 * It is used by {@link question.php} when saving new data from
35 * a form, and also by {@link import.php} when importing questions
36 * This function in turn calls {@link save_question_options}
37 * to save question-type specific options
38 * @return object A {@link question} object
39 * @param object $question The question object which should be updated
40 * @param object $form The form submitted by the teacher
41 * @param object $course The course we are in
42 */
43 function save_question($question, $form, $course) {
44 // This default implementation is suitable for most
45 // question types.
46
47 // First, save the basic question itself
48
49 $question->name = trim($form->name);
50 $question->questiontext = trim($form->questiontext);
51 $question->questiontextformat = $form->questiontextformat;
52 $question->parent = isset($form->parent)? $form->parent : 0;
53 $question->length = $this->actual_number_of_questions($question);
54 $question->penalty = isset($form->penalty) ? $form->penalty : 0;
55
56 if (empty($form->image)) {
57 $question->image = "";
58 } else {
59 $question->image = $form->image;
60 }
61
62 if (empty($question->name)) {
5433ee83 63 $question->name = substr(strip_tags($question->questiontext), 0, 15);
516cf3eb 64 if (empty($question->name)) {
65 $question->name = '-';
66 }
67 }
68
69 if ($question->penalty > 1 or $question->penalty < 0) {
70 $question->errors['penalty'] = get_string('invalidpenalty', 'quiz');
71 }
72
73 if (isset($form->defaultgrade)) {
74 $question->defaultgrade = $form->defaultgrade;
75 }
76
516cf3eb 77 if (!empty($question->id)) { // Question already exists
cbe20043 78 // keep existing unique stamp code
79 $question->stamp = get_field('question', 'stamp', 'id', $question->id);
4f48fb42 80 if (!update_record("question", $question)) {
516cf3eb 81 error("Could not update question!");
82 }
83 } else { // Question is a new one
cbe20043 84 // Set the unique code
85 $question->stamp = make_unique_id_code();
4f48fb42 86 if (!$question->id = insert_record("question", $question)) {
516cf3eb 87 error("Could not insert new question!");
88 }
89 }
90
91 // Now to save all the answers and type-specific options
92
93 $form->id = $question->id;
94 $form->qtype = $question->qtype;
95 $form->category = $question->category;
96
97 $result = $this->save_question_options($form);
98
99 if (!empty($result->error)) {
100 error($result->error);
101 }
102
103 if (!empty($result->notice)) {
104 notice($result->notice, "question.php?id=$question->id");
105 }
106
107 if (!empty($result->noticeyesno)) {
25ee4157 108 notice_yesno($result->noticeyesno, "question.php?id=$question->id&amp;courseid={$course->id}",
109 "edit.php?courseid={$course->id}");
516cf3eb 110 print_footer($course);
111 exit;
112 }
113
cbe20043 114 // Give the question a unique version stamp determined by question_hash()
115 if (!set_field('question', 'version', question_hash($question), 'id', $question->id)) {
116 error('Could not update question version field');
117 }
118
516cf3eb 119 return $question;
120 }
121
122 /**
123 * Saves question-type specific options
124 *
125 * This is called by {@link save_question()} to save the question-type specific data
126 * @return object $result->error or $result->noticeyesno or $result->notice
127 * @param object $question This holds the information from the editing form,
128 * it is not a standard question object.
129 */
130 function save_question_options($question) {
4eda4eec 131 return null;
516cf3eb 132 }
133
134 /**
135 * Changes all states for the given attempts over to a new question
136 *
137 * This is used by the versioning code if the teacher requests that a question
138 * gets replaced by the new version. In order for the attempts to be regraded
139 * properly all data in the states referring to the old question need to be
140 * changed to refer to the new version instead. In particular for question types
141 * that use the answers table the answers belonging to the old question have to
142 * be changed to those belonging to the new version.
143 *
144 * @param integer $oldquestionid The id of the old question
145 * @param object $newquestion The new question
146 * @param array $attempts An array of all attempt objects in whose states
147 * replacement should take place
148 */
149 function replace_question_in_attempts($oldquestionid, $newquestion, $attemtps) {
150 echo 'Not yet implemented';
151 return;
152 }
153
154 /**
155 * Loads the question type specific options for the question.
156 *
157 * This function loads any question type specific options for the
158 * question from the database into the question object. This information
159 * is placed in the $question->options field. A question type is
160 * free, however, to decide on a internal structure of the options field.
161 * @return bool Indicates success or failure.
162 * @param object $question The question object for the question. This object
163 * should be updated to include the question type
164 * specific information (it is passed by reference).
165 */
166 function get_question_options(&$question) {
167 if (!isset($question->options)) {
168 $question->options = new object;
169 }
170 // The default implementation attaches all answers for this question
ed28abca 171 $question->options->answers = get_records('question_answers', 'question',
172 $question->id);
516cf3eb 173 return true;
174 }
175
176 /**
0429cd86 177 * Deletes states from the question-type specific tables
178 *
179 * @param string $stateslist Comma separated list of state ids to be deleted
180 */
181 function delete_states($stateslist) {
182 /// The default question type does not have any tables of its own
183 // therefore there is nothing to delete
184
185 return true;
186 }
187
188 /**
189 * Deletes a question from the question-type specific tables
516cf3eb 190 *
191 * @return boolean Success/Failure
192 * @param object $question The question being deleted
193 */
90c3f310 194 function delete_question($questionid) {
516cf3eb 195 /// The default question type does not have any tables of its own
196 // therefore there is nothing to delete
197
198 return true;
199 }
200
201 /**
202 * Returns the number of question numbers which are used by the question
203 *
204 * This function returns the number of question numbers to be assigned
205 * to the question. Most question types will have length one; they will be
dfa47f96 206 * assigned one number. The 'description' type, however does not use up a
516cf3eb 207 * number and so has a length of zero. Other question types may wish to
208 * handle a bundle of questions and hence return a number greater than one.
209 * @return integer The number of question numbers which should be
210 * assigned to the question.
211 * @param object $question The question whose length is to be determined.
212 * Question type specific information is included.
213 */
214 function actual_number_of_questions($question) {
215 // By default, each question is given one number
216 return 1;
217 }
218
219 /**
220 * Creates empty session and response information for the question
221 *
222 * This function is called to start a question session. Empty question type
223 * specific session data (if any) and empty response data will be added to the
224 * state object. Session data is any data which must persist throughout the
225 * attempt possibly with updates as the user interacts with the
226 * question. This function does NOT create new entries in the database for
227 * the session; a call to the {@link save_session_and_responses} member will
228 * occur to do this.
229 * @return bool Indicates success or failure.
230 * @param object $question The question for which the session is to be
231 * created. Question type specific information is
232 * included.
233 * @param object $state The state to create the session for. Note that
234 * this will not have been saved in the database so
235 * there will be no id. This object will be updated
236 * to include the question type specific information
237 * (it is passed by reference). In particular, empty
238 * responses will be created in the ->responses
239 * field.
240 * @param object $cmoptions
241 * @param object $attempt The attempt for which the session is to be
242 * started. Questions may wish to initialize the
243 * session in different ways depending on the user id
244 * or time available for the attempt.
245 */
246 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
247 // The default implementation should work for the legacy question types.
248 // Most question types with only a single form field for the student's response
249 // will use the empty string '' as the index for that one response. This will
250 // automatically be stored in and restored from the answer field in the
4f48fb42 251 // question_states table.
516cf3eb 252 $state->responses = array('' => '');
253 return true;
254 }
255
256 /**
257 * Restores the session data and most recent responses for the given state
258 *
259 * This function loads any session data associated with the question
260 * session in the given state from the database into the state object.
261 * In particular it loads the responses that have been saved for the given
262 * state into the ->responses member of the state object.
263 *
264 * Question types with only a single form field for the student's response
265 * will not need not restore the responses; the value of the answer
4f48fb42 266 * field in the question_states table is restored to ->responses['']
516cf3eb 267 * before this function is called. Question types with more response fields
268 * should override this method and set the ->responses field to an
269 * associative array of responses.
270 * @return bool Indicates success or failure.
271 * @param object $question The question object for the question including any
272 * question type specific information.
273 * @param object $state The saved state to load the session for. This
274 * object should be updated to include the question
275 * type specific session information and responses
276 * (it is passed by reference).
277 */
278 function restore_session_and_responses(&$question, &$state) {
279 // The default implementation does nothing (successfully)
280 return true;
281 }
282
283 /**
284 * Saves the session data and responses for the given question and state
285 *
286 * This function saves the question type specific session data from the
287 * state object to the database. In particular for most question types it saves the
288 * responses from the ->responses member of the state object. The question type
4f48fb42 289 * non-specific data for the state has already been saved in the question_states
516cf3eb 290 * table and the state object contains the corresponding id and
291 * sequence number which may be used to index a question type specific table.
292 *
293 * Question types with only a single form field for the student's response
294 * which is contained in ->responses[''] will not have to save this response,
4f48fb42 295 * it will already have been saved to the answer field of the question_states table.
516cf3eb 296 * Question types with more response fields should override this method and save
297 * the responses in their own database tables.
298 * @return bool Indicates success or failure.
299 * @param object $question The question object for the question including
300 * the question type specific information.
301 * @param object $state The state for which the question type specific
302 * data and responses should be saved.
303 */
304 function save_session_and_responses(&$question, &$state) {
305 // The default implementation does nothing (successfully)
306 return true;
307 }
308
309 /**
310 * Returns an array of values which will give full marks if graded as
311 * the $state->responses field
312 *
313 * The correct answer to the question in the given state, or an example of
314 * a correct answer if there are many, is returned. This is used by some question
315 * types in the {@link grade_responses()} function but it is also used by the
316 * question preview screen to fill in correct responses.
317 * @return mixed A response array giving the responses corresponding
318 * to the (or a) correct answer to the question. If there is
319 * no correct answer that scores 100% then null is returned.
320 * @param object $question The question for which the correct answer is to
321 * be retrieved. Question type specific information is
322 * available.
323 * @param object $state The state of the question, for which a correct answer is
324 * needed. Question type specific information is included.
325 */
326 function get_correct_responses(&$question, &$state) {
327 /* The default implementation returns the response for the first answer
328 that gives full marks. */
4eda4eec 329 if ($question->options->answers) {
330 foreach ($question->options->answers as $answer) {
331 if (((int) $answer->fraction) === 1) {
332 return array('' => $answer->answer);
333 }
516cf3eb 334 }
335 }
336 return null;
337 }
338
339 /**
340 * Return an array of values with the texts for all possible responses stored
341 * for the question
342 *
343 * All answers are found and their text values isolated
344 * @return object A mixed object
345 * ->id question id. Needed to manage random questions:
346 * it's the id of the actual question presented to user in a given attempt
347 * ->responses An array of values giving the responses corresponding
348 * to all answers to the question. Answer ids are used as keys.
349 * The text and partial credit are the object components
350 * @param object $question The question for which the answers are to
351 * be retrieved. Question type specific information is
352 * available.
353 */
354 // ULPGC ecastro
355 function get_all_responses(&$question, &$state) {
356 unset($answers);
357 if (is_array($question->options->answers)) {
358 foreach ($question->options->answers as $aid=>$answer) {
359 unset ($r);
360 $r->answer = $answer->answer;
361 $r->credit = $answer->fraction;
362 $answers[$aid] = $r;
363 }
364 } else {
365 $answers[]="error"; // just for debugging, eliminate
366 }
367 $result->id = $question->id;
368 $result->responses = $answers;
369 return $result;
370 }
371
372 /**
373 * Return the actual response to the question in a given state
374 * for the question
375 *
376 * @return mixed An array containing the response or reponses (multiple answer, match)
377 * given by the user in a particular attempt.
378 * @param object $question The question for which the correct answer is to
379 * be retrieved. Question type specific information is
380 * available.
381 * @param object $state The state object that corresponds to the question,
382 * for which a correct answer is needed. Question
383 * type specific information is included.
384 */
385 // ULPGC ecastro
8cc274de 386 function get_actual_response($question, $state) {
387 // change length to truncate responses here if you want
388 $lmax = 40;
389 if (!empty($state->responses)) {
390 $responses[] = (strlen($state->responses['']) > $lmax) ?
391 substr($state->responses[''], 0, $lmax).'...' : $state->responses[''];
392 } else {
393 $responses[] = '';
394 }
395 return $responses;
516cf3eb 396 }
397
398 // ULPGC ecastro
399 function get_fractional_grade(&$question, &$state) {
400 $maxgrade = $question->maxgrade;
401 $grade = $state->grade;
402 if ($maxgrade) {
403 return (float)($grade/$maxgrade);
404 } else {
405 return (float)$grade;
406 }
407 }
408
409
410 /**
411 * Checks if the response given is correct and returns the id
412 *
413 * @return int The ide number for the stored answer that matches the response
414 * given by the user in a particular attempt.
415 * @param object $question The question for which the correct answer is to
416 * be retrieved. Question type specific information is
417 * available.
418 * @param object $state The state object that corresponds to the question,
419 * for which a correct answer is needed. Question
420 * type specific information is included.
421 */
422 // ULPGC ecastro
423 function check_response(&$question, &$state){
424 return false;
425 }
426
427 /**
428 * Prints the question including the number, grading details, content,
429 * feedback and interactions
430 *
431 * This function prints the question including the question number,
432 * grading details, content for the question, any feedback for the previously
433 * submitted responses and the interactions. The default implementation calls
434 * various other methods to print each of these parts and most question types
435 * will just override those methods.
516cf3eb 436 * @param object $question The question to be rendered. Question type
437 * specific information is included. The
438 * maximum possible grade is in ->maxgrade. The name
439 * prefix for any named elements is in ->name_prefix.
440 * @param object $state The state to render the question in. The grading
441 * information is in ->grade, ->raw_grade and
442 * ->penalty. The current responses are in
443 * ->responses. This is an associative array (or the
444 * empty string or null in the case of no responses
445 * submitted). The last graded state is in
446 * ->last_graded (hence the most recently graded
447 * responses are in ->last_graded->responses). The
448 * question type specific information is also
449 * included.
450 * @param integer $number The number for this question.
451 * @param object $cmoptions
452 * @param object $options An object describing the rendering options.
453 */
454 function print_question(&$question, &$state, $number, $cmoptions, $options) {
455 /* The default implementation should work for most question types
456 provided the member functions it calls are overridden where required.
37a12367 457 The layout is determined by the template question.html */
516cf3eb 458 global $CFG;
459
460 // For editing teachers print a link to an editing popup window
461 $editlink = '';
462 if (isteacheredit($cmoptions->course)) {
463 $stredit = get_string('edit');
464 $linktext = '<img src="'.$CFG->pixpath.'/t/edit.gif" border="0" alt="'.$stredit.'" />';
e586cfb4 465 $editlink = link_to_popup_window('/question/question.php?id='.$question->id, $stredit, $linktext, 450, 550, $stredit, '', true);
516cf3eb 466 }
467
468 $grade = '';
469 if ($question->maxgrade and $options->scores) {
6b11a0e8 470 if ($cmoptions->optionflags & QUESTION_ADAPTIVE) {
f30bbcaf 471 $grade = (!question_state_is_graded($state->last_graded)) ? '--/' : round($state->last_graded->grade, $cmoptions->decimalpoints).'/';
516cf3eb 472 }
473 $grade .= $question->maxgrade;
474 }
b6e907a2 475
da1cc5a4 476 $comment = stripslashes($state->comment);
b6e907a2 477 $commentlink = '';
478 if (isset($options->questioncommentlink)) {
479 $strcomment = get_string('commentorgrade', 'quiz');
097bd3f5 480 $commentlink = '<div class="commentlink">'.link_to_popup_window ($options->questioncommentlink.'?attempt='.$state->attempt.'&amp;question='.$question->id,
481 'commentquestion', $strcomment, 450, 650, $strcomment, 'none', true).'</div>';
b6e907a2 482 }
516cf3eb 483
fe9b5cfd 484 $history = $this->history($question, $state, $number, $cmoptions, $options);
485
aaae75b0 486 include "$CFG->dirroot/question/type/question.html";
fe9b5cfd 487 }
488
489 /*
490 * Print history of responses
491 *
492 * Used by print_question()
493 */
494 function history($question, $state, $number, $cmoptions, $options) {
516cf3eb 495 $history = '';
496 if(isset($options->history) and $options->history) {
497 if ($options->history == 'all') {
498 // show all states
755bddf1 499 $states = get_records_select('question_states', "attempt = '$state->attempt' AND question = '$question->id' AND event > '0'", 'seq_number ASC');
516cf3eb 500 } else {
501 // show only graded states
755bddf1 502 $states = get_records_select('question_states', "attempt = '$state->attempt' AND question = '$question->id' AND event IN (".QUESTION_EVENTGRADE.','.QUESTION_EVENTCLOSEANDGRADE.")", 'seq_number ASC');
516cf3eb 503 }
504 if (count($states) > 1) {
505 $strreviewquestion = get_string('reviewresponse', 'quiz');
506 unset($table);
507 $table->head = array (
508 get_string('numberabbr', 'quiz'),
509 get_string('action', 'quiz'),
510 get_string('response', 'quiz'),
511 get_string('time'),
512 get_string('score', 'quiz'),
b6e907a2 513 //get_string('penalty', 'quiz'),
516cf3eb 514 get_string('grade', 'quiz'),
515 );
516cf3eb 516 $table->width = '100%';
517 foreach ($states as $st) {
0a5b58af 518 $st->responses[''] = $st->answer;
519 $this->restore_session_and_responses($question, $st);
516cf3eb 520 $b = ($state->id == $st->id) ? '<b>' : '';
521 $be = ($state->id == $st->id) ? '</b>' : '';
fe9b5cfd 522 if ($state->id == $st->id) {
523 $link = '<b>'.$st->seq_number.'</b>';
524 } else {
525 if(isset($options->questionreviewlink)) {
526 $link = link_to_popup_window ($options->questionreviewlink.'?state='.$st->id.'&amp;number='.$number,
527 'reviewquestion', $st->seq_number, 450, 650, $strreviewquestion, 'none', true);
528 } else {
529 $link = $st->seq_number;
530 }
531 }
516cf3eb 532 $table->data[] = array (
fe9b5cfd 533 $link,
516cf3eb 534 $b.get_string('event'.$st->event, 'quiz').$be,
0a5b58af 535 $b.$this->response_summary($question, $st).$be,
516cf3eb 536 $b.userdate($st->timestamp, get_string('timestr', 'quiz')).$be,
537 $b.round($st->raw_grade, $cmoptions->decimalpoints).$be,
b6e907a2 538 //$b.round($st->penalty, $cmoptions->decimalpoints).$be,
516cf3eb 539 $b.round($st->grade, $cmoptions->decimalpoints).$be
540 );
541 }
542 $history = make_table($table);
543 }
544 }
fe9b5cfd 545 return $history;
516cf3eb 546 }
547
548
549 /**
550 * Prints the score obtained and maximum score available plus any penalty
551 * information
552 *
553 * This function prints a summary of the scoring in the most recently
554 * graded state (the question may not have been submitted for marking at
555 * the current state). The default implementation should be suitable for most
556 * question types.
557 * @param object $question The question for which the grading details are
558 * to be rendered. Question type specific information
559 * is included. The maximum possible grade is in
560 * ->maxgrade.
561 * @param object $state The state. In particular the grading information
562 * is in ->grade, ->raw_grade and ->penalty.
563 * @param object $cmoptions
564 * @param object $options An object describing the rendering options.
565 */
566 function print_question_grading_details(&$question, &$state, $cmoptions, $options) {
567 /* The default implementation prints the number of marks if no attempt
568 has been made. Otherwise it displays the grade obtained out of the
569 maximum grade available and a warning if a penalty was applied for the
570 attempt and displays the overall grade obtained counting all previous
571 responses (and penalties) */
572
f30bbcaf 573 if (QUESTION_EVENTDUPLICATE == $state->event) {
516cf3eb 574 echo ' ';
575 print_string('duplicateresponse', 'quiz');
576 }
577 if (!empty($question->maxgrade) && $options->scores) {
f30bbcaf 578 if (question_state_is_graded($state->last_graded)) {
516cf3eb 579 // Display the grading details from the last graded state
580 $grade->cur = round($state->last_graded->grade, $cmoptions->decimalpoints);
581 $grade->max = $question->maxgrade;
582 $grade->raw = round($state->last_graded->raw_grade, $cmoptions->decimalpoints);
583
584 // let student know wether the answer was correct
585 echo '<div class="correctness ';
586 if ($state->last_graded->raw_grade >= $question->maxgrade/1.01) { // We divide by 1.01 so that rounding errors dont matter.
587 echo ' correct">';
588 print_string('correct', 'quiz');
589 } else if ($state->last_graded->raw_grade > 0) {
590 echo ' partiallycorrect">';
591 print_string('partiallycorrect', 'quiz');
592 } else {
593 echo ' incorrect">';
594 print_string('incorrect', 'quiz');
595 }
596 echo '</div>';
597
598 echo '<div class="gradingdetails">';
599 // print grade for this submission
600 print_string('gradingdetails', 'quiz', $grade);
601 if ($cmoptions->penaltyscheme) {
602 // print details of grade adjustment due to penalties
603 if ($state->last_graded->raw_grade > $state->last_graded->grade){
604 print_string('gradingdetailsadjustment', 'quiz', $grade);
605 }
606 // print info about new penalty
607 // penalty is relevant only if the answer is not correct and further attempts are possible
f30bbcaf 608 if (($state->last_graded->raw_grade < $question->maxgrade) and (QUESTION_EVENTCLOSEANDGRADE !== $state->event)) {
516cf3eb 609 if ('' !== $state->last_graded->penalty && ((float)$state->last_graded->penalty) > 0.0) {
610 // A penalty was applied so display it
611 print_string('gradingdetailspenalty', 'quiz', $state->last_graded->penalty);
612 } else {
613 /* No penalty was applied even though the answer was
614 not correct (eg. a syntax error) so tell the student
615 that they were not penalised for the attempt */
616 print_string('gradingdetailszeropenalty', 'quiz');
617 }
618 }
619 }
620 echo '</div>';
621 }
622 }
623 }
624
625 /**
626 * Prints the main content of the question including any interactions
627 *
628 * This function prints the main content of the question including the
629 * interactions for the question in the state given. The last graded responses
630 * are printed or indicated and the current responses are selected or filled in.
631 * Any names (eg. for any form elements) are prefixed with $question->name_prefix.
632 * This method is called from the print_question method.
633 * @param object $question The question to be rendered. Question type
634 * specific information is included. The name
635 * prefix for any named elements is in ->name_prefix.
636 * @param object $state The state to render the question in. The grading
637 * information is in ->grade, ->raw_grade and
638 * ->penalty. The current responses are in
639 * ->responses. This is an associative array (or the
640 * empty string or null in the case of no responses
641 * submitted). The last graded state is in
642 * ->last_graded (hence the most recently graded
643 * responses are in ->last_graded->responses). The
644 * question type specific information is also
645 * included.
646 * The state is passed by reference because some adaptive
647 * questions may want to update it during rendering
648 * @param object $cmoptions
649 * @param object $options An object describing the rendering options.
650 */
651 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
652 /* This default implementation prints an error and must be overridden
653 by all question type implementations, unless the default implementation
654 of print_question has been overridden. */
655
656 notify('Error: Question formulation and input controls has not'
657 .' been implemented for question type '.$this->name());
658 }
659
660 /**
661 * Prints the submit button(s) for the question in the given state
662 *
663 * This function prints the submit button(s) for the question in the
664 * given state. The name of any button created will be prefixed with the
665 * unique prefix for the question in $question->name_prefix. The suffix
4dca7e51 666 * 'submit' is reserved for the single question submit button and the suffix
516cf3eb 667 * 'validate' is reserved for the single question validate button (for
668 * question types which support it). Other suffixes will result in a response
669 * of that name in $state->responses which the printing and grading methods
670 * can then use.
671 * @param object $question The question for which the submit button(s) are to
672 * be rendered. Question type specific information is
673 * included. The name prefix for any
674 * named elements is in ->name_prefix.
675 * @param object $state The state to render the buttons for. The
676 * question type specific information is also
677 * included.
678 * @param object $cmoptions
679 * @param object $options An object describing the rendering options.
680 */
681 function print_question_submit_buttons(&$question, &$state, $cmoptions, $options) {
682 /* The default implementation should be suitable for most question
683 types. It prints a mark button in the case where individual marking is
684 allowed. */
685
6b11a0e8 686 if (($cmoptions->optionflags & QUESTION_ADAPTIVE) and !$options->readonly) {
516cf3eb 687 echo '<input type="submit" name="';
688 echo $question->name_prefix;
4dca7e51 689 echo 'submit" value="';
516cf3eb 690 print_string('mark', 'quiz');
5bc57211 691 echo '" class="submit btn"';
692 echo ' />';
516cf3eb 693 }
694 }
695
696
697 /**
698 * Return a summary of the student response
699 *
700 * This function returns a short string of no more than a given length that
701 * summarizes the student's response in the given $state. This is used for
702 * example in the response history table
703 * @return string The summary of the student response
0a5b58af 704 * @param object $question
516cf3eb 705 * @param object $state The state whose responses are to be summarized
706 * @param int $length The maximum length of the returned string
707 */
0a5b58af 708 function response_summary($question, $state, $length=80) {
516cf3eb 709 // This should almost certainly be overridden
755bddf1 710 return substr(implode(',', $this->get_actual_response($question, $state)), 0, $length);
516cf3eb 711 }
712
713 /**
714 * Renders the question for printing and returns the LaTeX source produced
715 *
716 * This function should render the question suitable for a printed problem
717 * or solution sheet in LaTeX and return the rendered output.
718 * @return string The LaTeX output.
719 * @param object $question The question to be rendered. Question type
720 * specific information is included.
721 * @param object $state The state to render the question in. The
722 * question type specific information is also
723 * included.
724 * @param object $cmoptions
725 * @param string $type Indicates if the question or the solution is to be
726 * rendered with the values 'question' and
727 * 'solution'.
728 */
729 function get_texsource(&$question, &$state, $cmoptions, $type) {
730 // The default implementation simply returns a string stating that
731 // the question is only available online.
732
733 return get_string('onlineonly', 'texsheet');
734 }
735
736 /**
737 * Compares two question states for equivalence of the student's responses
738 *
739 * The responses for the two states must be examined to see if they represent
740 * equivalent answers to the question by the student. This method will be
741 * invoked for each of the previous states of the question before grading
742 * occurs. If the student is found to have already attempted the question
743 * with equivalent responses then the attempt at the question is ignored;
744 * grading does not occur and the state does not change. Thus they are not
745 * penalized for this case.
746 * @return boolean
747 * @param object $question The question for which the states are to be
748 * compared. Question type specific information is
749 * included.
750 * @param object $state The state of the question. The responses are in
751 * ->responses.
752 * @param object $teststate The state whose responses are to be
753 * compared. The state will be of the same age or
754 * older than $state.
755 */
756 function compare_responses(&$question, $state, $teststate) {
757 // The default implementation performs a comparison of the response
758 // arrays. The ordering of the arrays does not matter.
759 // Question types may wish to override this (eg. to ignore trailing
760 // white space or to make "7.0" and "7" compare equal).
761 return $state->responses == $teststate->responses;
762 }
763
37a12367 764 /**
765 * Checks whether a response matches a given answer
766 *
767 * This method only applies to questions that use teacher-defined answers
768 *
769 * @return boolean
770 */
771 function test_response(&$question, &$state, $answer) {
772 $response = isset($state->responses['']) ? $state->responses[''] : '';
773 return ($response == $answer->answer);
774 }
775
516cf3eb 776 /**
777 * Performs response processing and grading
778 *
779 * This function performs response processing and grading and updates
780 * the state accordingly.
781 * @return boolean Indicates success or failure.
782 * @param object $question The question to be graded. Question type
783 * specific information is included.
784 * @param object $state The state of the question to grade. The current
785 * responses are in ->responses. The last graded state
786 * is in ->last_graded (hence the most recently graded
787 * responses are in ->last_graded->responses). The
788 * question type specific information is also
789 * included. The ->raw_grade and ->penalty fields
790 * must be updated. The method is able to
791 * close the question session (preventing any further
792 * attempts at this question) by setting
f30bbcaf 793 * $state->event to QUESTION_EVENTCLOSEANDGRADE
516cf3eb 794 * @param object $cmoptions
795 */
796 function grade_responses(&$question, &$state, $cmoptions) {
797 /* The default implementation uses the comparison method to check if
798 the responses given are equivalent to the responses for each answer
799 in turn and sets the marks and penalty accordingly. This works for the
800 most simple question types. */
801
802 $teststate = clone($state);
803 $teststate->raw_grade = 0;
804 foreach($question->options->answers as $answer) {
805 $teststate->responses[''] = $answer->answer;
806
807 if($this->compare_responses($question, $state, $teststate)) {
808 $state->raw_grade = min(max((float) $answer->fraction,
809 0.0), 1.0) * $question->maxgrade;
810 break;
811 }
812 }
813 if (empty($state->raw_grade)) {
814 $state->raw_grade = 0.0;
815 }
816 // Only allow one attempt at the question
817 $state->penalty = 1;
818
f30bbcaf 819 // mark the state as graded
820 $state->event = ($state->event == QUESTION_EVENTCLOSE) ? QUESTION_EVENTCLOSEANDGRADE : QUESTION_EVENTGRADE;
821
516cf3eb 822 return true;
823 }
824
825
826 /**
827 * Includes configuration settings for the question type on the quiz admin
828 * page
829 *
7518b645 830 * TODO: It makes no sense any longer to do the admin for question types
831 * from the quiz admin page. This should be changed.
516cf3eb 832 * Returns an array of objects describing the options for the question type
833 * to be included on the quiz module admin page.
834 * Configuration options can be included by setting the following fields in
835 * the object:
836 * ->name The name of the option within this question type.
837 * The full option name will be constructed as
838 * "quiz_{$this->name()}_$name", the human readable name
839 * will be displayed with get_string($name, 'quiz').
840 * ->code The code to display the form element, help button, etc.
841 * i.e. the content for the central table cell. Be sure
842 * to name the element "quiz_{$this->name()}_$name" and
843 * set the value to $CFG->{"quiz_{$this->name()}_$name"}.
844 * ->help Name of the string from the quiz module language file
845 * to be used for the help message in the third column of
846 * the table. An empty string (or the field not set)
847 * means to leave the box empty.
848 * Links to custom settings pages can be included by setting the following
849 * fields in the object:
850 * ->name The name of the link text string.
851 * get_string($name, 'quiz') will be called.
852 * ->link The filename part of the URL for the link. The full URL
853 * is contructed as
aaae75b0 854 * "$CFG->wwwroot/question/type/{$this->name()}/$link?sesskey=$sesskey"
516cf3eb 855 * [but with the relavant calls to the s and rawurlencode
856 * functions] where $sesskey is the sesskey for the user.
857 * @return array Array of objects describing the configuration options to
858 * be included on the quiz module admin page.
859 */
860 function get_config_options() {
861 // No options by default
862
863 return false;
864 }
865
866 /**
dc1f00de 867 * Returns true if the editing wizard is finished, false otherwise.
868 *
869 * The default implementation returns true, which is suitable for all question-
516cf3eb 870 * types that only use one editing form. This function is used in
871 * question.php to decide whether we can regrade any states of the edited
872 * question and redirect to edit.php.
873 *
874 * The dataset dependent question-type, which is extended by the calculated
875 * question-type, overwrites this method because it uses multiple pages (i.e.
876 * a wizard) to set up the question and associated datasets.
877 *
878 * @param object $form The data submitted by the previous page.
879 *
880 * @return boolean Whether the wizard's last page was submitted or not.
881 */
882 function finished_edit_wizard(&$form) {
883 //In the default case there is only one edit page.
884 return true;
885 }
886
dc1f00de 887 /**
888 * Prints a table of course modules in which the question is used
889 *
7518b645 890 * TODO: This should be made quiz-independent
891 *
dc1f00de 892 * This function is used near the end of the question edit forms in all question types
893 * It prints the table of quizzes in which the question is used
894 * containing checkboxes to allow the teacher to replace the old question version
895 *
896 * @param object $question
897 * @param object $course
898 * @param integer $cmid optional The id of the course module currently being edited
899 */
900 function print_replacement_options($question, $course, $cmid='0') {
516cf3eb 901
902 // Disable until the versioning code has been fixed
516cf3eb 903 return;
904
905 // no need to display replacement options if the question is new
906 if(empty($question->id)) {
907 return true;
908 }
909
910 // get quizzes using the question (using the question_instances table)
911 $quizlist = array();
912 if(!$instances = get_records('quiz_question_instances', 'question', $question->id)) {
913 $instances = array();
914 }
915 foreach($instances as $instance) {
916 $quizlist[$instance->quiz] = $instance->quiz;
917 }
918 $quizlist = implode(',', $quizlist);
919 if(empty($quizlist) or !$quizzes = get_records_list('quiz', 'id', $quizlist)) {
920 $quizzes = array();
921 }
922
923 // do the printing
924 if(count($quizzes) > 0) {
925 // print the table
926 $strquizname = get_string('modulename', 'quiz');
927 $strdoreplace = get_string('replace', 'quiz');
928 $straffectedstudents = get_string('affectedstudents', 'quiz', $course->students);
929 echo "<tr valign=\"top\">\n";
930 echo "<td align=\"right\"><b>".get_string("replacementoptions", "quiz").":</b></td>\n";
931 echo "<td align=\"left\">\n";
932 echo "<table cellpadding=\"5\" align=\"left\" class=\"generalbox\" width=\"100%\">\n";
933 echo "<tr>\n";
934 echo "<th align=\"left\" valign=\"top\" nowrap=\"nowrap\" class=\"generaltableheader c0\">$strquizname</th>\n";
935 echo "<th align=\"center\" valign=\"top\" nowrap=\"nowrap\" class=\"generaltableheader c0\">$strdoreplace</th>\n";
936 echo "<th align=\"left\" valign=\"top\" nowrap=\"nowrap\" class=\"generaltableheader c0\">$straffectedstudents</th>\n";
937 echo "</tr>\n";
938 foreach($quizzes as $quiz) {
939 // work out whethere it should be checked by default
940 $checked = '';
dc1f00de 941 if((int)$cmid === (int)$quiz->id
516cf3eb 942 or empty($quiz->usercount)) {
943 $checked = "checked=\"checked\"";
944 }
945
946 // find how many different students have already attempted this quiz
947 $students = array();
948 if($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'")) {
949 foreach($attempts as $attempt) {
4f48fb42 950 if (record_exists('question_states', 'attempt', $attempt->uniqueid, 'question', $question->id, 'originalquestion', 0)) {
516cf3eb 951 $students[$attempt->userid] = 1;
952 }
953 }
954 }
955 $studentcount = count($students);
956
957 $strstudents = $studentcount === 1 ? $course->student : $course->students;
958 echo "<tr>\n";
959 echo "<td align=\"left\" class=\"generaltablecell c0\">".format_string($quiz->name)."</td>\n";
960 echo "<td align=\"center\" class=\"generaltablecell c0\"><input name=\"q{$quiz->id}replace\" type=\"checkbox\" ".$checked." /></td>\n";
961 echo "<td align=\"left\" class=\"generaltablecell c0\">".(($studentcount) ? $studentcount.' '.$strstudents : '-')."</td>\n";
962 echo "</tr>\n";
963 }
964 echo "</table>\n";
965 }
966 echo "</td></tr>\n";
967 }
968
969 function print_question_form_end($question, $submitscript='') {
970 // This function is used at the end of the question edit forms in all question types
971 // It prints the submit, copy, and cancel buttons and the standard hidden form fields
972 global $USER;
973 echo '<tr valign="top">
974 <td colspan="2" align="center">
975 <input type="submit" '.$submitscript.' value="'.get_string('savechanges').'" /> ';
976 if ($question->id) {
76de2cdf 977 echo '<input type="submit" name="makecopy" '.$submitscript.' value="'.get_string("makecopy", "quiz").'" /> ';
516cf3eb 978 }
979 echo '<input type="submit" name="cancel" value="'.get_string("cancel").'" />
980 <input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />
981 <input type="hidden" name="id" value="'.$question->id.'" />
982 <input type="hidden" name="qtype" value="'.$question->qtype.'" />';
983 // The following hidden field indicates that the versioning code should be turned on, i.e.,
984 // that old versions should be kept if necessary
985 echo '<input type="hidden" name="versioning" value="on" />
986 </td></tr>';
987 }
c5d94c41 988
989/// BACKUP FUNCTIONS ////////////////////////////
990
991 /*
992 * Backup the data in the question
993 *
994 * This is used in question/backuplib.php
995 */
996 function backup($bf,$preferences,$question,$level=6) {
997 // The default type has nothing to back up
998 return true;
999 }
1000
315559d3 1001/// RESTORE FUNCTIONS /////////////////
1002
1003 /*
1004 * Restores the data in the question
1005 *
1006 * This is used in question/restorelib.php
1007 */
1008 function restore($old_question_id,$new_question_id,$info,$restore) {
1009 // The default question type has nothing to restore
1010 return true;
1011 }
1012
1013 function restore_map($old_question_id,$new_question_id,$info,$restore) {
1014 // There is nothing to decode
1015 return true;
1016 }
1017
1018 function restore_recode_answer($state, $restore) {
1019 // There is nothing to decode
ba8f7ff0 1020 return $state->answer;
315559d3 1021 }
1022
1023 //This function restores the question_rqp_states
1024 function restore_state($state_id,$info,$restore) {
1025 // The default question type does not keep its own state information
1026 return true;
1027 }
516cf3eb 1028
1029}
1030
1031?>