[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/course/format/topics/ -> lib.php (source)

   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 Topic
  19   *
  20   * @since     Moodle 2.0
  21   * @package   format_topics
  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 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       *
  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       * Use section name is specified by user. Otherwise use default ("Topic #")
  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) {
  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;
  64          }
  65      }
  66  
  67      /**
  68       * The URL to use for the specified course (with section)
  69       *
  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()) {
  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       *
 116       * The returned object's property (boolean)capable indicates that
 117       * the course format supports Moodle course ajax features.
 118       *
 119       * @return stdClass
 120       */
 121      public function supports_ajax() {
 122          $ajaxsupport = new stdClass();
 123          $ajaxsupport->capable = true;
 124          return $ajaxsupport;
 125      }
 126  
 127      /**
 128       * Loads all of the course sections into the navigation
 129       *
 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;
 135          // if section is specified in course/view.php, make sure it is expanded in navigation
 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
 145          parent::extend_course_navigation($navigation, $node);
 146      }
 147  
 148      /**
 149       * Custom action after section has been moved in AJAX mode
 150       *
 151       * Used in course/rest.php
 152       *
 153       * @return array This will be passed in ajax respose
 154       */
 155      function ajax_section_move() {
 156          global $PAGE;
 157          $titles = array();
 158          $course = $this->get_course();
 159          $modinfo = get_fast_modinfo($course);
 160          $renderer = $this->get_renderer($PAGE);
 161          if ($renderer && ($sections = $modinfo->get_section_info_all())) {
 162              foreach ($sections as $number => $section) {
 163                  $titles[$number] = $renderer->section_title($section, $course);
 164              }
 165          }
 166          return array('sectiontitles' => $titles, 'action' => 'move');
 167      }
 168  
 169      /**
 170       * Returns the list of blocks to be automatically added for the newly created course
 171       *
 172       * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
 173       *     each of values is an array of block names (for left and right side columns)
 174       */
 175      public function get_default_blocks() {
 176          return array(
 177              BLOCK_POS_LEFT => array(),
 178              BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
 179          );
 180      }
 181  
 182      /**
 183       * Definitions of the additional options that this course format uses for course
 184       *
 185       * Topics format uses the following options:
 186       * - coursedisplay
 187       * - numsections
 188       * - hiddensections
 189       *
 190       * @param bool $foreditform
 191       * @return array of options
 192       */
 193      public function course_format_options($foreditform = false) {
 194          static $courseformatoptions = false;
 195          if ($courseformatoptions === false) {
 196              $courseconfig = get_config('moodlecourse');
 197              $courseformatoptions = array(
 198                  'numsections' => array(
 199                      'default' => $courseconfig->numsections,
 200                      'type' => PARAM_INT,
 201                  ),
 202                  'hiddensections' => array(
 203                      'default' => $courseconfig->hiddensections,
 204                      'type' => PARAM_INT,
 205                  ),
 206                  'coursedisplay' => array(
 207                      'default' => $courseconfig->coursedisplay,
 208                      'type' => PARAM_INT,
 209                  ),
 210              );
 211          }
 212          if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
 213              $courseconfig = get_config('moodlecourse');
 214              $max = $courseconfig->maxsections;
 215              if (!isset($max) || !is_numeric($max)) {
 216                  $max = 52;
 217              }
 218              $sectionmenu = array();
 219              for ($i = 0; $i <= $max; $i++) {
 220                  $sectionmenu[$i] = "$i";
 221              }
 222              $courseformatoptionsedit = array(
 223                  'numsections' => array(
 224                      'label' => new lang_string('numberweeks'),
 225                      'element_type' => 'select',
 226                      'element_attributes' => array($sectionmenu),
 227                  ),
 228                  'hiddensections' => array(
 229                      'label' => new lang_string('hiddensections'),
 230                      'help' => 'hiddensections',
 231                      'help_component' => 'moodle',
 232                      'element_type' => 'select',
 233                      'element_attributes' => array(
 234                          array(
 235                              0 => new lang_string('hiddensectionscollapsed'),
 236                              1 => new lang_string('hiddensectionsinvisible')
 237                          )
 238                      ),
 239                  ),
 240                  'coursedisplay' => array(
 241                      'label' => new lang_string('coursedisplay'),
 242                      'element_type' => 'select',
 243                      'element_attributes' => array(
 244                          array(
 245                              COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
 246                              COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
 247                          )
 248                      ),
 249                      'help' => 'coursedisplay',
 250                      'help_component' => 'moodle',
 251                  )
 252              );
 253              $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
 254          }
 255          return $courseformatoptions;
 256      }
 257  
 258      /**
 259       * Adds format options elements to the course/section edit form.
 260       *
 261       * This function is called from {@link course_edit_form::definition_after_data()}.
 262       *
 263       * @param MoodleQuickForm $mform form the elements are added to.
 264       * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form.
 265       * @return array array of references to the added form elements.
 266       */
 267      public function create_edit_form_elements(&$mform, $forsection = false) {
 268          $elements = parent::create_edit_form_elements($mform, $forsection);
 269  
 270          // Increase the number of sections combo box values if the user has increased the number of sections
 271          // using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been
 272          // reduced below the number of sections already set for the course on the site administration course
 273          // defaults page.  This is so that the number of sections is not reduced leaving unintended orphaned
 274          // activities / resources.
 275          if (!$forsection) {
 276              $maxsections = get_config('moodlecourse', 'maxsections');
 277              $numsections = $mform->getElementValue('numsections');
 278              $numsections = $numsections[0];
 279              if ($numsections > $maxsections) {
 280                  $element = $mform->getElement('numsections');
 281                  for ($i = $maxsections+1; $i <= $numsections; $i++) {
 282                      $element->addOption("$i", $i);
 283                  }
 284              }
 285          }
 286          return $elements;
 287      }
 288  
 289      /**
 290       * Updates format options for a course
 291       *
 292       * In case if course format was changed to 'topics', we try to copy options
 293       * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
 294       * If previous course format did not have 'numsections' option, we populate it with the
 295       * current number of sections
 296       *
 297       * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
 298       * @param stdClass $oldcourse if this function is called from {@link update_course()}
 299       *     this object contains information about the course before update
 300       * @return bool whether there were any changes to the options values
 301       */
 302      public function update_course_format_options($data, $oldcourse = null) {
 303          global $DB;
 304          if ($oldcourse !== null) {
 305              $data = (array)$data;
 306              $oldcourse = (array)$oldcourse;
 307              $options = $this->course_format_options();
 308              foreach ($options as $key => $unused) {
 309                  if (!array_key_exists($key, $data)) {
 310                      if (array_key_exists($key, $oldcourse)) {
 311                          $data[$key] = $oldcourse[$key];
 312                      } else if ($key === 'numsections') {
 313                          // If previous format does not have the field 'numsections'
 314                          // and $data['numsections'] is not set,
 315                          // we fill it with the maximum section number from the DB
 316                          $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
 317                              WHERE course = ?', array($this->courseid));
 318                          if ($maxsection) {
 319                              // If there are no sections, or just default 0-section, 'numsections' will be set to default
 320                              $data['numsections'] = $maxsection;
 321                          }
 322                      }
 323                  }
 324              }
 325          }
 326          return $this->update_format_options($data);
 327      }
 328  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1