MDL-20636 Add @package and GPL boiler-plate to files in /question.
[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
18
19/**
20 * Renderer for outputting parts of a question belonging to the legacy
21 * adaptive behaviour.
22 *
23 * @package qbehaviour_adaptive
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28class qbehaviour_adaptive_renderer extends qbehaviour_renderer {
29 protected function get_graded_step(question_attempt $qa) {
30 foreach ($qa->get_reverse_step_iterator() as $step) {
31 if ($step->has_behaviour_var('_try')) {
32 return $step;
33 }
34 }
35 }
36
37 public function controls(question_attempt $qa, question_display_options $options) {
38 return $this->submit_button($qa, $options);
39 }
40
41 public function feedback(question_attempt $qa, question_display_options $options) {
42 // Try to find the last graded step.
43
44 $gradedstep = $this->get_graded_step($qa);
45 if (is_null($gradedstep) || $qa->get_max_mark() == 0 || !$options->marks) {
46 return '';
47 }
48
49 // Display the grading details from the last graded state
0ff4bd08 50 $mark = new stdClass();
d1b7e03d
TH
51 $mark->max = $qa->format_max_mark($options->markdp);
52
53 $actualmark = $gradedstep->get_fraction() * $qa->get_max_mark();
54 $mark->cur = format_float($actualmark, $options->markdp);
55
56 $rawmark = $gradedstep->get_behaviour_var('_rawfraction') * $qa->get_max_mark();
57 $mark->raw = format_float($rawmark, $options->markdp);
58
59 // let student know wether the answer was correct
60 if ($qa->get_state()->is_commented()) {
61 $class = $qa->get_state()->get_feedback_class();
62 } else {
63 $class = question_state::graded_state_for_fraction(
64 $gradedstep->get_behaviour_var('_rawfraction'))->get_feedback_class();
65 }
66
67 $gradingdetails = get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
68
69 $gradingdetails .= $this->penalty_info($qa, $mark);
70
71 $output = '';
72 $output .= html_writer::tag('div', get_string($class, 'question'),
73 array('class' => 'correctness ' . $class));
74 $output .= html_writer::tag('div', $gradingdetails,
75 array('class' => 'gradingdetails'));
76 return $output;
77 }
78
79 protected function penalty_info($qa, $mark) {
80 if (!$qa->get_question()->penalty) {
81 return '';
82 }
83 $output = '';
84
85 // print details of grade adjustment due to penalties
86 if ($mark->raw != $mark->cur) {
87 $output .= ' ' . get_string('gradingdetailsadjustment', 'qbehaviour_adaptive', $mark);
88 }
89
90 // print info about new penalty
91 // penalty is relevant only if the answer is not correct and further attempts are possible
92 if (!$qa->get_state()->is_finished()) {
93 $output .= ' ' . get_string('gradingdetailspenalty', 'qbehaviour_adaptive', $qa->get_question()->penalty);
94 }
95
96 return $output;
97 }
98}