[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blocks/section_links/ -> block_section_links.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 the main class for the section links block.
  19   *
  20   * @package    block_section_links
  21   * @copyright  Jason Hardin
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Section links block class.
  27   *
  28   * @package    block_section_links
  29   * @copyright  Jason Hardin
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class block_section_links extends block_base {
  33  
  34      /**
  35       * Initialises the block instance.
  36       */
  37      public function init() {
  38          $this->title = get_string('pluginname', 'block_section_links');
  39      }
  40  
  41      /**
  42       * Returns an array of formats for which this block can be used.
  43       *
  44       * @return array
  45       */
  46      public function applicable_formats() {
  47          return array(
  48              'course-view-weeks' => true,
  49              'course-view-topics' => true
  50          );
  51      }
  52  
  53      /**
  54       * Generates the content of the block and returns it.
  55       *
  56       * If the content has already been generated then the previously generated content is returned.
  57       *
  58       * @return stdClass
  59       */
  60      public function get_content() {
  61  
  62          // The config should be loaded by now.
  63          // If its empty then we will use the global config for the section links block.
  64          if (isset($this->config)){
  65              $config = $this->config;
  66          } else{
  67              $config = get_config('block_section_links');
  68          }
  69  
  70          if ($this->content !== null) {
  71              return $this->content;
  72          }
  73  
  74          $this->content = new stdClass;
  75          $this->content->footer = '';
  76          $this->content->text   = '';
  77  
  78          if (empty($this->instance)) {
  79              return $this->content;
  80          }
  81  
  82          $course = $this->page->course;
  83          $courseformat = course_get_format($course);
  84          $courseformatoptions = $courseformat->get_format_options();
  85          $context = context_course::instance($course->id);
  86  
  87          // Course format options 'numsections' is required to display the block.
  88          if (empty($courseformatoptions['numsections'])) {
  89              return $this->content;
  90          }
  91  
  92          // Prepare the highlight value.
  93          if ($course->format == 'weeks') {
  94              $highlight = ceil((time() - $course->startdate) / 604800);
  95          } else if ($course->format == 'topics') {
  96              $highlight = $course->marker;
  97          } else {
  98              $highlight = 0;
  99          }
 100  
 101          // Prepare the increment value.
 102          if (!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) {
 103              $inc = $config->incby1;
 104          } else if ($courseformatoptions['numsections'] > 22) {
 105              $inc = 2;
 106          } else {
 107              $inc = 1;
 108          }
 109          if (!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) {
 110              $inc = $config->incby2;
 111          } else {
 112              if ($courseformatoptions['numsections'] > 40) {
 113                  $inc = 5;
 114              }
 115          }
 116  
 117          // Prepare an array of sections to create links for.
 118          $sections = array();
 119          $canviewhidden = has_capability('moodle/course:update', $context);
 120          $coursesections = $courseformat->get_sections();
 121          $coursesectionscount = count($coursesections);
 122          for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
 123              if ($i > $courseformatoptions['numsections'] || !isset($coursesections[$i])) {
 124                  continue;
 125              }
 126              $section = $coursesections[$i];
 127              if ($section->section && ($section->visible || $canviewhidden)) {
 128                  $sections[$i] = (object)array(
 129                      'section' => $section->section,
 130                      'visible' => $section->visible,
 131                      'highlight' => ($section->section == $highlight)
 132                  );
 133              }
 134          }
 135  
 136          if (!empty($sections)) {
 137              $sectiontojumpto = false;
 138              if ($highlight && isset($sections[$highlight]) && ($sections[$highlight]->visible || $canviewhidden)) {
 139                  $sectiontojumpto = $highlight;
 140              }
 141              // Render the sections.
 142              $renderer = $this->page->get_renderer('block_section_links');
 143              $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto);
 144          }
 145  
 146          return $this->content;
 147      }
 148      /**
 149       * Returns true if this block has instance config.
 150       *
 151       * @return bool
 152       **/
 153      public function instance_allow_config() {
 154          return true;
 155      }
 156  
 157      /**
 158       * Returns true if this block has global config.
 159       *
 160       * @return bool
 161       */
 162      public function has_config() {
 163          return true;
 164      }
 165  }
 166  
 167  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1