weekly release 2.4dev
[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 38class qbehaviour_adaptive_renderer extends qbehaviour_renderer {
d1b7e03d
TH
39
40 public function controls(question_attempt $qa, question_display_options $options) {
41 return $this->submit_button($qa, $options);
42 }
43
44 public function feedback(question_attempt $qa, question_display_options $options) {
096c3016
HB
45 if ($qa->get_state() == question_state::$invalid) {
46 // If the latest answer was invalid, display an informative message
47 $output = '';
48 $info = $this->disregarded_info();
49 if ($info) {
50 $output = html_writer::tag('div', $info, array('class' => 'gradingdetails'));
51 }
52 return $output;
53 }
54
d1b7e03d
TH
55 // Try to find the last graded step.
56
cc9fc649 57 $gradedstep = $qa->get_behaviour()->get_graded_step($qa);
6d03fd98
TH
58 if (is_null($gradedstep) || $qa->get_max_mark() == 0 ||
59 $options->marks < question_display_options::MARK_AND_MAX) {
d1b7e03d
TH
60 return '';
61 }
62
63 // Display the grading details from the last graded state
0ff4bd08 64 $mark = new stdClass();
d1b7e03d
TH
65 $mark->max = $qa->format_max_mark($options->markdp);
66
67 $actualmark = $gradedstep->get_fraction() * $qa->get_max_mark();
68 $mark->cur = format_float($actualmark, $options->markdp);
69
70 $rawmark = $gradedstep->get_behaviour_var('_rawfraction') * $qa->get_max_mark();
71 $mark->raw = format_float($rawmark, $options->markdp);
72
73 // let student know wether the answer was correct
74 if ($qa->get_state()->is_commented()) {
75 $class = $qa->get_state()->get_feedback_class();
76 } else {
77 $class = question_state::graded_state_for_fraction(
78 $gradedstep->get_behaviour_var('_rawfraction'))->get_feedback_class();
79 }
80
81 $gradingdetails = get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
82
5cc532e8 83 $gradingdetails .= $this->penalty_info($qa, $mark, $options);
d1b7e03d
TH
84
85 $output = '';
86 $output .= html_writer::tag('div', get_string($class, 'question'),
87 array('class' => 'correctness ' . $class));
88 $output .= html_writer::tag('div', $gradingdetails,
89 array('class' => 'gradingdetails'));
90 return $output;
91 }
92
5cc532e8
TH
93 /**
94 * Display the information about the penalty calculations.
95 * @param question_attempt $qa the question attempt.
96 * @param object $mark contains information about the current mark.
97 * @param question_display_options $options display options.
98 */
99 protected function penalty_info(question_attempt $qa, $mark,
100 question_display_options $options) {
4c0b3628
HB
101
102 $currentpenalty = $qa->get_question()->penalty * $qa->get_max_mark();
103 $totalpenalty = $currentpenalty * $qa->get_last_behaviour_var('_try', 0);
104
105 if ($currentpenalty == 0) {
d1b7e03d
TH
106 return '';
107 }
108 $output = '';
109
cc9fc649 110 // Print details of grade adjustment due to penalties
d1b7e03d
TH
111 if ($mark->raw != $mark->cur) {
112 $output .= ' ' . get_string('gradingdetailsadjustment', 'qbehaviour_adaptive', $mark);
113 }
114
cc9fc649
HB
115 // Print information about any new penalty, only relevant if the answer can be improved.
116 if ($qa->get_behaviour()->is_state_improvable($qa->get_state())) {
1631ceea 117 $output .= ' ' . get_string('gradingdetailspenalty', 'qbehaviour_adaptive',
4c0b3628
HB
118 format_float($currentpenalty, $options->markdp));
119
120 // Print information about total penalties so far, if larger than current penalty.
121 if ($totalpenalty > $currentpenalty) {
122 $output .= ' ' . get_string('gradingdetailspenaltytotal', 'qbehaviour_adaptive',
123 format_float($totalpenalty, $options->markdp));
124 }
d1b7e03d
TH
125 }
126
127 return $output;
128 }
096c3016
HB
129
130 /**
131 * Display information about a disregarded (incomplete) response.
132 */
133 protected function disregarded_info() {
134 return get_string('disregardedwithoutpenalty', 'qbehaviour_adaptive');
135 }
136
d1b7e03d 137}