Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Renderers for outputting parts of the question engine.
19
 *
20
 * @package    moodlecore
21
 * @subpackage questionengine
22
 * @copyright  2009 The Open University
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use core_question\output\question_version_info;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
 
31
/**
32
 * This renderer controls the overall output of questions. It works with a
33
 * {@link qbehaviour_renderer} and a {@link qtype_renderer} to output the
34
 * type-specific bits. The main entry point is the {@link question()} method.
35
 *
36
 * @copyright  2009 The Open University
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class core_question_renderer extends plugin_renderer_base {
40
 
41
    /**
42
     * @deprecated since Moodle 4.0
43
     */
44
    public function question_preview_link() {
45
        throw new coding_exception(__FUNCTION__ . '() has been removed.');
46
    }
47
 
48
    /**
49
     * Generate the display of a question in a particular state, and with certain
50
     * display options. Normally you do not call this method directly. Intsead
51
     * you call {@link question_usage_by_activity::render_question()} which will
52
     * call this method with appropriate arguments.
53
     *
54
     * @param question_attempt $qa the question attempt to display.
55
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
56
     *      specific parts.
57
     * @param qtype_renderer $qtoutput the renderer to output the question type
58
     *      specific parts.
59
     * @param question_display_options $options controls what should and should not be displayed.
60
     * @param string|null $number The question number to display. 'i' is a special
61
     *      value that gets displayed as Information. Null means no number is displayed.
62
     * @return string HTML representation of the question.
63
     */
64
    public function question(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
65
            qtype_renderer $qtoutput, question_display_options $options, $number) {
66
 
67
        // If not already set, record the questionidentifier.
68
        $options = clone($options);
69
        if (!$options->has_question_identifier()) {
70
            $options->questionidentifier = $this->question_number_text($number);
71
        }
72
 
73
        $output = '';
74
        $output .= html_writer::start_tag('div', array(
75
            'id' => $qa->get_outer_question_div_unique_id(),
76
            'class' => implode(' ', array(
77
                'que',
78
                $qa->get_question(false)->get_type_name(),
79
                $qa->get_behaviour_name(),
80
                $qa->get_state_class($options->correctness && $qa->has_marks()),
81
            ))
82
        ));
83
 
84
        $output .= html_writer::tag('div',
85
                $this->info($qa, $behaviouroutput, $qtoutput, $options, $number),
86
                array('class' => 'info'));
87
 
88
        $output .= html_writer::start_tag('div', array('class' => 'content'));
89
 
90
        $output .= html_writer::tag('div',
91
                $this->add_part_heading($qtoutput->formulation_heading(),
92
                    $this->formulation($qa, $behaviouroutput, $qtoutput, $options)),
93
                array('class' => 'formulation clearfix'));
94
        $output .= html_writer::nonempty_tag('div',
95
                $this->add_part_heading(get_string('feedback', 'question'),
96
                    $this->outcome($qa, $behaviouroutput, $qtoutput, $options)),
97
                array('class' => 'outcome clearfix'));
98
        $output .= html_writer::nonempty_tag('div',
99
                $this->add_part_heading(get_string('comments', 'question'),
100
                    $this->manual_comment($qa, $behaviouroutput, $qtoutput, $options)),
101
                array('class' => 'comment clearfix'));
102
        $output .= html_writer::nonempty_tag('div',
103
                $this->response_history($qa, $behaviouroutput, $qtoutput, $options),
104
                array('class' => 'history clearfix border p-2'));
105
 
106
        $output .= html_writer::end_tag('div');
107
        $output .= html_writer::end_tag('div');
108
        return $output;
109
    }
110
 
