forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
2457 lines (2235 loc) · 108 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
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 for use with the course section and all the goodness that falls
* within it.
*
* This renderer should contain methods useful to courses, and categories.
*
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The core course renderer
*
* Can be retrieved with the following:
* $renderer = $PAGE->get_renderer('core','course');
*/
class core_course_renderer extends plugin_renderer_base {
const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */
const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */
const COURSECAT_SHOW_COURSES_COLLAPSED = 10;
const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */
const COURSECAT_SHOW_COURSES_EXPANDED = 20;
const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
const COURSECAT_TYPE_CATEGORY = 0;
const COURSECAT_TYPE_COURSE = 1;
/**
* A cache of strings
* @var stdClass
*/
protected $strings;
/**
* Whether a category content is being initially rendered with children. This is mainly used by the
* core_course_renderer::corsecat_tree() to render the appropriate action for the Expand/Collapse all link on
* page load.
* @var bool
*/
protected $categoryexpandedonload = false;
/**
* Override the constructor so that we can initialise the string cache
*
* @param moodle_page $page
* @param string $target
*/
public function __construct(moodle_page $page, $target) {
$this->strings = new stdClass;
parent::__construct($page, $target);
}
/**
* Adds the item in course settings navigation to toggle modchooser
*
* Theme can overwrite as an empty function to exclude it (for example if theme does not
* use modchooser at all)
*
* @deprecated since 3.2
*/
protected function add_modchoosertoggle() {
debugging('core_course_renderer::add_modchoosertoggle() is deprecated.', DEBUG_DEVELOPER);
global $CFG;
// Only needs to be done once per page.
if (!$this->page->requires->should_create_one_time_item_now('core_course_modchoosertoggle')) {
return;
}
if ($this->page->state > moodle_page::STATE_PRINTING_HEADER ||
$this->page->course->id == SITEID ||
!$this->page->user_is_editing() ||
!($context = context_course::instance($this->page->course->id)) ||
!has_capability('moodle/course:manageactivities', $context) ||
!course_ajax_enabled($this->page->course) ||
!($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) ||
!($turneditingnode = $coursenode->get('turneditingonoff'))) {
// Too late, or we are on site page, or we could not find the
// adjacent nodes in course settings menu, or we are not allowed to edit.
return;
}
if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
// We are on the course page, retain the current page params e.g. section.
$modchoosertoggleurl = clone($this->page->url);
} else {
// Edit on the main course page.
$modchoosertoggleurl = new moodle_url('/course/view.php', array('id' => $this->page->course->id,
'return' => $this->page->url->out_as_local_url(false)));
}
$modchoosertoggleurl->param('sesskey', sesskey());
if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) {
$modchoosertogglestring = get_string('modchooserdisable', 'moodle');
$modchoosertoggleurl->param('modchooser', 'off');
} else {
$modchoosertogglestring = get_string('modchooserenable', 'moodle');
$modchoosertoggleurl->param('modchooser', 'on');
}
$modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING, null, 'modchoosertoggle');
// Insert the modchoosertoggle after the settings node 'turneditingonoff' (navigation_node only has function to insert before, so we insert before and then swap).
$coursenode->add_node($modchoosertoggle, 'turneditingonoff');
$turneditingnode->remove();
$coursenode->add_node($turneditingnode, 'modchoosertoggle');
$modchoosertoggle->add_class('modchoosertoggle');
$modchoosertoggle->add_class('visibleifjs');
user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
}
/**
* Renders course info box.
*
* @param stdClass|course_in_list $course
* @return string
*/
public function course_info_box(stdClass $course) {
$content = '';
$content .= $this->output->box_start('generalbox info');
$chelper = new coursecat_helper();
$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
$content .= $this->coursecat_coursebox($chelper, $course);
$content .= $this->output->box_end();
return $content;
}
/**
* Renderers a structured array of courses and categories into a nice XHTML tree structure.
*
* @deprecated since 2.5
*
* Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
*
* @param array $ignored argument ignored
* @return string
*/
public final function course_category_tree(array $ignored) {
debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER);
return $this->frontpage_combo_list();
}
/**
* Renderers a category for use with course_category_tree
*
* @deprecated since 2.5
*
* Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
*
* @param array $category
* @param int $depth
* @return string
*/
protected final function course_category_tree_category(stdClass $category, $depth=1) {
debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER);
return '';
}
/**
* Render a modchooser.
*
* @param renderable $modchooser The chooser.
* @return string
*/
public function render_modchooser(renderable $modchooser) {
return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
}
/**
* Build the HTML for the module chooser javascript popup
*
* @param array $modules A set of modules as returned form @see
* get_module_metadata
* @param object $course The course that will be displayed
* @return string The composed HTML for the module
*/
public function course_modchooser($modules, $course) {
if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
return '';
}
$modchooser = new \core_course\output\modchooser($course, $modules);
return $this->render($modchooser);
}
/**
* Build the HTML for a specified set of modules
*
* @param array $modules A set of modules as used by the
* course_modchooser_module function
* @return string The composed HTML for the module
*/
protected function course_modchooser_module_types($modules) {
debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
/**
* Return the HTML for the specified module adding any required classes
*
* @param object $module An object containing the title, and link. An
* icon, and help text may optionally be specified. If the module
* contains subtypes in the types option, then these will also be
* displayed.
* @param array $classes Additional classes to add to the encompassing
* div element
* @return string The composed HTML for the module
*/
protected function course_modchooser_module($module, $classes = array('option')) {
debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
protected function course_modchooser_title($title, $identifier = null) {
debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
/**
* Renders HTML for displaying the sequence of course module editing buttons
*
* @see course_get_cm_edit_actions()
*
* @param action_link[] $actions Array of action_link objects
* @param cm_info $mod The module we are displaying actions for.
* @param array $displayoptions additional display options:
* ownerselector => A JS/CSS selector that can be used to find an cm node.
* If specified the owning node will be given the class 'action-menu-shown' when the action
* menu is being displayed.
* constraintselector => A JS/CSS selector that can be used to find the parent node for which to constrain
* the action menu to when it is being displayed.
* donotenhance => If set to true the action menu that gets displayed won't be enhanced by JS.
* @return string
*/
public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) {
global $CFG;
if (empty($actions)) {
return '';
}
if (isset($displayoptions['ownerselector'])) {
$ownerselector = $displayoptions['ownerselector'];
} else if ($mod) {
$ownerselector = '#module-'.$mod->id;
} else {
debugging('You should upgrade your call to '.__FUNCTION__.' and provide $mod', DEBUG_DEVELOPER);
$ownerselector = 'li.activity';
}
if (isset($displayoptions['constraintselector'])) {
$constraint = $displayoptions['constraintselector'];
} else {
$constraint = '.course-content';
}
$menu = new action_menu();
$menu->set_owner_selector($ownerselector);
$menu->set_constraint($constraint);
$menu->set_alignment(action_menu::TR, action_menu::BR);
$menu->set_menu_trigger(get_string('edit'));
foreach ($actions as $action) {
if ($action instanceof action_menu_link) {
$action->add_class('cm-edit-action');
}
$menu->add($action);
}
$menu->attributes['class'] .= ' section-cm-edit-actions commands';
// Prioritise the menu ahead of all other actions.
$menu->prioritise = true;
return $this->render($menu);
}
/**
* Renders HTML for the menus to add activities and resources to the current course
*
* @param stdClass $course
* @param int $section relative section number (field course_sections.section)
* @param int $sectionreturn The section to link back to
* @param array $displayoptions additional display options, for example blocks add
* option 'inblock' => true, suggesting to display controls vertically
* @return string
*/
function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
global $CFG;
$vertical = !empty($displayoptions['inblock']);
// check to see if user can add menus and there are modules to add
if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
|| !$this->page->user_is_editing()
|| !($modnames = get_module_types_names()) || empty($modnames)) {
return '';
}
// Retrieve all modules with associated metadata
$modules = get_module_metadata($course, $modnames, $sectionreturn);
$urlparams = array('section' => $section);
// We'll sort resources and activities into two lists
$activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
foreach ($modules as $module) {
$activityclass = MOD_CLASS_ACTIVITY;
if ($module->archetype == MOD_ARCHETYPE_RESOURCE) {
$activityclass = MOD_CLASS_RESOURCE;
} else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) {
// System modules cannot be added by user, do not add to dropdown.
continue;
}
$link = $module->link->out(true, $urlparams);
$activities[$activityclass][$link] = $module->title;
}
$straddactivity = get_string('addactivity');
$straddresource = get_string('addresource');
$sectionname = get_section_name($course, $section);
$strresourcelabel = get_string('addresourcetosection', null, $sectionname);
$stractivitylabel = get_string('addactivitytosection', null, $sectionname);
$output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));
if (!$vertical) {
$output .= html_writer::start_tag('div', array('class' => 'horizontal'));
}
if (!empty($activities[MOD_CLASS_RESOURCE])) {
$select = new url_select($activities[MOD_CLASS_RESOURCE], '', array(''=>$straddresource), "ressection$section");
$select->set_help_icon('resources');
$select->set_label($strresourcelabel, array('class' => 'accesshide'));
$output .= $this->output->render($select);
}
if (!empty($activities[MOD_CLASS_ACTIVITY])) {
$select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array(''=>$straddactivity), "section$section");
$select->set_help_icon('activities');
$select->set_label($stractivitylabel, array('class' => 'accesshide'));
$output .= $this->output->render($select);
}
if (!$vertical) {
$output .= html_writer::end_tag('div');
}
$output .= html_writer::end_tag('div');
if (course_ajax_enabled($course) && $course->id == $this->page->course->id) {
// modchooser can be added only for the current course set on the page!
$straddeither = get_string('addresourceoractivity');
// The module chooser link
$modchooser = html_writer::start_tag('div', array('class' => 'mdl-right'));
$modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
$icon = $this->output->pix_icon('t/add', '');
$span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
$modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
$modchooser.= html_writer::end_tag('div');
$modchooser.= html_writer::end_tag('div');
// Wrap the normal output in a noscript div
$usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault);
if ($usemodchooser) {
$output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
} else {
// If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled
$output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
}
$output = $this->course_modchooser($modules, $course) . $modchooser . $output;
}
return $output;
}
/**
* Renders html to display a course search form
*
* @param string $value default value to populate the search field
* @param string $format display format - 'plain' (default), 'short' or 'navbar'
* @return string
*/
function course_search_form($value = '', $format = 'plain') {
static $count = 0;
$formid = 'coursesearch';
if ((++$count) > 1) {
$formid .= $count;
}
switch ($format) {
case 'navbar' :
$formid = 'coursesearchnavbar';
$inputid = 'navsearchbox';
$inputsize = 20;
break;
case 'short' :
$inputid = 'shortsearchbox';
$inputsize = 12;
break;
default :
$inputid = 'coursesearchbox';
$inputsize = 30;
}
$strsearchcourses= get_string("searchcourses");
$searchurl = new moodle_url('/course/search.php');
$output = html_writer::start_tag('form', array('id' => $formid, 'action' => $searchurl, 'method' => 'get'));
$output .= html_writer::start_tag('fieldset', array('class' => 'coursesearchbox invisiblefieldset'));
$output .= html_writer::tag('label', $strsearchcourses.': ', array('for' => $inputid));
$output .= html_writer::empty_tag('input', array('type' => 'text', 'id' => $inputid,
'size' => $inputsize, 'name' => 'search', 'value' => s($value)));
$output .= html_writer::empty_tag('input', array('type' => 'submit',
'value' => get_string('go')));
$output .= html_writer::end_tag('fieldset');
$output .= html_writer::end_tag('form');
return $output;
}
/**
* Renders html for completion box on course page
*
* If completion is disabled, returns empty string
* If completion is automatic, returns an icon of the current completion state
* If completion is manual, returns a form (with an icon inside) that allows user to
* toggle completion
*
* @param stdClass $course course object
* @param completion_info $completioninfo completion info for the course, it is recommended
* to fetch once for all modules in course/section for performance
* @param cm_info $mod module to show completion for
* @param array $displayoptions display options, not used in core
* @return string
*/
public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) {
global $CFG, $DB;
$output = '';
if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
return $output;
}
if ($completioninfo === null) {
$completioninfo = new completion_info($course);
}
$completion = $completioninfo->is_enabled($mod);
if ($completion == COMPLETION_TRACKING_NONE) {
if ($this->page->user_is_editing()) {
$output .= html_writer::span(' ', 'filler');
}
return $output;
}
$completiondata = $completioninfo->get_data($mod, true);
$completionicon = '';
if ($this->page->user_is_editing()) {
switch ($completion) {
case COMPLETION_TRACKING_MANUAL :
$completionicon = 'manual-enabled'; break;
case COMPLETION_TRACKING_AUTOMATIC :
$completionicon = 'auto-enabled'; break;
}
} else if ($completion == COMPLETION_TRACKING_MANUAL) {
switch($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'manual-n' . ($completiondata->overrideby ? '-override' : '');
break;
case COMPLETION_COMPLETE:
$completionicon = 'manual-y' . ($completiondata->overrideby ? '-override' : '');
break;
}
} else { // Automatic
switch($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'auto-n' . ($completiondata->overrideby ? '-override' : '');
break;
case COMPLETION_COMPLETE:
$completionicon = 'auto-y' . ($completiondata->overrideby ? '-override' : '');
break;
case COMPLETION_COMPLETE_PASS:
$completionicon = 'auto-pass'; break;
case COMPLETION_COMPLETE_FAIL:
$completionicon = 'auto-fail'; break;
}
}
if ($completionicon) {
$formattedname = $mod->get_formatted_name();
if ($completiondata->overrideby) {
$args = new stdClass();
$args->modname = $formattedname;
$overridebyuser = \core_user::get_user($completiondata->overrideby, '*', MUST_EXIST);
$args->overrideuser = fullname($overridebyuser);
$imgalt = get_string('completion-alt-' . $completionicon, 'completion', $args);
} else {
$imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
}
if ($this->page->user_is_editing()) {
// When editing, the icon is just an image.
$completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
array('title' => $imgalt, 'class' => 'iconsmall'));
$output .= html_writer::tag('span', $this->output->render($completionpixicon),
array('class' => 'autocompletion'));
} else if ($completion == COMPLETION_TRACKING_MANUAL) {
$newstate =
$completiondata->completionstate == COMPLETION_COMPLETE
? COMPLETION_INCOMPLETE
: COMPLETION_COMPLETE;
// In manual mode the icon is a toggle form...
// If this completion state is used by the
// conditional activities system, we need to turn
// off the JS.
$extraclass = '';
if (!empty($CFG->enableavailability) &&
core_availability\info::completion_value_used($course, $mod->id)) {
$extraclass = ' preventjs';
}
$output .= html_writer::start_tag('form', array('method' => 'post',
'action' => new moodle_url('/course/togglecompletion.php'),
'class' => 'togglecompletion'. $extraclass));
$output .= html_writer::start_tag('div');
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'modulename', 'value' => $mod->name));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
$output .= html_writer::tag('button',
$this->output->pix_icon('i/completion-' . $completionicon, $imgalt), array('class' => 'btn btn-link'));
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
} else {
// In auto mode, the icon is just an image.
$completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
array('title' => $imgalt));
$output .= html_writer::tag('span', $this->output->render($completionpixicon),
array('class' => 'autocompletion'));
}
}
return $output;
}
/**
* Checks if course module has any conditions that may make it unavailable for
* all or some of the students
*
* This function is internal and is only used to create CSS classes for the module name/text
*
* @param cm_info $mod
* @return bool
*/
protected function is_cm_conditionally_hidden(cm_info $mod) {
global $CFG;
$conditionalhidden = false;
if (!empty($CFG->enableavailability)) {
$info = new \core_availability\info_module($mod);
$conditionalhidden = !$info->is_available_for_all();
}
return $conditionalhidden;
}
/**
* Renders html to display a name with the link to the course module on a course page
*
* If module is unavailable for user but still needs to be displayed
* in the list, just the name is returned without a link
*
* Note, that for course modules that never have separate pages (i.e. labels)
* this function return an empty string
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
if (!$mod->is_visible_on_course_page() || !$mod->url) {
// Nothing to be displayed to the user.
return '';
}
list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
$groupinglabel = $mod->get_grouping_label($textclasses);
// Render element that allows to edit activity name inline. It calls {@link course_section_cm_name_title()}
// to get the display title of the activity.
$tmpl = new \core_course\output\course_module_name($mod, $this->page->user_is_editing(), $displayoptions);
return $this->output->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output)) .
$groupinglabel;
}
/**
* Returns the CSS classes for the activity name/content
*
* For items which are hidden, unavailable or stealth but should be displayed
* to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
* Students will also see as dimmed activities names that are not yet available
* but should still be displayed (without link) with availability info.
*
* @param cm_info $mod
* @return array array of two elements ($linkclasses, $textclasses)
*/
protected function course_section_cm_classes(cm_info $mod) {
$linkclasses = '';
$textclasses = '';
if ($mod->uservisible) {
$conditionalhidden = $this->is_cm_conditionally_hidden($mod);
$accessiblebutdim = (!$mod->visible || $conditionalhidden) &&
has_capability('moodle/course:viewhiddenactivities', $mod->context);
if ($accessiblebutdim) {
$linkclasses .= ' dimmed';
$textclasses .= ' dimmed_text';
if ($conditionalhidden) {
$linkclasses .= ' conditionalhidden';
$textclasses .= ' conditionalhidden';
}
}
if ($mod->is_stealth()) {
// Stealth activity is the one that is not visible on course page.
// It still may be displayed to the users who can manage it.
$linkclasses .= ' stealth';
$textclasses .= ' stealth';
}
} else {
$linkclasses .= ' dimmed';
$textclasses .= ' dimmed_text';
}
return array($linkclasses, $textclasses);
}
/**
* Renders html to display a name with the link to the course module on a course page
*
* If module is unavailable for user but still needs to be displayed
* in the list, just the name is returned without a link
*
* Note, that for course modules that never have separate pages (i.e. labels)
* this function return an empty string
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) {
$output = '';
$url = $mod->url;
if (!$mod->is_visible_on_course_page() || !$url) {
// Nothing to be displayed to the user.
return $output;
}
//Accessibility: for files get description via icon, this is very ugly hack!
$instancename = $mod->get_formatted_name();
$altname = $mod->modfullname;
// Avoid unnecessary duplication: if e.g. a forum name already
// includes the word forum (or Forum, etc) then it is unhelpful
// to include that in the accessible description that is added.
if (false !== strpos(core_text::strtolower($instancename),
core_text::strtolower($altname))) {
$altname = '';
}
// File type after name, for alphabetic lists (screen reader).
if ($altname) {
$altname = get_accesshide(' '.$altname);
}
list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
// Get on-click attribute value if specified and decode the onclick - it
// has already been encoded for display (puke).
$onclick = htmlspecialchars_decode($mod->onclick, ENT_QUOTES);
// Display link itself.
$activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(),
'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) .
html_writer::tag('span', $instancename . $altname, array('class' => 'instancename'));
if ($mod->uservisible) {
$output .= html_writer::link($url, $activitylink, array('class' => $linkclasses, 'onclick' => $onclick));
} else {
// We may be displaying this just in order to show information
// about visibility, without the actual link ($mod->is_visible_on_course_page()).
$output .= html_writer::tag('div', $activitylink, array('class' => $textclasses));
}
return $output;
}
/**
* Renders html to display the module content on the course page (i.e. text of the labels)
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
$output = '';
if (!$mod->is_visible_on_course_page()) {
// nothing to be displayed to the user
return $output;
}
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
if ($mod->url && $mod->uservisible) {
if ($content) {
// If specified, display extra content after link.
$output = html_writer::tag('div', $content, array('class' =>
trim('contentafterlink ' . $textclasses)));
}
} else {
$groupinglabel = $mod->get_grouping_label($textclasses);
// No link, so display only content.
$output = html_writer::tag('div', $content . $groupinglabel,
array('class' => 'contentwithoutlink ' . $textclasses));
}
return $output;
}
/**
* Displays availability info for a course section or course module
*
* @param string $text
* @param string $additionalclasses
* @return string
*/
public function availability_info($text, $additionalclasses = '') {
$data = ['text' => $text, 'classes' => $additionalclasses];
$additionalclasses = array_filter(explode(' ', $additionalclasses));
if (in_array('ishidden', $additionalclasses)) {
$data['ishidden'] = 1;
} else if (in_array('isstealth', $additionalclasses)) {
$data['isstealth'] = 1;
} else if (in_array('isrestricted', $additionalclasses)) {
$data['isrestricted'] = 1;
if (in_array('isfullinfo', $additionalclasses)) {
$data['isfullinfo'] = 1;
}
}
return $this->render_from_template('core/availability_info', $data);
}
/**
* Renders HTML to show course module availability information (for someone who isn't allowed
* to see the activity itself, or for staff)
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
global $CFG;
$output = '';
if (!$mod->is_visible_on_course_page()) {
return $output;
}
if (!$mod->uservisible) {
// this is a student who is not allowed to see the module but might be allowed
// to see availability info (i.e. "Available from ...")
if (!empty($mod->availableinfo)) {
$formattedinfo = \core_availability\info::format_info(
$mod->availableinfo, $mod->get_course());
$output = $this->availability_info($formattedinfo, 'isrestricted');
}
return $output;
}
// this is a teacher who is allowed to see module but still should see the
// information that module is not available to all/some students
$modcontext = context_module::instance($mod->id);
$canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
if ($canviewhidden && !$mod->visible) {
// This module is hidden but current user has capability to see it.
// Do not display the availability info if the whole section is hidden.
if ($mod->get_section_info()->visible) {
$output .= $this->availability_info(get_string('hiddenfromstudents'), 'ishidden');
}
} else if ($mod->is_stealth()) {
// This module is available but is normally not displayed on the course page
// (this user can see it because they can manage it).
$output .= $this->availability_info(get_string('hiddenoncoursepage'), 'isstealth');
}
if ($canviewhidden && !empty($CFG->enableavailability)) {
// Display information about conditional availability.
// Don't add availability information if user is not editing and activity is hidden.
if ($mod->visible || $this->page->user_is_editing()) {
$hidinfoclass = 'isrestricted isfullinfo';
if (!$mod->visible) {
$hidinfoclass .= ' hide';
}
$ci = new \core_availability\info_module($mod);
$fullinfo = $ci->get_full_information();
if ($fullinfo) {
$formattedinfo = \core_availability\info::format_info(
$fullinfo, $mod->get_course());
$output .= $this->availability_info($formattedinfo, $hidinfoclass);
}
}
}
return $output;
}
/**
* Renders HTML to display one course module for display within a section.
*
* This function calls:
* {@link core_course_renderer::course_section_cm()}
*
* @param stdClass $course
* @param completion_info $completioninfo
* @param cm_info $mod
* @param int|null $sectionreturn
* @param array $displayoptions
* @return String
*/
public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
$output = '';
if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
$modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses;
$output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id));
}
return $output;
}
/**
* Renders HTML to display one course module in a course section
*
* This includes link, content, availability, completion info and additional information
* that module type wants to display (i.e. number of unread forum posts)
*
* This function calls:
* {@link core_course_renderer::course_section_cm_name()}
* {@link core_course_renderer::course_section_cm_text()}
* {@link core_course_renderer::course_section_cm_availability()}
* {@link core_course_renderer::course_section_cm_completion()}
* {@link course_get_cm_edit_actions()}
* {@link core_course_renderer::course_section_cm_edit_actions()}
*
* @param stdClass $course
* @param completion_info $completioninfo
* @param cm_info $mod
* @param int|null $sectionreturn
* @param array $displayoptions
* @return string
*/
public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
$output = '';
// We return empty string (because course module will not be displayed at all)
// if:
// 1) The activity is not visible to users
// and
// 2) The 'availableinfo' is empty, i.e. the activity was
// hidden in a way that leaves no info, such as using the
// eye icon.
if (!$mod->is_visible_on_course_page()) {
return $output;
}
$indentclasses = 'mod-indent';
if (!empty($mod->indent)) {
$indentclasses .= ' mod-indent-'.$mod->indent;
if ($mod->indent > 15) {
$indentclasses .= ' mod-indent-huge';
}
}
$output .= html_writer::start_tag('div');
if ($this->page->user_is_editing()) {
$output .= course_get_cm_move($mod, $sectionreturn);
}
$output .= html_writer::start_tag('div', array('class' => 'mod-indent-outer'));
// This div is used to indent the content.
$output .= html_writer::div('', $indentclasses);
// Start a wrapper for the actual content to keep the indentation consistent
$output .= html_writer::start_tag('div');
// Display the link to the module (or do nothing if module has no url)
$cmname = $this->course_section_cm_name($mod, $displayoptions);
if (!empty($cmname)) {
// Start the div for the activity title, excluding the edit icons.
$output .= html_writer::start_tag('div', array('class' => 'activityinstance'));
$output .= $cmname;
// Module can put text after the link (e.g. forum unread)
$output .= $mod->afterlink;
// 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
}
// If there is content but NO link (eg label), then display the
// content here (BEFORE any icons). In this case cons must be
// displayed after the content so that it makes more sense visually
// and for accessibility reasons, e.g. if you have a one-line label
// it should work similarly (at least in terms of ordering) to an
// activity.
$contentpart = $this->course_section_cm_text($mod, $displayoptions);
$url = $mod->url;
if (empty($url)) {
$output .= $contentpart;
}
$modicons = '';
if ($this->page->user_is_editing()) {
$editactions = course_get_cm_edit_actions($mod, $mod->indent, $sectionreturn);
$modicons .= ' '. $this->course_section_cm_edit_actions($editactions, $mod, $displayoptions);
$modicons .= $mod->afterediticons;
}
$modicons .= $this->course_section_cm_completion($course, $completioninfo, $mod, $displayoptions);
if (!empty($modicons)) {
$output .= html_writer::span($modicons, 'actions');
}
// Show availability info (if module is not available).
$output .= $this->course_section_cm_availability($mod, $displayoptions);
// If there is content AND a link, then display the content here
// (AFTER any icons). Otherwise it was displayed before
if (!empty($url)) {
$output .= $contentpart;
}
$output .= html_writer::end_tag('div'); // $indentclasses
// End of indentation div.
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Message displayed to the user when they try to access unavailable activity following URL
*
* This method is a very simplified version of {@link course_section_cm()} to be part of the error
* notification only. It also does not check if module is visible on course page or not.
*
* The message will be displayed inside notification!
*
* @param cm_info $cm
* @return string
*/
public function course_section_cm_unavailable_error_message(cm_info $cm) {
if ($cm->uservisible) {
return null;
}
if (!$cm->availableinfo) {
return get_string('activityiscurrentlyhidden');
}
$altname = get_accesshide(' ' . $cm->modfullname);
$name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(),
'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) .
html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
$formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
return html_writer::div($name, 'activityinstance-error') .
html_writer::div($formattedinfo, 'availabilityinfo-error');
}
/**
* Renders HTML to display a list of course modules in a course section
* Also displays "move here" controls in Javascript-disabled mode
*
* This function calls {@link core_course_renderer::course_section_cm()}
*