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