MDL-52338 blocks: message link should not appear when set false
[moodle.git] / blocks / course_overview / renderer.php
CommitLineData
83ea0cc1 1<?php
83ea0cc1
AO
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
37b5e8fe
RT
17/**
18 * course_overview block rendrer
19 *
20 * @package block_course_overview
21 * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
83ea0cc1
AO
24defined('MOODLE_INTERNAL') || die;
25
37b5e8fe
RT
26/**
27 * Course_overview block rendrer
28 *
29 * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
83ea0cc1
AO
32class block_course_overview_renderer extends plugin_renderer_base {
33
37b5e8fe
RT
34 /**
35 * Construct contents of course_overview block
36 *
37 * @param array $courses list of courses in sorted order
38 * @param array $overviews list of course overviews
39 * @return string html to be displayed in course_overview block
40 */
41 public function course_overview($courses, $overviews) {
83ea0cc1
AO
42 $html = '';
43 $config = get_config('block_course_overview');
d512e5f8 44 if ($config->showcategories != BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE) {
076109df
JB
45 global $CFG;
46 require_once($CFG->libdir.'/coursecatlib.php');
47 }
9e1d4eda 48 $ismovingcourse = false;
37b5e8fe
RT
49 $courseordernumber = 0;
50 $maxcourses = count($courses);
9e1d4eda
RT
51 $userediting = false;
52 // Intialise string/icon etc if user is editing and courses > 1
53 if ($this->page->user_is_editing() && (count($courses) > 1)) {
54 $userediting = true;
af64bc61 55 $this->page->requires->js_init_call('M.block_course_overview.add_handles');
9e1d4eda
RT
56
57 // Check if course is moving
58 $ismovingcourse = optional_param('movecourse', FALSE, PARAM_BOOL);
59 $movingcourseid = optional_param('courseid', 0, PARAM_INT);
60 }
61
62 // Render first movehere icon.
63 if ($ismovingcourse) {
64 // Remove movecourse param from url.
65 $this->page->ensure_param_not_in_url('movecourse');
66
67 // Show moving course notice, so user knows what is being moved.
68 $html .= $this->output->box_start('notice');
69 $a = new stdClass();
70 $a->fullname = $courses[$movingcourseid]->fullname;
71 $a->cancellink = html_writer::link($this->page->url, get_string('cancel'));
72 $html .= get_string('movingcourse', 'block_course_overview', $a);
73 $html .= $this->output->box_end();
74
75 $moveurl = new moodle_url('/blocks/course_overview/move.php',
76 array('sesskey' => sesskey(), 'moveto' => 0, 'courseid' => $movingcourseid));
77 // Create move icon, so it can be used.
78 $movetofirsticon = html_writer::empty_tag('img',
79 array('src' => $this->output->pix_url('movehere'),
80 'alt' => get_string('movetofirst', 'block_course_overview', $courses[$movingcourseid]->fullname),
81 'title' => get_string('movehere')));
82 $moveurl = html_writer::link($moveurl, $movetofirsticon);
83 $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere'));
83ea0cc1 84 }
37b5e8fe
RT
85
86 foreach ($courses as $key => $course) {
9e1d4eda
RT
87 // If moving course, then don't show course which needs to be moved.
88 if ($ismovingcourse && ($course->id == $movingcourseid)) {
89 continue;
90 }
37b5e8fe 91 $html .= $this->output->box_start('coursebox', "course-{$course->id}");
83ea0cc1 92 $html .= html_writer::start_tag('div', array('class' => 'course_title'));
9e1d4eda
RT
93 // If user is editing, then add move icons.
94 if ($userediting && !$ismovingcourse) {
95 $moveicon = html_writer::empty_tag('img',
96 array('src' => $this->pix_url('t/move')->out(false),
3d231eda 97 'alt' => get_string('movecourse', 'block_course_overview', $course->fullname),
9e1d4eda
RT
98 'title' => get_string('move')));
99 $moveurl = new moodle_url($this->page->url, array('sesskey' => sesskey(), 'movecourse' => 1, 'courseid' => $course->id));
100 $moveurl = html_writer::link($moveurl, $moveicon);
101 $html .= html_writer::tag('div', $moveurl, array('class' => 'move'));
102
37b5e8fe
RT
103 }
104
1d8a1a87
SH
105 // No need to pass title through s() here as it will be done automatically by html_writer.
106 $attributes = array('title' => $course->fullname);
37b5e8fe 107 if ($course->id > 0) {
dc6b7864
DW
108 if (empty($course->visible)) {
109 $attributes['class'] = 'dimmed';
110 }
7d0eaf74 111 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
9d82f6a8 112 $coursefullname = format_string(get_course_display_name_for_list($course), true, $course->id);
3d231eda 113 $link = html_writer::link($courseurl, $coursefullname, $attributes);
37b5e8fe
RT
114 $html .= $this->output->heading($link, 2, 'title');
115 } else {
116 $html .= $this->output->heading(html_writer::link(
117 new moodle_url('/auth/mnet/jump.php', array('hostid' => $course->hostid, 'wantsurl' => '/course/view.php?id='.$course->remoteid)),
266bc294 118 format_string($course->shortname, true), $attributes) . ' (' . format_string($course->hostname) . ')', 2, 'title');
83ea0cc1 119 }
37b5e8fe 120 $html .= $this->output->box('', 'flush');
83ea0cc1
AO
121 $html .= html_writer::end_tag('div');
122
37b5e8fe
RT
123 if (!empty($config->showchildren) && ($course->id > 0)) {
124 // List children here.
83ea0cc1
AO
125 if ($children = block_course_overview_get_child_shortnames($course->id)) {
126 $html .= html_writer::tag('span', $children, array('class' => 'coursechildren'));
127 }
128 }
129
9e1d4eda
RT
130 // If user is moving courses, then down't show overview.
131 if (isset($overviews[$course->id]) && !$ismovingcourse) {
83ea0cc1
AO
132 $html .= $this->activity_display($course->id, $overviews[$course->id]);
133 }
134
d512e5f8 135 if ($config->showcategories != BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE) {
076109df
JB
136 // List category parent or categories path here.
137 $currentcategory = coursecat::get($course->category, IGNORE_MISSING);
138 if ($currentcategory !== null) {
139 $html .= html_writer::start_tag('div', array('class' => 'categorypath'));
d512e5f8 140 if ($config->showcategories == BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH) {
076109df
JB
141 foreach ($currentcategory->get_parents() as $categoryid) {
142 $category = coursecat::get($categoryid, IGNORE_MISSING);
143 if ($category !== null) {
144 $html .= $category->get_formatted_name().' / ';
145 }
146 }
147 }
148 $html .= $currentcategory->get_formatted_name();
149 $html .= html_writer::end_tag('div');
150 }
151 }
152
37b5e8fe
RT
153 $html .= $this->output->box('', 'flush');
154 $html .= $this->output->box_end();
155 $courseordernumber++;
9e1d4eda
RT
156 if ($ismovingcourse) {
157 $moveurl = new moodle_url('/blocks/course_overview/move.php',
158 array('sesskey' => sesskey(), 'moveto' => $courseordernumber, 'courseid' => $movingcourseid));
159 $a = new stdClass();
160 $a->movingcoursename = $courses[$movingcourseid]->fullname;
3d231eda 161 $a->currentcoursename = $course->fullname;
9e1d4eda
RT
162 $movehereicon = html_writer::empty_tag('img',
163 array('src' => $this->output->pix_url('movehere'),
164 'alt' => get_string('moveafterhere', 'block_course_overview', $a),
165 'title' => get_string('movehere')));
166 $moveurl = html_writer::link($moveurl, $movehereicon);
167 $html .= html_writer::tag('div', $moveurl, array('class' => 'movehere'));
168 }
83ea0cc1 169 }
9e1d4eda
RT
170 // Wrap course list in a div and return.
171 return html_writer::tag('div', $html, array('class' => 'course_list'));
83ea0cc1
AO
172 }
173
37b5e8fe
RT
174 /**
175 * Coustuct activities overview for a course
176 *
177 * @param int $cid course id
178 * @param array $overview overview of activities in course
179 * @return string html of activities overview
180 */
83ea0cc1 181 protected function activity_display($cid, $overview) {
83ea0cc1
AO
182 $output = html_writer::start_tag('div', array('class' => 'activity_info'));
183 foreach (array_keys($overview) as $module) {
184 $output .= html_writer::start_tag('div', array('class' => 'activity_overview'));
185 $url = new moodle_url("/mod/$module/index.php", array('id' => $cid));
37b5e8fe 186 $modulename = get_string('modulename', $module);
3b593110 187 $icontext = html_writer::link($url, $this->output->pix_icon('icon', $modulename, 'mod_'.$module, array('class'=>'iconlarge')));
37b5e8fe
RT
188 if (get_string_manager()->string_exists("activityoverview", $module)) {
189 $icontext .= get_string("activityoverview", $module);
83ea0cc1 190 } else {
37b5e8fe 191 $icontext .= get_string("activityoverview", 'block_course_overview', $modulename);
83ea0cc1
AO
192 }
193
37b5e8fe 194 // Add collapsible region with overview text in it.
83ea0cc1
AO
195 $output .= $this->collapsible_region($overview[$module], '', 'region_'.$cid.'_'.$module, $icontext, '', true);
196
197 $output .= html_writer::end_tag('div');
198 }
199 $output .= html_writer::end_tag('div');
200 return $output;
201 }
202
37b5e8fe
RT
203 /**
204 * Constructs header in editing mode
205 *
206 * @param int $max maximum number of courses
207 * @return string html of header bar.
208 */
83ea0cc1 209 public function editing_bar_head($max = 0) {
37b5e8fe 210 $output = $this->output->box_start('notice');
83ea0cc1
AO
211
212 $options = array('0' => get_string('alwaysshowall', 'block_course_overview'));
213 for ($i = 1; $i <= $max; $i++) {
214 $options[$i] = $i;
215 }
216 $url = new moodle_url('/my/index.php');
37b5e8fe 217 $select = new single_select($url, 'mynumber', $options, block_course_overview_get_max_user_courses(), array());
83ea0cc1 218 $select->set_label(get_string('numtodisplay', 'block_course_overview'));
37b5e8fe 219 $output .= $this->output->render($select);
83ea0cc1 220
37b5e8fe 221 $output .= $this->output->box_end();
83ea0cc1
AO
222 return $output;
223 }
224
37b5e8fe
RT
225 /**
226 * Show hidden courses count
227 *
228 * @param int $total count of hidden courses
229 * @return string html
230 */
83ea0cc1 231 public function hidden_courses($total) {
83ea0cc1
AO
232 if ($total <= 0) {
233 return;
234 }
37b5e8fe 235 $output = $this->output->box_start('notice');
83ea0cc1 236 $plural = $total > 1 ? 'plural' : '';
6fb07608
RT
237 $config = get_config('block_course_overview');
238 // Show view all course link to user if forcedefaultmaxcourses is not empty.
239 if (!empty($config->forcedefaultmaxcourses)) {
ca85839f 240 $output .= get_string('hiddencoursecount'.$plural, 'block_course_overview', $total);
6fb07608
RT
241 } else {
242 $a = new stdClass();
243 $a->coursecount = $total;
244 $a->showalllink = html_writer::link(new moodle_url('/my/index.php', array('mynumber' => block_course_overview::SHOW_ALL_COURSES)),
245 get_string('showallcourses'));
ca85839f 246 $output .= get_string('hiddencoursecountwithshowall'.$plural, 'block_course_overview', $a);
6fb07608
RT
247 }
248
37b5e8fe 249 $output .= $this->output->box_end();
83ea0cc1
AO
250 return $output;
251 }
252
37b5e8fe
RT
253 /**
254 * Creates collapsable region
255 *
256 * @param string $contents existing contents
257 * @param string $classes class names added to the div that is output.
258 * @param string $id id added to the div that is output. Must not be blank.
259 * @param string $caption text displayed at the top. Clicking on this will cause the region to expand or contract.
260 * @param string $userpref the name of the user preference that stores the user's preferred default state.
261 * (May be blank if you do not wish the state to be persisted.
262 * @param bool $default Initial collapsed state to use if the user_preference it not set.
263 * @return bool if true, return the HTML as a string, rather than printing it.
264 */
83ea0cc1
AO
265 protected function collapsible_region($contents, $classes, $id, $caption, $userpref = '', $default = false) {
266 $output = $this->collapsible_region_start($classes, $id, $caption, $userpref, $default);
267 $output .= $contents;
268 $output .= $this->collapsible_region_end();
269
270 return $output;
271 }
272
273 /**
274 * Print (or return) the start of a collapsible region, that has a caption that can
275 * be clicked to expand or collapse the region. If JavaScript is off, then the region
276 * will always be expanded.
277 *
278 * @param string $classes class names added to the div that is output.
279 * @param string $id id added to the div that is output. Must not be blank.
280 * @param string $caption text displayed at the top. Clicking on this will cause the region to expand or contract.
281 * @param string $userpref the name of the user preference that stores the user's preferred default state.
282 * (May be blank if you do not wish the state to be persisted.
37b5e8fe
RT
283 * @param bool $default Initial collapsed state to use if the user_preference it not set.
284 * @return bool if true, return the HTML as a string, rather than printing it.
83ea0cc1
AO
285 */
286 protected function collapsible_region_start($classes, $id, $caption, $userpref = '', $default = false) {
83ea0cc1
AO
287 // Work out the initial state.
288 if (!empty($userpref) and is_string($userpref)) {
289 user_preference_allow_ajax_update($userpref, PARAM_BOOL);
290 $collapsed = get_user_preferences($userpref, $default);
291 } else {
292 $collapsed = $default;
293 $userpref = false;
294 }
295
296 if ($collapsed) {
297 $classes .= ' collapsed';
298 }
299
300 $output = '';
301 $output .= '<div id="' . $id . '" class="collapsibleregion ' . $classes . '">';
302 $output .= '<div id="' . $id . '_sizer">';
303 $output .= '<div id="' . $id . '_caption" class="collapsibleregioncaption">';
304 $output .= $caption . ' ';
305 $output .= '</div><div id="' . $id . '_inner" class="collapsibleregioninner">';
306 $this->page->requires->js_init_call('M.block_course_overview.collapsible', array($id, $userpref, get_string('clicktohideshow')));
307
308 return $output;
309 }
310
311 /**
312 * Close a region started with print_collapsible_region_start.
313 *
37b5e8fe 314 * @return string return the HTML as a string, rather than printing it.
83ea0cc1
AO
315 */
316 protected function collapsible_region_end() {
317 $output = '</div></div></div>';
318 return $output;
319 }
320
37b5e8fe
RT
321 /**
322 * Cretes html for welcome area
323 *
324 * @param int $msgcount number of messages
325 * @return string html string for welcome area.
326 */
327 public function welcome_area($msgcount) {
f58c96f6 328 global $CFG, $USER;
37b5e8fe 329 $output = $this->output->box_start('welcome_area');
83ea0cc1 330
37b5e8fe 331 $picture = $this->output->user_picture($USER, array('size' => 75, 'class' => 'welcome_userpicture'));
83ea0cc1
AO
332 $output .= html_writer::tag('div', $picture, array('class' => 'profilepicture'));
333
37b5e8fe
RT
334 $output .= $this->output->box_start('welcome_message');
335 $output .= $this->output->heading(get_string('welcome', 'block_course_overview', $USER->firstname));
83ea0cc1 336
f58c96f6
DG
337 if (!empty($CFG->messaging)) {
338 $plural = 's';
339 if ($msgcount > 0) {
340 $output .= get_string('youhavemessages', 'block_course_overview', $msgcount);
341 if ($msgcount == 1) {
342 $plural = '';
343 }
344 } else {
345 $output .= get_string('youhavenomessages', 'block_course_overview');
83ea0cc1 346 }
f58c96f6
DG
347 $output .= html_writer::link(new moodle_url('/message/index.php'),
348 get_string('message'.$plural, 'block_course_overview'));
83ea0cc1 349 }
37b5e8fe
RT
350 $output .= $this->output->box_end();
351 $output .= $this->output->box('', 'flush');
352 $output .= $this->output->box_end();
83ea0cc1
AO
353
354 return $output;
355 }
356}