-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.php
executable file
·646 lines (568 loc) · 25.8 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?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/>.
/**
* Fileresponse question renderer class.
*
* @package qtype_fileresponse
* @copyright 2012 Luca Bösch [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for fileresponse questions.
*
* @package qtype_fileresponse
* @copyright 2012 Luca Bösch [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_fileresponse_renderer extends qtype_renderer {
public function formulation_and_controls(question_attempt $qa,
question_display_options $options) {
$question = $qa->get_question();
$responseoutput = $question->get_format_renderer($this->page);
// Answer field.
$step = $qa->get_last_step_with_qt_var('answer');
if (!$step->has_qt_var('answer') && empty($options->readonly)) {
// Question has never been answered, fill it with response template.
$step = new question_attempt_step(array('answer' => $question->responsetemplate));
}
if ($question->responsefieldlines > 0) {
if (empty($options->readonly)) {
$answer = $responseoutput->response_area_input('answer', $qa,
$step, $question->responsefieldlines, $options->context);
} else {
$answer = $responseoutput->response_area_read_only('answer', $qa,
$step, $question->responsefieldlines, $options->context);
}
} else {
$answer = '';
}
$files = '';
if ($question->attachments) {
if (empty($options->readonly)) {
$files = $this->files_input($qa, $question->attachments, $options,
$question->forcedownload, $question->allowpickerplugins);
} else {
$files = $this->files_read_only($qa, $options);
}
}
$result = '';
$result .= html_writer::tag('div', $question->format_questiontext($qa),
array('class' => 'qtext'));
$result .= html_writer::start_tag('div', array('class' => 'ablock'));
if ($answer) {
$result .= html_writer::tag('div', $answer, array('class' => 'qtext'
));
}
/* How many files are expected, already uploaded and saved ? */
$filecount = $this->get_already_uploaded_files_number($qa, $options);
$expectedattachments = (int) $question->attachments;
switch ($expectedattachments) {
case -1: // Unlimited, but at least one file expected.
// No explanation is needed.
break;
case 1: // One file required.
$result .= html_writer::tag('div',
get_string('oneattachmentexpected', 'qtype_fileresponse'),
array('class' => 'answer'
));
break;
default: // Two or three file required.
$result .= html_writer::tag('div',
get_string('nattachmentsexpected', 'qtype_fileresponse',
$expectedattachments),
array('class' => 'answer'
));
break;
}
if ($expectedattachments == -1) {
// Unlimited number, but at least one attachment expected.
switch ($filecount) {
case 0:
// No file of unlimited submitted.
// No explanation is needed.
break;
case 1:
// Exactly one file of unlimited submitted.
$result .= html_writer::tag('div',
get_string('oneattachmentsubmitted', 'qtype_fileresponse') .
"<br /> <br />",
array('class' => 'answer'
));
break;
default:
// Exactly n > 1 files of unlimited submitted.
$result .= html_writer::tag('div',
get_string('nattachmentssubmitted', 'qtype_fileresponse', $filecount) .
"<br /> <br />",
array('class' => 'answer'
));
break;
}
} else if ($expectedattachments == 1) {
// Exactly one attachment expected.
if ($filecount == 0) {
// No file of 1 submitted.
$result .= html_writer::tag('div',
get_string('noofoneattachmentsubmitted', 'qtype_fileresponse') .
"<br /> <br />",
array('class' => 'answer'
));
} else if ($filecount == 1) {
// Exactly 1 file of 1 submitted.
$result .= html_writer::tag('div',
get_string('oneofoneattachmentsubmitted', 'qtype_fileresponse') .
"<br /> <br />",
array('class' => 'answer'
));
} else {
// This should not happen: $filecount larger than $expectedattachments.
$result .= html_writer::tag('div',
get_string('nattachmentssubmitted', 'qtype_fileresponse', $filecount) .
"<br /> <br />",
array('class' => 'answer'
));
}
} else {
/* Exactly a certain amount (but more than one) of attachment expected. */
if ($filecount == 0) {
/* No file of n > 1 submitted yet. */
$result .= html_writer::tag('div',
get_string('noofnattachmentsubmitted', 'qtype_fileresponse',
$expectedattachments) . "<br /> <br />",
array('class' => 'answer'
));
} else if (($expectedattachments > 1) && ($filecount == 1)) {
/* Exactly one file of n > 1 submitted. */
$result .= html_writer::tag('div',
get_string('oneofnattachmentssubmitted', 'qtype_fileresponse',
$expectedattachments) . "<br /> <br />",
array('class' => 'answer'
));
} else if ($filecount > $expectedattachments) {
/* This should not happen: $filecount larger than $expectedattachments. */
$result .= html_writer::tag('div',
get_string('nattachmentssubmitted', 'qtype_fileresponse', $filecount) .
"<br /> <br />",
array('class' => 'answer'
));
} else {
/* n > 1 files of n > 1 submitted yet. */
$result .= html_writer::tag('div',
$filecount . get_string('ofnattachmentssubmitted', 'qtype_fileresponse',
$expectedattachments) . "<br /> <br />",
array('class' => 'answer'
));
}
}
$result .= html_writer::tag('div', $files, array('class' => 'attachments'
));
$result .= html_writer::end_tag('div');
return $result;
}
/**
* Displays any attached files when the question is in read-only mode.
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should
* not be displayed. Used to get the context.
*/
public function files_read_only(question_attempt $qa, question_display_options $options) {
$files = $qa->get_last_qt_files('attachments', $options->context->id);
$output = array();
foreach ($files as $file) {
$output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
$this->output->pix_icon(file_file_icon($file), get_mimetype_description($file),
'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
}
return implode($output);
}
/**
* Displays the input control for when the student should upload a single file.
* @param question_attempt $qa the question attempt to display.
* @param int $numallowed the maximum number of attachments allowed. -1 = unlimited.
* @param question_display_options $options controls what should and should
* not be displayed. Used to get the context.
* @param int $forcedownload
* @param bool $allowpickerplugins
*/
public function files_input(question_attempt $qa, $numallowed,
question_display_options $options, $forcedownload, $allowpickerplugins) {
global $CFG;
/* Removed the call to lib/form/filemanager.php because we've cloned the filepicker. */
require_once($CFG->dirroot . '/repository/lib.php');
$pickeroptions = new stdClass();
$pickeroptions->mainfile = null;
$pickeroptions->maxfiles = $numallowed;
$pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
'attachments', $options->context->id);
$pickeroptions->context = $options->context;
$pickeroptions->return_types = FILE_INTERNAL | FILE_CONTROLLED_LINK;
$pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
'attachments', $options->context->id);
$pickeroptions->accepted_types = $qa->get_question()->filetypeslist;
$pickeroptions->allowpickerplugins = $allowpickerplugins;
/* Removed the creation of an new form_filemanager. */
$filesrenderer = $this->page->get_renderer('core', 'files');
$text = '';
if (!empty($qa->get_question()->filetypeslist)) {
$text = html_writer::tag('p', get_string('acceptedfiletypes', 'qtype_fileresponse'));
$filetypesutil = new \core_form\filetypes_util();
$filetypes = $qa->get_question()->filetypeslist;
$filetypedescriptions = $filetypesutil->describe_file_types($filetypes);
$text .= $this->render_from_template('core_form/filetypes-descriptions', $filetypedescriptions);
}
if ($forcedownload) {
// Don't download fix.
require_once('fileresponsesimplifiedfilemanager.php');
$frsfm = new form_fileresponsesimplifiedfilemanager($pickeroptions);
$filesrenderer = $this->page->get_renderer('qtype_fileresponse',
'fileresponsesimplifiedfilemanager');
return $filesrenderer->render($frsfm) . html_writer::empty_tag('input',
array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
'value' => $pickeroptions->itemid
)) . $text;
} else {
// Allow download.
require_once('fileresponsefilemanager.php');
// Check allowed repositories.
$frfm = new form_fileresponsefilemanager($pickeroptions);
$filesrenderer = $this->page->get_renderer('qtype_fileresponse',
'fileresponsefilemanager');
return $filesrenderer->render($frfm) . html_writer::empty_tag('input',
array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
'value' => $pickeroptions->itemid
)) . $text;
}
}
/**
* Count how many files have already been uploaded.
* @param $qa the question attempt that will be displayed on the page.
* @param $options the options
* @return int how many files have already been uploaded
*/
public function get_already_uploaded_files_number($qa, $options) {
global $skipfile;
$files = $qa->get_last_qt_files('attachments', $options->context->id);
$counter = 0;
foreach ($files as $file) {
if ($file->get_filename() != $skipfile) { // Minus the zipped file!
$counter++;
}
}
if ($counter < 0) {
$counter = 0;
}
return $counter;
}
public function manual_comment(question_attempt $qa, question_display_options $options) {
if ($options->manualcomment != question_display_options::EDITABLE) {
return '';
}
$question = $qa->get_question();
return html_writer::nonempty_tag('div',
$question->format_text($question->graderinfo, $question->questiontextformat, $qa,
'qtype_fileresponse', 'graderinfo', $question->id),
array('class' => 'graderinfo'
));
}
}
/**
* A base class to abstract out the differences between different type of
* response format.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class qtype_fileresponse_format_renderer_base extends plugin_renderer_base {
/**
* Render the students respone when the question is in read-only mode.
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param int $lines approximate size of input box to display.
* @param object $context the context teh output belongs to.
* @return string html to display the response.
*/
abstract public function response_area_read_only($name, question_attempt $qa,
question_attempt_step $step, $lines, $context);
/**
* Render the students respone when the question is in read-only mode.
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param int $lines approximate size of input box to display.
* @param object $context the context teh output belongs to.
* @return string html to display the response for editing.
*/
abstract public function response_area_input($name, question_attempt $qa,
question_attempt_step $step, $lines, $context);
/**
* @return string specific class name to add to the input element.
*/
abstract protected function class_name();
}
/**
* An essay format renderer for essays where the student should not enter
* any inline response.
*
* @copyright 2013 Binghamton University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_fileresponse_format_noinline_renderer extends plugin_renderer_base {
protected function class_name() {
return 'qtype_fileresponse_noinline';
}
public function response_area_read_only($name, $qa, $step, $lines, $context) {
return '';
}
public function response_area_input($name, $qa, $step, $lines, $context) {
return '';
}
}
/**
* An essay format renderer for essays where the student should use the HTML
* editor without the file picker.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_fileresponse_format_editor_renderer extends plugin_renderer_base {
protected function class_name() {
return 'qtype_fileresponse_editor';
}
public function response_area_read_only($name, $qa, $step, $lines, $context) {
return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
array('class' => $this->class_name() . ' qtype_fileresponse_response readonly'));
}
public function response_area_input($name, $qa, $step, $lines, $context) {
global $CFG;
require_once($CFG->dirroot . '/repository/lib.php');
$inputname = $qa->get_qt_field_name($name);
$responseformat = $step->get_qt_var($name . 'format');
$id = $inputname . '_id';
$editor = editors_get_preferred_editor($responseformat);
$strformats = format_text_menu();
$formats = $editor->get_supported_formats();
foreach ($formats as $fid) {
$formats[$fid] = $strformats[$fid];
}
list($draftitemid, $response) = $this->prepare_response_for_editing(
$name, $step, $context);
$editor->set_text($response);
$editor->use_editor($id, $this->get_editor_options($context),
$this->get_filepicker_options($context, $draftitemid));
$output = '';
$output .= html_writer::start_tag('div', array('class' =>
$this->class_name() . ' qtype_fileresponse_response'));
$output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
array('id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60)));
$output .= html_writer::start_tag('div');
if (count($formats) == 1) {
reset($formats);
$output .= html_writer::empty_tag('input', array('type' => 'hidden',
'name' => $inputname . 'format', 'value' => key($formats)));
} else {
$output .= html_writer::label(get_string('format'), 'menu' . $inputname . 'format', false);
$output .= ' ';
$output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
}
$output .= html_writer::end_tag('div');
$output .= $this->filepicker_html($inputname, $draftitemid);
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Prepare the response for read-only display.
* @param string $name the variable name this input edits.
* @param question_attempt $qa the question attempt being display.
* @param question_attempt_step $step the current step.
* @param object $context the context the attempt belongs to.
* @return string the response prepared for display.
*/
protected function prepare_response($name, question_attempt $qa,
question_attempt_step $step, $context) {
if (!$step->has_qt_var($name)) {
return '';
}
$formatoptions = new stdClass();
$formatoptions->para = false;
return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
$formatoptions);
}
/**
* Prepare the response for editing.
* @param string $name the variable name this input edits.
* @param question_attempt_step $step the current step.
* @param object $context the context the attempt belongs to.
* @return string the response prepared for display.
*/
protected function prepare_response_for_editing($name,
question_attempt_step $step, $context) {
return array(0, $step->get_qt_var($name));
}
/**
* @param object $context the context the attempt belongs to.
* @return array options for the editor.
*/
protected function get_editor_options($context) {
// Disable the text-editor autosave because quiz has it's own auto save function.
return array('context' => $context, 'autosave' => false);
}
/**
* @param object $context the context the attempt belongs to.
* @param int $draftitemid draft item id.
* @return array filepicker options for the editor.
*/
protected function get_filepicker_options($context, $draftitemid) {
return array('return_types' => FILE_INTERNAL | FILE_EXTERNAL);
}
/**
* @param string $inputname input field name.
* @param int $draftitemid draft file area itemid.
* @return string HTML for the filepicker, if used.
*/
protected function filepicker_html($inputname, $draftitemid) {
return '';
}
}
/**
* An essay format renderer for essays where the student should use the HTML
* editor with the file picker.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_fileresponse_format_editorfilepicker_renderer extends qtype_fileresponse_format_editor_renderer {
protected function class_name() {
return 'qtype_fileresponse_editorfilepicker';
}
protected function prepare_response($name, question_attempt $qa,
question_attempt_step $step, $context) {
if (!$step->has_qt_var($name)) {
return '';
}
$formatoptions = new stdClass();
$formatoptions->para = false;
$text = $qa->rewrite_response_pluginfile_urls($step->get_qt_var($name),
$context->id, 'answer', $step);
return format_text($text, $step->get_qt_var($name . 'format'), $formatoptions);
}
protected function prepare_response_for_editing($name,
question_attempt_step $step, $context) {
return $step->prepare_response_files_draft_itemid_with_text(
$name, $context->id, $step->get_qt_var($name));
}
/**
* Get editor options for question response text area.
* @param object $context the context the attempt belongs to.
* @return array options for the editor.
*/
protected function get_editor_options($context) {
return question_utils::get_editor_options($context);
}
/**
* Get the options required to configure the filepicker for one of the editor
* toolbar buttons.
* @deprecated since 3.5
* @param mixed $acceptedtypes array of types of '*'.
* @param int $draftitemid the draft area item id.
* @param object $context the context.
* @return object the required options.
*/
protected function specific_filepicker_options($acceptedtypes, $draftitemid, $context) {
debugging('qtype_fileresponse_format_editorfilepicker_renderer::specific_filepicker_options() is deprecated, ' .
'use question_utils::specific_filepicker_options() instead.', DEBUG_DEVELOPER);
$filepickeroptions = new stdClass();
$filepickeroptions->accepted_types = $acceptedtypes;
$filepickeroptions->return_types = FILE_INTERNAL | FILE_EXTERNAL;
$filepickeroptions->context = $context;
$filepickeroptions->env = 'filepicker';
$options = initialise_filepicker($filepickeroptions);
$options->context = $context;
$options->client_id = uniqid();
$options->env = 'editor';
$options->itemid = $draftitemid;
return $options;
}
/**
* @param object $context the context the attempt belongs to.
* @param int $draftitemid draft item id.
* @return array filepicker options for the editor.
*/
protected function get_filepicker_options($context, $draftitemid) {
return question_utils::get_filepicker_options($context, $draftitemid);
}
protected function filepicker_html($inputname, $draftitemid) {
$nonjspickerurl = new moodle_url('/repository/draftfiles_manager.php', array(
'action' => 'browse',
'env' => 'editor',
'itemid' => $draftitemid,
'subdirs' => false,
'maxfiles' => -1,
'sesskey' => sesskey(),
));
return html_writer::empty_tag('input', array('type' => 'hidden',
'name' => $inputname . ':itemid', 'value' => $draftitemid)) .
html_writer::tag('noscript', html_writer::tag('div',
html_writer::tag('object', '', array('type' => 'text/html',
'data' => $nonjspickerurl, 'height' => 160, 'width' => 600,
'style' => 'border: 1px solid #000;'))));
}
}
/**
* An essay format renderer for essays where the student should use a plain
* input box, but with a normal, proportional font.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_fileresponse_format_plain_renderer extends plugin_renderer_base {
/**
* @return string the HTML for the textarea.
*/
protected function textarea($response, $lines, $attributes) {
$attributes['class'] = $this->class_name() . ' qtype_fileresponse_response';
$attributes['rows'] = $lines;
$attributes['cols'] = 60;
return html_writer::tag('textarea', s($response), $attributes);
}
protected function class_name() {
return 'qtype_fileresponse_plain';
}
public function response_area_read_only($name, $qa, $step, $lines, $context) {
return $this->textarea($step->get_qt_var($name), $lines, array('readonly' => 'readonly'));
}
public function response_area_input($name, $qa, $step, $lines, $context) {
$inputname = $qa->get_qt_field_name($name);
return $this->textarea($step->get_qt_var($name), $lines, array('name' => $inputname)) .
html_writer::empty_tag('input', array('type' => 'hidden',
'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
}
}
/**
* An essay format renderer for essays where the student should use a plain
* input box with a monospaced font. You might use this, for example, for a
* question where the students should type computer code.
*
* @package qtype_fileresponse
* @copyright 2012 Luca Bösch [email protected]
*/
class qtype_fileresponse_format_monospaced_renderer extends qtype_fileresponse_format_plain_renderer {
protected function class_name() {
return 'qtype_fileresponse_monospaced';
}
}