mirrored from git://git.moodle.org/moodle.git
/
edit_renderer.php
1242 lines (1073 loc) · 52 KB
/
edit_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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Renderer outputting the quiz editing UI.
*
* @package mod_quiz
* @copyright 2013 The Open University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\output;
defined('MOODLE_INTERNAL') || die();
use \mod_quiz\structure;
use \html_writer;
use renderable;
/**
* Renderer outputting the quiz editing UI.
*
* @copyright 2013 The Open University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.7
*/
class edit_renderer extends \plugin_renderer_base {
/**
* Render the edit page
*
* @param \quiz $quizobj object containing all the quiz settings information.
* @param structure $structure object containing the structure of the quiz.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param \moodle_url $pageurl the canonical URL of this page.
* @param array $pagevars the variables from {@link question_edit_setup()}.
* @return string HTML to output.
*/
public function edit_page(\quiz $quizobj, structure $structure,
\question_edit_contexts $contexts, \moodle_url $pageurl, array $pagevars) {
$output = '';
// Page title.
$output .= $this->heading_with_help(get_string('editingquizx', 'quiz',
format_string($quizobj->get_quiz_name())), 'editingquiz', 'quiz', '',
get_string('basicideasofquiz', 'quiz'), 2);
// Information at the top.
$output .= $this->quiz_state_warnings($structure);
$output .= html_writer::start_div('mod_quiz-edit-top-controls');
$output .= $this->quiz_information($structure);
$output .= $this->maximum_grade_input($structure, $pageurl);
$output .= html_writer::start_div('mod_quiz-edit-action-buttons btn-group edit-toolbar', ['role' => 'group']);
$output .= $this->repaginate_button($structure, $pageurl);
$output .= $this->selectmultiple_button($structure);
$output .= html_writer::end_tag('div');
$output .= $this->total_marks($quizobj->get_quiz());
$output .= $this->selectmultiple_controls($structure);
$output .= html_writer::end_tag('div');
// Show the questions organised into sections and pages.
$output .= $this->start_section_list($structure);
foreach ($structure->get_sections() as $section) {
$output .= $this->start_section($structure, $section);
$output .= $this->questions_in_section($structure, $section, $contexts, $pagevars, $pageurl);
if ($structure->is_last_section($section)) {
$output .= \html_writer::start_div('last-add-menu');
$output .= html_writer::tag('span', $this->add_menu_actions($structure, 0,
$pageurl, $contexts, $pagevars), array('class' => 'add-menu-outer'));
$output .= \html_writer::end_div();
}
$output .= $this->end_section();
}
$output .= $this->end_section_list();
// Initialise the JavaScript.
$this->initialise_editing_javascript($structure, $contexts, $pagevars, $pageurl);
// Include the contents of any other popups required.
if ($structure->can_be_edited()) {
$thiscontext = $contexts->lowest();
$this->page->requires->js_call_amd('mod_quiz/quizquestionbank', 'init', [
$thiscontext->id
]);
$this->page->requires->js_call_amd('mod_quiz/add_random_question', 'init', [
$thiscontext->id,
$pagevars['cat'],
$pageurl->out_as_local_url(true),
$pageurl->param('cmid')
]);
// Include the question chooser.
$output .= $this->question_chooser();
}
return $output;
}
/**
* Render any warnings that might be required about the state of the quiz,
* e.g. if it has been attempted, or if the shuffle questions option is
* turned on.
*
* @param structure $structure the quiz structure.
* @return string HTML to output.
*/
public function quiz_state_warnings(structure $structure) {
$warnings = $structure->get_edit_page_warnings();
if (empty($warnings)) {
return '';
}
$output = array();
foreach ($warnings as $warning) {
$output[] = \html_writer::tag('p', $warning);
}
return $this->box(implode("\n", $output), 'statusdisplay');
}
/**
* Render the status bar.
*
* @param structure $structure the quiz structure.
* @return string HTML to output.
*/
public function quiz_information(structure $structure) {
list($currentstatus, $explanation) = $structure->get_dates_summary();
$output = html_writer::span(
get_string('numquestionsx', 'quiz', $structure->get_question_count()),
'numberofquestions') . ' | ' .
html_writer::span($currentstatus, 'quizopeningstatus',
array('title' => $explanation));
return html_writer::div($output, 'statusbar');
}
/**
* Render the form for setting a quiz' overall grade
*
* @param structure $structure the quiz structure.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function maximum_grade_input($structure, \moodle_url $pageurl) {
$output = '';
$output .= html_writer::start_div('maxgrade');
$output .= html_writer::start_tag('form', array('method' => 'post', 'action' => 'edit.php',
'class' => 'quizsavegradesform form-inline'));
$output .= html_writer::start_tag('fieldset', array('class' => 'invisiblefieldset'));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$output .= html_writer::input_hidden_params($pageurl);
$output .= html_writer::tag('label', get_string('maximumgrade') . ' ',
array('for' => 'inputmaxgrade'));
$output .= html_writer::empty_tag('input', array('type' => 'text', 'id' => 'inputmaxgrade',
'name' => 'maxgrade', 'size' => ($structure->get_decimal_places_for_grades() + 2),
'value' => $structure->formatted_quiz_grade(),
'class' => 'form-control'));
$output .= html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'btn btn-secondary m-l-1',
'name' => 'savechanges', 'value' => get_string('save', 'quiz')));
$output .= html_writer::end_tag('fieldset');
$output .= html_writer::end_tag('form');
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Return the repaginate button
* @param structure $structure the structure of the quiz being edited.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
protected function repaginate_button(structure $structure, \moodle_url $pageurl) {
$header = html_writer::tag('span', get_string('repaginatecommand', 'quiz'), array('class' => 'repaginatecommand'));
$form = $this->repaginate_form($structure, $pageurl);
$buttonoptions = array(
'type' => 'submit',
'name' => 'repaginate',
'id' => 'repaginatecommand',
'value' => get_string('repaginatecommand', 'quiz'),
'class' => 'btn btn-secondary m-b-1',
'data-header' => $header,
'data-form' => $form,
);
if (!$structure->can_be_repaginated()) {
$buttonoptions['disabled'] = 'disabled';
} else {
$this->page->requires->yui_module('moodle-mod_quiz-repaginate', 'M.mod_quiz.repaginate.init');
}
return html_writer::empty_tag('input', $buttonoptions);
}
/**
* Generate the bulk action button.
*
* @param structure $structure the structure of the quiz being edited.
* @return string HTML to output.
*/
protected function selectmultiple_button(structure $structure) {
$buttonoptions = array(
'type' => 'button',
'name' => 'selectmultiple',
'id' => 'selectmultiplecommand',
'value' => get_string('selectmultipleitems', 'quiz'),
'class' => 'btn btn-secondary m-b-1'
);
if (!$structure->can_be_edited()) {
$buttonoptions['disabled'] = 'disabled';
}
return html_writer::tag('button', get_string('selectmultipleitems', 'quiz'), $buttonoptions);
}
/**
* Generate the controls that appear when the bulk action button is pressed.
*
* @param structure $structure the structure of the quiz being edited.
* @return string HTML to output.
*/
protected function selectmultiple_controls(structure $structure) {
$output = '';
// Bulk action button delete and bulk action button cancel.
$buttondeleteoptions = array(
'type' => 'button',
'id' => 'selectmultipledeletecommand',
'value' => get_string('deleteselected', 'mod_quiz'),
'class' => 'btn btn-secondary'
);
$buttoncanceloptions = array(
'type' => 'button',
'id' => 'selectmultiplecancelcommand',
'value' => get_string('cancel', 'moodle'),
'class' => 'btn btn-secondary'
);
$groupoptions = array(
'class' => 'btn-group selectmultiplecommand actions',
'role' => 'group'
);
$output .= html_writer::tag('div',
html_writer::tag('button', get_string('deleteselected', 'mod_quiz'), $buttondeleteoptions) .
" " .
html_writer::tag('button', get_string('cancel', 'moodle'),
$buttoncanceloptions), $groupoptions);
$toolbaroptions = array(
'class' => 'btn-toolbar',
'role' => 'toolbar',
'aria-label' => get_string('selectmultipletoolbar', 'quiz'),
);
// Select all/deselect all questions.
$buttonselectalloptions = array(
'role' => 'button',
'id' => 'questionselectall',
'class' => 'btn btn-link'
);
$buttondeselectalloptions = array(
'role' => 'button',
'id' => 'questiondeselectall',
'class' => 'btn btn-link'
);
$output .= html_writer::tag('div',
html_writer::tag('div',
html_writer::link('#', get_string('selectall', 'quiz'), $buttonselectalloptions) .
html_writer::tag('span', "/", ['class' => 'separator']) .
html_writer::link('#', get_string('selectnone', 'quiz'), $buttondeselectalloptions),
array('class' => 'btn-group selectmultiplecommandbuttons')),
$toolbaroptions);
return $output;
}
/**
* Return the repaginate form
* @param structure $structure the structure of the quiz being edited.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
protected function repaginate_form(structure $structure, \moodle_url $pageurl) {
$perpage = array();
$perpage[0] = get_string('allinone', 'quiz');
for ($i = 1; $i <= 50; ++$i) {
$perpage[$i] = $i;
}
$hiddenurl = clone($pageurl);
$hiddenurl->param('sesskey', sesskey());
$select = html_writer::select($perpage, 'questionsperpage',
$structure->get_questions_per_page(), false, array('class' => 'custom-select'));
$buttonattributes = array(
'type' => 'submit',
'name' => 'repaginate',
'value' => get_string('go'),
'class' => 'btn btn-secondary m-l-1'
);
$formcontent = html_writer::tag('form', html_writer::div(
html_writer::input_hidden_params($hiddenurl) .
get_string('repaginate', 'quiz', $select) .
html_writer::empty_tag('input', $buttonattributes)
), array('action' => 'edit.php', 'method' => 'post'));
return html_writer::div($formcontent, '', array('id' => 'repaginatedialog'));
}
/**
* Render the total marks available for the quiz.
*
* @param \stdClass $quiz the quiz settings from the database.
* @return string HTML to output.
*/
public function total_marks($quiz) {
$totalmark = html_writer::span(quiz_format_grade($quiz, $quiz->sumgrades), 'mod_quiz_summarks');
return html_writer::tag('span',
get_string('totalmarksx', 'quiz', $totalmark),
array('class' => 'totalpoints'));
}
/**
* Generate the starting container html for the start of a list of sections
* @param structure $structure the structure of the quiz being edited.
* @return string HTML to output.
*/
protected function start_section_list(structure $structure) {
$class = 'slots';
if ($structure->get_section_count() == 1) {
$class .= ' only-one-section';
}
return html_writer::start_tag('ul', array('class' => $class));
}
/**
* Generate the closing container html for the end of a list of sections
* @return string HTML to output.
*/
protected function end_section_list() {
return html_writer::end_tag('ul');
}
/**
* Display the start of a section, before the questions.
*
* @param structure $structure the structure of the quiz being edited.
* @param \stdClass $section The quiz_section entry from DB
* @return string HTML to output.
*/
protected function start_section($structure, $section) {
$output = '';
$sectionstyle = '';
if ($structure->is_only_one_slot_in_section($section)) {
$sectionstyle = ' only-has-one-slot';
}
$output .= html_writer::start_tag('li', array('id' => 'section-'.$section->id,
'class' => 'section main clearfix'.$sectionstyle, 'role' => 'region',
'aria-label' => $section->heading));
$output .= html_writer::start_div('content');
$output .= html_writer::start_div('section-heading');
$headingtext = $this->heading(html_writer::span(
html_writer::span($section->heading, 'instancesection'), 'sectioninstance'), 3);
if (!$structure->can_be_edited()) {
$editsectionheadingicon = '';
} else {
$editsectionheadingicon = html_writer::link(new \moodle_url('#'),
$this->pix_icon('t/editstring', get_string('sectionheadingedit', 'quiz', $section->heading),
'moodle', array('class' => 'editicon visibleifjs')),
array('class' => 'editing_section', 'data-action' => 'edit_section_title'));
}
$output .= html_writer::div($headingtext . $editsectionheadingicon, 'instancesectioncontainer');
if (!$structure->is_first_section($section) && $structure->can_be_edited()) {
$output .= $this->section_remove_icon($section);
}
$output .= $this->section_shuffle_questions($structure, $section);
$output .= html_writer::end_div($output, 'section-heading');
return $output;
}
/**
* Display a checkbox for shuffling question within a section.
*
* @param structure $structure object containing the structure of the quiz.
* @param \stdClass $section data from the quiz_section table.
* @return string HTML to output.
*/
public function section_shuffle_questions(structure $structure, $section) {
$checkboxattributes = array(
'type' => 'checkbox',
'id' => 'shuffle-' . $section->id,
'value' => 1,
'data-action' => 'shuffle_questions',
'class' => 'cm-edit-action',
);
if (!$structure->can_be_edited()) {
$checkboxattributes['disabled'] = 'disabled';
}
if ($section->shufflequestions) {
$checkboxattributes['checked'] = 'checked';
}
if ($structure->is_first_section($section)) {
$help = $this->help_icon('shufflequestions', 'quiz');
} else {
$help = '';
}
$helpspan = html_writer::span($help, 'shuffle-help-tip');
$progressspan = html_writer::span('', 'shuffle-progress');
$checkbox = html_writer::empty_tag('input', $checkboxattributes);
$label = html_writer::label(get_string('shufflequestions', 'quiz'),
$checkboxattributes['id'], false);
return html_writer::span($progressspan . $checkbox . $label. ' ' . $helpspan,
'instanceshufflequestions', array('data-action' => 'shuffle_questions'));
}
/**
* Display the end of a section, after the questions.
*
* @return string HTML to output.
*/
protected function end_section() {
$output = html_writer::end_tag('div');
$output .= html_writer::end_tag('li');
return $output;
}
/**
* Render an icon to remove a section from the quiz.
*
* @param object $section the section to be removed.
* @return string HTML to output.
*/
public function section_remove_icon($section) {
$title = get_string('sectionheadingremove', 'quiz', $section->heading);
$url = new \moodle_url('/mod/quiz/edit.php',
array('sesskey' => sesskey(), 'removesection' => '1', 'sectionid' => $section->id));
$image = $this->pix_icon('t/delete', $title);
return $this->action_link($url, $image, null, array(
'class' => 'cm-edit-action editing_delete', 'data-action' => 'deletesection'));
}
/**
* Renders HTML to display the questions in a section of the quiz.
*
* This function calls {@link core_course_renderer::quiz_section_question()}
*
* @param structure $structure object containing the structure of the quiz.
* @param \stdClass $section information about the section.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function questions_in_section(structure $structure, $section,
$contexts, $pagevars, $pageurl) {
$output = '';
foreach ($structure->get_slots_in_section($section->id) as $slot) {
$output .= $this->question_row($structure, $slot, $contexts, $pagevars, $pageurl);
}
return html_writer::tag('ul', $output, array('class' => 'section img-text'));
}
/**
* Displays one question with the surrounding controls.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot which slot we are outputting.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function question_row(structure $structure, $slot, $contexts, $pagevars, $pageurl) {
$output = '';
$output .= $this->page_row($structure, $slot, $contexts, $pagevars, $pageurl);
// Page split/join icon.
$joinhtml = '';
if ($structure->can_be_edited() && !$structure->is_last_slot_in_quiz($slot) &&
!$structure->is_last_slot_in_section($slot)) {
$joinhtml = $this->page_split_join_button($structure, $slot);
}
// Question HTML.
$questionhtml = $this->question($structure, $slot, $pageurl);
$qtype = $structure->get_question_type_for_slot($slot);
$questionclasses = 'activity ' . $qtype . ' qtype_' . $qtype . ' slot';
$output .= html_writer::tag('li', $questionhtml . $joinhtml,
array('class' => $questionclasses, 'id' => 'slot-' . $structure->get_slot_id_for_slot($slot),
'data-canfinish' => $structure->can_finish_during_the_attempt($slot)));
return $output;
}
/**
* Displays one question with the surrounding controls.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function page_row(structure $structure, $slot, $contexts, $pagevars, $pageurl) {
$output = '';
$pagenumber = $structure->get_page_number_for_slot($slot);
// Put page in a heading for accessibility and styling.
$page = $this->heading(get_string('page') . ' ' . $pagenumber, 4);
if ($structure->is_first_slot_on_page($slot)) {
// Add the add-menu at the page level.
$addmenu = html_writer::tag('span', $this->add_menu_actions($structure,
$pagenumber, $pageurl, $contexts, $pagevars),
array('class' => 'add-menu-outer'));
$addquestionform = $this->add_question_form($structure,
$pagenumber, $pageurl, $pagevars);
$output .= html_writer::tag('li', $page . $addmenu . $addquestionform,
array('class' => 'pagenumber activity yui3-dd-drop page', 'id' => 'page-' . $pagenumber));
}
return $output;
}
/**
* Returns the add menu that is output once per page.
* @param structure $structure object containing the structure of the quiz.
* @param int $page the page number that this menu will add to.
* @param \moodle_url $pageurl the canonical URL of this page.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @return string HTML to output.
*/
public function add_menu_actions(structure $structure, $page, \moodle_url $pageurl,
\question_edit_contexts $contexts, array $pagevars) {
$actions = $this->edit_menu_actions($structure, $page, $pageurl, $pagevars);
if (empty($actions)) {
return '';
}
$menu = new \action_menu();
$menu->set_alignment(\action_menu::TR, \action_menu::TR);
$menu->set_constraint('.mod-quiz-edit-content');
$trigger = html_writer::tag('span', get_string('add', 'quiz'), array('class' => 'add-menu'));
$menu->set_menu_trigger($trigger);
// The menu appears within an absolutely positioned element causing width problems.
// Make sure no-wrap is set so that we don't get a squashed menu.
$menu->set_nowrap_on_items(true);
// Disable the link if quiz has attempts.
if (!$structure->can_be_edited()) {
return $this->render($menu);
}
foreach ($actions as $action) {
if ($action instanceof \action_menu_link) {
$action->add_class('add-menu');
}
$menu->add($action);
}
$menu->attributes['class'] .= ' page-add-actions commands';
// Prioritise the menu ahead of all other actions.
$menu->prioritise = true;
return $this->render($menu);
}
/**
* Returns the list of actions to go in the add menu.
* @param structure $structure object containing the structure of the quiz.
* @param int $page the page number that this menu will add to.
* @param \moodle_url $pageurl the canonical URL of this page.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @return array the actions.
*/
public function edit_menu_actions(structure $structure, $page,
\moodle_url $pageurl, array $pagevars) {
$questioncategoryid = question_get_category_id_from_pagevars($pagevars);
static $str;
if (!isset($str)) {
$str = get_strings(array('addasection', 'addaquestion', 'addarandomquestion',
'addarandomselectedquestion', 'questionbank'), 'quiz');
}
// Get section, page, slotnumber and maxmark.
$actions = array();
// Add a new question to the quiz.
$returnurl = new \moodle_url($pageurl, array('addonpage' => $page));
$params = array('returnurl' => $returnurl->out_as_local_url(false),
'cmid' => $structure->get_cmid(), 'category' => $questioncategoryid,
'addonpage' => $page, 'appendqnumstring' => 'addquestion');
$actions['addaquestion'] = new \action_menu_link_secondary(
new \moodle_url('/question/addquestion.php', $params),
new \pix_icon('t/add', $str->addaquestion, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$str->addaquestion, array('class' => 'cm-edit-action addquestion', 'data-action' => 'addquestion')
);
// Call question bank.
$icon = new \pix_icon('t/add', $str->questionbank, 'moodle', array('class' => 'iconsmall', 'title' => ''));
if ($page) {
$title = get_string('addquestionfrombanktopage', 'quiz', $page);
} else {
$title = get_string('addquestionfrombankatend', 'quiz');
}
$attributes = array('class' => 'cm-edit-action questionbank',
'data-header' => $title, 'data-action' => 'questionbank', 'data-addonpage' => $page);
$actions['questionbank'] = new \action_menu_link_secondary($pageurl, $icon, $str->questionbank, $attributes);
// Add a random question.
$returnurl = new \moodle_url('/mod/quiz/edit.php', array('cmid' => $structure->get_cmid(), 'data-addonpage' => $page));
$params = array('returnurl' => $returnurl, 'cmid' => $structure->get_cmid(), 'appendqnumstring' => 'addarandomquestion');
$url = new \moodle_url('/mod/quiz/addrandom.php', $params);
$icon = new \pix_icon('t/add', $str->addarandomquestion, 'moodle', array('class' => 'iconsmall', 'title' => ''));
$attributes = array('class' => 'cm-edit-action addarandomquestion', 'data-action' => 'addarandomquestion');
if ($page) {
$title = get_string('addrandomquestiontopage', 'quiz', $page);
} else {
$title = get_string('addrandomquestionatend', 'quiz');
}
$attributes = array_merge(array('data-header' => $title, 'data-addonpage' => $page), $attributes);
$actions['addarandomquestion'] = new \action_menu_link_secondary($url, $icon, $str->addarandomquestion, $attributes);
// Add a new section to the add_menu if possible. This is always added to the HTML
// then hidden with CSS when no needed, so that as things are re-ordered, etc. with
// Ajax it can be relevaled again when necessary.
$params = array('cmid' => $structure->get_cmid(), 'addsectionatpage' => $page);
$actions['addasection'] = new \action_menu_link_secondary(
new \moodle_url($pageurl, $params),
new \pix_icon('t/add', $str->addasection, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$str->addasection, array('class' => 'cm-edit-action addasection', 'data-action' => 'addasection')
);
return $actions;
}
/**
* Render the form that contains the data for adding a new question to the quiz.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $page the page number that this menu will add to.
* @param \moodle_url $pageurl the canonical URL of this page.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @return string HTML to output.
*/
protected function add_question_form(structure $structure, $page, \moodle_url $pageurl, array $pagevars) {
$questioncategoryid = question_get_category_id_from_pagevars($pagevars);
$output = html_writer::tag('input', null,
array('type' => 'hidden', 'name' => 'returnurl',
'value' => $pageurl->out_as_local_url(false, array('addonpage' => $page))));
$output .= html_writer::tag('input', null,
array('type' => 'hidden', 'name' => 'cmid', 'value' => $structure->get_cmid()));
$output .= html_writer::tag('input', null,
array('type' => 'hidden', 'name' => 'appendqnumstring', 'value' => 'addquestion'));
$output .= html_writer::tag('input', null,
array('type' => 'hidden', 'name' => 'category', 'value' => $questioncategoryid));
return html_writer::tag('form', html_writer::div($output),
array('class' => 'addnewquestion', 'method' => 'post',
'action' => new \moodle_url('/question/addquestion.php')));
}
/**
* Display a question.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function question(structure $structure, $slot, \moodle_url $pageurl) {
$output = '';
$output .= html_writer::start_tag('div');
if ($structure->can_be_edited()) {
$output .= $this->question_move_icon($structure, $slot);
}
$output .= html_writer::start_div('mod-indent-outer');
$output .= html_writer::tag('input', '', array('id' => 'selectquestion-' .
$structure->get_displayed_number_for_slot($slot), 'name' => 'selectquestion[]',
'type' => 'checkbox', 'class' => 'select-multiple-checkbox',
'value' => $structure->get_displayed_number_for_slot($slot)));
$output .= $this->question_number($structure->get_displayed_number_for_slot($slot));
// This div is used to indent the content.
$output .= html_writer::div('', 'mod-indent');
// Display the link to the question (or do nothing if question has no url).
if ($structure->get_question_type_for_slot($slot) == 'random') {
$questionname = $this->random_question($structure, $slot, $pageurl);
} else {
$questionname = $this->question_name($structure, $slot, $pageurl);
}
// Start the div for the activity title, excluding the edit icons.
$output .= html_writer::start_div('activityinstance');
$output .= $questionname;
// Closing the tag which contains everything but edit icons. Content part of the module should not be part of this.
$output .= html_writer::end_tag('div'); // .activityinstance.
// Action icons.
$questionicons = '';
$questionicons .= $this->question_preview_icon($structure->get_quiz(), $structure->get_question_in_slot($slot));
if ($structure->can_be_edited()) {
$questionicons .= $this->question_remove_icon($structure, $slot, $pageurl);
}
$questionicons .= $this->marked_out_of_field($structure, $slot);
$output .= html_writer::span($questionicons, 'actions'); // Required to add js spinner icon.
if ($structure->can_be_edited()) {
$output .= $this->question_dependency_icon($structure, $slot);
}
// End of indentation div.
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Render the move icon.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @return string The markup for the move action.
*/
public function question_move_icon(structure $structure, $slot) {
return html_writer::link(new \moodle_url('#'),
$this->pix_icon('i/dragdrop', get_string('move'), 'moodle', array('class' => 'iconsmall', 'title' => '')),
array('class' => 'editing_move', 'data-action' => 'move')
);
}
/**
* Output the question number.
* @param string $number The number, or 'i'.
* @return string HTML to output.
*/
public function question_number($number) {
if (is_numeric($number)) {
$number = html_writer::span(get_string('question'), 'accesshide') . ' ' . $number;
}
return html_writer::tag('span', $number, array('class' => 'slotnumber'));
}
/**
* Render the preview icon.
*
* @param \stdClass $quiz the quiz settings from the database.
* @param \stdClass $question data from the question and quiz_slots tables.
* @param bool $label if true, show the preview question label after the icon
* @param int $variant which question variant to preview (optional).
* @return string HTML to output.
*/
public function question_preview_icon($quiz, $question, $label = null, $variant = null) {
$url = quiz_question_preview_url($quiz, $question, $variant);
// Do we want a label?
$strpreviewlabel = '';
if ($label) {
$strpreviewlabel = ' ' . get_string('preview', 'quiz');
}
// Build the icon.
$strpreviewquestion = get_string('previewquestion', 'quiz');
$image = $this->pix_icon('t/preview', $strpreviewquestion);
$action = new \popup_action('click', $url, 'questionpreview',
question_preview_popup_params());
return $this->action_link($url, $image . $strpreviewlabel, $action,
array('title' => $strpreviewquestion, 'class' => 'preview'));
}
/**
* Render an icon to remove a question from the quiz.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @param \moodle_url $pageurl the canonical URL of the edit page.
* @return string HTML to output.
*/
public function question_remove_icon(structure $structure, $slot, $pageurl) {
$url = new \moodle_url($pageurl, array('sesskey' => sesskey(), 'remove' => $slot));
$strdelete = get_string('delete');
$image = $this->pix_icon('t/delete', $strdelete);
return $this->action_link($url, $image, null, array('title' => $strdelete,
'class' => 'cm-edit-action editing_delete', 'data-action' => 'delete'));
}
/**
* Display an icon to split or join two pages of the quiz.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @return string HTML to output.
*/
public function page_split_join_button($structure, $slot) {
$insertpagebreak = !$structure->is_last_slot_on_page($slot);
$url = new \moodle_url('repaginate.php', array('quizid' => $structure->get_quizid(),
'slot' => $slot, 'repag' => $insertpagebreak ? 2 : 1, 'sesskey' => sesskey()));
if ($insertpagebreak) {
$title = get_string('addpagebreak', 'quiz');
$image = $this->image_icon('e/insert_page_break', $title);
$action = 'addpagebreak';
} else {
$title = get_string('removepagebreak', 'quiz');
$image = $this->image_icon('e/remove_page_break', $title);
$action = 'removepagebreak';
}
// Disable the link if quiz has attempts.
$disabled = null;
if (!$structure->can_be_edited()) {
$disabled = 'disabled';
}
return html_writer::span($this->action_link($url, $image, null, array('title' => $title,
'class' => 'page_split_join cm-edit-action', 'disabled' => $disabled, 'data-action' => $action)),
'page_split_join_wrapper');
}
/**
* Display the icon for whether this question can only be seen if the previous
* one has been answered.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot the first slot on the page we are outputting.
* @return string HTML to output.
*/
public function question_dependency_icon($structure, $slot) {
$a = array(
'thisq' => $structure->get_displayed_number_for_slot($slot),
'previousq' => $structure->get_displayed_number_for_slot(max($slot - 1, 1)),
);
if ($structure->is_question_dependent_on_previous_slot($slot)) {
$title = get_string('questiondependencyremove', 'quiz', $a);
$image = $this->pix_icon('t/locked', get_string('questiondependsonprevious', 'quiz'),
'moodle', array('title' => ''));
$action = 'removedependency';
} else {
$title = get_string('questiondependencyadd', 'quiz', $a);
$image = $this->pix_icon('t/unlocked', get_string('questiondependencyfree', 'quiz'),
'moodle', array('title' => ''));
$action = 'adddependency';
}
// Disable the link if quiz has attempts.
$disabled = null;
if (!$structure->can_be_edited()) {
$disabled = 'disabled';
}
$extraclass = '';
if (!$structure->can_question_depend_on_previous_slot($slot)) {
$extraclass = ' question_dependency_cannot_depend';
}
return html_writer::span($this->action_link('#', $image, null, array('title' => $title,
'class' => 'cm-edit-action', 'disabled' => $disabled, 'data-action' => $action)),
'question_dependency_wrapper' . $extraclass);
}
/**
* Renders html to display a name with the link to the question on a quiz edit page
*
* If the user does not have permission to edi the question, it is rendered
* without a link
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slot which slot we are outputting.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function question_name(structure $structure, $slot, $pageurl) {
$output = '';
$question = $structure->get_question_in_slot($slot);
$editurl = new \moodle_url('/question/question.php', array(
'returnurl' => $pageurl->out_as_local_url(),
'cmid' => $structure->get_cmid(), 'id' => $question->id));
$instancename = quiz_question_tostring($question);
$qtype = \question_bank::get_qtype($question->qtype, false);
$namestr = $qtype->local_name();
$icon = $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr,
'class' => 'icon activityicon', 'alt' => ' ', 'role' => 'presentation'));
$editicon = $this->pix_icon('t/edit', '', 'moodle', array('title' => ''));
// Need plain question name without html tags for link title.
$title = shorten_text(format_string($question->name), 100);
// Display the link itself.
$activitylink = $icon . html_writer::tag('span', $editicon . $instancename, array('class' => 'instancename'));
$output .= html_writer::link($editurl, $activitylink,
array('title' => get_string('editquestion', 'quiz').' '.$title));
return $output;
}
/**
* Renders html to display a random question the link to edit the configuration
* and also to see that category in the question bank.
*
* @param structure $structure object containing the structure of the quiz.
* @param int $slotnumber which slot we are outputting.
* @param \moodle_url $pageurl the canonical URL of this page.
* @return string HTML to output.
*/
public function random_question(structure $structure, $slotnumber, $pageurl) {
$question = $structure->get_question_in_slot($slotnumber);
$slot = $structure->get_slot_by_number($slotnumber);
$slottags = $structure->get_slot_tags_for_slot_id($slot->id);
$editurl = new \moodle_url('/mod/quiz/editrandom.php',
array('returnurl' => $pageurl->out_as_local_url(), 'slotid' => $slot->id));
$temp = clone($question);
$temp->questiontext = '';
$instancename = quiz_question_tostring($temp);
$configuretitle = get_string('configurerandomquestion', 'quiz');
$qtype = \question_bank::get_qtype($question->qtype, false);
$namestr = $qtype->local_name();
$icon = $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr,
'class' => 'icon activityicon', 'alt' => ' ', 'role' => 'presentation'));
$editicon = $this->pix_icon('t/edit', $configuretitle, 'moodle', array('title' => ''));
$qbankurlparams = array(
'cmid' => $structure->get_cmid(),
'cat' => $question->category . ',' . $question->contextid,
'recurse' => !empty($question->questiontext)
);
foreach ($slottags as $index => $slottag) {
$qbankurlparams["qtagids[{$index}]"] = $slottag->tagid;
}
// If this is a random question, display a link to show the questions
// selected from in the question bank.
$qbankurl = new \moodle_url('/question/edit.php', $qbankurlparams);
$qbanklink = ' ' . \html_writer::link($qbankurl,
get_string('seequestions', 'quiz'), array('class' => 'mod_quiz_random_qbank_link'));
return html_writer::link($editurl, $icon . $editicon, array('title' => $configuretitle)) .