• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FormulasQuestion / moodle-qtype_formulas / 17019138792

17 Aug 2025 09:01AM UTC coverage: 97.399% (-0.2%) from 97.629%
17019138792

Pull #264

github

web-flow
Merge f09dd629a into c107cc5e2
Pull Request #264: Allow parts to have empty fields

78 of 92 new or added lines in 10 files covered. (84.78%)

12 existing lines in 5 files now uncovered.

4381 of 4498 relevant lines covered (97.4%)

1618.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.49
/renderer.php
1
<?php
2
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16

17
/**
18
 * Formulas question renderer class.
19
 *
20
 * @package    qtype_formulas
21
 * @copyright  2009 The Open University
22
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24

25
/**
26
 * Base class for generating the bits of output for formulas questions.
27
 *
28
 * @copyright  2009 The Open University
29
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class qtype_formulas_renderer extends qtype_with_combined_feedback_renderer {
32

33
    /** @var string */
34
    const UNIT_FIELD = 'u';
35

36
    /** @var string */
37
    const COMBINED_FIELD = '';
38

39
    /**
40
     * Generate the display of the formulation part of the question. This is the area that
41
     * contains the question text and the controls for students to input their answers.
42
     * Once the question is answered, it will contain the green tick or the red cross and
43
     * the part's general / combined feedback.
44
     *
45
     * @param question_attempt $qa the question attempt to display.
46
     * @param question_display_options $options controls what should and should not be displayed.
47
     * @return ?string HTML fragment.
48
     */
49
    public function formulation_and_controls(question_attempt $qa, question_display_options $options): ?string {
50
        // First, fetch the instantiated question from the attempt.
51
        /** @var qtype_formulas_question $question */
52
        $question = $qa->get_question();
1,722✔
53

54
        if (count($question->textfragments) !== $question->numparts + 1) {
1,722✔
55
            $this->output->notification(get_string('error_question_damaged', 'qtype_formulas'), 'error');
×
56
            return null;
×
57
        }
58

59
        $questiontext = '';
1,722✔
60
        // First, iterate over all parts, put the corresponding fragment of the main question text at the
61
        // right position, followed by the part's text, input and (if applicable) feedback elements.
62
        foreach ($question->parts as $part) {
1,722✔
63
            $questiontext .= $question->format_text(
1,722✔
64
                $question->textfragments[$part->partindex], $question->questiontextformat,
1,722✔
65
                $qa, 'question', 'questiontext', $question->id, false
1,722✔
66
            );
1,722✔
67
            $questiontext .= $this->part_formulation_and_controls($qa, $options, $part);
1,722✔
68
        }
69
        // All parts are done. We now append the final fragment of the main question text. Note that this fragment
70
        // might be empty.
71
        $questiontext .= $question->format_text(
1,722✔
72
            end($question->textfragments), $question->questiontextformat, $qa, 'question', 'questiontext', $question->id, false
1,722✔
73
        );
1,722✔
74

75
        // Pack everything in a <div> and, if the question is in an invalid state, append the appropriate error message
76
        // at the very end.
77
        $result = html_writer::tag('div', $questiontext, ['class' => 'qtext']);
1,722✔
78
        if ($qa->get_state() == question_state::$invalid) {
1,722✔
79
            $result .= html_writer::nonempty_tag(
42✔
80
                'div',
42✔
81
                $question->get_validation_error($qa->get_last_qt_data()),
42✔
82
                ['class' => 'validationerror']
42✔
83
            );
42✔
84
        }
85

86
        return $result;
1,722✔
87
    }
88

89
    /**
90
     * Return HTML that needs to be included in the page's <head> when this
91
     * question is used.
92
     *
93
     * @param question_attempt $qa question attempt that will be displayed on the page
94
     * @return string HTML fragment
95
     */
96
    public function head_code(question_attempt $qa): string {
97
        global $CFG;
98
        $this->page->requires->js_call_amd('qtype_formulas/answervalidation', 'init');
×
99

100
        // Include backwards-compatibility layer for Bootstrap 4 data attributes, if available.
101
        // We may safely assume that if the uncompiled version is there, the minified one exists as well.
102
        if (file_exists($CFG->dirroot . '/theme/boost/amd/src/bs4-compat.js')) {
×
103
            $this->page->requires->js_call_amd('theme_boost/bs4-compat', 'init');
×
104
        }
105

106
        return '';
×
107
    }
108

109
    /**
110
     * Return the part text, controls, grading details and feedbacks.
111
     *
112
     * @param question_attempt $qa question attempt that will be displayed on the page
113
     * @param question_display_options $options controls what should and should not be displayed
114
     * @param qtype_formulas_part $part question part
115
     * @return void
116
     */
