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
26 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot. '/course/format/lib.php');
30 * Main class for the Weeks course format
32 * @package format_weeks
33 * @copyright 2012 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class format_weeks extends format_base {
39 * Returns true if this course format uses sections
43 public function uses_sections() {
48 * Returns the display name of the given section that the course prefers.
50 * @param int|stdClass $section Section object from database or just field section.section
51 * @return string Display name that the course format prefers, e.g. "Topic 2"
53 public function get_section_name($section) {
54 $section = $this->get_section($section);
55 if ((string)$section->name !== '') {
56 // Return the name the user set.
57 return format_string($section->name, true, array('context' => context_course::instance($this->courseid)));
58 } else if ($section->section == 0) {
59 // Return the general section.
60 return get_string('section0name', 'format_weeks');
62 $dates = $this->get_section_dates($section);
64 // We subtract 24 hours for display purposes.
65 $dates->end = ($dates->end - 86400);
67 $dateformat = ' '.get_string('strftimedateshort');
68 $weekday = userdate($dates->start, $dateformat);
69 $endweekday = userdate($dates->end, $dateformat);
70 return $weekday.' - '.$endweekday;
75 * The URL to use for the specified course (with section)
77 * @param int|stdClass $section Section object from database or just field course_sections.section
78 * if omitted the course view page is returned
79 * @param array $options options for view URL. At the moment core uses:
80 * 'navigation' (bool) if true and section has no separate page, the function returns null
81 * 'sr' (int) used by multipage formats to specify to which section to return
82 * @return null|moodle_url
84 public function get_view_url($section, $options = array()) {
85 $course = $this->get_course();
86 $url = new moodle_url('/course/view.php', array('id' => $course->id));
89 if (array_key_exists('sr', $options)) {
92 if (is_object($section)) {
93 $sectionno = $section->section;
95 $sectionno = $section;
97 if ($sectionno !== null) {
100 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
103 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
106 $usercoursedisplay = $course->coursedisplay;
108 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
109 $url->param('section', $sectionno);
111 if (!empty($options['navigation'])) {
114 $url->set_anchor('section-'.$sectionno);
121 * Returns the information about the ajax support in the given source format
123 * The returned object's property (boolean)capable indicates that
124 * the course format supports Moodle course ajax features.
125 * The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
129 public function supports_ajax() {
130 $ajaxsupport = new stdClass();
131 $ajaxsupport->capable = true;
132 $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
137 * Loads all of the course sections into the navigation
139 * @param global_navigation $navigation
140 * @param navigation_node $node The course node within the navigation
142 public function extend_course_navigation($navigation, navigation_node $node) {
144 // if section is specified in course/view.php, make sure it is expanded in navigation
145 if ($navigation->includesectionnum === false) {
146 $selectedsection = optional_param('section', null, PARAM_INT);
147 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
148 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
149 $navigation->includesectionnum = $selectedsection;
152 parent::extend_course_navigation($navigation, $node);
156 * Custom action after section has been moved in AJAX mode
158 * Used in course/rest.php
160 * @return array This will be passed in ajax respose
162 function ajax_section_move() {
165 $course = $this->get_course();
166 $modinfo = get_fast_modinfo($course);
167 $renderer = $this->get_renderer($PAGE);
168 if ($renderer && ($sections = $modinfo->get_section_info_all())) {
169 foreach ($sections as $number => $section) {
170 $titles[$number] = $renderer->section_title($section, $course);
173 return array('sectiontitles' => $titles, 'action' => 'move');
177 * Returns the list of blocks to be automatically added for the newly created course
179 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
180 * each of values is an array of block names (for left and right side columns)
182 public function get_default_blocks() {
184 BLOCK_POS_LEFT => array(),
185 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
190 * Definitions of the additional options that this course format uses for course
192 * Weeks format uses the following options:
197 * @param bool $foreditform
198 * @return array of options
200 public function course_format_options($foreditform = false) {
201 static $courseformatoptions = false;
202 if ($courseformatoptions === false) {
203 $courseconfig = get_config('moodlecourse');
204 $courseformatoptions = array(
205 'numsections' => array(
206 'default' => $courseconfig->numsections,
209 'hiddensections' => array(
210 'default' => $courseconfig->hiddensections,
213 'coursedisplay' => array(
214 'default' => $courseconfig->coursedisplay,
219 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
220 $courseconfig = get_config('moodlecourse');
221 $sectionmenu = array();
222 $max = $courseconfig->maxsections;
223 if (!isset($max) || !is_numeric($max)) {
226 for ($i = 0; $i <= $max; $i++) {
227 $sectionmenu[$i] = "$i";
229 $courseformatoptionsedit = array(
230 'numsections' => array(
231 'label' => new lang_string('numberweeks'),
232 'element_type' => 'select',
233 'element_attributes' => array($sectionmenu),
235 'hiddensections' => array(
236 'label' => new lang_string('hiddensections'),
237 'help' => 'hiddensections',
238 'help_component' => 'moodle',
239 'element_type' => 'select',
240 'element_attributes' => array(
242 0 => new lang_string('hiddensectionscollapsed'),
243 1 => new lang_string('hiddensectionsinvisible')
247 'coursedisplay' => array(
248 'label' => new lang_string('coursedisplay'),
249 'element_type' => 'select',
250 'element_attributes' => array(
252 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
253 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
256 'help' => 'coursedisplay',
257 'help_component' => 'moodle',
260 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
262 return $courseformatoptions;
266 * Updates format options for a course
268 * In case if course format was changed to 'weeks', we try to copy options
269 * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
270 * If previous course format did not have 'numsections' option, we populate it with the
271 * current number of sections
273 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
274 * @param stdClass $oldcourse if this function is called from {@link update_course()}
275 * this object contains information about the course before update
276 * @return bool whether there were any changes to the options values
278 public function update_course_format_options($data, $oldcourse = null) {
279 if ($oldcourse !== null) {
280 $data = (array)$data;
281 $oldcourse = (array)$oldcourse;
282 $options = $this->course_format_options();
283 foreach ($options as $key => $unused) {
284 if (!array_key_exists($key, $data)) {
285 if (array_key_exists($key, $oldcourse)) {
286 $data[$key] = $oldcourse[$key];
287 } else if ($key === 'numsections') {
288 // If previous format does not have the field 'numsections'
289 // and $data['numsections'] is not set,
290 // we fill it with the maximum section number from the DB
291 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
292 WHERE course = ?', array($this->courseid));
294 // If there are no sections, or just default 0-section, 'numsections' will be set to default
295 $data['numsections'] = $maxsection;
301 return $this->update_format_options($data);
305 * Return the start and end date of the passed section
307 * @param int|stdClass|section_info $section section to get the dates for
308 * @return stdClass property start for startdate, property end for enddate
310 public function get_section_dates($section) {
311 $course = $this->get_course();
312 if (is_object($section)) {
313 $sectionnum = $section->section;
315 $sectionnum = $section;
317 $oneweekseconds = 604800;
318 // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight
319 // savings and the date changes.
320 $startdate = $course->startdate + 7200;
322 $dates = new stdClass();
323 $dates->start = $startdate + ($oneweekseconds * ($sectionnum - 1));
324 $dates->end = $dates->start + $oneweekseconds;
330 * Returns true if the specified week is current
332 * @param int|stdClass|section_info $section
335 public function is_section_current($section) {
336 if (is_object($section)) {
337 $sectionnum = $section->section;
339 $sectionnum = $section;
341 if ($sectionnum < 1) {
345 $dates = $this->get_section_dates($section);
346 return (($timenow >= $dates->start) && ($timenow < $dates->end));