on-demand release 2.1beta
[moodle.git] / question / behaviour / adaptive / renderer.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d1b7e03d
TH
17/**
18 * Renderer for outputting parts of a question belonging to the legacy
19 * adaptive behaviour.
20 *
017bc1d9
TH
21 * @package qbehaviour
22 * @subpackage adaptive
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
25 */
26
017bc1d9 27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
017bc1d9
TH
31/**
32 * Renderer for outputting parts of a question belonging to the legacy
33 * adaptive behaviour.
34 *
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
d1b7e03d
TH
38class qbehaviour_adaptive_renderer extends qbehaviour_renderer {
39 protected function get_graded_step(question_attempt $qa) {
40 foreach ($qa->get_reverse_step_iterator() as $step) {
41 if ($step->has_behaviour_var('_try')) {
42 return $step;
43 }
44 }
45 }
46
47 public function controls(question_attempt $qa, question_display_options $options) {
48 return $this->submit_button($qa, $options);
49 }
50
51 public function feedback(question_attempt $qa, question_display_options $options) {
52 // Try to find the last graded step.
53
54 $gradedstep = $this->get_graded_step($qa);
6d03fd98
TH
55 if (is_null($gradedstep) || $qa->get_max_mark() == 0 ||
56 $options->marks < question_display_options::MARK_AND_MAX) {
d1b7e03d
TH
57 return '';
58 }
59
60 // Display the grading details from the last graded state
0ff4bd08 61 $mark = new stdClass();
d1b7e03d
TH
62 $mark->max = $qa->format_max_mark($options->markdp);
63
64 $actualmark = $gradedstep->get_fraction() * $qa->get_max_mark();
65 $mark->cur = format_float($actualmark, $options->markdp);
66
67 $rawmark = $gradedstep->get_behaviour_var('_rawfraction') * $qa->get_max_mark();
68 $mark->raw = format_float($rawmark, $options->markdp);
69
70 // let student know wether the answer was correct
71 if ($qa->get_state()->is_commented()) {
72 $class = $qa->get_state()->get_feedback_class();
73 } else {
74 $class = question_state::graded_state_for_fraction(
75 $gradedstep->get_behaviour_var('_rawfraction'))->get_feedback_class();
76 }
77
78 $gradingdetails = get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
79
80 $gradingdetails .= $this->penalty_info($qa, $mark);
81
82 $output = '';
83 $output .= html_writer::tag('div', get_string($class, 'question'),
84 array('class' => 'correctness ' . $class));
85 $output .= html_writer::tag('div', $gradingdetails,
86 array('class' => 'gradingdetails'));
87 return $output;
88 }
89
90 protected function penalty_info($qa, $mark) {
91 if (!$qa->get_question()->penalty) {
92 return '';
93 }
94 $output = '';
95
96 // print details of grade adjustment due to penalties
97 if ($mark->raw != $mark->cur) {
98 $output .= ' ' . get_string('gradingdetailsadjustment', 'qbehaviour_adaptive', $mark);
99 }
100
101 // print info about new penalty
102 // penalty is relevant only if the answer is not correct and further attempts are possible
103 if (!$qa->get_state()->is_finished()) {
1631ceea
TH
104 $output .= ' ' . get_string('gradingdetailspenalty', 'qbehaviour_adaptive',
105 $qa->get_question()->penalty);
d1b7e03d
TH
106 }
107
108 return $output;
109 }
110}