117
    public function part_formulation_and_controls(question_attempt $qa, question_display_options $options,
118
            qtype_formulas_part $part): string {
119

120
        // The behaviour might change the display options per part, so it is safer to clone them here.
121
        $partoptions = clone $options;
1,722✔
122
        if ($qa->get_behaviour_name() === 'adaptivemultipart') {
1,722✔
123
            $qa->get_behaviour()->adjust_display_options_for_part($part->partindex, $partoptions);
105✔
124
        }
125

126
        // Fetch information about the outcome: grade, feedback symbol, CSS class to be used.
127
        $outcomedata = $this->get_part_feedback_class_and_symbol($qa, $partoptions, $part);
1,722✔
128

129
        // First of all, we take the part's question text and its input fields.
130
        $output = $this->get_part_formulation($qa, $partoptions, $part, $outcomedata);
1,722✔
131

132
        // If the user has requested the feedback symbol to be placed at a special position, we
133
        // do that now. Otherwise, we just append it after the part's text and input boxes.
134
        if (strpos($output, '{_m}') !== false) {
1,722✔
135
            $output = str_replace('{_m}', $outcomedata->feedbacksymbol, $output);
×
136
        } else {
137
            $output .= $outcomedata->feedbacksymbol;
1,722✔
138
        }
139

140
        // The part's feedback consists of the combined feedback (correct, partially correct, incorrect -- depending on the
141
        // outcome) and the general feedback which is given in all cases.
142
        $feedback = $this->part_combined_feedback($qa, $partoptions, $part, $outcomedata->fraction);
1,722✔
143
        $feedback .= $this->part_general_feedback($qa, $partoptions, $part);
1,722✔
144

145
        // If requested, the correct answer should be appended to the feedback.
146
        if ($partoptions->rightanswer) {
1,722✔
147
            $feedback .= $this->part_correct_response($part);
378✔
148
        }
149

150
        // Put all feedback into a <div> with the appropriate CSS class and append it to the output.
151
        $output .= html_writer::nonempty_tag('div', $feedback, ['class' => 'formulaspartoutcome outcome']);
1,722✔
152

153
        return html_writer::tag('div', $output , ['class' => 'formulaspart']);
1,722✔
154
    }
155

156
    /**
157
     * Return class and symbol for the part feedback.
158
     *
159
     * @param question_attempt $qa question attempt that will be displayed on the page
160
     * @param question_display_options $options controls what should and should not be displayed
161
     * @param qtype_formulas_part $part question part
162
     * @return stdClass
163
     */
164
    public function get_part_feedback_class_and_symbol(question_attempt $qa, question_display_options $options,
165
            qtype_formulas_part $part): stdClass {
166
        // Prepare a new object to hold the different elements.
167
        $result = new stdClass;
1,722✔
168

169
        // Fetch the last response data and grade it.
170
        $response = $qa->get_last_qt_data();
1,722✔
171
        list('answer' => $answergrade, 'unit' => $unitcorrect) = $part->grade($response);
1,722✔
172

173
        // The fraction will later be used to determine which feedback (correct, partially correct or incorrect)
174
        // to use. We have to take into account a possible deduction for a wrong unit.
175
        $result->fraction = $answergrade;
1,722✔
176
        if ($unitcorrect === false) {
1,722✔
177
            $result->fraction *= (1 - $part->unitpenalty);
735✔
178
        }
179

180
        // By default, we add no feedback at all...
181
        $result->feedbacksymbol = '';
1,722✔
182
        $result->feedbackclass = '';
1,722✔
183
        // ... unless correctness is requested in the display options.
184
        if ($options->correctness) {
1,722✔
185
            $result->feedbacksymbol = $this->feedback_image($result->fraction);
462✔
186
            $result->feedbackclass = $this->feedback_class($result->fraction);
462✔
187
        }
188
        return $result;
1,722✔
189
    }
190

191
    /**
192
     * Format given number according to numbering style, e. g. abc or 123.
193
     *
194
     * @param int $num number
195
     * @param string $style style to render the number in, acccording to {@see qtype_multichoice::get_numbering_styles()}
196
     * @return string number $num in the requested style
197
     */
198
    protected static function number_in_style(int $num, string $style): string {
199
        switch ($style) {
200
            case 'abc':
63✔
201
                $number = chr(ord('a') + $num);
42✔
202
                break;
42✔
203
            case 'ABCD':
21✔
204
                $number = chr(ord('A') + $num);
×
205
                break;
×
206
            case '123':
21✔
207
                $number = $num + 1;
×
208
                break;
×
209
            case 'iii':
21✔
210
                $number = question_utils::int_to_roman($num + 1);
×
211
                break;
×
212
            case 'IIII':
21✔
213
                $number = strtoupper(question_utils::int_to_roman($num + 1));
×
214
                break;
×
215
            case 'none':
21✔
216
                return '';
×
217
            default:
218
                // Default similar to none for compatibility with old questions.
219
                return '';
21✔
220
        }
221
        return $number . '. ';
42✔
222
    }
223

224
    /**
225
     * Create a set of radio boxes for a multiple choice answer input.
226
     *
227
     * @param qtype_formulas_part $part question part
228
     * @param int|string $answerindex index of the answer (starting at 0) or special value for combined/separate unit field
229
     * @param question_attempt $qa question attempt that will be displayed on the page
230
     * @param array $answeroptions array of strings containing the answer options to choose from
231
     * @param bool $shuffle whether the options should be shuffled
232
     * @param question_display_options $displayoptions controls what should and should not be displayed
233
     * @param string $feedbackclass
234
     * @return string HTML fragment
235
     */
