-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1605 lines (1502 loc) · 62.9 KB
/
lib.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/>.
/**
* This file contains main class for Designer course format.
*
* @package format_designer
* @copyright 2021 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot. '/course/format/lib.php');
require_once($CFG->dirroot . '/user/profile/lib.php');
use core\output\inplace_editable;
/**
* Collapsible format settings: Expand all the sections in intial state.
*/
define('SECTION_EXPAND', 1);
/**
* Collapsible format settings: Collapse all the sections in intial state.
*/
define('SECTION_COLLAPSE', 2);
/**
* Collapsible format settings: Expand the first the section only in intial state.
*/
define('FIRST_EXPAND', 3);
define('DESIGNER_ENABLE_POPUPACTIVITIES', 1);
define('DESIGNER_DISABLE_POPUPACTIVITIES', 0);
define('DESIGNER_TYPE_KANBAN', 1);
define('DESIGNER_TYPE_COLLAPSIBLE', 2);
define('DESIGNER_TYPE_FLOW', 3);
/**
* Main class for the Designer course format.
*
* @package format_designer
* @copyright 2021 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_designer extends format_base {
/**
* Returns true if this course format uses sections.
*
* @return bool
*/
public function uses_sections() {
return true;
}
/**
* Returns the display name of the given section that the course prefers.
*
* Use section name is specified by user. Otherwise use default ("Topic #").
*
* @param int|stdClass $section Section object from database or just field section.section
* @return string Display name that the course format prefers, e.g. "Topic 2"
*/
public function get_section_name($section) {
$section = $this->get_section($section);
if ((string)$section->name !== '') {
return format_string($section->name, true,
['context' => context_course::instance($this->courseid)]);
} else {
return $this->get_default_section_name($section);
}
}
/**
* Returns the default section name for the Designer course format.
*
* If the section number is 0, it will use the string with key = section0name from the course format's lang file.
* If the section number is not 0, the base implementation of format_base::get_default_section_name which uses
* the string with the key = 'sectionname' from the course format's lang file + the section number will be used.
*
* @param stdClass $section Section object from database or just field course_sections section
* @return string The default value for the section name.
*/
public function get_default_section_name($section) {
if ($section->section == 0) {
// Return the general section.
return get_string('section0name', 'format_designer');
} else {
// Use format_base::get_default_section_name implementation which
// will display the section name in "Topic n" format.
return parent::get_default_section_name($section);
}
}
/**
* The URL to use for the specified course (with section).
*
* @param int|stdClass $section Section object from database or just field course_sections.section
* if omitted the course view page is returned
* @param array $options options for view URL. At the moment core uses:
* 'navigation' (bool) if true and section has no separate page, the function returns null
* 'sr' (int) used by multipage formats to specify to which section to return
* @return null|moodle_url
*/
public function get_view_url($section, $options = []) {
global $CFG;
$course = $this->get_course();
$url = new moodle_url('/course/view.php', ['id' => $course->id]);
$sr = null;
if (array_key_exists('sr', $options)) {
$sr = $options['sr'];
}
if (is_object($section)) {
$sectionno = $section->section;
} else {
$sectionno = $section;
}
if ($sectionno !== null) {
if ($sr !== null) {
if ($sr) {
$usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
$sectionno = $sr;
} else {
$usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
}
} else {
$usercoursedisplay = $course->coursedisplay;
}
if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$url->param('section', $sectionno);
} else {
if (empty($CFG->linkcoursesections) && !empty($options['navigation'])) {
return null;
}
$url->set_anchor('section-'.$sectionno);
}
}
return $url;
}
/**
* Returns the information about the ajax support in the given source format.
*
* The returned object's property (boolean)capable indicates that
* the course format supports Moodle course ajax features.
*
* @return stdClass
*/
public function supports_ajax() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
return $ajaxsupport;
}
/**
* Loads all of the course sections into the navigation.
*
* @param global_navigation $navigation
* @param navigation_node $node The course node within the navigation
* @return void
*/
public function extend_course_navigation($navigation, navigation_node $node) {
global $PAGE;
// If section is specified in course/view.php, make sure it is expanded in navigation.
if ($navigation->includesectionnum === false) {
$selectedsection = optional_param('section', null, PARAM_INT);
if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
$PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
$navigation->includesectionnum = $selectedsection;
}
}
// Check if there are callbacks to extend course navigation.
parent::extend_course_navigation($navigation, $node);
// We want to remove the general section if it is empty.
$modinfo = get_fast_modinfo($this->get_course());
$sections = $modinfo->get_sections();
if (!isset($sections[0])) {
// The general section is empty to find the navigation node for it we need to get its ID.
$section = $modinfo->get_section_info(0);
$generalsection = $node->get($section->id, navigation_node::TYPE_SECTION);
if ($generalsection) {
// We found the node - now remove it.
$generalsection->remove();
}
}
}
/**
* Custom action after section has been moved in AJAX mode.
*
* Used in course/rest.php
*
* @return array This will be passed in ajax respose
*/
public function ajax_section_move() {
global $PAGE;
$titles = [];
$course = $this->get_course();
$modinfo = get_fast_modinfo($course);
$renderer = $this->get_renderer($PAGE);
if ($renderer && ($sections = $modinfo->get_section_info_all())) {
foreach ($sections as $number => $section) {
$titles[$number] = $renderer->section_title($section, $course);
}
}
return ['sectiontitles' => $titles, 'action' => 'move'];
}
/**
* Returns the list of blocks to be automatically added for the newly created course.
*
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
* each of values is an array of block names (for left and right side columns)
*/
public function get_default_blocks() {
return [
BLOCK_POS_LEFT => [],
BLOCK_POS_RIGHT => [],
];
}
/**
* Definitions of the additional options that this course format uses for course.
*
* Designer format uses the following options:
* - coursedisplay
* - hiddensections
*
* @param bool $foreditform
* @return array of options
*/
public function designer_course_format_options($foreditform = false) {
global $PAGE, $COURSE;
static $courseformatoptions = false;
$courseformatoptions = self::course_format_options_list($foreditform);
if ($foreditform) {
$courseformatoptionsedit['coursecompletiondate'] = [
'label' => new lang_string('coursecompletiondate', 'format_designer'),
'element_type' => $this->designer_completion_enabled() ? 'select' : 'hidden',
'element_attributes' => [
[
1 => new lang_string('show'),
0 => new lang_string('hide'),
],
],
'help' => 'coursecompletiondate',
'help_component' => 'format_designer',
'disabledif' => [['enablecompletion', 'neq', 1]],
];
if ($this->designer_completion_enabled()) {
$courseformatoptionsedit['coursecompletiondateinfo'] = [
'element_type' => 'hidden',
];
} else {
$courseformatoptionsedit['coursecompletiondateinfo'] = [
'element_type' => 'static',
];
}
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
// Set designer default options to course config.
$design = \format_designer\options::get_default_options();
foreach ($courseformatoptions as $name => $value) {
if (isset($design->$name)) {
$courseformatoptions[$name]['default'] = $design->$name;
}
$adv = $name.'_adv';
if (isset($design->$adv) && $design->$adv) {
$courseformatoptions[$name]['adv'] = true;
}
}
}
return $courseformatoptions;
}
/**
* Definitions of the additional options that this course format uses for course.
*
* Designer format uses the following options:
* - coursedisplay
* - hiddensections
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false) {
return $this->designer_course_format_options($foreditform);
}
/**
* Designer course format options list.
*
* @param bool $foreditform
* @return array List of format options.
*/
public static function course_format_options_list($foreditform = false) {
global $CFG;
static $courseformatoptions = false;
$teacher = get_archetype_roles('editingteacher');
$teacher = reset($teacher);
if ($courseformatoptions === false) {
$courseconfig = get_config('moodlecourse');
$courseformatoptions = [
'coursetype' => [
'default' => 0,
'type' => PARAM_INT
],
'popupactivities' => [
'default' => 0,
'type' => PARAM_INT
],
'popupactivitiesinfo' => [
'default' => get_string('popupactivitiesnotinstalled', 'format_designer'),
'type' => PARAM_RAW,
'label' => get_string('popupactivities', 'format_designer'),
],
'coursedisplay' => [
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT,
],
'hiddensections' => [
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT,
],
'showanimation' => [
'default' => true,
'type' => PARAM_INT
],
'accordion' => [
'default' => 0,
'type' => PARAM_INT
],
'initialstate' => [
'default' => 3,
'type' => PARAM_INT,
],
'listwidth' => [
'default' => '400px',
'type' => PARAM_ALPHANUMEXT,
],
'courseheader' => [
'default' => get_string('courseheader', 'format_designer'),
'type' => PARAM_TEXT,
],
'activityprogress' => [
'default' => 0,
'type' => PARAM_INT
],
'enrolmentstartdate' => [
'default' => 0,
'type' => PARAM_INT
],
'enrolmentenddate' => [
'default' => 0,
'type' => PARAM_INT
],
'coursecompletiondate' => [
'default' => 0,
'type' => PARAM_INT
],
'coursecompletiondateinfo' => [
'default' => get_string('completiontrackingmissing', 'format_designer'),
'type' => PARAM_TEXT,
'label' => new lang_string('coursecompletiondate', 'format_designer'),
],
'courseduedate' => [
'default' => 0,
'type' => PARAM_INT
],
'courseduedateinfo' => [
'default' => get_string('timemanagementmissing', 'format_designer'),
'type' => PARAM_RAW_TRIMMED,
'label' => new lang_string('courseduedate', 'format_designer'),
],
'coursestaff' => [
'default' => $teacher->id,
'type' => PARAM_TEXT
]
];
$userprofilefields = profile_get_user_fields_with_data(0);
if (!empty($userprofilefields)) {
foreach ($userprofilefields as $field) {
$courseformatoptions[$field->inputname] = [
'default' => 0,
'type' => PARAM_INT
];
}
}
}
if (format_designer_has_pro()) {
require_once($CFG->dirroot."/local/designer/lib.php");
if (function_exists('local_designer_course_format_options_list')) {
$courseformatoptions += local_designer_course_format_options_list();
}
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
$courseformatoptionsedit = [
'hiddensections' => [
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
'help_component' => 'moodle',
'element_type' => 'select',
'element_attributes' => [
[
0 => new lang_string('hiddensectionscollapsed'),
1 => new lang_string('hiddensectionsinvisible')
],
],
],
'coursedisplay' => [
'label' => new lang_string('coursedisplay'),
'element_type' => 'select',
'element_attributes' => [
[
COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi'),
],
],
'help' => 'coursedisplay',
'help_component' => 'moodle',
'disabledif' => [['coursetype', 'eq', DESIGNER_TYPE_KANBAN]],
],
'accordion' => [
'label' => new lang_string('accordion', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
array(
0 => new lang_string('disable'),
1 => new lang_string('enable')
)
],
'disabledif' => [
['coursetype', 'eq', DESIGNER_TYPE_KANBAN],
['coursetype', 'eq', 0],
]
],
'initialstate' => [
'label' => new lang_string('initialstate', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
SECTION_EXPAND => new lang_string('expand', 'format_designer'),
SECTION_COLLAPSE => new lang_string('collapse', 'format_designer'),
FIRST_EXPAND => new lang_string('firstexpand', 'format_designer')
],
],
'disabledif' => [
['coursetype', 'eq', DESIGNER_TYPE_KANBAN],
['coursetype', 'eq', 0],
]
],
'enrolmentstartdate' => [
'label' => new lang_string('enrolmentstartdate', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
1 => new lang_string('show'),
0 => new lang_string('hide'),
],
],
'help' => 'enrolmentstartdate',
'help_component' => 'format_designer',
],
'enrolmentenddate' => [
'label' => new lang_string('enrolmentenddate', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
1 => new lang_string('show'),
0 => new lang_string('hide'),
],
],
'help' => 'enrolmentenddate',
'help_component' => 'format_designer',
],
'activityprogress' => [
'label' => new lang_string('activityprogress', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
1 => new lang_string('show'),
0 => new lang_string('hide'),
],
],
'help' => 'activityprogress',
'help_component' => 'format_designer',
'disabledif' => [['enablecompletion', 'neq', 1]],
],
'coursetype' => [
'label' => new lang_string('coursetype', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
0 => new lang_string('normal'),
DESIGNER_TYPE_KANBAN => new lang_string('kanbanboard', 'format_designer'),
DESIGNER_TYPE_COLLAPSIBLE => new lang_string('collapsiblesections', 'format_designer'),
DESIGNER_TYPE_FLOW => new lang_string('type_flow', 'format_designer')
],
],
'help' => 'coursetype',
'help_component' => 'format_designer',
],
'showanimation' => [
'label' => new lang_string('showanimation', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
array(
0 => new lang_string('disable'),
1 => new lang_string('enable')
)
],
'help' => 'showanimation',
'help_component' => 'format_designer',
'disabledif' => [['coursetype', 'neq', DESIGNER_TYPE_FLOW]]
],
'courseheader' => [
'label' => new lang_string('courseheader', 'format_designer'),
'element_type' => 'header',
],
'listwidth' => [
'label' => new lang_string('listwidth', 'format_designer'),
'element_type' => 'text',
'hideif' => ['coursetype', 'neq', DESIGNER_TYPE_KANBAN]
]
];
if (format_designer_popup_installed()) {
$courseformatoptionsedit['popupactivities'] = [
'label' => new lang_string('popupactivities', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
DESIGNER_DISABLE_POPUPACTIVITIES => new lang_string('disable'),
DESIGNER_ENABLE_POPUPACTIVITIES => new lang_string('enable'),
],
],
'help' => 'popupactivities',
'help_component' => 'format_designer',
];
$courseformatoptionsedit['popupactivitiesinfo'] = [
'element_type' => 'hidden',
];
} else {
$courseformatoptionsedit['popupactivitiesinfo'] = [
'element_type' => 'static',
];
$courseformatoptionsedit['popupactivities'] = [
'element_type' => 'hidden',
'label' => get_string('popupactivities', 'format_designer'),
];
}
if (format_designer_timemanagement_installed()) {
$courseformatoptionsedit['courseduedate'] = [
'label' => new lang_string('courseduedate', 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
[
1 => new lang_string('show'),
0 => new lang_string('hide'),
],
],
'help' => 'courseduedate',
'help_component' => 'format_designer',
];
$courseformatoptionsedit['courseduedateinfo'] = [
'element_type' => 'hidden',
];
} else {
$courseformatoptionsedit['courseduedate'] = [
'element_type' => 'hidden',
'label' => new lang_string('courseduedate', 'format_designer'),
];
$courseformatoptionsedit['courseduedateinfo'] = [
'element_type' => 'static',
'help' => 'courseduedate',
'help_component' => 'format_designer',
];
}
$coursestaffroles = get_default_enrol_roles(context_system::instance());
$courseformatoptionsedit['coursestaff'] = [
'label' => new lang_string('displayheaderroleusers', 'format_designer'),
'element_type' => 'autocomplete',
'element_attributes' => [$coursestaffroles, ['multiple' => true]],
'help' => 'displayheaderroleusers',
'help_component' => 'format_designer',
];
$userprofilefields = profile_get_user_fields_with_data(0);
if (!empty($userprofilefields)) {
foreach ($userprofilefields as $field) {
$courseformatoptionsedit[$field->inputname] = [
'label' => $field->field->name,
'element_type' => 'advcheckbox',
'help' => 'profilefieditem',
'help_component' => 'format_designer',
];
}
}
if (format_designer_has_pro()) {
require_once($CFG->dirroot."/local/designer/lib.php");
if (function_exists('local_designer_course_format_options_editlist')) {
$courseformatoptionsedit += local_designer_course_format_options_editlist();
}
}
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
}
return $courseformatoptions;
}
/**
* Find the course completion enabled for the current course.
*
* @return void
*/
public function designer_completion_enabled() {
global $COURSE;
if ($COURSE->enablecompletion) {
return true;
}
return false;
}
/**
* Adds format options elements to the course/section edit form.
*
* This function is called from {@see course_edit_form::definition_after_data()}.
*
* @param MoodleQuickForm $mform form the elements are added to.
* @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form.
* @return array array of references to the added form elements.
*/
public function create_edit_form_elements(&$mform, $forsection = false) {
global $COURSE, $PAGE;
$elements = parent::create_edit_form_elements($mform, $forsection);
if (!$forsection && (empty($COURSE->id) || $COURSE->id == SITEID)) {
// Add "numsections" element to the create course form - it will force new course to be prepopulated
// with empty sections.
// The "Number of sections" option is no longer available when editing course, instead teachers should
// delete and add sections when needed.
$courseconfig = get_config('moodlecourse');
$max = (int)$courseconfig->maxsections;
$element = $mform->addElement('select', 'numsections', get_string('numberweeks'), range(0, $max ?: 52));
$mform->setType('numsections', PARAM_INT);
if (is_null($mform->getElementValue('numsections'))) {
$mform->setDefault('numsections', $courseconfig->numsections);
}
array_unshift($elements, $element);
}
if ($forsection) {
$options = $this->section_format_options(true);
} else {
$options = $this->designer_course_format_options(true);
}
$design = \format_designer\options::get_default_options();
foreach ($options as $optionname => $option) {
if (isset($option['disabledif'])) {
$disabledif = $option['disabledif'];
foreach ($disabledif as $disable) {
if (isset($disable[2])) {
$mform->disabledif($optionname, $disable[0], $disable[1], $disable[2]);
}
}
}
if (isset($option['hideif'])) {
$hideif = $option['hideif'];
if (isset($hideif[1])) {
$hide = (isset($hideif[2]))
? $mform->hideif($optionname, $hideif[0], $hideif[1], $hideif[2])
: $mform->hideif($optionname, $hideif[0], $hideif[1]);
}
}
if (isset($option['adv'])) {
$mform->setAdvanced($optionname);
}
if ($optionname == 'coursestaff' && isset($design->coursestaff)) {
$select = $mform->getElement($optionname);
$select->setSelected($design->{$optionname});
}
}
if ($forsection) {
$PAGE->requires->js_init_code('
require(["core/config"], function(CFG) {
document.querySelectorAll("input[name$=\"width\"]").forEach((v) => {
var px = document.createElement("label");
px.classList.add("px-string");
px.innerHTML = "Px";
v.parentNode.append(px);
})
if (document.querySelectorAll("input[name=\"sectiontype\"]").length > 0) {
var sectionType = document.querySelectorAll("input[name=\"sectiontype\"]")[0].value;
if (sectionType !== "circles" && sectionType != "horizontal_circles"
&& document.querySelectorAll("select[name=\"circlesize\"]").length > 0) {
document.querySelectorAll("select[name=\"circlesize\"]")[0].setAttribute("disabled", "disabled");
}
}
})
');
}
return $elements;
}
/**
* Definitions of the additional options that this course format uses for section
*
* See course_format::course_format_options() for return array definition.
*
* Additionally section format options may have property 'cache' set to true
* if this option needs to be cached in get_fast_modinfo(). The 'cache' property
* is recommended to be set only for fields used in course_format::get_section_name(),
* course_format::extend_course_navigation() and course_format::get_view_url()
*
* For better performance cached options are recommended to have 'cachedefault' property
* Unlike 'default', 'cachedefault' should be static and not access get_config().
*
* Regardless of value of 'cache' all options are accessed in the code as
* $sectioninfo->OPTIONNAME
* where $sectioninfo is instance of section_info, returned by
* get_fast_modinfo($course)->get_section_info($sectionnum)
* or get_fast_modinfo($course)->get_section_info_all()
*
* All format options for particular section are returned by calling:
* $this->get_format_options($section);
*
* @param bool $foreditform
* @return array
*/
public function section_format_options($foreditform = false) {
global $PAGE, $COURSE;
$sectionid = optional_param('id', 0, PARAM_INT);
if ($sectionid && $PAGE->pagetype == 'course-editsection') {
$sectionbackdraftid = 0;
$sectioncompletionbackdraftid = 0;
$coursecontext = \context_course::instance($COURSE->id);
$format = course_get_format($COURSE);
file_prepare_draft_area($sectionbackdraftid, $coursecontext->id, 'format_designer',
'sectiondesignbackground', $sectionid, array('accepted_types' => 'images',
'maxfiles' => 1));
file_prepare_draft_area($sectioncompletionbackdraftid, $coursecontext->id, 'format_designer',
'sectiondesigncompletionbackground', $sectionid, array('accepted_types' => 'images',
'maxfiles' => 1));
$format->set_section_option($sectionid, 'sectiondesignerbackgroundimage', $sectionbackdraftid);
$format->set_section_option($sectionid, 'sectiondesignercompletionbg', $sectioncompletionbackdraftid);
}
return self::section_format_options_list($foreditform);
}
/**
* Config list for sections. Used in global settings and section edit.
*
* @param bool $foreditform
* @return array List of section settings.
*/
public static function section_format_options_list($foreditform) {
global $CFG, $PAGE;
$design = \format_designer\options::get_default_options();
$sectionoptions = array(
'sectiontype' => array(
'type' => PARAM_ALPHANUMEXT,
'label' => '',
'element_type' => 'hidden',
'default' => 'default',
),
);
$width = [
0 => '100%',
1 => '50%',
2 => '33%',
3 => '25%',
4 => '20%'
];
$sectionoptions['sectionlayoutheader'] = array(
'type' => PARAM_TEXT,
'element_type' => 'header',
'default' => get_string('sectionlayouts', 'format_designer'),
'label' => '',
);
$course = course_get_format($PAGE->course)->get_course();
$settingspage = ($PAGE->course->id == SITEID);
if ($settingspage || (isset($course->coursetype) && $course->coursetype != DESIGNER_TYPE_FLOW)) {
foreach (['desktop' => 5, 'tablet' => 3, 'mobile' => 2] as $name => $size) {
$name = $name.'width';
$availablewidth = array_slice($width, 0, $size);
$sectionoptions[$name] = [
'default' => isset($design->$name) && $foreditform ? $design->$name : 0,
'type' => PARAM_INT,
'label' => new lang_string($name, 'format_designer'),
'element_type' => 'select',
'element_attributes' => [
$availablewidth
],
'help' => $name,
'help_component' => 'format_designer',
];
$adv = $name.'_adv';
if (isset($design->$adv) && $design->$adv) {
$sectionoptions[$name]['adv'] = true;
}
}
}
// Include pro feature options for section.
if (format_designer_has_pro()) {
require_once($CFG->dirroot."/local/designer/lib.php");
$prosectionoptions = local_designer_get_pro_section_options($foreditform);
$sectionoptions = array_merge($sectionoptions, $prosectionoptions);
}
return $sectionoptions;
}
/**
* Updates format options for a section
*
* Section id is expected in $data->id (or $data['id'])
* If $data does not contain property with the option name, the option will not be updated
*
* @param stdClass|array $data return value from {@see moodleform::get_data()} or array with data
* @return bool whether there were any changes to the options values
*/
public function update_section_format_options($data) {
global $COURSE;
$data = (array) $data;
if (empty($data['sectionlayoutheader'])) {
$data['sectionlayoutheader'] = get_string('sectionlayouts', 'format_designer');
}
if (format_designer_has_pro()) {
local_designer\options::update_section_format_options($data);
}
return $this->update_format_options($data, $data['id']);
}
/**
* Updates format options for a course.
*
* In case if course format was changed to 'designer', we try to copy options
* 'coursedisplay' and 'hiddensections' from the previous format.
*
* @param stdClass|array $data return value from {@see moodleform::get_data()} or array with data
* @param stdClass $oldcourse if this function is called from {@see update_course()}
* this object contains information about the course before update
* @return bool whether there were any changes to the options values
*/
public function update_course_format_options($data, $oldcourse = null) {
global $CFG;
$data = (array)$data;
if ($oldcourse !== null) {
$oldcourse = (array)$oldcourse;
$options = $this->course_format_options();
foreach ($options as $key => $unused) {
if (!array_key_exists($key, $data)) {
if (array_key_exists($key, $oldcourse)) {
$data[$key] = $oldcourse[$key];
}
}
if ($key == 'courseduedateinfo') {
$data[$key] = get_string('timemanagementmissing', 'format_designer');
}
if ($key == 'coursecompletiondateinfo') {
$data[$key] = get_string('completiontrackingmissing', 'format_designer');
}
if ($key == 'popupactivities' && !format_designer_popup_installed()) {
$data[$key] = false;
}
}
if (isset($oldcourse['coursetype'])
&& $oldcourse['coursetype'] != DESIGNER_TYPE_KANBAN
&& isset($data['coursetype'])
&& $data['coursetype'] == DESIGNER_TYPE_KANBAN) {
$this->setup_kanban_layouts($oldcourse);
}
if (isset($data['coursetype']) && $data['coursetype'] == DESIGNER_TYPE_KANBAN) {
$data['coursedisplay'] = 0;
}
} else {
if (isset($data['coursetype'])) {
if ($data['coursetype'] == DESIGNER_TYPE_KANBAN) {
$this->setup_kanban_layouts($data);
$data['coursedisplay'] = 0;
}
}
}
unset($data['courseheader']);
unset($data['popupactivitiesinfo']);
unset($data['courseprerequisites']);
if (isset($data['coursestaff'])) {
$data['coursestaff'] = implode(",", $data['coursestaff']);
}
if (isset($data['prerequisiteinfo']) && is_array($data['prerequisiteinfo'])) {
$editoroptions = array('maxfiles' => -1, 'maxbytes' => $CFG->maxbytes, 'trusttext' => false,
'noclean' => true);
$context = context_course::instance($this->courseid, MUST_EXIST);
// Setup the editor to save areafiles. hack.
$data['prerequisiteinfo_editor'] = $data['prerequisiteinfo'];
$data = file_postupdate_standard_editor(
// The submitted data.
(object) $data,
// The field name in the database.
'prerequisiteinfo',
// The options.
$editoroptions,
// The combination of contextid, component, filearea, and itemid.
$context,
'local_designer',
'prerequisiteinfo',
0
);
}
return $this->update_format_options($data);
}
/**
* Returns a record from course database table plus additional fields
* that course format defines
*
* @return stdClass
*/
public function get_course() {
global $CFG;
$course = parent::get_course();
if (isset($course->prerequisiteinfo) && is_string($course->prerequisiteinfo)) {
$coursecontext = context_course::instance($course->id);
$editoroptions = array('maxfiles' => -1, 'maxbytes' => $CFG->maxbytes,
'trusttext' => false, 'noclean' => true);
$editoroptions['context'] = $coursecontext;
$editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'local_designer', 'prerequisiteinfo', 0);
$course = file_prepare_standard_editor(
$course, 'prerequisiteinfo', $editoroptions,
$coursecontext, 'local_designer', 'prerequisiteinfo', 0
);
$course->prerequisiteinfo = $course->prerequisiteinfo_editor;
unset($course->prerequisiteinfo_editor);
}
return $course;
}
/**
* Update the kanban layouts default options when layout changed to kanban mode.
*
* @param array $course
* @return void
*/
public function setup_kanban_layouts($course) {
global $DB;
$sections = $DB->get_records('course_sections', ['course' => $course['id']]);
foreach ($sections as $section) {
if ($section->section == 0) {
continue;
}
$this->set_section_option($section->id, 'sectiontype', 'cards');
$this->set_section_option($section->id, 'layoutmobilecolumn', '1');
$this->set_section_option($section->id, 'layouttabletcolumn', '1');
$this->set_section_option($section->id, 'layoutdesktopcolumn', '1');
}
}
/**
* Whether this format allows to delete sections.
*