Skip to content

Commit

Permalink
MDL-79874 qtype_ordering: Move feedback into exporter
Browse files Browse the repository at this point in the history
Part of: MDL-79863
  • Loading branch information
Chocolate-lightning committed Apr 4, 2024
1 parent e1357ee commit 9d06092
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 42 deletions.
88 changes: 88 additions & 0 deletions question/type/ordering/classes/output/feedback.php
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace qtype_ordering\output;

use renderer_base;
use question_attempt;
use question_display_options;

/**
* Renderable class for the displaying the feedback.
*
* @package qtype_ordering
* @copyright 2023 Mathew May <mathew.solutions>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback extends renderable_base {

/** @var question_display_options $options The question options. */
protected $options;

/**
* The class constructor.
*
* @param question_attempt $qa The question attempt object.
* @param question_display_options $options Controls what should and should not be displayed via question_display_options but unit tests are fickle.
*/
public function __construct(question_attempt $qa, question_display_options $options) {
parent::__construct($qa);
$this->options = $options;
}

/**
* Export the data for the mustache template.
*
* @param renderer_base $output renderer to be used to render the feedback elements.
* @return array
*/
public function export_for_template(renderer_base $output): array {
global $PAGE;

$data = [];
$question = $this->qa->get_question();
$qtyperenderer = $PAGE->get_renderer('qtype_ordering');

if ($this->options->feedback) {
// Literal render out but we trust the teacher.
$data['specificfeedback'] = $qtyperenderer->specific_feedback($this->qa);

$specificgradedetailfeedback = new specific_grade_detail_feedback($this->qa);
$data['specificgradedetailfeedback'] = $specificgradedetailfeedback->export_for_template($output);

if ($hint = $this->qa->get_applicable_hint()) {
$data['hint'] = $question->format_hint($hint, $this->qa);
}
}

if ($this->options->numpartscorrect) {
$numpartscorrect = new num_parts_correct($this->qa);
$data['numpartscorrect'] = $numpartscorrect->export_for_template($output);
}

if ($this->options->generalfeedback) {
// Literal render out but we trust the teacher.
$data['generalfeedback'] = $question->format_generalfeedback($this->qa);
}

if ($this->options->rightanswer) {
$correctresponse = new correct_response($this->qa);
$data['rightanswer'] = $correctresponse->export_for_template($output);
}

return $data;
}
}
44 changes: 5 additions & 39 deletions question/type/ordering/renderer.php
Expand Up @@ -200,50 +200,15 @@ public function formulation_and_controls(question_attempt $qa, question_display_
* area that contains the various forms of feedback. This function generates
* the content of this area belonging to the question type.
*
* @codeCoverageIgnore This is tested by the feedback exporter.
* @param question_attempt $qa The question attempt to display.
* @param question_display_options $options Controls what should and should not be displayed.
* @return string HTML fragment.
*/
public function feedback(question_attempt $qa, question_display_options $options) {
$output = '';
$hint = null;

$isshownumpartscorrect = true;

if ($options->feedback) {
$output .= html_writer::nonempty_tag('div', $this->specific_feedback($qa),
array('class' => 'specificfeedback'));

if ($options->numpartscorrect) {
$output .= html_writer::nonempty_tag('div', $this->num_parts_correct($qa),
array('class' => 'numpartscorrect'));
$isshownumpartscorrect = false;
}

$output .= $this->specific_grade_detail_feedback($qa);
$hint = $qa->get_applicable_hint();
}

if ($options->numpartscorrect && $isshownumpartscorrect) {
$output .= html_writer::nonempty_tag('div', $this->num_parts_correct($qa),
array('class' => 'numpartscorrect'));
}

if ($hint) {
$output .= $this->hint($qa, $hint);
}

if ($options->generalfeedback) {
$output .= html_writer::nonempty_tag('div', $this->general_feedback($qa),
array('class' => 'generalfeedback'));
}

if ($options->rightanswer) {
$output .= html_writer::nonempty_tag('div', $this->correct_response($qa),
array('class' => 'rightanswer'));
}

return $output;
$feedback = new \qtype_ordering\output\feedback($qa, $options);
return $this->output->render_from_template('qtype_ordering/feedback',
$feedback->export_for_template($this->output));
}

/**
Expand All @@ -262,6 +227,7 @@ public function specific_grade_detail_feedback(question_attempt $qa): string {
* Generate the specific feedback. This is feedback that varies according to
* the response the student gave.
*
* @codeCoverageIgnore This is tested by the feedback exporter.
* @param question_attempt $qa The question attempt to display.
* @return string HTML fragment.
*/
Expand Down
100 changes: 100 additions & 0 deletions question/type/ordering/templates/feedback.mustache
@@ -0,0 +1,100 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template qtype_ordering/feedback
Renders the feedback for the question.
Context variables required for this template:
* specificfeedback - Any specific feedback for the question.
* numpartscorrect - The number of parts correct.
* specificgradedetailfeedback - The specific grade detail feedback.
* hint - The hint for the question.
* generalfeedback - The general feedback for the question.
* rightanswer - The right answer for the question.
Example context (json):
{
"specificfeedback": "Maybe you should try again.",
"numpartscorrect": {
"numcorrect": 1,
"numpartial": 3,
"numincorrect": 0
},
"specificgradedetailfeedback": {
"showpartialwrong": true,
"gradingtype": "Grading type: Absolute position",
"gradedetails": "93",
"orderinglayoutclass": "vertical",
"scoredetails": [
{
"score": "1",
"maxscore": "1",
"percent": "100"
},
{
"score": "0",
"maxscore": "1",
"percent": "0"
}
]
},
"hint": "The cake is a lie.",
"generalfeedback": "Here is some general feedback.",
"rightanswer": {
"hascorrectresponse": true,
"showcorrect": "true",
"orderinglayoutclass": "vertical",
"correctanswers": [
{
"answertext": "Correct answer 1"
},
{
"answertext": "Correct answer 2"
}
]
}
}
}}
{{#specificfeedback}}
<div class="specificfeedback">
{{{.}}}
</div>
{{/specificfeedback}}
{{#numpartscorrect}}
<div class="numpartscorrect">
{{>qtype_ordering/num_parts_correct}}
</div>
{{/numpartscorrect}}
{{#specificgradedetailfeedback}}
{{>qtype_ordering/specific_grade_detail_feedback}}
{{/specificgradedetailfeedback}}
{{#hint}}
<div class="hint">
{{{.}}}
</div>
{{/hint}}
{{#generalfeedback}}
<div class="generalfeedback">
{{{.}}}
</div>
{{/generalfeedback}}
{{#rightanswer}}
<div class="rightanswer">
{{>qtype_ordering/correct_response}}
</div>
{{/rightanswer}}
6 changes: 3 additions & 3 deletions question/type/ordering/tests/behat/preview.feature
Expand Up @@ -33,7 +33,7 @@ Feature: Preview an Ordering question
And I drag "Dynamic" to space "4" in the ordering question
And I drag "Learning" to space "5" in the ordering question
And I press "Submit and finish"
Then the state of "Put these words in order." question is shown as "Correct"
Then I should see "Correct items: 6"
And I should see "Mark 1.00 out of 1.00"

@javascript
Expand All @@ -48,8 +48,8 @@ Feature: Preview an Ordering question
And I drag "Learning" to space "5" in the ordering question
And I drag "Environment" to space "2" in the ordering question
And I press "Submit and finish"
And I should see "You have 1 item correct."
And I should see "You have 5 items partially correct."
And I should see "Correct items: 1"
And I should see "Partially correct items: 5"

@javascript
Scenario: Preview an Ordering question with no show number of correct option.
Expand Down

0 comments on commit 9d06092

Please sign in to comment.