236
    protected function create_radio_mc_answer(qtype_formulas_part $part, $answerindex, question_attempt $qa,
237
            array $answeroptions, bool $shuffle, question_display_options $displayoptions, string $feedbackclass = ''): string {
238
        /** @var qype_formulas_question $question */
239
        $question = $qa->get_question();
63✔
240

241
        $variablename = "{$part->partindex}_{$answerindex}";
63✔
242
        $currentanswer = $qa->get_last_qt_var($variablename);
63✔
243
        $inputname = $qa->get_qt_field_name($variablename);
63✔
244

245
        $inputattributes['type'] = 'radio';
63✔
246
        $inputattributes['name'] = $inputname;
63✔
247
        if ($displayoptions->readonly) {
63✔
248
            $inputattributes['disabled'] = 'disabled';
21✔
249
        }
250

251
        // First, we open a <fieldset> around the entire group of options.
252
        $output = html_writer::start_tag('fieldset', ['class' => 'multichoice_answer']);
63✔
253

254
        // Inside the fieldset, we put the accessibility label, following the example of core's multichoice
255
        // question type, i. e. the label is inside a <span> with class 'sr-only', wrapped in a <legend>.
256
        $output .= html_writer::start_tag('legend', ['class' => 'sr-only']);
63✔
257
        $output .= html_writer::span(
63✔
258
            $this->generate_accessibility_label_text($answerindex, $part->numbox, $part->partindex, $question->numparts),
63✔
259
            'sr-only'
63✔
260
        );
63✔
261
        $output .= html_writer::end_tag('legend');
63✔
262

263
        // If needed, shuffle the options while maintaining the keys.
264
        if ($shuffle) {
63✔
265
            $keys = array_keys($answeroptions);
21✔
266
            shuffle($keys);
21✔
267

268
            $shuffledoptions = [];
21✔
269
            foreach ($keys as $key) {
21✔
270
                $shuffledoptions[$key] = $answeroptions[$key];
21✔
271
            }
272
            $answeroptions = $shuffledoptions;
21✔
273
        }
274

275
        // Iterate over all options.
276
        foreach ($answeroptions as $i => $optiontext) {
63✔
277
            $numbering = html_writer::span(self::number_in_style($i, $question->answernumbering), 'answernumber');
63✔
278
            $labeltext = $question->format_text(
63✔
279
                $numbering . $optiontext, $part->subqtextformat , $qa, 'qtype_formulas', 'answersubqtext', $part->id, false
63✔
280
            );
63✔
281

282
            $inputattributes['id'] = $inputname . '_' . $i;
63✔
283
            $inputattributes['value'] = $i;
63✔
284
            // Class ml-3 is Bootstrap's class for margin-left: 1rem; it used to be m-l-1.
285
            $label = $this->create_label_for_input($labeltext, $inputattributes['id'], ['class' => 'ml-3']);
63✔
286
            $inputattributes['aria-labelledby'] = $label['id'];
63✔
287

288
            // We must make sure $currentanswer is not null, because otherwise the first radio box
289
            // might be selected if there is no answer at all. It seems better to avoid strict equality,
290
            // because we might compare a string to a number.
291
            $isselected = ($i == $currentanswer && !is_null($currentanswer));
63✔
292

293
            // We do not reset the $inputattributes array on each iteration, so we have to add/remove the
294
            // attribute every time.
295
            if ($isselected) {
63✔
296
                $inputattributes['checked'] = 'checked';
21✔
297
            } else {
298
                unset($inputattributes['checked']);
63✔
299
            }
300

301
            // Each option (radio box element plus label) is wrapped in its own <div> element.
302
            $divclass = 'r' . ($i % 2);
63✔
303
            if ($displayoptions->correctness && $isselected) {
63✔
304
                $divclass .= ' ' . $feedbackclass;
21✔
305
            }
306
            $output .= html_writer::start_div($divclass);
63✔
307

308
            // Now add the <input> tag and its <label>.
309
            $output .= html_writer::empty_tag('input', $inputattributes);
63✔
310
            $output .= $label['html'];
63✔
311

312
            // Close the option's <div>.
313
            $output .= html_writer::end_div();
63✔
314
        }
315

316
        // Close the option group's <fieldset>.
317
        $output .= html_writer::end_tag('fieldset');
63✔
318

319
        return $output;
63✔
320
    }
321

322
    /**
323
     * Translate an array containing formatting options into a CSS format string, e. g. from
324
     * ['w' => '50px', 'bgcol' => 'yellow'] to 'width: 50px; background-color: yellow'. Note:
325
     * - colors can be defined in 3 or 6 digit hex RGB, in 4 or 8 digit hex RGBA or as CSS named color
326
     * - widths can be defined as a number followed by the units px, rem or em; if the unit is omitted, rem will be used
327
     * - alignment can be defined as left, right, center, start or end
328
     *
329
     * @param array $options associative array containing options (in our own denomination) and their settings
330
     * @return string
331
     */
332
    protected function get_css_properties(array $options): string {
333
        // Define some regex pattern.
334
        $hexcolor = '#([0-9A-F]{8}|[0-9A-F]{6}|[0-9A-F]{3}|[0-9A-F]{4})';
1,638✔
335
        $namedcolor = '[A-Z]+';
1,638✔
336
        // We accept floating point numbers with or without a leading integer part and integers.
337
        // Floating point numbers with a trailing decimal point do not work in all browsers.
338
        $length = '(\d+\.\d+|\d*\.\d+|\d+)(px|em|rem)?';
1,638✔
339
        $alignment = 'start|end|left|right|center';
1,638✔
340

341
        $styles = [];
1,638✔
342
        foreach ($options as $name => $value) {
1,638✔
343
            switch ($name) {
344
                case 'bgcol':
1,638✔
345
                    if (!preg_match("/^(($hexcolor)|($namedcolor))$/i", $value)) {
252✔
346
                        break;
21✔
347
                    }
348
                    $styles[] = "background-color: $value";
231✔
349
                    break;
231✔
350
                case 'txtcol':
1,638✔
351
                    if (!preg_match("/^(($hexcolor)|($namedcolor))$/i", $value)) {
126✔
352
                        break;
21✔
353
                    }
354
                    $styles[] = "color: $value";
105✔
355
                    break;
105✔
356
                case 'w':
1,638✔
357
                    if (!preg_match("/^($length)$/i", $value)) {
1,638✔
358
                        break;
84✔
359
                    }
360
                    // If no unit is given, append rem.
361
                    $styles[] = "width: $value" . (preg_match('/\d$/', $value) ? 'rem' : '');
1,554✔
362
                    break;
1,554✔
363
                case 'align':
126✔
364
                    if (!preg_match("/^($alignment)$/i", $value)) {
126✔
365
                        break;
21✔
366
                    }
367
                    $styles[] = "text-align: $value";
105✔
368
                    break;
105✔
369
            }
370
        }
371

372
        return implode(';', $styles);
1,638✔
373
    }
