MDL-30392 DML - Fix old-style delete_records() call
[moodle.git] / blocks / section_links / block_section_links.php
CommitLineData
4ca6cfbf 1<?php
d208d07b 2
285fd4ea
DC
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/>.
17
18
19/**
20 * Section links block
21 *
22 * @package moodlecore
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
e89d741a 25class block_section_links extends block_base {
d208d07b 26
9b4b78fd 27 function init() {
61e2e775 28 $this->title = get_string('pluginname', 'block_section_links');
9b4b78fd 29 }
30
31 function instance_config($instance) {
219f652b 32 global $DB;
33
9b4b78fd 34 parent::instance_config($instance);
3179b000 35 $course = $this->page->course;
1b04b36b 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');
41 } else {
150135ab 42 $this->title = get_string('pluginname', 'block_section_links');
1b04b36b 43 }
d208d07b 44 }
d208d07b 45 }
89adb174 46
d208d07b 47 function applicable_formats() {
66b10689 48 return (array('course-view-weeks' => true, 'course-view-topics' => true));
d208d07b 49 }
50
51 function get_content() {
cb640229 52 global $CFG, $USER, $DB;
d208d07b 53
54 $highlight = 0;
6c33f532
DC
55 if(isset($this->config)){
56 $config = $this->config;
57 } else{
08fa81d6 58 // TODO: Move these config settings to proper ones using component name
6c33f532
DC
59 $config = get_config('blocks/section_links');
60 }
d208d07b 61
675dbdd9 62 if ($this->content !== NULL) {
63 return $this->content;
64 }
65
764d1d20 66 $this->content = new stdClass;
9b4b78fd 67 $this->content->footer = '';
68 $this->content->text = '';
69
70 if (empty($this->instance)) {
d208d07b 71 return $this->content;
72 }
73
3179b000 74 $course = $this->page->course;
67e4cf27 75 $context = get_context_instance(CONTEXT_COURSE, $course->id);
9b4b78fd 76
2f8d5316 77 if ($course->format == 'weeks' or $course->format == 'weekscss') {
9b4b78fd 78 $highlight = ceil((time()-$course->startdate)/604800);
d208d07b 79 $linktext = get_string('jumptocurrentweek', 'block_section_links');
f1020d0d 80 $sectionname = 'week';
d208d07b 81 }
9b4b78fd 82 else if ($course->format == 'topics') {
83 $highlight = $course->marker;
d208d07b 84 $linktext = get_string('jumptocurrenttopic', 'block_section_links');
f1020d0d 85 $sectionname = 'topic';
d208d07b 86 }
87 $inc = 1;
6c33f532
DC
88
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 }
d208d07b 95 }
6c33f532
DC
96
97 if(!empty($config->numsections2) and ($course->numsections > $config->numsections2)) {
65a86aa7 98 $inc = $config->incby2;
6c33f532
DC
99 } else {
100 if ($course->numsections > 40) {
101 $inc = 5;
102 }
d208d07b 103 }
9b4b78fd 104
4f0c2d00 105 if (isloggedin()) {
3179b000 106 $display = $DB->get_field('course_display', 'display', array('course'=>$this->page->course->id, 'userid'=>$USER->id));
63b1bd7c 107 }
108 if (!empty($display)) {
3179b000 109 $link = $CFG->wwwroot.'/course/view.php?id='.$this->page->course->id.'&amp;'.$sectionname.'=';
d208d07b 110 } else {
a7ef57d4 111 $link = '#section-';
d208d07b 112 }
dd97c328 113
114 $sql = "SELECT section, visible
f28f2d90 115 FROM {course_sections}
116 WHERE course = ? AND
dd97c328 117 section < ".($course->numsections+1)."
118 ORDER BY section";
119
f28f2d90 120 if ($sections = $DB->get_records_sql($sql, array($course->id))) {
dd97c328 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 }
e44497f3 130 $style = ($isvisible) ? '' : ' class="dimmed"';
dd97c328 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 }
e44497f3 144 }
d208d07b 145 }
89adb174 146
d208d07b 147 $this->content->text = $text;
148 return $this->content;
149 }
6c33f532
DC
150 /**
151 * Has instance config
152 * @return boolean
153 **/
154 function instance_allow_config() {
155 return true;
156 }
157 function before_delete() {
78bcd545 158 global $DB;
08fa81d6
EL
159 // TODO: Move these config settings to proper ones using component name
160 $DB->delete_records('config_plugins', array('plugin' => 'blocks/section_links'));
6c33f532 161 }
a63bb431
DC
162
163 function has_config() {
164 return true;
165 }
d208d07b 166}
764d1d20 167
4ca6cfbf 168