111
    /**
112
     * Generate the information bit of the question display that contains the
113
     * metadata like the question number, current state, and mark.
114
     * @param question_attempt $qa the question attempt to display.
115
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
116
     *      specific parts.
117
     * @param qtype_renderer $qtoutput the renderer to output the question type
118
     *      specific parts.
119
     * @param question_display_options $options controls what should and should not be displayed.
120
     * @param string|null $number The question number to display. 'i' is a special
121
     *      value that gets displayed as Information. Null means no number is displayed.
122
     * @return HTML fragment.
123
     */
124
    protected function info(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
125
            qtype_renderer $qtoutput, question_display_options $options, $number) {
126
        $output = '';
127
        $output .= $this->number($number);
128
        $output .= $this->status($qa, $behaviouroutput, $options);
129
        $output .= $this->mark_summary($qa, $behaviouroutput, $options);
130
        $output .= $this->question_flag($qa, $options->flags);
131
        $output .= $this->edit_question_link($qa, $options);
132
        if ($options->versioninfo) {
133
            $output .= $this->render(new question_version_info($qa->get_question(), true));
134
        }
135
        return $output;
136
    }
137
 
138
    /**
139
     * Generate the display of the question number.
140
     * @param string|null $number The question number to display. 'i' is a special
141
     *      value that gets displayed as Information. Null means no number is displayed.
142
     * @return HTML fragment.
143
     */
144
    protected function number($number) {
145
        if (trim($number ?? '') === '') {
146
            return '';
147
        }
148
        if (trim($number) === 'i') {
149
            $numbertext = get_string('information', 'question');
150
        } else {
151
            $numbertext = get_string('questionx', 'question',
152
                    html_writer::tag('span', s($number), array('class' => 'qno')));
153
        }
154
        return html_writer::tag('h3', $numbertext, array('class' => 'no'));
155
    }
156
 
157
    /**
158
     * Get the question number as a string.
159
     *
160
     * @param string|null $number e.g. '123' or 'i'. null or '' means do not display anything number-related.
161
     * @return string e.g. 'Question 123' or 'Information' or ''.
162
     */
163
    protected function question_number_text(?string $number): string {
164
        $number = $number ?? '';
165
        // Trim the question number of whitespace, including &nbsp;.
166
        $trimmed = trim(html_entity_decode($number), " \n\r\t\v\x00\xC2\xA0");
167
        if ($trimmed === '') {
168
            return '';
169
        }
170
        if (trim($number) === 'i') {
171
            return get_string('information', 'question');
172
        } else {
173
            return get_string('questionx', 'question', s($number));
174
        }
175
    }
176
 
177
    /**
178
     * Add an invisible heading like 'question text', 'feebdack' at the top of
179
     * a section's contents, but only if the section has some content.
180
     * @param string $heading the heading to add.
181
     * @param string $content the content of the section.
182
     * @return string HTML fragment with the heading added.
183
     */
184
    protected function add_part_heading($heading, $content) {
185
        if ($content) {
186
            $content = html_writer::tag('h4', $heading, array('class' => 'accesshide')) . $content;
187
        }
188
        return $content;
189
    }
190
 
191
    /**
192
     * Generate the display of the status line that gives the current state of
193
     * the question.
194
     * @param question_attempt $qa the question attempt to display.
195
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
196
     *      specific parts.
197
     * @param question_display_options $options controls what should and should not be displayed.
198
     * @return HTML fragment.
199
     */
200
    protected function status(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
201
            question_display_options $options) {
202
        return html_writer::tag('div', $qa->get_state_string($options->correctness),
203
                array('class' => 'state'));
204
    }
205
 
206
    /**
207
     * Generate the display of the marks for this question.
208
     * @param question_attempt $qa the question attempt to display.
209
     * @param qbehaviour_renderer $behaviouroutput the behaviour renderer, which can generate a custom display.
210
     * @param question_display_options $options controls what should and should not be displayed.
211
     * @return HTML fragment.
212
     */
213
    protected function mark_summary(question_attempt $qa, qbehaviour_renderer $behaviouroutput, question_display_options $options) {
214
        return html_writer::nonempty_tag('div',
215
                $behaviouroutput->mark_summary($qa, $this, $options),
216
                array('class' => 'grade'));
217
    }
218
 
219
    /**
220
     * Generate the display of the marks for this question.
221
     * @param question_attempt $qa the question attempt to display.
222
     * @param question_display_options $options controls what should and should not be displayed.
223
     * @return HTML fragment.
224
     */
225
    public function standard_mark_summary(question_attempt $qa, qbehaviour_renderer $behaviouroutput, question_display_options $options) {
226
        if (!$options->marks) {
227
            return '';
228
 
229
        } else if ($qa->get_max_mark() == 0) {
230
            return get_string('notgraded', 'question');
231
 
232
        } else if ($options->marks == question_display_options::MAX_ONLY ||
233
                is_null($qa->get_fraction())) {
234
            return $behaviouroutput->marked_out_of_max($qa, $this, $options);
235
 
236
        } else {
237
            return $behaviouroutput->mark_out_of_max($qa, $this, $options);
238
        }
239
    }
240
 
241
    /**
242
     * Generate the display of the available marks for this question.
243
     * @param question_attempt $qa the question attempt to display.
244
     * @param question_display_options $options controls what should and should not be displayed.
245
     * @return HTML fragment.
246
     */
247
    public function standard_marked_out_of_max(question_attempt $qa, question_display_options $options) {
248
        return get_string('markedoutofmax', 'question', $qa->format_max_mark($options->markdp));
249
    }
250
 
251
    /**
252
     * Generate the display of the marks for this question out of the available marks.
253
     * @param question_attempt $qa the question attempt to display.
254
     * @param question_display_options $options controls what should and should not be displayed.
255
     * @return HTML fragment.
256
     */
257
    public function standard_mark_out_of_max(question_attempt $qa, question_display_options $options) {
258
        $a = new stdClass();
259
        $a->mark = $qa->format_mark($options->markdp);
260
        $a->max = $qa->format_max_mark($options->markdp);
261
        return get_string('markoutofmax', 'question', $a);
262
    }
263
 
264
    /**
265
     * Render the question flag, assuming $flagsoption allows it.
266
     *
267
     * @param question_attempt $qa the question attempt to display.
268
     * @param int $flagsoption the option that says whether flags should be displayed.
269
     */
270
    protected function question_flag(question_attempt $qa, $flagsoption) {
271
        $divattributes = array('class' => 'questionflag');
272
 
273
        switch ($flagsoption) {
274
            case question_display_options::VISIBLE:
275
                $flagcontent = $this->get_flag_html($qa->is_flagged());
276
                break;
277
 
278
            case question_display_options::EDITABLE:
279
                $id = $qa->get_flag_field_name();
280
                // The checkbox id must be different from any element name, because
281
                // of a stupid IE bug:
282
                // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
283
                $checkboxattributes = array(
284
                    'type' => 'checkbox',
285
                    'id' => $id . 'checkbox',
286
                    'name' => $id,
287
                    'value' => 1,
288
                );
289
                if ($qa->is_flagged()) {
290
                    $checkboxattributes['checked'] = 'checked';
291
                }
292
                $postdata = question_flags::get_postdata($qa);
293
 
294
                $flagcontent = html_writer::empty_tag('input',
295
                                array('type' => 'hidden', 'name' => $id, 'value' => 0)) .
296
                        html_writer::empty_tag('input',
297
                                array('type' => 'hidden', 'value' => $postdata, 'class' => 'questionflagpostdata')) .
298
                        html_writer::empty_tag('input', $checkboxattributes) .
299
                        html_writer::tag('label', $this->get_flag_html($qa->is_flagged(), $id . 'img'),
300
                                array('id' => $id . 'label', 'for' => $id . 'checkbox')) . "\n";
301
 
302
                $divattributes = array(
303
                    'class' => 'questionflag editable',
304
                );
305
 
306
                break;
307
 
308
            default:
309
                $flagcontent = '';
310
        }
311
 
312
        return html_writer::nonempty_tag('div', $flagcontent, $divattributes);
313
    }
