3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 class block_section_links extends block_base {
28 $this->title = get_string('pluginname', 'block_section_links');
31 function instance_config($instance) {
34 parent::instance_config($instance);
35 $course = $this->page->course;
36 if (isset($course->format)) {
37 if ($course->format == 'topics') {
38 $this->title = get_string('topics', 'block_section_links');
39 } else if ($course->format == 'weeks') {
40 $this->title = get_string('weeks', 'block_section_links');
42 $this->title = get_string('pluginname', 'block_section_links');
47 function applicable_formats() {
48 return (array('course-view-weeks' => true, 'course-view-topics' => true));
51 function get_content() {
52 global $CFG, $USER, $DB;
55 if(isset($this->config)){
56 $config = $this->config;
58 // TODO: Move these config settings to proper ones using component name
59 $config = get_config('blocks/section_links');
62 if ($this->content !== NULL) {
63 return $this->content;
66 $this->content = new stdClass;
67 $this->content->footer = '';
68 $this->content->text = '';
70 if (empty($this->instance)) {
71 return $this->content;
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';
82 else if ($course->format == 'topics') {
83 $highlight = $course->marker;
84 $linktext = get_string('jumptocurrenttopic', 'block_section_links');
85 $sectionname = 'topic';
89 if(!empty($config->numsections1) and ($course->numsections > $config->numsections1)) {
90 $inc = $config->incby1;
92 if ($course->numsections > 22) {
97 if(!empty($config->numsections2) and ($course->numsections > $config->numsections2)) {
98 $inc = $config->incby2;
100 if ($course->numsections > 40) {
105 $sql = "SELECT section, visible
106 FROM {course_sections}
108 section < ".($course->numsections+1)."
111 if ($sections = $DB->get_records_sql($sql, array($course->id))) {
112 $text = '<ol class="inline-list">';
113 for ($i = $inc; $i <= $course->numsections; $i += $inc) {
114 if (!isset($sections[$i])) {
117 $isvisible = $sections[$i]->visible;
118 if (!$isvisible and !has_capability('moodle/course:update', $context)) {
121 $style = ($isvisible) ? '' : ' class="dimmed"';
122 if ($i == $highlight) {
123 $text .= '<li><a href="'.course_get_url($course, $i)."\"$style><strong>$i</strong></a></li>\n";
125 $text .= '<li><a href="'.course_get_url($course, $i)."\"$style>$i</a></li>\n";
129 if ($highlight and isset($sections[$highlight])) {
130 $isvisible = $sections[$highlight]->visible;
131 if ($isvisible or has_capability('moodle/course:update', $context)) {
132 $style = ($isvisible) ? '' : ' class="dimmed"';
133 $text .= "\n<a href=\"".course_get_url($course, $highlight)."\"$style>$linktext</a>";
138 $this->content->text = $text;
139 return $this->content;
142 * Has instance config
145 function instance_allow_config() {
148 function before_delete() {
150 // TODO: Move these config settings to proper ones using component name
151 $DB->delete_records('config_plugins', array('plugin' => 'blocks/section_links'));
154 function has_config() {