374

375
    /**
376
     * Create a <label> element for a given input control (e. g. a text field). Returns the
377
     * HTML and the label's ID.
378
     *
379
     * @param string $text the label's text
380
     * @param string $inputid ID of the input control for which the label is created
381
     * @param array $additionalattributes possibility to add custom attributes, attribute name => value
382
     * @return array 'id' => label's ID to be used in 'aria-labelledby' attribute, 'html' => HTML code
383
     */
384
    protected function create_label_for_input(string $text, string $inputid, array $additionalattributes = []): array {
385
        $labelid = 'lbl_' . str_replace(':', '__', $inputid);
1,722✔
386
        $attributes = [
1,722✔
387
            'class' => 'subq accesshide',
1,722✔
388
            'for' => $inputid,
1,722✔
389
            'id' => $labelid,
1,722✔
390
        ];
1,722✔
391

392
        // Merging the additional attributes with the default attributes; left array has precedence when
393
        // using the + operator.
394
        $attributes = $additionalattributes + $attributes;
1,722✔
395

396
        return [
1,722✔
397
            'id' => $labelid,
1,722✔
398
            'html' => html_writer::tag('label', $text, $attributes),
1,722✔
399
        ];
1,722✔
400
    }
401

402
    /**
403
     * Create a <select> field for a multiple choice answer input.
404
     *
405
     * @param qtype_formulas_part $part question part
406
     * @param int|string $answerindex index of the answer (starting at 0) or special value for combined/separate unit field
407
     * @param question_attempt $qa question attempt that will be displayed on the page
408
     * @param array $answeroptions array of strings containing the answer options to choose from
409
     * @param bool $shuffle whether the options should be shuffled
410
     * @param question_display_options $displayoptions controls what should and should not be displayed
411
     * @return string HTML fragment
412
     */
413
    protected function create_dropdown_mc_answer(qtype_formulas_part $part, $answerindex, question_attempt $qa,
414
            array $answeroptions, bool $shuffle, question_display_options $displayoptions): string {
415
        /** @var qype_formulas_question $question */
416
        $question = $qa->get_question();
63✔
417

418
        $variablename = "{$part->partindex}_{$answerindex}";
63✔
419
        $currentanswer = $qa->get_last_qt_var($variablename);
63✔
420
        $inputname = $qa->get_qt_field_name($variablename);
63✔
421

422
        $inputattributes['name'] = $inputname;
63✔
423
        $inputattributes['value'] = $currentanswer;
63✔
424
        $inputattributes['id'] = $inputname;
63✔
425
        $inputattributes['class'] = 'formulas-select';
63✔
426

427
        $label = $this->create_label_for_input(
63✔
428
            $this->generate_accessibility_label_text($answerindex, $part->numbox, $part->partindex, $question->numparts),
63✔
429
            $inputname
63✔
430
        );
63✔
431
        $inputattributes['aria-labelledby'] = $label['id'];
63✔
432

433
        if ($displayoptions->readonly) {
63✔
434
            $inputattributes['disabled'] = 'disabled';
21✔
435
        }
436

437
        // First, we open a <span> around the dropdown field and its accessibility label.
438
        $output = html_writer::start_tag('span', ['class' => 'formulas_menu']);
63✔
439
        $output .= $label['html'];
63✔
440

441
        // Iterate over all options.
442
        $entries = [];
63✔
443
        foreach ($answeroptions as $optiontext) {
63✔
444
            $entries[] = $question->format_text(
63✔
445
                $optiontext, $part->subqtextformat , $qa, 'qtype_formulas', 'answersubqtext', $part->id, false
63✔
446
            );
63✔
447
        }
448

449
        // If needed, shuffle the options while maintaining the keys.
450
        if ($shuffle) {
63✔
451
            $keys = array_keys($entries);
21✔
452
            shuffle($keys);
21✔
453

454
            $shuffledentries = [];
21✔
455
            foreach ($keys as $key) {
21✔
456
                $shuffledentries[$key] = $entries[$key];
21✔
457
            }
458
            $entries = $shuffledentries;
21✔
459
        }
460

461
        $output .= html_writer::select($entries, $inputname, $currentanswer, ['' => ''], $inputattributes);
63✔
462
        $output .= html_writer::end_tag('span');
63✔
463

464
        return $output;
63✔
465
    }
466

467
    /**
468
     * Generate the right label for the input control, depending on the number of answers in the given part
469
     * and the number of parts in the question. Special cases (combined field, unit field) are also taken
470
     * into account. Returns the appropriate string from the language file. Examples are "Answer field for
471
     * part X", "Answer field X for part Y" or "Answer and unit for part X".
472
     *
473
     * @param int|string $answerindex index of the answer (starting at 0) or special value for combined/separate unit field
474
     * @param int $totalanswers number of answers for the given part
475
     * @param int $partindex number of the part (starting at 0) in this question
476
     * @param int $totalparts number of parts in the question
477
     * @return string localized string
478
     */
479
    protected function generate_accessibility_label_text($answerindex, int $totalanswers, int $partindex,
480
            int $totalparts): string {
481

482
        // Some language strings need parameters.
483
        $labeldata = new stdClass();
1,722✔
484

485
        // The language strings start with 'answerunit' for a separate unit field, 'answercombinedunit' for
486
        // a combined field, 'answercoordinate' for an answer field when there are multiple answers in the
487
        // part or just 'answer' if there is a single field.
488
        $labelstring = 'answer';
1,722✔
489
        if ($answerindex === self::UNIT_FIELD) {
1,722✔
490
            $labelstring .= 'unit';
147✔
491
        } else if ($answerindex === self::COMBINED_FIELD) {
1,722✔
492
            $labelstring .= 'combinedunit';
672✔
493
        } else if ($totalanswers > 1) {
1,449✔
494
            $labelstring .= 'coordinate';
63✔
495
            $labeldata->numanswer = $answerindex + 1;
63✔
496
        }
497

498
        // The language strings end with 'multi' for multi-part questions or 'single' for single-part
499
        // questions.
500
        if ($totalparts > 1) {
1,722✔
501
            $labelstring .= 'multi';
105✔
502
            $labeldata->part = $partindex + 1;
105✔
503
        } else {
504
            $labelstring .= 'single';
1,659✔
505
        }
506

507
        return get_string($labelstring, 'qtype_formulas', $labeldata);
1,722✔
508
    }
