PageRenderTime 44ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/course/format/topics/lib.php

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