Commit | Line | Data |
---|---|---|
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 | /** | |
2fb8c9a2 | 18 | * This file contains main class for the course format Topic |
7d2a0492 | 19 | * |
5bcfd504 | 20 | * @since Moodle 2.0 |
2fb8c9a2 | 21 | * @package format_topics |
7d2a0492 | 22 | * @copyright 2009 Sam Hemelryk |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
399ad6bf MG |
26 | defined('MOODLE_INTERNAL') || die(); |
27 | require_once($CFG->dirroot. '/course/format/lib.php'); | |
28 | ||
2fb8c9a2 MG |
29 | /** |
30 | * Main class for the Topics course format | |
31 | * | |
32 | * @package format_topics | |
33 | * @copyright 2012 Marina Glancy | |
34 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
35 | */ | |
36 | class format_topics extends format_base { | |
37 | ||
38 | /** | |
39 | * Returns true if this course format uses sections | |
40 | * | |
2fb8c9a2 MG |
41 | * @return bool |
42 | */ | |
43 | public function uses_sections() { | |
222cd9c9 | 44 | return true; |
2fb8c9a2 MG |
45 | } |
46 | ||
47 | /** | |
48 | * Returns the display name of the given section that the course prefers. | |
49 | * | |
222cd9c9 | 50 | * Use section name is specified by user. Otherwise use default ("Topic #") |
2fb8c9a2 MG |
51 | * |
52 | * @param int|stdClass $section Section object from database or just field section.section | |
53 | * @return string Display name that the course format prefers, e.g. "Topic 2" | |
54 | */ | |
55 | public function get_section_name($section) { | |
222cd9c9 MG |
56 | $section = $this->get_section($section); |
57 | if ((string)$section->name !== '') { | |
58 | return format_string($section->name, true, | |
59 | array('context' => context_course::instance($this->courseid))); | |
60 | } else if ($section->section == 0) { | |
61 | return get_string('section0name', 'format_topics'); | |
62 | } else { | |
63 | return get_string('topic').' '.$section->section; | |
2fb8c9a2 | 64 | } |
2fb8c9a2 MG |
65 | } |
66 | ||
67 | /** | |
68 | * The URL to use for the specified course (with section) | |
69 | * | |
2fb8c9a2 MG |
70 | * @param int|stdClass $section Section object from database or just field course_sections.section |
71 | * if omitted the course view page is returned | |
72 | * @param array $options options for view URL. At the moment core uses: | |
73 | * 'navigation' (bool) if true and section has no separate page, the function returns null | |
74 | * 'sr' (int) used by multipage formats to specify to which section to return | |
75 | * @return null|moodle_url | |
76 | */ | |
77 | public function get_view_url($section, $options = array()) { | |
2fb8c9a2 MG |
78 | $course = $this->get_course(); |
79 | $url = new moodle_url('/course/view.php', array('id' => $course->id)); | |
80 | ||
81 | $sr = null; | |
82 | if (array_key_exists('sr', $options)) { | |
83 | $sr = $options['sr']; | |
84 | } | |
85 | if (is_object($section)) { | |
86 | $sectionno = $section->section; | |
87 | } else { | |
88 | $sectionno = $section; | |
89 | } | |
90 | if ($sectionno !== null) { | |
91 | if ($sr !== null) { | |
92 | if ($sr) { | |
93 | $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE; | |
94 | $sectionno = $sr; | |
95 | } else { | |
96 | $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE; | |
97 | } | |
98 | } else { | |
99 | $usercoursedisplay = $course->coursedisplay; | |
100 | } | |
101 | if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) { | |
102 | $url->param('section', $sectionno); | |
103 | } else { | |
104 | if (!empty($options['navigation'])) { | |
105 | return null; | |
106 | } | |
107 | $url->set_anchor('section-'.$sectionno); | |
108 | } | |
109 | } | |
110 | return $url; | |
111 | } | |
112 | ||
113 | /** | |
114 | * Returns the information about the ajax support in the given source format | |
115 | * | |
2fb8c9a2 MG |
116 | * The returned object's property (boolean)capable indicates that |
117 | * the course format supports Moodle course ajax features. | |
2fb8c9a2 MG |
118 | * |
119 | * @return stdClass | |
120 | */ | |
121 | public function supports_ajax() { | |
222cd9c9 MG |
122 | $ajaxsupport = new stdClass(); |
123 | $ajaxsupport->capable = true; | |
2fb8c9a2 MG |
124 | return $ajaxsupport; |
125 | } | |
126 | ||
127 | /** | |
128 | * Loads all of the course sections into the navigation | |
129 | * | |
2fb8c9a2 MG |
130 | * @param global_navigation $navigation |
131 | * @param navigation_node $node The course node within the navigation | |
132 | */ | |
133 | public function extend_course_navigation($navigation, navigation_node $node) { | |
134 | global $PAGE; | |
222cd9c9 | 135 | // if section is specified in course/view.php, make sure it is expanded in navigation |
2fb8c9a2 MG |
136 | if ($navigation->includesectionnum === false) { |
137 | $selectedsection = optional_param('section', null, PARAM_INT); | |
138 | if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') && | |
139 | $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { | |
140 | $navigation->includesectionnum = $selectedsection; | |
141 | } | |
142 | } | |
143 | ||
144 | // check if there are callbacks to extend course navigation | |
222cd9c9 | 145 | parent::extend_course_navigation($navigation, $node); |
701772c0 SH |
146 | |
147 | // We want to remove the general section if it is empty. | |
148 | $modinfo = get_fast_modinfo($this->get_course()); | |
149 | $sections = $modinfo->get_sections(); | |
150 | if (!isset($sections[0])) { | |
151 | // The general section is empty to find the navigation node for it we need to get its ID. | |
152 | $section = $modinfo->get_section_info(0); | |
153 | $generalsection = $node->get($section->id, navigation_node::TYPE_SECTION); | |
154 | if ($generalsection) { | |
155 | // We found the node - now remove it. | |
156 | $generalsection->remove(); | |
157 | } | |
158 | } | |
2fb8c9a2 MG |
159 | } |
160 | ||
161 | /** | |
162 | * Custom action after section has been moved in AJAX mode | |
163 | * | |
164 | * Used in course/rest.php | |
165 | * | |
2fb8c9a2 MG |
166 | * @return array This will be passed in ajax respose |
167 | */ | |
168 | function ajax_section_move() { | |
222cd9c9 MG |
169 | global $PAGE; |
170 | $titles = array(); | |
171 | $course = $this->get_course(); | |
172 | $modinfo = get_fast_modinfo($course); | |
173 | $renderer = $this->get_renderer($PAGE); | |
174 | if ($renderer && ($sections = $modinfo->get_section_info_all())) { | |
175 | foreach ($sections as $number => $section) { | |
176 | $titles[$number] = $renderer->section_title($section, $course); | |
177 | } | |
2fb8c9a2 | 178 | } |
222cd9c9 | 179 | return array('sectiontitles' => $titles, 'action' => 'move'); |
2fb8c9a2 MG |
180 | } |
181 | ||
182 | /** | |
183 | * Returns the list of blocks to be automatically added for the newly created course | |
184 | * | |
2fb8c9a2 MG |
185 | * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT |
186 | * each of values is an array of block names (for left and right side columns) | |
187 | */ | |
188 | public function get_default_blocks() { | |
222cd9c9 MG |
189 | return array( |
190 | BLOCK_POS_LEFT => array(), | |
191 | BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity') | |
192 | ); | |
2fb8c9a2 MG |
193 | } |
194 | ||
195 | /** | |
196 | * Definitions of the additional options that this course format uses for course | |
197 | * | |
222cd9c9 | 198 | * Topics format uses the following options: |
2fb8c9a2 MG |
199 | * - coursedisplay |
200 | * - numsections | |
201 | * - hiddensections | |
202 | * | |
203 | * @param bool $foreditform | |
204 | * @return array of options | |
205 | */ | |
206 | public function course_format_options($foreditform = false) { | |
207 | static $courseformatoptions = false; | |
208 | if ($courseformatoptions === false) { | |
209 | $courseconfig = get_config('moodlecourse'); | |
210 | $courseformatoptions = array( | |
211 | 'numsections' => array( | |
212 | 'default' => $courseconfig->numsections, | |
213 | 'type' => PARAM_INT, | |
214 | ), | |
215 | 'hiddensections' => array( | |
216 | 'default' => $courseconfig->hiddensections, | |
217 | 'type' => PARAM_INT, | |
218 | ), | |
219 | 'coursedisplay' => array( | |
220 | 'default' => $courseconfig->coursedisplay, | |
221 | 'type' => PARAM_INT, | |
222 | ), | |
223 | ); | |
224 | } | |
225 | if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) { | |
226 | $courseconfig = get_config('moodlecourse'); | |
341bfedf AG |
227 | $max = $courseconfig->maxsections; |
228 | if (!isset($max) || !is_numeric($max)) { | |
229 | $max = 52; | |
230 | } | |
2fb8c9a2 | 231 | $sectionmenu = array(); |
341bfedf | 232 | for ($i = 0; $i <= $max; $i++) { |
2fb8c9a2 MG |
233 | $sectionmenu[$i] = "$i"; |
234 | } | |
235 | $courseformatoptionsedit = array( | |
236 | 'numsections' => array( | |
237 | 'label' => new lang_string('numberweeks'), | |
238 | 'element_type' => 'select', | |
239 | 'element_attributes' => array($sectionmenu), | |
240 | ), | |
241 | 'hiddensections' => array( | |
242 | 'label' => new lang_string('hiddensections'), | |
243 | 'help' => 'hiddensections', | |
244 | 'help_component' => 'moodle', | |
245 | 'element_type' => 'select', | |
246 | 'element_attributes' => array( | |
247 | array( | |
248 | 0 => new lang_string('hiddensectionscollapsed'), | |
249 | 1 => new lang_string('hiddensectionsinvisible') | |
250 | ) | |
251 | ), | |
252 | ), | |
253 | 'coursedisplay' => array( | |
254 | 'label' => new lang_string('coursedisplay'), | |
255 | 'element_type' => 'select', | |
256 | 'element_attributes' => array( | |
257 | array( | |
258 | COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'), | |
259 | COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi') | |
260 | ) | |
261 | ), | |
262 | 'help' => 'coursedisplay', | |
263 | 'help_component' => 'moodle', | |
264 | ) | |
265 | ); | |
266 | $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit); | |
267 | } | |
268 | return $courseformatoptions; | |
269 | } | |
270 | ||
86f590b4 GB |
271 | /** |
272 | * Adds format options elements to the course/section edit form. | |
273 | * | |
274 | * This function is called from {@link course_edit_form::definition_after_data()}. | |
275 | * | |
276 | * @param MoodleQuickForm $mform form the elements are added to. | |
277 | * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form. | |
278 | * @return array array of references to the added form elements. | |
279 | */ | |
280 | public function create_edit_form_elements(&$mform, $forsection = false) { | |
281 | $elements = parent::create_edit_form_elements($mform, $forsection); | |
aed9401e EL |
282 | |
283 | // Increase the number of sections combo box values if the user has increased the number of sections | |
284 | // using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been | |
285 | // reduced below the number of sections already set for the course on the site administration course | |
286 | // defaults page. This is so that the number of sections is not reduced leaving unintended orphaned | |
287 | // activities / resources. | |
86f590b4 GB |
288 | if (!$forsection) { |
289 | $maxsections = get_config('moodlecourse', 'maxsections'); | |
290 | $numsections = $mform->getElementValue('numsections'); | |
291 | $numsections = $numsections[0]; | |
292 | if ($numsections > $maxsections) { | |
293 | $element = $mform->getElement('numsections'); | |
294 | for ($i = $maxsections+1; $i <= $numsections; $i++) { | |
295 | $element->addOption("$i", $i); | |
296 | } | |
297 | } | |
298 | } | |
299 | return $elements; | |
300 | } | |
301 | ||
2fb8c9a2 MG |
302 | /** |
303 | * Updates format options for a course | |
304 | * | |
222cd9c9 MG |
305 | * In case if course format was changed to 'topics', we try to copy options |
306 | * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format. | |
307 | * If previous course format did not have 'numsections' option, we populate it with the | |
308 | * current number of sections | |
2fb8c9a2 MG |
309 | * |
310 | * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data | |
311 | * @param stdClass $oldcourse if this function is called from {@link update_course()} | |
312 | * this object contains information about the course before update | |
313 | * @return bool whether there were any changes to the options values | |
314 | */ | |
315 | public function update_course_format_options($data, $oldcourse = null) { | |
2333bfa7 | 316 | global $DB; |
2fb8c9a2 MG |
317 | if ($oldcourse !== null) { |
318 | $data = (array)$data; | |
319 | $oldcourse = (array)$oldcourse; | |
320 | $options = $this->course_format_options(); | |
321 | foreach ($options as $key => $unused) { | |
322 | if (!array_key_exists($key, $data)) { | |
323 | if (array_key_exists($key, $oldcourse)) { | |
324 | $data[$key] = $oldcourse[$key]; | |
325 | } else if ($key === 'numsections') { | |
222cd9c9 MG |
326 | // If previous format does not have the field 'numsections' |
327 | // and $data['numsections'] is not set, | |
328 | // we fill it with the maximum section number from the DB | |
2fb8c9a2 MG |
329 | $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections} |
330 | WHERE course = ?', array($this->courseid)); | |
331 | if ($maxsection) { | |
332 | // If there are no sections, or just default 0-section, 'numsections' will be set to default | |
333 | $data['numsections'] = $maxsection; | |
334 | } | |
335 | } | |
336 | } | |
337 | } | |
338 | } | |
339 | return $this->update_format_options($data); | |
340 | } | |
341 | } |