314
 
315
    /**
316
     * Work out the actual img tag needed for the flag
317
     *
318
     * @param bool $flagged whether the question is currently flagged.
319
     * @param string $id an id to be added as an attribute to the img (optional).
320
     * @return string the img tag.
321
     */
322
    protected function get_flag_html($flagged, $id = '') {
323
        if ($flagged) {
324
            $icon = 'i/flagged';
325
            $label = get_string('clickunflag', 'question');
326
        } else {
327
            $icon = 'i/unflagged';
328
            $label = get_string('clickflag', 'question');
329
        }
330
        $attributes = [
331
            'src' => $this->image_url($icon),
332
            'alt' => '',
333
            'class' => 'questionflagimage',
334
        ];
335
        if ($id) {
336
            $attributes['id'] = $id;
337
        }
338
        $img = html_writer::empty_tag('img', $attributes);
339
        $img .= html_writer::span($label);
340
 
341
        return $img;
342
    }
343
 
344
    /**
345
     * Generate the display of the edit question link.
346
     *
347
     * @param question_attempt $qa The question attempt to display.
348
     * @param question_display_options $options controls what should and should not be displayed.
349
     * @return string
350
     */
351
    protected function edit_question_link(question_attempt $qa, question_display_options $options) {
352
        if (empty($options->editquestionparams)) {
353
            return '';
354
        }
355
 
356
        $params = $options->editquestionparams;
357
        if ($params['returnurl'] instanceof moodle_url) {
358
            $params['returnurl'] = $params['returnurl']->out_as_local_url(false);
359
        }
360
        $params['id'] = $qa->get_question_id();
361
        $editurl = new moodle_url('/question/bank/editquestion/question.php', $params);
362
 
363
        return html_writer::tag('div', html_writer::link(
364
                $editurl, $this->pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall')) .
365
                get_string('editquestion', 'question')),
366
                array('class' => 'editquestion'));
367
    }
368
 
369
    /**
370
     * Generate the display of the formulation part of the question. This is the
371
     * area that contains the quetsion text, and the controls for students to
372
     * input their answers. Some question types also embed feedback, for
373
     * example ticks and crosses, in this area.
374
     *
375
     * @param question_attempt $qa the question attempt to display.
376
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
377
     *      specific parts.
378
     * @param qtype_renderer $qtoutput the renderer to output the question type
379
     *      specific parts.
380
     * @param question_display_options $options controls what should and should not be displayed.
381
     * @return HTML fragment.
382
     */
