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