MDL-45565 phpunit: Changed testsuite_name, so as to avoid class collision
[moodle.git] / blocks / course_overview / renderer.php
blob76ef098c1dbeb075a98fd6595f9e2a26f36e5aaf
1 <?php
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.
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/>.
17 /**
18 * course_overview block rendrer
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
24 defined('MOODLE_INTERNAL') || die;
26 /**
27 * Course_overview block rendrer
29 * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class block_course_overview_renderer extends plugin_renderer_base {
34 /**
35 * Construct contents of course_overview block
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
41 public function course_overview($courses, $overviews) {
42 $html = '';
43 $config = get_config('block_course_overview');
44 $ismovingcourse = false;
45 $courseordernumber = 0;
46 $maxcourses = count($courses);
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 $this->page->requires->js_init_call('M.block_course_overview.add_handles');
53 // Check if course is moving
54 $ismovingcourse = optional_param('movecourse', FALSE, PARAM_BOOL);
55 $movingcourseid = optional_param('courseid', 0, PARAM_INT);
58 // Render first movehere icon.
59 if ($ismovingcourse) {
60 // Remove movecourse param from url.
61 $this->page->ensure_param_not_in_url('movecourse');
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();
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'));
82 foreach ($courses as $key => $course) {
83 // If moving course, then don't show course which needs to be moved.
84 if ($ismovingcourse && ($course->id == $movingcourseid)) {
85 continue;
87 $html .= $this->output->box_start('coursebox', "course-{$course->id}");
88 $html .= html_writer::start_tag('div', array('class' => 'course_title'));
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),
93 'alt' => get_string('movecourse', 'block_course_overview', $course->fullname),
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'));
101 // No need to pass title through s() here as it will be done automatically by html_writer.
102 $attributes = array('title' => $course->fullname);
103 if ($course->id > 0) {
104 if (empty($course->visible)) {
105 $attributes['class'] = 'dimmed';
107 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
108 $coursefullname = format_string(get_course_display_name_for_list($course), true, $course->id);
109 $link = html_writer::link($courseurl, $coursefullname, $attributes);
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)),
114 format_string($course->shortname, true), $attributes) . ' (' . format_string($course->hostname) . ')', 2, 'title');
116 $html .= $this->output->box('', 'flush');
117 $html .= html_writer::end_tag('div');
119 if (!empty($config->showchildren) && ($course->id > 0)) {
120 // List children here.
121 if ($children = block_course_overview_get_child_shortnames($course->id)) {
122 $html .= html_writer::tag('span', $children, array('class' => 'coursechildren'));
126 // If user is moving courses, then down't show overview.
127 if (isset($overviews[$course->id]) && !$ismovingcourse) {
128 $html .= $this->activity_display($course->id, $overviews[$course->id]);
131 $html .= $this->output->box('', 'flush');
132 $html .= $this->output->box_end();
133 $courseordernumber++;
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;
139 $a->currentcoursename = $course->fullname;
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'));
148 // Wrap course list in a div and return.
149 return html_writer::tag('div', $html, array('class' => 'course_list'));
153 * Coustuct activities overview for a course
155 * @param int $cid course id
156 * @param array $overview overview of activities in course
157 * @return string html of activities overview
159 protected function activity_display($cid, $overview) {
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));
164 $modulename = get_string('modulename', $module);
165 $icontext = html_writer::link($url, $this->output->pix_icon('icon', $modulename, 'mod_'.$module, array('class'=>'iconlarge')));
166 if (get_string_manager()->string_exists("activityoverview", $module)) {
167 $icontext .= get_string("activityoverview", $module);
168 } else {
169 $icontext .= get_string("activityoverview", 'block_course_overview', $modulename);
172 // Add collapsible region with overview text in it.
173 $output .= $this->collapsible_region($overview[$module], '', 'region_'.$cid.'_'.$module, $icontext, '', true);
175 $output .= html_writer::end_tag('div');
177 $output .= html_writer::end_tag('div');
178 return $output;
182 * Constructs header in editing mode
184 * @param int $max maximum number of courses
185 * @return string html of header bar.
187 public function editing_bar_head($max = 0) {
188 $output = $this->output->box_start('notice');
190 $options = array('0' => get_string('alwaysshowall', 'block_course_overview'));
191 for ($i = 1; $i <= $max; $i++) {
192 $options[$i] = $i;
194 $url = new moodle_url('/my/index.php');
195 $select = new single_select($url, 'mynumber', $options, block_course_overview_get_max_user_courses(), array());
196 $select->set_label(get_string('numtodisplay', 'block_course_overview'));
197 $output .= $this->output->render($select);
199 $output .= $this->output->box_end();
200 return $output;
204 * Show hidden courses count
206 * @param int $total count of hidden courses
207 * @return string html
209 public function hidden_courses($total) {
210 if ($total <= 0) {
211 return;
213 $output = $this->output->box_start('notice');
214 $plural = $total > 1 ? 'plural' : '';
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)) {
218 $output .= get_string('hiddencoursecount'.$plural, 'block_course_overview', $total);
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'));
224 $output .= get_string('hiddencoursecountwithshowall'.$plural, 'block_course_overview', $a);
227 $output .= $this->output->box_end();
228 return $output;
232 * Creates collapsable region
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.
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();
248 return $output;
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.
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.
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.
264 protected function collapsible_region_start($classes, $id, $caption, $userpref = '', $default = false) {
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;
274 if ($collapsed) {
275 $classes .= ' collapsed';
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')));
286 return $output;
290 * Close a region started with print_collapsible_region_start.
292 * @return string return the HTML as a string, rather than printing it.
294 protected function collapsible_region_end() {
295 $output = '</div></div></div>';
296 return $output;
300 * Cretes html for welcome area
302 * @param int $msgcount number of messages
303 * @return string html string for welcome area.
305 public function welcome_area($msgcount) {
306 global $USER;
307 $output = $this->output->box_start('welcome_area');
309 $picture = $this->output->user_picture($USER, array('size' => 75, 'class' => 'welcome_userpicture'));
310 $output .= html_writer::tag('div', $picture, array('class' => 'profilepicture'));
312 $output .= $this->output->box_start('welcome_message');
313 $output .= $this->output->heading(get_string('welcome', 'block_course_overview', $USER->firstname));
315 $plural = 's';
316 if ($msgcount > 0) {
317 $output .= get_string('youhavemessages', 'block_course_overview', $msgcount);
318 if ($msgcount == 1) {
319 $plural = '';
321 } else {
322 $output .= get_string('youhavenomessages', 'block_course_overview');
324 $output .= html_writer::link(new moodle_url('/message/index.php'), get_string('message'.$plural, 'block_course_overview'));
325 $output .= $this->output->box_end();
326 $output .= $this->output->box('', 'flush');
327 $output .= $this->output->box_end();
329 return $output;