8d68825cb42dbf63fd98ec6766bfc5a528dc42eb
[moodle.git] / blocks / section_links / block_section_links.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
19 /**
20  * Section links block
21  *
22  * @package    moodlecore
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
25 class block_section_links extends block_base {
27     function init() {
28         $this->title = get_string('pluginname', 'block_section_links');
29         $this->version = 2007101511;
30     }
32     function instance_config($instance) {
33         global $DB;
35         parent::instance_config($instance);
36         $course = $this->page->course;
37         if (isset($course->format)) {
38             if ($course->format == 'topics') {
39                 $this->title = get_string('topics', 'block_section_links');
40             } else if ($course->format == 'weeks') {
41                 $this->title = get_string('weeks', 'block_section_links');
42             } else {
43                 $this->title = get_string('pluginname', 'block_section_links');
44             }
45         }
46     }
48     function applicable_formats() {
49         return (array('course-view-weeks' => true, 'course-view-topics' => true));
50     }
52     function get_content() {
53         global $CFG, $USER, $DB;
55         $highlight = 0;
56         if(isset($this->config)){
57             $config = $this->config;
58         } else{
59             $config = get_config('blocks/section_links');
60         }
62         if ($this->content !== NULL) {
63             return $this->content;
64         }
66         $this->content = new stdClass;
67         $this->content->footer = '';
68         $this->content->text   = '';
70         if (empty($this->instance)) {
71             return $this->content;
72         }
74         $course = $this->page->course;
75         $context = get_context_instance(CONTEXT_COURSE, $course->id);
77         if ($course->format == 'weeks' or $course->format == 'weekscss') {
78             $highlight = ceil((time()-$course->startdate)/604800);
79             $linktext = get_string('jumptocurrentweek', 'block_section_links');
80             $sectionname = 'week';
81         }
82         else if ($course->format == 'topics') {
83             $highlight = $course->marker;
84             $linktext = get_string('jumptocurrenttopic', 'block_section_links');
85             $sectionname = 'topic';
86         }
87         $inc = 1;
89         if(!empty($config->numsections1) and ($course->numsections > $config->numsections1)) {
90             $inc = $config->incby1;
91         } else {
92             if ($course->numsections > 22) {
93                 $inc = 2;
94             }
95         }
97         if(!empty($config->numsections2) and ($course->numsections > $config->numsections2)) {
98             $inc = $config->incby2;
99         } else {
100             if ($course->numsections > 40) {
101                 $inc = 5;
102             }
103         }
105         if (isloggedin()) {
106             $display = $DB->get_field('course_display', 'display', array('course'=>$this->page->course->id, 'userid'=>$USER->id));
107         }
108         if (!empty($display)) {
109             $link = $CFG->wwwroot.'/course/view.php?id='.$this->page->course->id.'&amp;'.$sectionname.'=';
110         } else {
111             $link = '#section-';
112         }
114         $sql = "SELECT section, visible
115                   FROM {course_sections}
116                  WHERE course = ? AND
117                        section < ".($course->numsections+1)."
118               ORDER BY section";
120         if ($sections = $DB->get_records_sql($sql, array($course->id))) {
121             $text = '<ol class="inline-list">';
122             for ($i = $inc; $i <= $course->numsections; $i += $inc) {
123                 if (!isset($sections[$i])) {
124                     continue;
125                 }
126                 $isvisible = $sections[$i]->visible;
127                 if (!$isvisible and !has_capability('moodle/course:update', $context)) {
128                     continue;
129                 }
130                 $style = ($isvisible) ? '' : ' class="dimmed"';
131                 if ($i == $highlight) {
132                     $text .= "<li><a href=\"$link$i\"$style><strong>$i</strong></a></li>\n";
133                 } else {
134                     $text .= "<li><a href=\"$link$i\"$style>$i</a></li>\n";
135                 }
136             }
137             $text .= '</ol>';
138             if ($highlight and isset($sections[$highlight])) {
139                 $isvisible = $sections[$highlight]->visible;
140                 if ($isvisible or has_capability('moodle/course:update', $context)) {
141                     $style = ($isvisible) ? '' : ' class="dimmed"';
142                     $text .= "\n<a href=\"$link$highlight\"$style>$linktext</a>";
143                 }
144             }
145         }
147         $this->content->text = $text;
148         return $this->content;
149     }
150     /**
151      * Has instance config
152      * @return boolean
153      **/
154     function instance_allow_config() {
155         return true;
156     }
157     function before_delete() {
158         global $DB;
159         $DB->delete_records('config_plugins', 'plugin', 'blocks/section_links');
160     }