509

510
    /**
511
     * Create an <input> field.
512
     *
513
     * @param qtype_formulas_part $part question part
514
     * @param int|string $answerindex index of the answer (starting at 0) or special value for combined/separate unit field
515
     * @param question_attempt $qa question attempt that will be displayed on the page
516
     * @param question_display_options $displayoptions controls what should and should not be displayed
517
     * @param array $formatoptions associative array 'optionname' => 'value', e. g. 'w' => '50px'
518
     * @param string $feedbackclass
519
     * @return string HTML fragment
520
     */
521
    protected function create_input_box(qtype_formulas_part $part, $answerindex,
522
            question_attempt $qa, question_display_options $displayoptions, array $formatoptions = [],
523
            string $feedbackclass = ''): string {
524
        /** @var qype_formulas_question $question */
525
        $question = $qa->get_question();
1,638✔
526

527
        // The variable name will be N_ for the (single) combined unit field of part N,
528
        // or N_M for answer #M in part #N. If #M is equal to the part's numbox (i. e. the
529
        // number of answers), it is a unit field; note that the fields are numbered starting
530
        // from 0, so with 3 answers, we have N_0, N_1, N_2 and only use N_3 if there is a
531
        // unit.
532
        $variablename = $part->partindex . '_';
1,638✔
533
        if ($answerindex === self::UNIT_FIELD) {
1,638✔
534
            $variablename .= $part->numbox;
147✔
535
        } else {
536
            $variablename .= ($answerindex === self::COMBINED_FIELD ? '' : $answerindex);
1,638✔
537
        }
538

539
        $currentanswer = $qa->get_last_qt_var($variablename);
1,638✔
540
        $inputname = $qa->get_qt_field_name($variablename);
1,638✔
541

542
        // Text fields will have a tooltip attached. The tooltip's content depends on the
543
        // answer type. Special tooltips exist for combined or separate unit fields.
544
        switch ($part->answertype) {
1,638✔
545
            case qtype_formulas::ANSWER_TYPE_NUMERIC:
546
                $titlestring = 'numeric';
126✔
547
                break;
126✔
548
            case qtype_formulas::ANSWER_TYPE_NUMERICAL_FORMULA:
549
                $titlestring = 'numerical_formula';
126✔
550
                break;
126✔
551
            case qtype_formulas::ANSWER_TYPE_ALGEBRAIC:
552
                $titlestring = 'algebraic_formula';
168✔
553
                break;
168✔
554
            case qtype_formulas::ANSWER_TYPE_NUMBER:
555
            default:
556
                $titlestring = 'number';
1,302✔
557
        }
558
        if ($answerindex === self::COMBINED_FIELD) {
1,638✔
559
            $titlestring .= '_unit';
672✔
560
        }
561
        if ($answerindex === self::UNIT_FIELD) {
1,638✔
562
            $titlestring = 'unit';
147✔
563
        }
564
        $title = get_string($titlestring, 'qtype_formulas');
1,638✔
565

566
        // Fetch the configured default width and use it, if the setting exists. Otherwise,
567
        // the plugin's default as defined in styles.css will be used. If the user did not
568
        // specify a unit, we use pixels (px). Note that this is different from the renderer
569
        // where rem is used in order to allow for the short syntax 'w=3' (3 chars wide).
570
        $defaultformat = [];
1,638✔
571
        $defaultwidth = get_config('qtype_formulas', "defaultwidth_{$titlestring}");
1,638✔
572
        // If the default width has not been set for the current answer box type, $defaultwidth will
573
        // be false and thus not numeric.
574
        if (is_numeric($defaultwidth)) {
1,638✔
575
            $defaultwidthunit = get_config('qtype_formulas', "defaultwidthunit");
1,638✔
576
            if (!in_array($defaultwidthunit, ['px', 'rem', 'em'])) {
1,638✔
577
                $defaultwidthunit = 'px';
168✔
578
            }
579
            $defaultformat = ['w' => $defaultwidth . $defaultwidthunit];
1,638✔
580
        }
581
        // Using the union operator the values from the left array will be kept.
582
        $formatoptions = $formatoptions + $defaultformat;
1,638✔
583

584
        $inputattributes = [
1,638✔
585
            'type' => 'text',
1,638✔
586
            'name' => $inputname,
1,638✔
587
            'value' => $currentanswer,
1,638✔
588
            'id' => $inputname,
1,638✔
589
            'style' => $this->get_css_properties($formatoptions),
1,638✔
590

591
            'data-answertype' => ($answerindex === self::UNIT_FIELD ? 'unit' : $part->answertype),
1,638✔
592
            'data-withunit' => ($answerindex === self::COMBINED_FIELD ? '1' : '0'),
1,638✔
593

594
            'data-toggle' => 'tooltip',
1,638✔
595
            'data-title' => $title,
1,638✔
596
            'data-custom-class' => 'qtype_formulas-tooltip',
1,638✔
597
            'title' => $title,
1,638✔
598
            'class' => "form-control formulas_{$titlestring} {$feedbackclass}",
1,638✔
599
            'maxlength' => 128,
1,638✔
600
        ];
1,638✔
601

602
        if ($displayoptions->readonly) {
1,638✔
603
            $inputattributes['readonly'] = 'readonly';
357✔
604
        }
605

606
        $label = $this->create_label_for_input(
1,638✔
607
            $this->generate_accessibility_label_text($answerindex, $part->numbox, $part->partindex, $question->numparts),
1,638✔
608
            $inputname
1,638✔
609
        );
1,638✔
610
        $inputattributes['aria-labelledby'] = $label['id'];
1,638✔
611

612
        $output = $label['html'];
1,638✔
613
        $output .= html_writer::empty_tag('input', $inputattributes);
1,638✔
614

615
        return $output;
1,638✔
616
    }
