Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 [
48
            'course-view-weeks' => true,
49
            'course-view-topics' => true,
50
            'course-view-section-weeks' => true,
51
            'course-view-section-topics' => true,
52
        ];
53
    }
54
 
55
    /**
56
     * Generates the content of the block and returns it.
57
     *
58
     * If the content has already been generated then the previously generated content is returned.
59
     *
60
     * @return stdClass
61
     */
62
    public function get_content() {
63
 
64
        // The config should be loaded by now.
65
        // If its empty then we will use the global config for the section links block.
66
        if (isset($this->config)){
67
            $config = $this->config;
68
        } else{
69
            $config = get_config('block_section_links');
70
        }
71
 
72
        if ($this->content !== null) {
73
            return $this->content;
74
        }
75
 
76
        $this->content = new stdClass;
77
        $this->content->footer = '';
78
        $this->content->text   = '';
79
 
80
        if (empty($this->instance)) {
81
            return $this->content;
82
        }
83
 
84
        $course = $this->page->course;
85
        $courseformat = course_get_format($course);
86
        $numsections = $courseformat->get_last_section_number();
87
        $context = context_course::instance($course->id);
88
 
89
        // Course format options 'numsections' is required to display the block.
90
        if (empty($numsections)) {
91
            return $this->content;
92
        }
93
 
94
        // Prepare the increment value.
95
        if (!empty($config->numsections1) and ($numsections > $config->numsections1)) {
96
            $inc = $config->incby1;
97
        } else if ($numsections > 22) {
98
            $inc = 2;
99
        } else {
100
            $inc = 1;
101
        }
102
        if (!empty($config->numsections2) and ($numsections > $config->numsections2)) {
103
            $inc = $config->incby2;
104
        } else {
105
            if ($numsections > 40) {
106
                $inc = 5;
107
            }
108
        }
109
 
110
        // Whether or not section name should be displayed.
111
        $showsectionname = !empty($config->showsectionname) ? true : false;
112
 
113
        // Prepare an array of sections to create links for.
1441 ariadna 114
        $sections = [];
1 efrain 115
        $canviewhidden = has_capability('moodle/course:update', $context);
116
        $coursesections = $courseformat->get_sections();
117
        $coursesectionscount = count($coursesections);
118
        $sectiontojumpto = false;
119
        for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
120
            if ($i > $numsections || !isset($coursesections[$i])) {
121
                continue;
122
            }
123
            $section = $coursesections[$i];
1441 ariadna 124
            // Delegated sections (like subsections) are not listed in the block.
125
            if ($section->get_component_instance() !== null) {
126
                continue;
127
            }
1 efrain 128
            if ($section->section && ($section->visible || $canviewhidden)) {
1441 ariadna 129
                $sections[$i] = (object) [
1 efrain 130
                    'section' => $section->section,
131
                    'visible' => $section->visible,
132
                    'highlight' => false
1441 ariadna 133
                ];
1 efrain 134
                if ($courseformat->is_section_current($section)) {
135
                    $sections[$i]->highlight = true;
136
                    $sectiontojumpto = $section->section;
137
                }
138
                if ($showsectionname) {
139
                    $sections[$i]->name = $courseformat->get_section_name($i);
140
                }
141
            }
142
        }
143
 
144
        if (!empty($sections)) {
145
            // Render the sections.
146
            $renderer = $this->page->get_renderer('block_section_links');
147
            $this->content->text = $renderer->render_section_links($this->page->course, $sections,
148
                $sectiontojumpto, $showsectionname);
149
        }
150
 
151
        return $this->content;
152
    }
153
    /**
154
     * Returns true if this block has instance config.
155
     *
156
     * @return bool
157
     **/
158
    public function instance_allow_config() {
159
        return true;
160
    }
161
 
162
    /**
163
     * Returns true if this block has global config.
164
     *
165
     * @return bool
166
     */
167
    public function has_config() {
168
        return true;
169
    }
170
 
171
    /**
172
     * Return the plugin config settings for external functions.
173
     *
174
     * @return stdClass the configs for both the block instance and plugin
175
     * @since Moodle 3.8
176
     */
177
    public function get_config_for_external() {
178
        // Return all settings for all users since it is safe (no private keys, etc..).
179
        $instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
180
        $pluginconfigs = get_config('block_section_links');
181
 
182
        return (object) [
183
            'instance' => $instanceconfigs,
184
            'plugin' => $pluginconfigs,
185
        ];
186
    }
187
}