Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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
 
17
namespace qtype_ordering\output;
18
 
19
use question_attempt;
20
use question_display_options;
21
 
22
/**
23
 * Create the question formulation, controls ready for output.
24
 *
25
 * @package    qtype_ordering
26
 * @copyright  2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class formulation_and_controls extends renderable_base {
30
 
31
    /**
32
     * Construct the rendarable as we also need to pass the question options.
33
     *
34
     * @param question_attempt $qa The question attempt object.
35
     * @param question_display_options $options The question options.
36
     */
37
    public function __construct(
38
        question_attempt $qa,
39
        /** @var question_display_options The question display options. */
40
        protected question_display_options $options
41
    ) {
42
        parent::__construct($qa);
43
    }
44
 
45
    public function export_for_template(\renderer_base $output): array {
46
        global $PAGE;
47
 
48
        $data = [];
49
        $question = $this->qa->get_question();
50
 
51
        $response = $this->qa->get_last_qt_data();
52
        $question->update_current_response($response);
53
 
54
        $currentresponse = $question->currentresponse;
55
        $correctresponse = $question->correctresponse;
56
 
11 efrain 57
        // If we are running behat, force the question into a consistently known state for the sake of avoiding DnD funkyness.
58
        if (defined('BEHAT_SITE_RUNNING')) {
59
            $currentresponse = array_reverse($correctresponse);
60
        }
61
 
1 efrain 62
        // Generate fieldnames and ids.
63
        $responsefieldname = $question->get_response_fieldname();
64
        $responsename = $this->qa->get_qt_field_name($responsefieldname);
65
        $data['questiontext'] = $question->format_questiontext($this->qa);
66
        $data['ablockid'] = 'id_ablock_' . $question->id;
67
        $data['sortableid'] = 'id_sortable_' . $question->id;
68
        $data['responsename'] = $responsename;
69
        $data['responseid'] = 'id_' . preg_replace('/[^a-zA-Z0-9]+/', '_', $responsename);
70
 
71
        // Set CSS classes for sortable list.
72
        if ($class = $question->get_ordering_layoutclass()) {
73
            $data['layoutclass'] = $class;
74
        }
75
        if ($numberingstyle = $question->numberingstyle) {
76
            $data['numberingstyle'] = $numberingstyle;
77
        }
78
 
79
        $data['horizontallayout'] = $question->layouttype == \qtype_ordering_question::LAYOUT_HORIZONTAL;
80
 
81
        // In the multi-tries, the highlight response base on the hint highlight option.
82
        if (
83
            (isset($this->options->highlightresponse) && $this->options->highlightresponse) ||
84
            !$this->qa->get_state()->is_active()
85
        ) {
86
            $data['active'] = false;
87
        } else if ($this->qa->get_state()->is_active()) {
88
            $data['active'] = true;
89
        }
90
 
91
        $data['readonly'] = $this->options->readonly;
92
 
93
        if (count($currentresponse)) {
94
 
95
            // Initialize the cache for the  answers' md5keys
96
            // this represents the initial position of the items.
97
            $md5keys = [];
98
 
99
            // Generate ordering items.
100
            foreach ($currentresponse as $position => $answerid) {
101
 
102
                if (!array_key_exists($answerid, $question->answers) || !array_key_exists($position, $correctresponse)) {
103
                    // @codeCoverageIgnoreStart
104
                    continue; // This shouldn't happen.
105
                    // @codeCoverageIgnoreEnd
106
                }
107
 
108
                // Format the answer text.
109
                $answer = $question->answers[$answerid];
110
                $answertext = $question->format_text($answer->answer, $answer->answerformat,
111
                    $this->qa, 'question', 'answer', $answerid);
112
 
113
                // The original "id" revealed the correct order of the answers
114
                // because $answer->fraction holds the correct order number.
115
                // Therefore, we use the $answer's md5key for the "id".
116
                $answerdata = [
117
                    'answertext' => $answertext,
118
                    'id' => $answer->md5key,
119
                ];
120
 
121
                if ($this->options->correctness === question_display_options::VISIBLE ||
122
                        !empty($this->options->highlightresponse)) {
123
                    $score = $question->get_ordering_item_score($question, $position, $answerid);
124
                    if (isset($score['maxscore'])) {
125
                        $renderer = $PAGE->get_renderer('qtype_ordering');
126
                        $answerdata['feedbackimage'] = $renderer->feedback_image($score['fraction']);
127
                    }
128
                    $answerdata['scoreclass'] = $score['class'];
129
                }
130
 
131
                $data['answers'][] = $answerdata;
132
 
133
                // Cache this answer key.
134
                $md5keys[] = $answer->md5key;
135
            }
136
        }
137
 
138
        $data['value'] = implode(',', $md5keys);
139
 
140
        return $data;
141
    }
142
}