2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains main class for the course format Weeks
21 * @package format_weeks
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * Main class for the Weeks course format
29 * @package format_weeks
30 * @copyright 2012 Marina Glancy
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class format_weeks extends format_base {
36 * Returns true if this course format uses sections
38 * This function calls function callback_FORMATNAME_uses_sections() if it exists
42 public function uses_sections() {
44 // Note that lib.php in course format folder is already included by now
45 $featurefunction = 'callback_'.$this->format.'_uses_sections';
46 if (function_exists($featurefunction)) {
47 return $featurefunction();
53 * Returns the display name of the given section that the course prefers.
55 * This function calls function callback_FORMATNAME_get_section_name() if it exists
57 * @param int|stdClass $section Section object from database or just field section.section
58 * @return string Display name that the course format prefers, e.g. "Topic 2"
60 public function get_section_name($section) {
61 // Use course formatter callback if it exists
62 $namingfunction = 'callback_'.$this->format.'_get_section_name';
63 if (function_exists($namingfunction) && ($course = $this->get_course())) {
64 return $namingfunction($course, $this->get_section($section));
67 // else, default behavior:
68 return parent::get_section_name($section);
72 * The URL to use for the specified course (with section)
74 * This function calls function callback_FORMATNAME_get_section_url() if it exists
76 * @param int|stdClass $section Section object from database or just field course_sections.section
77 * if omitted the course view page is returned
78 * @param array $options options for view URL. At the moment core uses:
79 * 'navigation' (bool) if true and section has no separate page, the function returns null
80 * 'sr' (int) used by multipage formats to specify to which section to return
81 * @return null|moodle_url
83 public function get_view_url($section, $options = array()) {
84 // Use course formatter callback if it exists
85 $featurefunction = 'callback_'.$this->format.'_get_section_url';
86 if (function_exists($featurefunction) && ($course = $this->get_course())) {
87 if (is_object($section)) {
88 $sectionnum = $section->section;
90 $sectionnum = $section;
93 $url = $featurefunction($course, $sectionnum);
94 if ($url || !empty($options['navigation'])) {
100 // if function is not defined
101 if (!$this->uses_sections() ||
102 !array_key_exists('coursedisplay', $this->course_format_options())) {
104 return parent::get_view_url($section, $options);
107 $course = $this->get_course();
108 $url = new moodle_url('/course/view.php', array('id' => $course->id));
111 if (array_key_exists('sr', $options)) {
112 $sr = $options['sr'];
114 if (is_object($section)) {
115 $sectionno = $section->section;
117 $sectionno = $section;
119 if ($sectionno !== null) {
122 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
125 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
128 $usercoursedisplay = $course->coursedisplay;
130 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
131 $url->param('section', $sectionno);
133 if (!empty($options['navigation'])) {
136 $url->set_anchor('section-'.$sectionno);
143 * Returns the information about the ajax support in the given source format
145 * This function calls function callback_FORMATNAME_ajax_support() if it exists
147 * The returned object's property (boolean)capable indicates that
148 * the course format supports Moodle course ajax features.
149 * The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
153 public function supports_ajax() {
154 // set up default values
155 $ajaxsupport = parent::supports_ajax();
157 // get the information from the course format library
158 $featurefunction = 'callback_'.$this->format.'_ajax_support';
159 if (function_exists($featurefunction)) {
160 $formatsupport = $featurefunction();
161 if (isset($formatsupport->capable)) {
162 $ajaxsupport->capable = $formatsupport->capable;
164 if (is_array($formatsupport->testedbrowsers)) {
165 $ajaxsupport->testedbrowsers = $formatsupport->testedbrowsers;
172 * Loads all of the course sections into the navigation
174 * First this function calls callback_FORMATNAME_display_content() if it exists to check
175 * if the navigation should be extended at all
177 * Then it calls function callback_FORMATNAME_load_content() if it exists to actually extend
180 * By default the parent method is called
182 * @param global_navigation $navigation
183 * @param navigation_node $node The course node within the navigation
185 public function extend_course_navigation($navigation, navigation_node $node) {
187 // if course format displays section on separate pages and we are on course/view.php page
188 // and the section parameter is specified, make sure this section is expanded in
190 if ($navigation->includesectionnum === false) {
191 $selectedsection = optional_param('section', null, PARAM_INT);
192 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
193 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
194 $navigation->includesectionnum = $selectedsection;
198 // check if there are callbacks to extend course navigation
199 $displayfunc = 'callback_'.$this->format.'_display_content';
200 if (function_exists($displayfunc) && !$displayfunc()) {
203 $featurefunction = 'callback_'.$this->format.'_load_content';
204 if (function_exists($featurefunction) && ($course = $this->get_course())) {
205 $featurefunction($navigation, $course, $node);
207 parent::extend_course_navigation($navigation, $node);
212 * Custom action after section has been moved in AJAX mode
214 * Used in course/rest.php
216 * This function calls function callback_FORMATNAME_ajax_section_move() if it exists
218 * @return array This will be passed in ajax respose
220 function ajax_section_move() {
221 $featurefunction = 'callback_'.$this->format.'_ajax_section_move';
222 if (function_exists($featurefunction) && ($course = $this->get_course())) {
223 return $featurefunction($course);
225 return parent::ajax_section_move();
230 * Returns the list of blocks to be automatically added for the newly created course
232 * This function checks the existence of the file config.php in the course format folder.
233 * If file exists and contains the code
234 * $format['defaultblocks'] = 'leftblock1,leftblock2:rightblock1,rightblock2';
235 * these blocks are used, otherwise parent function is called
237 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
238 * each of values is an array of block names (for left and right side columns)
240 public function get_default_blocks() {
242 $formatconfig = $CFG->dirroot.'/course/format/'.$this->format.'/config.php';
243 $format = array(); // initialize array in external file
244 if (is_readable($formatconfig)) {
245 include($formatconfig);
247 if (!empty($format['defaultblocks'])) {
248 return blocks_parse_default_blocks_list($format['defaultblocks']);
250 return parent::get_default_blocks();
254 * Definitions of the additional options that this course format uses for course
256 * By default course formats have the options that existed in Moodle 2.3:
261 * @param bool $foreditform
262 * @return array of options
264 public function course_format_options($foreditform = false) {
265 static $courseformatoptions = false;
266 if ($courseformatoptions === false) {
267 $courseconfig = get_config('moodlecourse');
268 $courseformatoptions = array(
269 'numsections' => array(
270 'default' => $courseconfig->numsections,
273 'hiddensections' => array(
274 'default' => $courseconfig->hiddensections,
277 'coursedisplay' => array(
278 'default' => $courseconfig->coursedisplay,
283 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
284 $courseconfig = get_config('moodlecourse');
285 $sectionmenu = array();
286 for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
287 $sectionmenu[$i] = "$i";
289 $courseformatoptionsedit = array(
290 'numsections' => array(
291 'label' => new lang_string('numberweeks'),
292 'element_type' => 'select',
293 'element_attributes' => array($sectionmenu),
295 'hiddensections' => array(
296 'label' => new lang_string('hiddensections'),
297 'help' => 'hiddensections',
298 'help_component' => 'moodle',
299 'element_type' => 'select',
300 'element_attributes' => array(
302 0 => new lang_string('hiddensectionscollapsed'),
303 1 => new lang_string('hiddensectionsinvisible')
307 'coursedisplay' => array(
308 'label' => new lang_string('coursedisplay'),
309 'element_type' => 'select',
310 'element_attributes' => array(
312 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
313 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
316 'help' => 'coursedisplay',
317 'help_component' => 'moodle',
320 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
322 return $courseformatoptions;
326 * Updates format options for a course
328 * Legacy course formats may assume that course format options
329 * ('coursedisplay', 'numsections' and 'hiddensections') are shared between formats.
330 * Therefore we make sure to copy them from the previous format
332 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
333 * @param stdClass $oldcourse if this function is called from {@link update_course()}
334 * this object contains information about the course before update
335 * @return bool whether there were any changes to the options values
337 public function update_course_format_options($data, $oldcourse = null) {
338 if ($oldcourse !== null) {
339 $data = (array)$data;
340 $oldcourse = (array)$oldcourse;
341 $options = $this->course_format_options();
342 foreach ($options as $key => $unused) {
343 if (!array_key_exists($key, $data)) {
344 if (array_key_exists($key, $oldcourse)) {
345 $data[$key] = $oldcourse[$key];
346 } else if ($key === 'numsections') {
347 // If previous format does not have the field 'numsections' and this one does,
348 // and $data['numsections'] is not set fill it with the maximum section number from the DB
349 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
350 WHERE course = ?', array($this->courseid));
352 // If there are no sections, or just default 0-section, 'numsections' will be set to default
353 $data['numsections'] = $maxsection;
359 return $this->update_format_options($data);
364 * Indicates this format uses sections.
366 * @return bool Returns true
368 function callback_weeks_uses_sections() {
373 * Used to display the course structure for a course where format=weeks
375 * This is called automatically by {@link load_course()} if the current course
378 * @param navigation_node $navigation The course node
379 * @param array $path An array of keys to the course node
380 * @param stdClass $course The course we are loading the section for
382 function callback_weeks_load_content(&$navigation, $course, $coursenode) {
383 return $navigation->load_generic_course_sections($course, $coursenode, 'weeks');
387 * The string that is used to describe a section of the course
388 * e.g. Topic, Week...
392 function callback_weeks_definition() {
393 return get_string('week');
397 * Gets the name for the provided section.
399 * @param stdClass $course
400 * @param stdClass $section
403 function callback_weeks_get_section_name($course, $section) {
404 // We can't add a node without text
405 if ((string)$section->name !== '') {
406 // Return the name the user set.
407 return format_string($section->name, true, array('context' => context_course::instance($course->id)));
408 } else if ($section->section == 0) {
409 // Return the general section.
410 return get_string('section0name', 'format_weeks');
412 $dates = format_weeks_get_section_dates($section, $course);
414 // We subtract 24 hours for display purposes.
415 $dates->end = ($dates->end - 86400);
417 $dateformat = ' '.get_string('strftimedateshort');
418 $weekday = userdate($dates->start, $dateformat);
419 $endweekday = userdate($dates->end, $dateformat);
420 return $weekday.' - '.$endweekday;
425 * Declares support for course AJAX features
427 * @see course_format_ajax_support()
430 function callback_weeks_ajax_support() {
431 $ajaxsupport = new stdClass();
432 $ajaxsupport->capable = true;
433 $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
438 * Return the start and end date of the passed section
440 * @param stdClass $section The course_section entry from the DB
441 * @param stdClass $course The course entry from DB
442 * @return stdClass property start for startdate, property end for enddate
444 function format_weeks_get_section_dates($section, $course) {
445 $oneweekseconds = 604800;
446 // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight
447 // savings and the date changes.
448 $startdate = $course->startdate + 7200;
450 $dates = new stdClass();
451 $dates->start = $startdate + ($oneweekseconds * ($section->section - 1));
452 $dates->end = $dates->start + $oneweekseconds;
458 * Callback function to do some action after section move
460 * @param stdClass $course The course entry from DB
461 * @return array This will be passed in ajax respose.
463 function callback_weeks_ajax_section_move($course) {
464 global $COURSE, $PAGE;
467 rebuild_course_cache($course->id);
468 $modinfo = get_fast_modinfo($COURSE);
469 $renderer = $PAGE->get_renderer('format_weeks');
470 if ($renderer && ($sections = $modinfo->get_section_info_all())) {
471 foreach ($sections as $number => $section) {
472 $titles[$number] = $renderer->section_title($section, $course);
475 return array('sectiontitles' => $titles, 'action' => 'move');