Merge branch 'MDL-61971-master' of git://github.com/mickhawkins/moodle
[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) {
a3e6ad77
TH
45
46 // If the latest answer was invalid, display an informative message.
096c3016 47 if ($qa->get_state() == question_state::$invalid) {
a3e6ad77
TH
48 return html_writer::nonempty_tag('div', $this->disregarded_info(),
49 array('class' => 'gradingdetails'));
096c3016
HB
50 }
51
a3e6ad77
TH
52 // Otherwise get the details.
53 return $this->render_adaptive_marks(
54 $qa->get_behaviour()->get_adaptive_marks(), $options);
55 }
d1b7e03d 56
a3e6ad77
TH
57 /**
58 * Display the scoring information about an adaptive attempt.
59 * @param qbehaviour_adaptive_mark_details contains all the score details we need.
60 * @param question_display_options $options display options.
61 */
62 public function render_adaptive_marks(qbehaviour_adaptive_mark_details $details, question_display_options $options) {
63 if ($details->state == question_state::$todo || $options->marks < question_display_options::MARK_AND_MAX) {
64 // No grades yet.
d1b7e03d
TH
65 return '';
66 }
67
a3e6ad77
TH
68 // Display the grading details from the last graded state.
69 $class = $details->state->get_feedback_class();
70 return html_writer::tag('div', get_string($class, 'question'),
71 array('class' => 'correctness ' . $class))
72 . html_writer::tag('div', $this->grading_details($details, $options),
73 array('class' => 'gradingdetails'));
d1b7e03d
TH
74 }
75
5cc532e8
TH
76 /**
77 * Display the information about the penalty calculations.
a3e6ad77 78 * @param qbehaviour_adaptive_mark_details contains all the score details we need.
5cc532e8 79 * @param question_display_options $options display options.
a3e6ad77 80 * @return string html fragment
5cc532e8 81 */
a3e6ad77 82 protected function grading_details(qbehaviour_adaptive_mark_details $details, question_display_options $options) {
4c0b3628 83
a3e6ad77 84 $mark = $details->get_formatted_marks($options->markdp);
4c0b3628 85
a3e6ad77
TH
86 if ($details->currentpenalty == 0 && $details->totalpenalty == 0) {
87 return get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
d1b7e03d 88 }
a3e6ad77 89
d1b7e03d
TH
90 $output = '';
91
cc9fc649 92 // Print details of grade adjustment due to penalties
a3e6ad77
TH
93 if ($details->rawmark != $details->actualmark) {
94 if (!$details->improvable) {
95 return get_string('gradingdetailswithadjustment', 'qbehaviour_adaptive', $mark);
96 } else if ($details->totalpenalty > $details->currentpenalty) {
97 return get_string('gradingdetailswithadjustmenttotalpenalty', 'qbehaviour_adaptive', $mark);
98 } else {
99 return get_string('gradingdetailswithadjustmentpenalty', 'qbehaviour_adaptive', $mark);
100 }
4c0b3628 101
a3e6ad77
TH
102 } else {
103 if (!$details->improvable) {
104 return get_string('gradingdetails', 'qbehaviour_adaptive', $mark);
105 } else if ($details->totalpenalty > $details->currentpenalty) {
106 return get_string('gradingdetailswithtotalpenalty', 'qbehaviour_adaptive', $mark);
107 } else {
108 return get_string('gradingdetailswithpenalty', 'qbehaviour_adaptive', $mark);
4c0b3628 109 }
d1b7e03d
TH
110 }
111
112 return $output;
113 }
096c3016
HB
114
115 /**
116 * Display information about a disregarded (incomplete) response.
117 */
118 protected function disregarded_info() {
119 return get_string('disregardedwithoutpenalty', 'qbehaviour_adaptive');
120 }
d1b7e03d 121}