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');
29 $this->version = 2007101511;
32 function instance_config($instance) {
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');
43 $this->title = get_string('pluginname', 'block_section_links');
48 function applicable_formats() {
49 return (array('course-view-weeks' => true, 'course-view-topics' => true));
52 function get_content() {
53 global $CFG, $USER, $DB;
56 if(isset($this->config)){
57 $config = $this->config;
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) {
106 $display = $DB->get_field('course_display', 'display', array('course'=>$this->page->course->id, 'userid'=>$USER->id));
108 if (!empty($display)) {
109 $link = $CFG->wwwroot.'/course/view.php?id='.$this->page->course->id.'&'.$sectionname.'=';
114 $sql = "SELECT section, visible
115 FROM {course_sections}
117 section < ".($course->numsections+1)."
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])) {
126 $isvisible = $sections[$i]->visible;
127 if (!$isvisible and !has_capability('moodle/course:update', $context)) {
130 $style = ($isvisible) ? '' : ' class="dimmed"';
131 if ($i == $highlight) {
132 $text .= "<li><a href=\"$link$i\"$style><strong>$i</strong></a></li>\n";
134 $text .= "<li><a href=\"$link$i\"$style>$i</a></li>\n";
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>";
147 $this->content->text = $text;
148 return $this->content;
151 * Has instance config
154 function instance_allow_config() {
157 function before_delete() {
159 $DB->delete_records('config_plugins', 'plugin', 'blocks/section_links');