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