2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the main class for the section links block.
20 * @package block_section_links
21 * @copyright Jason Hardin
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Section links block class.
28 * @package block_section_links
29 * @copyright Jason Hardin
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class block_section_links
extends block_base
{
35 * Initialises the block instance.
37 public function init() {
38 $this->title
= get_string('pluginname', 'block_section_links');
42 * Returns an array of formats for which this block can be used.
46 public function applicable_formats() {
48 'course-view-weeks' => true,
49 'course-view-topics' => true
54 * Generates the content of the block and returns it.
56 * If the content has already been generated then the previously generated content is returned.
60 public function get_content() {
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
;
67 $config = get_config('block_section_links');
70 if ($this->content
!== null) {
71 return $this->content
;
74 $this->content
= new stdClass
;
75 $this->content
->footer
= '';
76 $this->content
->text
= '';
78 if (empty($this->instance
)) {
79 return $this->content
;
82 $course = $this->page
->course
;
83 $courseformat = course_get_format($course);
84 $courseformatoptions = $courseformat->get_format_options();
85 $context = context_course
::instance($course->id
);
87 // Course format options 'numsections' is required to display the block.
88 if (empty($courseformatoptions['numsections'])) {
89 return $this->content
;
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
;
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) {
109 if (!empty($config->numsections2
) and ($courseformatoptions['numsections'] > $config->numsections2
)) {
110 $inc = $config->incby2
;
112 if ($courseformatoptions['numsections'] > 40) {
117 // Prepare an array of sections to create links for.
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])) {
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)
136 if (!empty($sections)) {
137 $sectiontojumpto = false;
138 if ($highlight && isset($sections[$highlight]) && ($sections[$highlight]->visible ||
$canviewhidden)) {
139 $sectiontojumpto = $highlight;
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);
146 return $this->content
;
149 * Returns true if this block has instance config.
153 public function instance_allow_config() {
158 * Returns true if this block has global config.
162 public function has_config() {