MDL-35770 Include file defining base class in order to work correctly in cron
[moodle.git] / course / format / weeks / lib.php
CommitLineData
7d2a0492 1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
62a3f7ef 18 * This file contains main class for the course format Weeks
7d2a0492 19 *
62a3f7ef
MG
20 * @since 2.0
21 * @package format_weeks
7d2a0492 22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
399ad6bf
MG
26defined('MOODLE_INTERNAL') || die();
27require_once($CFG->dirroot. '/course/format/lib.php');
28
62a3f7ef
MG
29/**
30 * Main class for the Weeks course format
31 *
32 * @package format_weeks
33 * @copyright 2012 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class format_weeks extends format_base {
37
38 /**
39 * Returns true if this course format uses sections
40 *
62a3f7ef
MG
41 * @return bool
42 */
43 public function uses_sections() {
0f070867 44 return true;
62a3f7ef
MG
45 }
46
47 /**
48 * Returns the display name of the given section that the course prefers.
49 *
62a3f7ef
MG
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"
52 */
53 public function get_section_name($section) {
0f070867
MG
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');
61 } else {
1b2581f4 62 $dates = $this->get_section_dates($section);
62a3f7ef 63
0f070867
MG
64 // We subtract 24 hours for display purposes.
65 $dates->end = ($dates->end - 86400);
66
67 $dateformat = ' '.get_string('strftimedateshort');
68 $weekday = userdate($dates->start, $dateformat);
69 $endweekday = userdate($dates->end, $dateformat);
70 return $weekday.' - '.$endweekday;
71 }
62a3f7ef
MG
72 }
73
74 /**
75 * The URL to use for the specified course (with section)
76 *
62a3f7ef
MG
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
83 */
84 public function get_view_url($section, $options = array()) {
62a3f7ef
MG
85 $course = $this->get_course();
86 $url = new moodle_url('/course/view.php', array('id' => $course->id));
87
88 $sr = null;
89 if (array_key_exists('sr', $options)) {
90 $sr = $options['sr'];
91 }
92 if (is_object($section)) {
93 $sectionno = $section->section;
94 } else {
95 $sectionno = $section;
96 }
97 if ($sectionno !== null) {
98 if ($sr !== null) {
99 if ($sr) {
100 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
101 $sectionno = $sr;
102 } else {
103 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
104 }
105 } else {
106 $usercoursedisplay = $course->coursedisplay;
107 }
108 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
109 $url->param('section', $sectionno);
110 } else {
111 if (!empty($options['navigation'])) {
112 return null;
113 }
114 $url->set_anchor('section-'.$sectionno);
115 }
116 }
117 return $url;
118 }
119
120 /**
121 * Returns the information about the ajax support in the given source format
122 *
62a3f7ef
MG
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()}.
126 *
127 * @return stdClass
128 */
129 public function supports_ajax() {
0f070867
MG
130 $ajaxsupport = new stdClass();
131 $ajaxsupport->capable = true;
132 $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
62a3f7ef
MG
133 return $ajaxsupport;
134 }
135
136 /**
137 * Loads all of the course sections into the navigation
138 *
62a3f7ef
MG
139 * @param global_navigation $navigation
140 * @param navigation_node $node The course node within the navigation
141 */
142 public function extend_course_navigation($navigation, navigation_node $node) {
143 global $PAGE;
0f070867 144 // if section is specified in course/view.php, make sure it is expanded in navigation
62a3f7ef
MG
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;
150 }
151 }
0f070867 152 parent::extend_course_navigation($navigation, $node);
62a3f7ef
MG
153 }
154
155 /**
156 * Custom action after section has been moved in AJAX mode
157 *
158 * Used in course/rest.php
159 *
62a3f7ef
MG
160 * @return array This will be passed in ajax respose
161 */
162 function ajax_section_move() {
0f070867
MG
163 global $PAGE;
164 $titles = array();
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);
171 }
62a3f7ef 172 }
0f070867 173 return array('sectiontitles' => $titles, 'action' => 'move');
62a3f7ef
MG
174 }
175
176 /**
177 * Returns the list of blocks to be automatically added for the newly created course
178 *
62a3f7ef
MG
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)
181 */
182 public function get_default_blocks() {
0f070867
MG
183 return array(
184 BLOCK_POS_LEFT => array(),
185 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
186 );
62a3f7ef
MG
187 }
188
189 /**
190 * Definitions of the additional options that this course format uses for course
191 *
0f070867 192 * Weeks format uses the following options:
62a3f7ef
MG
193 * - coursedisplay
194 * - numsections
195 * - hiddensections
196 *
197 * @param bool $foreditform
198 * @return array of options
199 */
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,
207 'type' => PARAM_INT,
208 ),
209 'hiddensections' => array(
210 'default' => $courseconfig->hiddensections,
211 'type' => PARAM_INT,
212 ),
213 'coursedisplay' => array(
214 'default' => $courseconfig->coursedisplay,
215 'type' => PARAM_INT,
216 ),
217 );
218 }
219 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
220 $courseconfig = get_config('moodlecourse');
221 $sectionmenu = array();
222 for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
223 $sectionmenu[$i] = "$i";
224 }
225 $courseformatoptionsedit = array(
226 'numsections' => array(
227 'label' => new lang_string('numberweeks'),
228 'element_type' => 'select',
229 'element_attributes' => array($sectionmenu),
230 ),
231 'hiddensections' => array(
232 'label' => new lang_string('hiddensections'),
233 'help' => 'hiddensections',
234 'help_component' => 'moodle',
235 'element_type' => 'select',
236 'element_attributes' => array(
237 array(
238 0 => new lang_string('hiddensectionscollapsed'),
239 1 => new lang_string('hiddensectionsinvisible')
240 )
241 ),
242 ),
243 'coursedisplay' => array(
244 'label' => new lang_string('coursedisplay'),
245 'element_type' => 'select',
246 'element_attributes' => array(
247 array(
248 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
249 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
250 )
251 ),
252 'help' => 'coursedisplay',
253 'help_component' => 'moodle',
254 )
255 );
256 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
257 }
258 return $courseformatoptions;
259 }
260
261 /**
262 * Updates format options for a course
263 *
0f070867
MG
264 * In case if course format was changed to 'weeks', we try to copy options
265 * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
266 * If previous course format did not have 'numsections' option, we populate it with the
267 * current number of sections
62a3f7ef
MG
268 *
269 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
270 * @param stdClass $oldcourse if this function is called from {@link update_course()}
271 * this object contains information about the course before update
272 * @return bool whether there were any changes to the options values
273 */
274 public function update_course_format_options($data, $oldcourse = null) {
275 if ($oldcourse !== null) {
276 $data = (array)$data;
277 $oldcourse = (array)$oldcourse;
278 $options = $this->course_format_options();
279 foreach ($options as $key => $unused) {
280 if (!array_key_exists($key, $data)) {
281 if (array_key_exists($key, $oldcourse)) {
282 $data[$key] = $oldcourse[$key];
283 } else if ($key === 'numsections') {
0f070867
MG
284 // If previous format does not have the field 'numsections'
285 // and $data['numsections'] is not set,
286 // we fill it with the maximum section number from the DB
62a3f7ef
MG
287 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
288 WHERE course = ?', array($this->courseid));
289 if ($maxsection) {
290 // If there are no sections, or just default 0-section, 'numsections' will be set to default
291 $data['numsections'] = $maxsection;
292 }
293 }
294 }
295 }
296 }
297 return $this->update_format_options($data);
298 }
7487c856 299
1b2581f4
MG
300 /**
301 * Return the start and end date of the passed section
302 *
303 * @param int|stdClass|section_info $section section to get the dates for
304 * @return stdClass property start for startdate, property end for enddate
305 */
306 public function get_section_dates($section) {
307 $course = $this->get_course();
308 if (is_object($section)) {
309 $sectionnum = $section->section;
310 } else {
311 $sectionnum = $section;
312 }
313 $oneweekseconds = 604800;
314 // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight
315 // savings and the date changes.
316 $startdate = $course->startdate + 7200;
b6283a49 317
1b2581f4
MG
318 $dates = new stdClass();
319 $dates->start = $startdate + ($oneweekseconds * ($sectionnum - 1));
320 $dates->end = $dates->start + $oneweekseconds;
b6283a49 321
1b2581f4
MG
322 return $dates;
323 }
081c8f7f
MG
324
325 /**
326 * Returns true if the specified week is current
327 *
328 * @param int|stdClass|section_info $section
329 * @return bool
330 */
331 public function is_section_current($section) {
332 if (is_object($section)) {
333 $sectionnum = $section->section;
334 } else {
335 $sectionnum = $section;
336 }
337 if ($sectionnum < 1) {
338 return false;
339 }
340 $timenow = time();
341 $dates = $this->get_section_dates($section);
342 return (($timenow >= $dates->start) && ($timenow < $dates->end));
343 }
b6283a49 344}