617

618
    /**
619
     * Return the part's text with variables replaced by their values.
620
     *
621
     * @param question_attempt $qa question attempt that will be displayed on the page
622
     * @param question_display_options $options controls what should and should not be displayed
623
     * @param qtype_formulas_part $part question part
624
     * @param stdClass $sub class and symbol for the part feedback
625
     * @return string HTML fragment
626
     */
627
    public function get_part_formulation(question_attempt $qa, question_display_options $options,
628
            qtype_formulas_part $part, stdClass $sub): string {
629
        /** @var qype_formulas_question $question */
630
        $question = $qa->get_question();
1,722✔
631

632
        // Clone the part's evaluator and remove special variables like _0 etc., because they must
633
        // not be substituted here; otherwise, we would lose input boxes.
634
        $evaluator = clone $part->evaluator;
1,722✔
635
        $evaluator->remove_special_vars();
1,722✔
636
        $text = $evaluator->substitute_variables_in_text($part->subqtext);
1,722✔
637

638
        $subqreplaced = $question->format_text(
1,722✔
639
            $text, $part->subqtextformat, $qa, 'qtype_formulas', 'answersubqtext', $part->id, false
1,722✔
640
        );
1,722✔
641

642
        // Get the set of defined placeholders and their options.
643
        $boxes = $part->scan_for_answer_boxes($subqreplaced);
1,722✔
644

645
        // Append missing placholders at the end of part. We do not put a space before the opening
646
        // or after the closing brace, in order to get {_0}{_u} for questions with one answer and
647
        // a unit. This makes sure that the question will receive a combined unit field.
648
        for ($i = 0; $i <= $part->numbox; $i++) {
1,722✔
649
            // If no unit has been set, we do not append the {_u} placeholder.
650
            if ($i == $part->numbox && empty($part->postunit)) {
1,722✔
651
                continue;
1,407✔
652
            }
653
            $placeholder = ($i == $part->numbox) ? '_u' : "_{$i}";
1,722✔
654
            // If the placeholder does not exist yet, we create it with default settings, i. e. no multi-choice
655
            // and no styling.
656
            if (!array_key_exists($placeholder, $boxes)) {
1,722✔
657
                $boxes[$placeholder] = [
609✔
658
                    'placeholder' => '{' . $placeholder . '}',
609✔
659
                    'options' => '',
609✔
660
                    'dropdown' => false,
609✔
661
                    'format' => [],
609✔
662
                ];
609✔
663
                $subqreplaced .= '{' . $placeholder . '}';
609✔
664
            }
665
        }
666

667
        // If part has combined unit answer input.
668
        if ($part->has_combined_unit_field()) {
1,722✔
669
            $combinedfieldhtml = $this->create_input_box(
672✔
670
                $part, self::COMBINED_FIELD, $qa, $options, $boxes[$placeholder]['format'], $sub->feedbackclass
672✔
671
            );
672✔
672
            return str_replace('{_0}{_u}', $combinedfieldhtml, $subqreplaced);
672✔
673
        }
674

675
        // Iterate over all boxes again, this time creating the appropriate input control and insert it
676
        // at the position indicated by the placeholder.
677
        for ($i = 0; $i <= $part->numbox; $i++) {
1,449✔
678
            // For normal answer fields, the placeholder is {_N} with N being the number of the
679
            // answer, starting from 0. The unit field, if there is one, comes last and has the
680
            // {_u} placeholder.
681
            if ($i < $part->numbox) {
1,449✔
682
                $answerindex = $i;
1,449✔
683
                $placeholder = "_$i";
1,449✔
684
            } else if (!empty($part->postunit)) {
1,449✔
685
                $answerindex = self::UNIT_FIELD;
147✔
686
                $placeholder = '_u';
147✔
687
            }
688

689
            // If the user has requested a multi-choice element, they must have specified an array
690
            // variable containing the options. We try to fetch that variable. If this fails, we
691
            // simply continue and build a text field instead.
692
            $optiontexts = null;
1,449✔
693
            if (!empty($boxes[$placeholder]['options'])) {
1,449✔
694
                try {
695
                    $optiontexts = $part->evaluator->export_single_variable($boxes[$placeholder]['options']);
147✔
696
                } catch (Exception $e) {
21✔
697
                    // TODO: use non-capturing catch.
698
                    unset($e);
21✔
699
                }
700
            }
701

702
            if ($optiontexts === null) {
1,449✔
703
                $inputfieldhtml = $this->create_input_box(
1,365✔
704
                    $part, $answerindex, $qa, $options, $boxes[$placeholder]['format'], $sub->feedbackclass
1,365✔
705
                );
1,365✔
706
            } else if ($boxes[$placeholder]['dropdown']) {
126✔
707
                $inputfieldhtml = $this->create_dropdown_mc_answer(
63✔
708
                    $part, $i, $qa, $optiontexts->value, $boxes[$placeholder]['shuffle'], $options
63✔
709
                );
63✔
710
            } else {
711
                $inputfieldhtml = $this->create_radio_mc_answer(
63✔
712
                    $part, $i, $qa, $optiontexts->value, $boxes[$placeholder]['shuffle'], $options, $sub->feedbackclass
63✔
713
                );
63✔
714
            }
715

716
            // The replacement text *might* contain a backslash and in the worst case this might
717
            // lead to an erroneous backreference, e. g. if the student's answer was \1. Thus,
718
            // we better use preg_replace_callback() instead of just preg_replace(), as this allows
719
            // us to ignore such unintentional backreferences.
720
            $subqreplaced = preg_replace_callback(
1,449✔
721
                '/' . preg_quote($boxes[$placeholder]['placeholder'], '/') . '/',
1,449✔
722
                function ($matches) use ($inputfieldhtml) {
1,449✔
723
                    return $inputfieldhtml;
1,449✔
724
                },
1,449✔
725
                $subqreplaced,
1,449✔
726
                1
1,449✔
727
            );
1,449✔
728
        }
729

730
        return $subqreplaced;
1,449✔
731
    }
