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