MDL-35770 Created class format_topics as copy of format_legacy
[moodle.git] / course / format / weeks / lib.php
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/>.
17 /**
18  * This file contains general functions for the course format Week
19  *
20  * @since 2.0
21  * @package moodlecore
22  * @copyright 2009 Sam Hemelryk
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 /**
28  * Indicates this format uses sections.
29  *
30  * @return bool Returns true
31  */
32 function callback_weeks_uses_sections() {
33     return true;
34 }
36 /**
37  * Used to display the course structure for a course where format=weeks
38  *
39  * This is called automatically by {@link load_course()} if the current course
40  * format = weeks.
41  *
42  * @param navigation_node $navigation The course node
43  * @param array $path An array of keys to the course node
44  * @param stdClass $course The course we are loading the section for
45  */
46 function callback_weeks_load_content(&$navigation, $course, $coursenode) {
47     return $navigation->load_generic_course_sections($course, $coursenode, 'weeks');
48 }
50 /**
51  * The string that is used to describe a section of the course
52  * e.g. Topic, Week...
53  *
54  * @return string
55  */
56 function callback_weeks_definition() {
57     return get_string('week');
58 }
60 /**
61  * Gets the name for the provided section.
62  *
63  * @param stdClass $course
64  * @param stdClass $section
65  * @return string
66  */
67 function callback_weeks_get_section_name($course, $section) {
68     // We can't add a node without text
69     if ((string)$section->name !== '') {
70         // Return the name the user set.
71         return format_string($section->name, true, array('context' => context_course::instance($course->id)));
72     } else if ($section->section == 0) {
73         // Return the general section.
74         return get_string('section0name', 'format_weeks');
75     } else {
76         $dates = format_weeks_get_section_dates($section, $course);
78         // We subtract 24 hours for display purposes.
79         $dates->end = ($dates->end - 86400);
81         $dateformat = ' '.get_string('strftimedateshort');
82         $weekday = userdate($dates->start, $dateformat);
83         $endweekday = userdate($dates->end, $dateformat);
84         return $weekday.' - '.$endweekday;
85     }
86 }
88 /**
89  * Declares support for course AJAX features
90  *
91  * @see course_format_ajax_support()
92  * @return stdClass
93  */
94 function callback_weeks_ajax_support() {
95     $ajaxsupport = new stdClass();
96     $ajaxsupport->capable = true;
97     $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
98     return $ajaxsupport;
99 }
101 /**
102  * Return the start and end date of the passed section
103  *
104  * @param stdClass $section The course_section entry from the DB
105  * @param stdClass $course The course entry from DB
106  * @return stdClass property start for startdate, property end for enddate
107  */
108 function format_weeks_get_section_dates($section, $course) {
109     $oneweekseconds = 604800;
110     // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight
111     // savings and the date changes.
112     $startdate = $course->startdate + 7200;
114     $dates = new stdClass();
115     $dates->start = $startdate + ($oneweekseconds * ($section->section - 1));
116     $dates->end = $dates->start + $oneweekseconds;
118     return $dates;
121 /**
122  * Callback function to do some action after section move
123  *
124  * @param stdClass $course The course entry from DB
125  * @return array This will be passed in ajax respose.
126  */
127 function callback_weeks_ajax_section_move($course) {
128     global $COURSE, $PAGE;
130     $titles = array();
131     rebuild_course_cache($course->id);
132     $modinfo = get_fast_modinfo($COURSE);
133     $renderer = $PAGE->get_renderer('format_weeks');
134     if ($renderer && ($sections = $modinfo->get_section_info_all())) {
135         foreach ($sections as $number => $section) {
136             $titles[$number] = $renderer->section_title($section, $course);
137         }
138     }
139     return array('sectiontitles' => $titles, 'action' => 'move');