732

733
    /**
734
     * Correct response for the question. This is not needed for the Formulas question, because
735
     * answers are relative to parts.
736
     *
737
     * @param question_attempt $qa question attempt that will be displayed on the page
738
     * @return string empty string
739
     */
740
    public function correct_response(question_attempt $qa) {
741
        return '';
378✔
742
    }
743

744
    /**
745
     * Generate an automatic description of the correct response for a given part.
746
     *
747
     * @param qtype_formulas_part $part question part
748
     * @return string HTML fragment
749
     */
750
    public function part_correct_response($part) {
751
        $answers = $part->get_correct_response(true);
378✔
752
        foreach ($answers as &$answer) {
378✔
753
            if ($answer === '') {
378✔
NEW
UNCOV
754
                $answer = get_string('emptyanswer', 'qtype_formulas');
×
755
            }
756
        }
757
        $answertext = implode('; ', $answers);
378✔
758

759
        $string = ($part->answernotunique ? 'correctansweris' : 'uniquecorrectansweris');
378✔
760
        return html_writer::nonempty_tag(
378✔
761
            'div', get_string($string, 'qtype_formulas', $answertext), ['class' => 'formulaspartcorrectanswer']
378✔
762
        );
378✔
763
    }
764

765
    /**
766
     * Generate a brief statement of how many sub-parts of this question the
767
     * student got right.
768
     *
769
     * @param question_attempt $qa question attempt that will be displayed on the page
770
     * @return string HTML fragment
771
     */
772
    protected function num_parts_correct(question_attempt $qa) {
773
        /** @var qtype_formulas_question $question */
774
        $question = $qa->get_question();
252✔
775
        $response = $qa->get_last_qt_data();
252✔
776
        if (!$question->is_gradable_response($response) || array_key_exists('_seed', $response)) {
252✔
777
            return '';
105✔
778
        }
779

780
        $numright = $question->get_num_parts_right($response)[0];
252✔
781
        if ($numright === 1) {
252✔
782
            return get_string('yougotoneright', 'qtype_formulas');
42✔
783
        } else {
784
            return get_string('yougotnright', 'qtype_formulas', $numright);
231✔
785
        }
786
    }
787

788
    /**
789
     * We need to owerwrite this method to replace global variables by their value.
790
     *
791
     * @param question_attempt $qa question attempt that will be displayed on the page
792
     * @param question_hint $hint the hint to be shown
793
     * @return string HTML fragment
794
     */
795
    protected function hint(question_attempt $qa, question_hint $hint) {
796
        /** @var qtype_formulas_question $question */
797
        $question = $qa->get_question();
42✔
798
        $hint->hint = $question->evaluator->substitute_variables_in_text($hint->hint);
42✔
799

800
        return html_writer::nonempty_tag('div', $question->format_hint($hint, $qa), ['class' => 'hint']);
42✔
801
    }
802

803
    /**
804
     * Generate HTML fragment for the question's combined feedback.
805
     *
806
     * @param question_attempt $qa question attempt that will be displayed on the page
807
     * @return string HTML fragment
808
     */
809
    protected function combined_feedback(question_attempt $qa) {
810
        /** @var qtype_formulas_question $question */
811
        $question = $qa->get_question();
483✔
812

813
        $state = $qa->get_state();
483✔
814
        if (!$state->is_finished()) {
483✔
815
            $response = $qa->get_last_qt_data();
147✔
816
            // When starting a question, there will be no response, but the _seed variable will be set.
817
            // As we now accept questions with an empty response, the grading mechanism would try to grade
818
            // this "startup data" like an entirely empty response and hence give the feedback for a wrong
819
            // answer, before the student has even submitted anything.
820
            if (!$question->is_gradable_response($response) || array_key_exists('_seed', $response)) {
147✔
821
                return '';
105✔
822
            }
823
            $state = $question->grade_response($response)[1];
147✔
824
        }
825

826
        // The feedback will be in ->correctfeedback, ->partiallycorrectfeedback or ->incorrectfeedback,
827
        // with the corresponding ->...feedbackformat setting. We create the property names here to simplify
828
        // access.
829
        $fieldname = $state->get_feedback_class() . 'feedback';
483✔
830
        $formatname = $state->get_feedback_class() . 'feedbackformat';
483✔
831

832
        // If there is no feedback, we return an empty string.
833
        if (strlen(trim($question->$fieldname)) === 0) {
483✔
UNCOV
834
            return '';
×
835
        }
836

837
        // Otherwise, we return the appropriate feedback. The text is run through format_text() to have
838
        // variables replaced.
839
        return $question->format_text(
483✔
840
            $question->$fieldname, $question->$formatname, $qa, 'question', $fieldname, $question->id, false
483✔
841
        );
483✔
842
    }
843

