[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 /** 18 * This file contains main class for the course format Weeks 19 * 20 * @since Moodle 2.0 21 * @package format_weeks 22 * @copyright 2009 Sam Hemelryk 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 require_once($CFG->dirroot. '/course/format/lib.php'); 28 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 */ 36 class format_weeks extends format_base { 37 38 /** 39 * Returns true if this course format uses sections 40 * 41 * @return bool 42 */ 43 public function uses_sections() { 44 return true; 45 } 46 47 /** 48 * Returns the display name of the given section that the course prefers. 49 * 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) { 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 { 62 $dates = $this->get_section_dates($section); 63 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 } 72 } 73 74 /** 75 * The URL to use for the specified course (with section) 76 * 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()) { 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 * 123 * The returned object's property (boolean)capable indicates that 124 * the course format supports Moodle course ajax features. 125 * 126 * @return stdClass 127 */ 128 public function supports_ajax() { 129 $ajaxsupport = new stdClass(); 130 $ajaxsupport->capable = true; 131 return $ajaxsupport; 132 } 133 134 /** 135 * Loads all of the course sections into the navigation 136 * 137 * @param global_navigation $navigation 138 * @param navigation_node $node The course node within the navigation 139 */ 140 public function extend_course_navigation($navigation, navigation_node $node) { 141 global $PAGE; 142 // if section is specified in course/view.php, make sure it is expanded in navigation 143 if ($navigation->includesectionnum === false) { 144 $selectedsection = optional_param('section', null, PARAM_INT); 145 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') && 146 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { 147 $navigation->includesectionnum = $selectedsection; 148 } 149 } 150 parent::extend_course_navigation($navigation, $node); 151 } 152 153 /** 154 * Custom action after section has been moved in AJAX mode 155 * 156 * Used in course/rest.php 157 * 158 * @return array This will be passed in ajax respose 159 */ 160 function ajax_section_move() { 161 global $PAGE; 162 $titles = array(); 163 $current = -1; 164 $course = $this->get_course(); 165 $modinfo = get_fast_modinfo($course); 166 $renderer = $this->get_renderer($PAGE); 167 if ($renderer && ($sections = $modinfo->get_section_info_all())) { 168 foreach ($sections as $number => $section) { 169 $titles[$number] = $renderer->section_title($section, $course); 170 if ($this->is_section_current($section)) { 171 $current = $number; 172 } 173 } 174 } 175 return array('sectiontitles' => $titles, 'current' => $current, 'action' => 'move'); 176 } 177 178 /** 179 * Returns the list of blocks to be automatically added for the newly created course 180 * 181 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT 182 * each of values is an array of block names (for left and right side columns) 183 */ 184 public function get_default_blocks() { 185 return array( 186 BLOCK_POS_LEFT => array(), 187 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity') 188 ); 189 } 190 191 /** 192 * Definitions of the additional options that this course format uses for course 193 * 194 * Weeks format uses the following options: 195 * - coursedisplay 196 * - numsections 197 * - hiddensections 198 * 199 * @param bool $foreditform 200 * @return array of options 201 */ 202 public function course_format_options($foreditform = false) { 203 static $courseformatoptions = false; 204 if ($courseformatoptions === false) { 205 $courseconfig = get_config('moodlecourse'); 206 $courseformatoptions = array( 207 'numsections' => array( 208 'default' => $courseconfig->numsections, 209 'type' => PARAM_INT, 210 ), 211 'hiddensections' => array( 212 'default' => $courseconfig->hiddensections, 213 'type' => PARAM_INT, 214 ), 215 'coursedisplay' => array( 216 'default' => $courseconfig->coursedisplay, 217 'type' => PARAM_INT, 218 ), 219 ); 220 } 221 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) { 222 $courseconfig = get_config('moodlecourse'); 223 $sectionmenu = array(); 224 $max = $courseconfig->maxsections; 225 if (!isset($max) || !is_numeric($max)) { 226 $max = 52; 227 } 228 for ($i = 0; $i <= $max; $i++) { 229 $sectionmenu[$i] = "$i"; 230 } 231 $courseformatoptionsedit = array( 232 'numsections' => array( 233 'label' => new lang_string('numberweeks'), 234 'element_type' => 'select', 235 'element_attributes' => array($sectionmenu), 236 ), 237 'hiddensections' => array( 238 'label' => new lang_string('hiddensections'), 239 'help' => 'hiddensections', 240 'help_component' => 'moodle', 241 'element_type' => 'select', 242 'element_attributes' => array( 243 array( 244 0 => new lang_string('hiddensectionscollapsed'), 245 1 => new lang_string('hiddensectionsinvisible') 246 ) 247 ), 248 ), 249 'coursedisplay' => array( 250 'label' => new lang_string('coursedisplay'), 251 'element_type' => 'select', 252 'element_attributes' => array( 253 array( 254 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'), 255 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi') 256 ) 257 ), 258 'help' => 'coursedisplay', 259 'help_component' => 'moodle', 260 ) 261 ); 262 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit); 263 } 264 return $courseformatoptions; 265 } 266 267 /** 268 * Adds format options elements to the course/section edit form. 269 * 270 * This function is called from {@link course_edit_form::definition_after_data()}. 271 * 272 * @param MoodleQuickForm $mform form the elements are added to. 273 * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form. 274 * @return array array of references to the added form elements. 275 */ 276 public function create_edit_form_elements(&$mform, $forsection = false) { 277 $elements = parent::create_edit_form_elements($mform, $forsection); 278 279 // Increase the number of sections combo box values if the user has increased the number of sections 280 // using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been 281 // reduced below the number of sections already set for the course on the site administration course 282 // defaults page. This is so that the number of sections is not reduced leaving unintended orphaned 283 // activities / resources. 284 if (!$forsection) { 285 $maxsections = get_config('moodlecourse', 'maxsections'); 286 $numsections = $mform->getElementValue('numsections'); 287 $numsections = $numsections[0]; 288 if ($numsections > $maxsections) { 289 $element = $mform->getElement('numsections'); 290 for ($i = $maxsections+1; $i <= $numsections; $i++) { 291 $element->addOption("$i", $i); 292 } 293 } 294 } 295 return $elements; 296 } 297 298 /** 299 * Updates format options for a course 300 * 301 * In case if course format was changed to 'weeks', we try to copy options 302 * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format. 303 * If previous course format did not have 'numsections' option, we populate it with the 304 * current number of sections 305 * 306 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data 307 * @param stdClass $oldcourse if this function is called from {@link update_course()} 308 * this object contains information about the course before update 309 * @return bool whether there were any changes to the options values 310 */ 311 public function update_course_format_options($data, $oldcourse = null) { 312 global $DB; 313 if ($oldcourse !== null) { 314 $data = (array)$data; 315 $oldcourse = (array)$oldcourse; 316 $options = $this->course_format_options(); 317 foreach ($options as $key => $unused) { 318 if (!array_key_exists($key, $data)) { 319 if (array_key_exists($key, $oldcourse)) { 320 $data[$key] = $oldcourse[$key]; 321 } else if ($key === 'numsections') { 322 // If previous format does not have the field 'numsections' 323 // and $data['numsections'] is not set, 324 // we fill it with the maximum section number from the DB 325 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections} 326 WHERE course = ?', array($this->courseid)); 327 if ($maxsection) { 328 // If there are no sections, or just default 0-section, 'numsections' will be set to default 329 $data['numsections'] = $maxsection; 330 } 331 } 332 } 333 } 334 } 335 return $this->update_format_options($data); 336 } 337 338 /** 339 * Return the start and end date of the passed section 340 * 341 * @param int|stdClass|section_info $section section to get the dates for 342 * @return stdClass property start for startdate, property end for enddate 343 */ 344 public function get_section_dates($section) { 345 $course = $this->get_course(); 346 if (is_object($section)) { 347 $sectionnum = $section->section; 348 } else { 349 $sectionnum = $section; 350 } 351 $oneweekseconds = 604800; 352 // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight 353 // savings and the date changes. 354 $startdate = $course->startdate + 7200; 355 356 $dates = new stdClass(); 357 $dates->start = $startdate + ($oneweekseconds * ($sectionnum - 1)); 358 $dates->end = $dates->start + $oneweekseconds; 359 360 return $dates; 361 } 362 363 /** 364 * Returns true if the specified week is current 365 * 366 * @param int|stdClass|section_info $section 367 * @return bool 368 */ 369 public function is_section_current($section) { 370 if (is_object($section)) { 371 $sectionnum = $section->section; 372 } else { 373 $sectionnum = $section; 374 } 375 if ($sectionnum < 1) { 376 return false; 377 } 378 $timenow = time(); 379 $dates = $this->get_section_dates($section); 380 return (($timenow >= $dates->start) && ($timenow < $dates->end)); 381 } 382 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |