forked from davidherney/moodle-format_onetopic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1082 lines (955 loc) · 40.8 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 the course format Onetopic
*
* @since 2.0
* @package format_onetopic
* @copyright 2012 David Herney Bernal - cirano
* @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');
use core\output\inplace_editable;
/**
* Main class for the Onetopic course format
*
* @since 2.0
* @package format_onetopic
* @copyright 2012 David Herney Bernal - cirano
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_onetopic extends core_courseformat\base {
/** @var int The summary is not a template */
const TEMPLATETOPIC_NOT = 0;
/** @var int The summary is a single template */
const TEMPLATETOPIC_SINGLE = 1;
/** @var int The summary is a template, list the resources that are not referenced */
const TEMPLATETOPIC_LIST = 2;
/** @var int Default tabs view */
const TABSVIEW_DEFAULT = 0;
/** @var int Vertical view */
const TABSVIEW_VERTICAL = 1;
/** @var int One line view */
const TABSVIEW_ONELINE = 2;
/** @var int Embedded course index */
const TABSVIEW_COURSEINDEX = 3;
/** @var int Only if theme not support "usescourseindex" */
const SECTIONSNAVIGATION_SUPPORT = 1;
/** @var int Not use */
const SECTIONSNAVIGATION_NOT = 2;
/** @var int Only at the bottom */
const SECTIONSNAVIGATION_BOTTOM = 3;
/** @var int Only at the bottom */
const SECTIONSNAVIGATION_BOTH = 4;
/** @var int Like slides */
const SECTIONSNAVIGATION_SLIDES = 5;
/** @var string Course index scope */
const SCOPE_COURSE = 'course';
/** @var string Course modules scope */
const SCOPE_MOD = 'mod';
/** @var bool If the class was previously instanced, in one execution cycle */
private static $loaded = false;
/** @var array Messages to display */
public static $formatmsgs = [];
/** @var stdClass Onetopic-specific extra section information */
private $parentsections = null;
/** @var array Modules used in template */
public $tplcmsused = [];
/** @var bool If the course has topic tabs */
public $hastopictabs = false;
/** @var string Unique id for the course format */
public $uniqid;
/** @var bool If print the tabs menu in the current scope */
public $printable = true;
/** @var string Current format scope */
public $currentscope = null;
/**
* Creates a new instance of class
*
* Please use {@see course_get_format($courseorid)} to get an instance of the format class
*
* @param string $format
* @param int $courseid
* @return format_base
*/
protected function __construct($format, $courseid) {
parent::__construct($format, $courseid);
$this->uniqid = uniqid();
// Hack for section number, when not is like a param in the url or section is not available.
global $section, $sectionid, $PAGE, $USER, $urlparams, $DB, $context;
$inpopup = optional_param('inpopup', 0, PARAM_INT);
if ($inpopup) {
$this->printable = false;
} else {
$defaultscope = get_config('format_onetopic', 'defaultscope');
if ($defaultscope) {
$scope = explode(',', $defaultscope);
} else {
$scope = [];
}
$pagesavailable = ['course-view-onetopic', 'course-view'];
if (!in_array($PAGE->pagetype, $pagesavailable)) {
if (in_array(self::SCOPE_MOD, $scope)) {
$this->currentscope = self::SCOPE_MOD;
$patternavailable = '/^mod-.*-view$/';
$this->printable = preg_match($patternavailable, $PAGE->pagetype);
} else {
$this->printable = false;
}
} else {
$this->currentscope = self::SCOPE_COURSE;
}
}
if ($this->printable) {
if (!self::$loaded && isset($section) && $courseid &&
($PAGE->pagetype == 'course-view-onetopic' || $PAGE->pagetype == 'course-view')) {
self::$loaded = true;
$this->singlesection = $section;
$course = $this->get_course();
// Onetopic format is always multipage.
$course->realcoursedisplay = property_exists($course, 'coursedisplay') ? $course->coursedisplay : false;
if ($sectionid <= 0) {
$section = optional_param('section', -1, PARAM_INT);
}
$numsections = (int)$DB->get_field('course_sections', 'MAX(section)', ['course' => $courseid], MUST_EXIST);
if ($section >= 0 && $numsections >= $section) {
$realsection = $section;
} else {
if (isset($USER->display[$course->id]) && $numsections >= $USER->display[$course->id]) {
$realsection = $USER->display[$course->id];
} else if ($course->marker && $course->marker > 0) {
$realsection = (int)$course->marker;
} else {
$realsection = 0;
}
}
if ($realsection < 0 || $realsection > $numsections) {
$realsection = 0;
}
if ($course->realcoursedisplay == COURSE_DISPLAY_MULTIPAGE && $realsection === 0 && $numsections >= 1) {
$realsection = 1;
}
// Can view the hidden sections in current course?
$canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
$modinfo = get_fast_modinfo($course);
$sections = $modinfo->get_section_info_all();
// Check if the display section is available.
if ((!$canviewhidden && (!$sections[$realsection]->uservisible || !$sections[$realsection]->available))) {
self::$formatmsgs[] = get_string('hidden_message', 'format_onetopic', $this->get_section_name($realsection));
$valid = false;
$k = $course->realcoursedisplay ? 1 : 0;
do {
$formatoptions = $this->get_format_options($k);
if ($formatoptions['level'] == 0
&& ($sections[$k]->available && $sections[$k]->uservisible)
|| $canviewhidden) {
$valid = true;
break;
}
$k++;
} while (!$valid && $k <= $numsections);
$realsection = $valid ? $k : 0;
}
$section = $realsection;
$USER->display[$course->id] = $realsection;
$urlparams['section'] = $realsection;
$PAGE->set_url('/course/view.php', $urlparams);
}
}
}
/**
* Returns true if this course format uses sections.
*
* @return bool
*/
public function uses_sections() {
return true;
}
/**
* Returns true if this course format uses course index
*
* @return bool
*/
public function uses_course_index() {
$course = $this->get_course();
if ($course->tabsview == self::TABSVIEW_COURSEINDEX) {
return false;
}
if ($this->show_editor()) {
return true;
}
// The 2 value is Use the site configuration.
if (isset($course->usescourseindex) && $course->usescourseindex < 2) {
return $course->usescourseindex;
}
return get_config('format_onetopic', 'courseindex') == 1;
}
/**
* Returns true if this course format uses activity indentation.
*
* @return bool if the course format uses indentation.
*/
public function uses_indentation(): bool {
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 topics 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 course_format::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_topics');
} else {
// Use course_format::get_default_section_name implementation which
// will display the section name in "Topic n" format.
return parent::get_default_section_name($section);
}
}
/**
* Generate the title for this section page.
*
* @return string the page title
*/
public function page_title(): string {
return get_string('topicoutline');
}
/**
* 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;
}
$url->param('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() {
global $COURSE, $USER;
if (!isset($USER->onetopic_da)) {
$USER->onetopic_da = [];
}
if (empty($COURSE)) {
$disableajax = false;
} else {
$disableajax = isset($USER->onetopic_da[$COURSE->id]) ? $USER->onetopic_da[$COURSE->id] : false;
}
$ajaxsupport = new stdClass();
$ajaxsupport->capable = !$disableajax;
return $ajaxsupport;
}
/**
* Returns true if this course format is compatible with content components.
*
* Using components means the content elements can watch the frontend course state and
* react to the changes. Formats with component compatibility can have more interactions
* without refreshing the page, like having drag and drop from the course index to reorder
* sections and activities.
*
* @return bool if the format is compatible with components.
*/
public function supports_components() {
return true;
}
/**
* 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, $COURSE, $USER;
// Set the section number for the course node.
$node->action->param('section', 0);
// 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 ((!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
$PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
if ($selectedsection !== null) {
$navigation->includesectionnum = $selectedsection;
} else if (isset($USER->display[$COURSE->id])) {
$navigation->includesectionnum = $USER->display[$COURSE->id];
}
}
}
// 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.
*
* Topics format uses the following options:
* - coursedisplay
* - hiddensections
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false) {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
$courseconfig = get_config('moodlecourse');
$courseformatoptions = [
'hiddensections' => [
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT,
],
'hidetabsbar' => [
'default' => 0,
'type' => PARAM_INT,
],
'coursedisplay' => [
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT,
],
'templatetopic' => [
'default' => self::TEMPLATETOPIC_NOT,
'type' => PARAM_INT,
],
'templatetopic_icons' => [
'default' => 0,
'type' => PARAM_INT,
],
'tabsview' => [
'default' => 0,
'type' => PARAM_INT,
],
'usessectionsnavigation' => [
'default' => 0,
'type' => PARAM_INT,
],
'usescourseindex' => [
'default' => 2,
'type' => PARAM_INT,
],
];
}
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'),
2 => new lang_string('hiddensectionshelp', 'format_onetopic'),
],
],
],
'hidetabsbar' => [
'label' => get_string('hidetabsbar', 'format_onetopic'),
'help' => 'hidetabsbar',
'help_component' => 'format_onetopic',
'element_type' => 'select',
'element_attributes' => [
[
0 => new lang_string('no'),
1 => new lang_string('yes'),
],
],
],
'coursedisplay' => [
'label' => new lang_string('coursedisplay', 'format_onetopic'),
'element_type' => 'select',
'element_attributes' => [
[
COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single', 'format_onetopic'),
COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi', 'format_onetopic'),
],
],
'help' => 'coursedisplay',
'help_component' => 'format_onetopic',
],
'templatetopic' => [
'label' => new lang_string('templatetopic', 'format_onetopic'),
'element_type' => 'select',
'element_attributes' => [
[
self::TEMPLATETOPIC_NOT => new lang_string('templetetopic_not', 'format_onetopic'),
self::TEMPLATETOPIC_SINGLE => new lang_string('templetetopic_single', 'format_onetopic'),
self::TEMPLATETOPIC_LIST => new lang_string('templetetopic_list', 'format_onetopic'),
],
],
'help' => 'templatetopic',
'help_component' => 'format_onetopic',
],
'templatetopic_icons' => [
'label' => get_string('templatetopic_icons', 'format_onetopic'),
'help' => 'templatetopic_icons',
'help_component' => 'format_onetopic',
'element_type' => 'select',
'element_attributes' => [
[
0 => new lang_string('no'),
1 => new lang_string('yes'),
],
],
],
'tabsview' => [
'label' => new lang_string('tabsview', 'format_onetopic'),
'element_type' => 'select',
'element_attributes' => [
[
self::TABSVIEW_DEFAULT => new lang_string('tabsview_default', 'format_onetopic'),
self::TABSVIEW_VERTICAL => new lang_string('tabsview_vertical', 'format_onetopic'),
self::TABSVIEW_ONELINE => new lang_string('tabsview_oneline', 'format_onetopic'),
self::TABSVIEW_COURSEINDEX => new lang_string('tabsview_courseindex', 'format_onetopic'),
],
],
'help' => 'tabsview',
'help_component' => 'format_onetopic',
],
'usessectionsnavigation' => [
'label' => new lang_string('usessectionsnavigation', 'format_onetopic'),
'element_type' => 'select',
'element_attributes' => [
[
'0' => new lang_string('sectionsnavigation_sitelevel', 'format_onetopic'),
self::SECTIONSNAVIGATION_SUPPORT => new lang_string('sectionsnavigation_support', 'format_onetopic'),
self::SECTIONSNAVIGATION_NOT => new lang_string('sectionsnavigation_not', 'format_onetopic'),
self::SECTIONSNAVIGATION_BOTTOM => new lang_string('sectionsnavigation_bottom', 'format_onetopic'),
self::SECTIONSNAVIGATION_BOTH => new lang_string('sectionsnavigation_both', 'format_onetopic'),
self::SECTIONSNAVIGATION_SLIDES => new lang_string('sectionsnavigation_slides', 'format_onetopic'),
],
],
'help' => 'usessectionsnavigation',
'help_component' => 'format_onetopic',
],
'usescourseindex' => [
'label' => get_string('usescourseindex', 'format_onetopic'),
'help' => 'usescourseindex',
'help_component' => 'format_onetopic',
'element_type' => 'select',
'element_attributes' => [
[
2 => new lang_string('usecourseindexsite', 'format_onetopic'),
0 => new lang_string('no'),
1 => new lang_string('yes'),
],
],
],
];
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
}
return $courseformatoptions;
}
/**
* 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;
$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);
}
return $elements;
}
/**
* Updates format options for a course.
*
* In case if course format was changed to 'onetopic', we try to copy
* special options 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 $DB;
$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];
} else if ($key === 'hidetabsbar') {
// If previous format does not have the field 'hidetabsbar' and $data['hidetabsbar'] is not set,
// we fill it with the default option.
$data['hidetabsbar'] = 0;
} else if ($key === 'templatetopic') {
$data['templatetopic'] = self::TEMPLATETOPIC_NOT;
} else if ($key === 'templatetopic_icons') {
$data['templatetopic_icons'] = 0;
} else if ($key === 'tabsview') {
$data['tabsview'] = self::TABSVIEW_DEFAULT;
} else if ($key === 'usessectionsnavigation') {
$data['usessectionsnavigation'] = 0;
} else if ($key === 'usescourseindex') {
$data['usescourseindex'] = 2;
}
}
}
}
return $this->update_format_options($data);
}
/**
* Definitions of the additional options that this course format uses for section.
*
* See {@see format_base::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 {@see get_fast_modinfo()}. The 'cache' property
* is recommended to be set only for fields used in {@see format_base::get_section_name()},
* {@see format_base::extend_course_navigation()} and {@see format_base::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) {
static $sectionformatoptions = false;
$enablecustomstyles = get_config('format_onetopic', 'enablecustomstyles');
if ($sectionformatoptions === false) {
$sectionformatoptions = [
'level' => [
'default' => 0,
'type' => PARAM_INT,
],
'firsttabtext' => [
'default' => get_string('index', 'format_onetopic'),
'type' => PARAM_TEXT,
],
];
if ($enablecustomstyles) {
$sectionformatoptions['fontcolor'] = [
'default' => '',
'type' => PARAM_RAW,
];
$sectionformatoptions['bgcolor'] = [
'default' => '',
'type' => PARAM_RAW,
];
$sectionformatoptions['cssstyles'] = [
'default' => '',
'type' => PARAM_RAW,
];
}
}
if ($foreditform) {
$sectionformatoptionsedit = [
'level' => [
'default' => 0,
'type' => PARAM_INT,
'label' => get_string('level', 'format_onetopic'),
'element_type' => 'select',
'element_attributes' => [
[
0 => get_string('asprincipal', 'format_onetopic'),
1 => get_string('aschild', 'format_onetopic'),
],
],
'help' => 'level',
'help_component' => 'format_onetopic',
],
'firsttabtext' => [
'default' => get_string('index', 'format_onetopic'),
'type' => PARAM_TEXT,
'label' => get_string('firsttabtext', 'format_onetopic'),
'help' => 'firsttabtext',
'help_component' => 'format_onetopic',
],
];
if ($enablecustomstyles) {
$sectionformatoptionsedit['fontcolor'] = [
'default' => '',
'type' => PARAM_RAW,
'label' => get_string('fontcolor', 'format_onetopic'),
'help' => 'fontcolor',
'help_component' => 'format_onetopic',
];
$sectionformatoptionsedit['bgcolor'] = [
'default' => '',
'type' => PARAM_RAW,
'label' => get_string('bgcolor', 'format_onetopic'),
'help' => 'bgcolor',
'help_component' => 'format_onetopic',
];
$sectionformatoptionsedit['cssstyles'] = [
'default' => '',
'type' => PARAM_RAW,
'label' => get_string('cssstyles', 'format_onetopic'),
'help' => 'cssstyles',
'help_component' => 'format_onetopic',
];
}
$sectionformatoptions = $sectionformatoptionsedit;
}
return $sectionformatoptions;
}
/**
* Whether this format allows to delete sections.
*
* Do not call this function directly, instead use {@see course_can_delete_section()}
*
* @param int|stdClass|section_info $section
* @return bool
*/
public function can_delete_section($section) {
return true;
}
/**
* Allows course format to execute code on moodle_page::set_cm()
*
* Current module can be accessed as $page->cm (returns instance of cm_info)
*
* @param moodle_page $page instance of page calling set_cm
*/
public function page_set_cm(moodle_page $page) {
$this->set_section_number($page->cm->sectionnum);
}
/**
* Course-specific information to be output immediately above content on any course page
*
* See {@see core_courseformat\base::course_header()} for usage
*
* @return null|renderable null for no output or object with data for plugin renderer
*/
public function course_content_header() {
if ($this->printable) {
return new \format_onetopic\header($this);
} else {
return null;
}
}
/**
* Course-specific information to be output immediately below content on any course page
*
* See course_format::course_header() for usage
*
* @return null|renderable null for no output or object with data for plugin renderer
*/
public function course_content_footer() {
if ($this->printable) {
return new \format_onetopic\footer($this);
} else {
return null;
}
}
/**
* Prepares the templateable object to display section name.
*
* @param \section_info|\stdClass $section
* @param bool $linkifneeded
* @param bool $editable
* @param null|lang_string|string $edithint
* @param null|lang_string|string $editlabel
* @return inplace_editable
*/
public function inplace_editable_render_section_name($section, $linkifneeded = true,
$editable = null, $edithint = null, $editlabel = null) {
if (empty($edithint)) {
$edithint = new lang_string('editsectionname', 'format_topics');
}
if (empty($editlabel)) {
$title = get_section_name($section->course, $section);
$editlabel = new lang_string('newsectionname', 'format_topics', $title);
}
return parent::inplace_editable_render_section_name($section, $linkifneeded, $editable, $edithint, $editlabel);
}
/**
* Indicates whether the course format supports the creation of a news forum.
*
* @return bool
*/
public function supports_news() {
return true;
}
/**
* Returns whether this course format allows the activity to
* have "triple visibility state" - visible always, hidden on course page but available, hidden.
*
* @param stdClass|cm_info $cm course module (may be null if we are displaying a form for adding a module)
* @param stdClass|section_info $section section where this module is located or will be added to
* @return bool
*/
public function allow_stealth_module_visibility($cm, $section) {
// Allow the third visibility state inside visible sections or in section 0.
return !$section->section || $section->visible;
}
/**
* Callback used in WS core_course_edit_section when teacher performs an AJAX action on a section (show/hide).
*
* Access to the course is already validated in the WS but the callback has to make sure
* that particular action is allowed by checking capabilities
*
* Course formats should register.
*
* @param section_info|stdClass $section
* @param string $action
* @param int $sr
* @return null|array any data for the Javascript post-processor (must be json-encodeable)
*/
public function section_action($section, $action, $sr) {
global $PAGE;
if ($section->section && ($action === 'setmarker' || $action === 'removemarker')) {
// Format 'topics' allows to set and remove markers in addition to common section actions.
require_capability('moodle/course:setcurrentsection', context_course::instance($this->courseid));
course_set_marker($this->courseid, ($action === 'setmarker') ? $section->section : 0);
return null;
}
// For show/hide actions call the parent method and return the new content for .section_availability element.
$rv = parent::section_action($section, $action, $sr);
$renderer = $PAGE->get_renderer('format_topics');
if (!($section instanceof section_info)) {
$modinfo = course_modinfo::instance($this->courseid);
$section = $modinfo->get_section_info($section->section);
}
$elementclass = $this->get_output_classname('content\\section\\availability');
$availability = new $elementclass($this, $section);
$rv['section_availability'] = $renderer->render($availability);
return $rv;
}
/**
* Return the plugin configs for external functions.
*
* @return array the list of configuration settings
* @since Moodle 3.5
*/
public function get_config_for_external() {
// Return everything (nothing to hide).
return $this->get_format_options();
}
/**
* Return Onetopic-specific extra section information.
*
* @return bool
*/
public function fot_get_sections_extra() {
if (isset($this->parentsections)) {
return $this->parentsections;
}
$course = $this->get_course();
$realcoursedisplay = property_exists($course, 'coursedisplay') ? $course->coursedisplay : false;
$firstsection = ($realcoursedisplay == COURSE_DISPLAY_MULTIPAGE) ? 1 : 0;
$sections = $this->get_sections();
$parentsections = [];
$level0section = null;
foreach ($sections as $section) {
if ($section->section <= $firstsection || $section->level <= 0) {
$parent = null;