weekly release 2.3dev
[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) {
45 // Try to find the last graded step.
46
cc9fc649 47 $gradedstep = $qa->get_behaviour()->get_graded_step($qa);
6d03fd98
TH
48 if (is_null($gradedstep) || $qa->get_max_mark() == 0 ||
49 $options->marks < question_display_options::MARK_AND_MAX) {
d1b7e03d
TH
50 return '';
51 }
52
53 // Display the grading details from the last graded state
0ff4bd08 54 $mark = new stdClass();
d1b7e03d
TH
55 $mark->max = $qa->format_max_mark($options->markdp);
56
57 $actualmark = $gradedstep->get_fraction() * $qa->get_max_mark();
58 $mark->cur = format_float($actualmark, $options->markdp);
59
60 $rawmark = $gradedstep->get_behaviour_var('_rawfraction') * $qa->get_max_mark();
61 $mark->raw = format_float($rawmark, $options->markdp);
62
63 // let student know wether the answer was correct
64 if ($qa->get_state()->is_commented()) {
65 $class = $qa->get_state()->get_feedback_class();
66 } else {
67 $class = question_state::graded_state_for_fraction(
68 $gradedstep->get_behaviour_var('_rawfraction'))->get_feedback_class();
69 }
70
71 $gradingdetails = get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
72
5cc532e8 73 $gradingdetails .= $this->penalty_info($qa, $mark, $options);
d1b7e03d
TH
74
75 $output = '';
76 $output .= html_writer::tag('div', get_string($class, 'question'),
77 array('class' => 'correctness ' . $class));
78 $output .= html_writer::tag('div', $gradingdetails,
79 array('class' => 'gradingdetails'));
80 return $output;
81 }
82
5cc532e8
TH
83 /**
84 * Display the information about the penalty calculations.
85 * @param question_attempt $qa the question attempt.
86 * @param object $mark contains information about the current mark.
87 * @param question_display_options $options display options.
88 */
89 protected function penalty_info(question_attempt $qa, $mark,
90 question_display_options $options) {
d1b7e03d
TH
91 if (!$qa->get_question()->penalty) {
92 return '';
93 }
94 $output = '';
95
cc9fc649 96 // Print details of grade adjustment due to penalties
d1b7e03d
TH
97 if ($mark->raw != $mark->cur) {
98 $output .= ' ' . get_string('gradingdetailsadjustment', 'qbehaviour_adaptive', $mark);
99 }
100
cc9fc649
HB
101 // Print information about any new penalty, only relevant if the answer can be improved.
102 if ($qa->get_behaviour()->is_state_improvable($qa->get_state())) {
1631ceea 103 $output .= ' ' . get_string('gradingdetailspenalty', 'qbehaviour_adaptive',
5cc532e8 104 format_float($qa->get_question()->penalty, $options->markdp));
d1b7e03d
TH
105 }
106
107 return $output;
108 }
109}