383
    protected function formulation(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
384
            qtype_renderer $qtoutput, question_display_options $options) {
385
        $output = '';
386
        $output .= html_writer::empty_tag('input', array(
387
                'type' => 'hidden',
388
                'name' => $qa->get_control_field_name('sequencecheck'),
389
                'value' => $qa->get_sequence_check_count()));
390
        $output .= $qtoutput->formulation_and_controls($qa, $options);
391
        if ($options->clearwrong) {
392
            $output .= $qtoutput->clear_wrong($qa);
393
        }
394
        $output .= html_writer::nonempty_tag('div',
395
                $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
396
        return $output;
397
    }
398
 
399
    /**
400
     * Generate the display of the outcome part of the question. This is the
401
     * area that contains the various forms of feedback.
402
     *
403
     * @param question_attempt $qa the question attempt to display.
404
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
405
     *      specific parts.
406
     * @param qtype_renderer $qtoutput the renderer to output the question type
407
     *      specific parts.
408
     * @param question_display_options $options controls what should and should not be displayed.
409
     * @return HTML fragment.
410
     */
411
    protected function outcome(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
412
            qtype_renderer $qtoutput, question_display_options $options) {
413
        $output = '';
414
        $output .= html_writer::nonempty_tag('div',
415
                $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
416
        $output .= html_writer::nonempty_tag('div',
417
                $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
418
        $output .= html_writer::nonempty_tag('div',
419
                $options->extrainfocontent, array('class' => 'extra-feedback'));
420
        return $output;
421
    }
422
 
423
    protected function manual_comment(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
424
            qtype_renderer $qtoutput, question_display_options $options) {
425
        return $qtoutput->manual_comment($qa, $options) .
426
                $behaviouroutput->manual_comment($qa, $options);
427
    }
428
 
429
    /**
430
     * Generate the display of the response history part of the question. This
431
     * is the table showing all the steps the question has been through.
432
     *
433
     * @param question_attempt $qa the question attempt to display.
434
     * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
435
     *      specific parts.
436
     * @param qtype_renderer $qtoutput the renderer to output the question type
437
     *      specific parts.
438
     * @param question_display_options $options controls what should and should not be displayed.
439
     * @return HTML fragment.
440
     */
441
    protected function response_history(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
442
            qtype_renderer $qtoutput, question_display_options $options) {
443
 
444
        if (!$options->history) {
445
            return '';
446
        }
447
 
448
        $table = new html_table();
449
        $table->head  = array (
450
            get_string('step', 'question'),
451
            get_string('time'),
452
            get_string('action', 'question'),
453
            get_string('state', 'question'),
454
        );
455
        if ($options->marks >= question_display_options::MARK_AND_MAX) {
456
            $table->head[] = get_string('marks', 'question');
457
        }
458
 
459
        foreach ($qa->get_full_step_iterator() as $i => $step) {
460
            $stepno = $i + 1;
461
 
462
            $rowclass = '';
463
            if ($stepno == $qa->get_num_steps()) {
464
                $rowclass = 'current';
465
            } else if (!empty($options->questionreviewlink)) {
466
                $url = new moodle_url($options->questionreviewlink,
467
                        array('slot' => $qa->get_slot(), 'step' => $i));
468
                $stepno = $this->output->action_link($url, $stepno,
469
                        new popup_action('click', $url, 'reviewquestion',
470
                                array('width' => 450, 'height' => 650)),
471
                        array('title' => get_string('reviewresponse', 'question')));
472
            }
473
 
474
            $restrictedqa = new question_attempt_with_restricted_history($qa, $i, null);
475
 
476
            $row = [$stepno,
477
                    userdate($step->get_timecreated(), get_string('strftimedatetimeshortaccurate', 'core_langconfig')),
478
                    s($qa->summarise_action($step)) . $this->action_author($step, $options),
479
                    $restrictedqa->get_state_string($options->correctness)];
480
 
481
            if ($options->marks >= question_display_options::MARK_AND_MAX) {
482
                $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp);
483
            }
484
 
485
            $table->rowclasses[] = $rowclass;
486
            $table->data[] = $row;
487
        }
488
 
489
        return html_writer::tag('h4', get_string('responsehistory', 'question'),
490
                        array('class' => 'responsehistoryheader')) .
491
                $options->extrahistorycontent .
492
                html_writer::tag('div', html_writer::table($table, true),
493
                        array('class' => 'responsehistoryheader'));
494
    }
495
 
496
    /**
497
     * Action author's profile link.
498
     *
499
     * @param question_attempt_step $step The step.
500
     * @param question_display_options $options The display options.
501
     * @return string The link to user's profile.
502
     */
503
    protected function action_author(question_attempt_step $step, question_display_options $options): string {
504
        if ($options->userinfoinhistory && $step->get_user_id() != $options->userinfoinhistory) {
505
            return html_writer::link(
506
                    new moodle_url('/user/view.php', ['id' => $step->get_user_id(), 'course' => $this->page->course->id]),
507
                    $step->get_user_fullname(), ['class' => 'd-table-cell']);
508
        } else {
509
            return '';
510
        }
511
    }
512
}