forked from oohoo/moodle-format_tabtopics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
445 lines (405 loc) · 16.6 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
<?php
/**
* *************************************************************************
* * OOHOO Tab topics Course format **
* *************************************************************************
* @package format **
* @subpackage tabtopics **
* @name tabtopics **
* @copyright oohoo.biz **
* @link http://oohoo.biz **
* @author Nicolas Bretin **
* @author Braedan Jongerius **
* @author Dustin Durand **
* @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');
class format_tabtopics 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, array('context' => context_course::instance($this->courseid)));
} else if ($section->section == 0) {
return get_string('section0name', 'format_tabtopics');
} else {
return get_string('topic') . ' ' . $section->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 = array()) {
$course = $this->get_course();
$url = new moodle_url('/course/view.php', array('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_DISPLAY_SINGLEPAGE;
}
if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$url->param('section', $sectionno);
} else {
if (!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.
* The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
*
* @return stdClass
*/
public function supports_ajax() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
$ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
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
*/
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);
}
/**
* Custom action after section has been moved in AJAX mode
*
* Used in course/rest.php
*
* @return array This will be passed in ajax respose
*/
function ajax_section_move() {
global $PAGE;
$titles = array();
$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 array('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 array(
BLOCK_POS_LEFT => array(),
BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
);
}
/**
* Definitions of the additional options that this course format uses for course
*
* TabTopics format uses the following options:
* - coursedisplay
* - numsections
* - hiddensections
* - is zero section a tab
*
* @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 = array(
'numsections' => array(
'default' => $courseconfig->numsections,
'type' => PARAM_INT,
),
'hiddensections' => array(
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT,
),
'isZeroTab' => array(
'label' => new lang_string('tabtopics_zero_as_tab', 'format_tabtopics'),
'help' => 'tabtopics_zero_as_tab',
'element_type' => 'advcheckbox',
'default' => 0,
'element_attributes' => array('', array('group' => 1), array(0, 1)),
'type' => PARAM_INT,
),
'remember_last_tab_session' => array(
'label' => new lang_string('tabtopics_remember_last_tab_session', 'format_tabtopics'),
'help' => 'tabtopics_remember_last_tab_session',
'element_type' => 'selectyesno',
'default' => 1,
'type' => PARAM_INT,
),
'tabdisplay_vertical' => array(
'label' => new lang_string('tabtopics_tabdisplay_vertical', 'format_tabtopics'),
'help' => 'tabtopics_tabdisplay_vertical',
'element_type' => 'advcheckbox',
'default' => 0,
'element_attributes' => array('', array('group' => 1), array(0, 1)),
'type' => PARAM_INT,
),
);
}
if ($foreditform) {
$courseconfig = get_config('moodlecourse');
$max = $courseconfig->maxsections;
if (!isset($max) || !is_numeric($max)) {
$max = 52;
}
$sectionmenu = array();
for ($i = 0; $i <= $max; $i++) {
$sectionmenu[$i] = "$i";
}
$courseformatoptionsedit = array(
'numsections' => array(
'label' => new lang_string('numberweeks'),
'element_type' => 'select',
'element_attributes' => array($sectionmenu),
),
'hiddensections' => array(
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
'help_component' => 'moodle',
'element_type' => 'select',
'element_attributes' => array(
array(
0 => new lang_string('hiddensectionscollapsed'),
1 => new lang_string('hiddensectionsinvisible')
)
),
),
);
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
}
return $courseformatoptions;
}
/**
* Returns whether the current course should have the section 0 as a header
* or a tab. If for any reason the setting doesn't exist then it defaults to
* header. (false)
*
* Defaults to false if setting doesn't exist.
*
* @global type $DB
* @return boolean
*/
public function is_section_zero_tab() {
global $DB;
//get course id
$courseid = $this->get_courseid();
//course id is zero id course doesn't exist - shouldn't happen
if ($courseid == 0)
return false;
//get option
$option = $DB->get_record('course_format_options', array('courseid' => $courseid, 'name' => 'isZeroTab'));
//if this value never existed, then we assume false
if (!$option)
return false;
return $option->value == 1 ? true : false;
}
/**
* Returns whether the current course should save the last tab the user clicked or returned to the default tab
*
* Defaults to false if setting doesn't exist.
*
* @global type $DB
* @return boolean
*/
public function is_remember_last_tab_session() {
global $DB;
//get course id
$courseid = $this->get_courseid();
//course id is zero id course doesn't exist - shouldn't happen
if ($courseid == 0)
return false;
//get option
$option = $DB->get_record('course_format_options', array('courseid' => $courseid, 'name' => 'remember_last_tab_session'));
//if this value never existed, then we assume false
if (!$option)
return false;
return $option->value == 1 ? true : false;
}
/**
* Returns whether the tabs for the current course should display vertical or not.
*
* Defaults to false if setting doesn't exist.
*
* @global type $DB
* @return boolean
*/
public function is_tabdisplay_vertical() {
global $DB;
//get course id
$courseid = $this->get_courseid();
//course id is zero id course doesn't exist - shouldn't happen
if ($courseid == 0) {
return false;
}
//get option
$option = $DB->get_record('course_format_options', array('courseid' => $courseid, 'name' => 'tabdisplay_vertical'));
//if this value never existed, then we assume false
if (!$option) {
return false;
}
return $option->value == 1 ? true : false;
}
/**
* Updates format options for a course
*
* In case if course format was changed to 'tabtopics', we try to copy options
* 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
* If previous course format did not have 'numsections' option, we populate it with the
* current number of sections
*
* @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
* @param stdClass $oldcourse if this function is called from {@link 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) {
if ($oldcourse !== null) {
$data = (array) $data;
$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 === 'numsections') {
// If previous format does not have the field 'numsections'
// and $data['numsections'] is not set,
// we fill it with the maximum section number from the DB
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
if ($maxsection) {
// If there are no sections, or just default 0-section, 'numsections' will be set to default
$data['numsections'] = $maxsection;
}
}
}
}
}
return $this->update_format_options($data);
}
/**
* Determines whether the unavaliable override is avaliable
*
* @param type $thissection
* @return boolean
*/
public function is_unavailable_override($thissection) {
$course = $this->get_course();
if (!$course->hiddensections && $thissection->available) {
return true;
}
return false;
}
/**
* Determines whether the user can access or view the section
*
* @global Object $USER
* @global Object $CFG
* @param Object $thissection
* @return boolean true on visible to user, else false
*/
public function check_user_access($thissection) {
// Show the section if the user is permitted to access it, OR if it's not available
// but showavailability is turned on (and there is some available info text).
$showsection = $thissection->uservisible;
//print_object($thissection);
if (!$showsection) {
return false;
}
return true;
}
/**
* Whether this format allows to delete sections
*
* @param int|stdClass|section_info $section
* @return bool
*/
public function can_delete_section($section) {
return true;
}
}
/**
* Implements callback inplace_editable() allowing to edit values in-place
*
* @param string $itemtype
* @param int $itemid
* @param mixed $newvalue
* @return \core\output\inplace_editable
*/
function format_tabtopics_inplace_editable($itemtype, $itemid, $newvalue) {
global $DB, $CFG;
require_once($CFG->dirroot . '/course/lib.php');
if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
$section = $DB->get_record_sql(
'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?',
array($itemid, 'tabtopics'), MUST_EXIST);
return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
}
}