844
    /**
845
     * Generate the specific feedback. This is feedback that varies according to
846
     * the response the student gave.
847
     *
848
     * @param question_attempt $qa question attempt that will be displayed on the page
849
     * @return string
850
     */
851
    public function specific_feedback(question_attempt $qa) {
852
        return $this->combined_feedback($qa);
483✔
853
    }
854

855
    /**
856
     * Gereate the part's general feedback. This is feedback is shown to all students.
857
     *
858
     * @param question_attempt $qa question attempt that will be displayed on the page
859
     * @param question_display_options $options controls what should and should not be displayed
860
     * @param qtype_formulas_part $part question part
861
     * @return string HTML fragment
862
     */
863
    protected function part_general_feedback(question_attempt $qa, question_display_options $options, qtype_formulas_part $part) {
864
        /** @var qtype_formulas_question $question */
865
        $question = $qa->get_question();
1,722✔
866
        $state = $qa->get_state();
1,722✔
867

868
        // If no feedback should be shown, we return an empty string.
869
        if (!$options->feedback) {
1,722✔
870
            return '';
1,680✔
871
        }
872

873
        // If we use the adaptive multipart behaviour, there will be some feedback about the grading,
874
        // e. g. the obtained marks for this submission and the attracted penalty.
875
        $gradingdetailsdiv = '';
483✔
876
        if ($qa->get_behaviour_name() == 'adaptivemultipart') {
483✔
877
            // This is rather a hack, but it will probably work.
878
            $renderer = $this->page->get_renderer('qbehaviour_adaptivemultipart');
105✔
879
            $details = $qa->get_behaviour()->get_part_mark_details($part->partindex);
105✔
880
            $gradingdetailsdiv = $renderer->render_adaptive_marks($details, $options);
105✔
881
            $state = $details->state;
105✔
882
        }
883
        // If the question is in a state that does not yet allow to give a feedback,
884
        // we return an empty string.
885
        if (empty($state->get_feedback_class())) {
483✔
886
            return '';
42✔
887
        }
888

889
        // If we have a general feedback, we substitute local / grading variables and
890
        // wrap it in a <div>.
891
        $feedbackdiv = '';
462✔
892
        if (strlen(trim($part->feedback)) !== 0) {
462✔
893
            $feedbacktext = $part->evaluator->substitute_variables_in_text($part->feedback);
273✔
894
            $feedbacktext = $question->format_text(
273✔
895
                $feedbacktext,
273✔
896
                FORMAT_HTML,
273✔
897
                $qa,
273✔
898
                'qtype_formulas',
273✔
899
                'answerfeedback',
273✔
900
                $part->id,
273✔
901
                false
273✔
902
            );
273✔
903
            $feedbackdiv = html_writer::tag('div', $feedbacktext , ['class' => 'feedback formulaslocalfeedback']);
273✔
904
        }
905

906
        // Append the grading details, if they exist. If the result is not empty, wrap in
907
        // a <div> and return.
908
        $feedbackdiv .= $gradingdetailsdiv;
462✔
909
        if (!empty($feedbackdiv)) {
462✔
910
            return html_writer::nonempty_tag(
294✔
911
                'div', $feedbackdiv, ['class' => 'formulaspartfeedback formulaspartfeedback-' . $part->partindex]
294✔
912
            );
294✔
913
        }
914

915
        // Still here? Then we return an empty string.
916
        return '';
189✔
917
    }
918

919
    /**
920
     * Generate HTML fragment for the part's combined feedback.
921
     *
922
     * @param question_attempt $qa question attempt that will be displayed on the page
923
     * @param question_display_options $options controls what should and should not be displayed
924
     * @param qtype_formulas_part $part question part
925
     * @param float $fraction the obtained grade
926
     * @return string HTML fragment
927
     */
928
    protected function part_combined_feedback(question_attempt $qa, question_display_options $options,
929
        qtype_formulas_part $part, float $fraction): string {
930
        $feedback = '';
1,722✔
931
        $showfeedback = false;
1,722✔
932
        /** @var qtype_formulas_question $question */
933
        $question = $qa->get_question();
1,722✔
934
        $state = $qa->get_state();
1,722✔
935
        $feedbackclass = $state->get_feedback_class();
1,722✔
936

937
        if ($qa->get_behaviour_name() == 'adaptivemultipart') {
1,722✔
938
            $details = $qa->get_behaviour()->get_part_mark_details($part->partindex);
105✔
939
            $feedbackclass = $details->state->get_feedback_class();
105✔
940
        } else {
941
            $state = question_state::graded_state_for_fraction($fraction);
1,638✔
942
            $feedbackclass = $state->get_feedback_class();
1,638✔
943
        }
944
        if ($feedbackclass != '') {
1,722✔
945
            $showfeedback = $options->feedback;
1,722✔
946
            $field = 'part' . $feedbackclass . 'fb';
1,722✔
947
            $format = 'part' . $feedbackclass . 'fbformat';
1,722✔
948
            if ($part->$field) {
1,722✔
949
                // Clone the part's evaluator and substitute local / grading vars first.
950
                $part->$field = $part->evaluator->substitute_variables_in_text($part->$field);
1,701✔
951
                $feedback = $question->format_text($part->$field, $part->$format,
1,701✔
952
                        $qa, 'qtype_formulas', $field, $part->id, false);
1,701✔
953
            }
954
        }
955
        if ($showfeedback && $feedback) {
1,722✔
956
                $feedback = html_writer::tag('div', $feedback , ['class' => 'feedback formulaslocalfeedback']);
462✔
957
                return html_writer::nonempty_tag('div', $feedback,
462✔
958
                        ['class' => 'formulaspartfeedback formulaspartfeedback-' . $part->partindex]);
462✔
959
        }
960
        return '';
1,680✔
961
    }
962
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc