2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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;
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
{
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) {
43 $config = get_config('block_course_overview');
44 if ($config->showcategories
!= BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE
) {
46 require_once($CFG->libdir
.'/coursecatlib.php');
48 $ismovingcourse = false;
49 $courseordernumber = 0;
50 $maxcourses = count($courses);
52 // Intialise string/icon etc if user is editing and courses > 1
53 if ($this->page
->user_is_editing() && (count($courses) > 1)) {
55 $this->page
->requires
->js_init_call('M.block_course_overview.add_handles');
57 // Check if course is moving
58 $ismovingcourse = optional_param('movecourse', FALSE, PARAM_BOOL
);
59 $movingcourseid = optional_param('courseid', 0, PARAM_INT
);
62 // Render first movehere icon.
63 if ($ismovingcourse) {
64 // Remove movecourse param from url.
65 $this->page
->ensure_param_not_in_url('movecourse');
67 // Show moving course notice, so user knows what is being moved.
68 $html .= $this->output
->box_start('notice');
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();
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'));
86 foreach ($courses as $key => $course) {
87 // If moving course, then don't show course which needs to be moved.
88 if ($ismovingcourse && ($course->id
== $movingcourseid)) {
91 $html .= $this->output
->box_start('coursebox', "course-{$course->id}");
92 $html .= html_writer
::start_tag('div', array('class' => 'course_title'));
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),
97 'alt' => get_string('movecourse', 'block_course_overview', $course->fullname
),
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'));
105 // No need to pass title through s() here as it will be done automatically by html_writer.
106 $attributes = array('title' => $course->fullname
);
107 if ($course->id
> 0) {
108 if (empty($course->visible
)) {
109 $attributes['class'] = 'dimmed';
111 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id
));
112 $coursefullname = format_string(get_course_display_name_for_list($course), true, $course->id
);
113 $link = html_writer
::link($courseurl, $coursefullname, $attributes);
114 $html .= $this->output
->heading($link, 2, 'title');
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
)),
118 format_string($course->shortname
, true), $attributes) . ' (' . format_string($course->hostname
) . ')', 2, 'title');
120 $html .= $this->output
->box('', 'flush');
121 $html .= html_writer
::end_tag('div');
123 if (!empty($config->showchildren
) && ($course->id
> 0)) {
124 // List children here.
125 if ($children = block_course_overview_get_child_shortnames($course->id
)) {
126 $html .= html_writer
::tag('span', $children, array('class' => 'coursechildren'));
130 // If user is moving courses, then down't show overview.
131 if (isset($overviews[$course->id
]) && !$ismovingcourse) {
132 $html .= $this->activity_display($course->id
, $overviews[$course->id
]);
135 if ($config->showcategories
!= BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE
) {
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'));
140 if ($config->showcategories
== BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH
) {
141 foreach ($currentcategory->get_parents() as $categoryid) {
142 $category = coursecat
::get($categoryid, IGNORE_MISSING
);
143 if ($category !== null) {
144 $html .= $category->get_formatted_name().' / ';
148 $html .= $currentcategory->get_formatted_name();
149 $html .= html_writer
::end_tag('div');
153 $html .= $this->output
->box('', 'flush');
154 $html .= $this->output
->box_end();
155 $courseordernumber++
;
156 if ($ismovingcourse) {
157 $moveurl = new moodle_url('/blocks/course_overview/move.php',
158 array('sesskey' => sesskey(), 'moveto' => $courseordernumber, 'courseid' => $movingcourseid));
160 $a->movingcoursename
= $courses[$movingcourseid]->fullname
;
161 $a->currentcoursename
= $course->fullname
;
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'));
170 // Wrap course list in a div and return.
171 return html_writer
::tag('div', $html, array('class' => 'course_list'));
175 * Coustuct activities overview for a course
177 * @param int $cid course id
178 * @param array $overview overview of activities in course
179 * @return string html of activities overview
181 protected function activity_display($cid, $overview) {
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));
186 $modulename = get_string('modulename', $module);
187 $icontext = html_writer
::link($url, $this->output
->pix_icon('icon', $modulename, 'mod_'.$module, array('class'=>'iconlarge')));
188 if (get_string_manager()->string_exists("activityoverview", $module)) {
189 $icontext .= get_string("activityoverview", $module);
191 $icontext .= get_string("activityoverview", 'block_course_overview', $modulename);
194 // Add collapsible region with overview text in it.
195 $output .= $this->collapsible_region($overview[$module], '', 'region_'.$cid.'_'.$module, $icontext, '', true);
197 $output .= html_writer
::end_tag('div');
199 $output .= html_writer
::end_tag('div');
204 * Constructs header in editing mode
206 * @param int $max maximum number of courses
207 * @return string html of header bar.
209 public function editing_bar_head($max = 0) {
210 $output = $this->output
->box_start('notice');
212 $options = array('0' => get_string('alwaysshowall', 'block_course_overview'));
213 for ($i = 1; $i <= $max; $i++
) {
216 $url = new moodle_url('/my/index.php');
217 $select = new single_select($url, 'mynumber', $options, block_course_overview_get_max_user_courses(), array());
218 $select->set_label(get_string('numtodisplay', 'block_course_overview'));
219 $output .= $this->output
->render($select);
221 $output .= $this->output
->box_end();
226 * Show hidden courses count
228 * @param int $total count of hidden courses
229 * @return string html
231 public function hidden_courses($total) {
235 $output = $this->output
->box_start('notice');
236 $plural = $total > 1 ?
'plural' : '';
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
)) {
240 $output .= get_string('hiddencoursecount'.$plural, 'block_course_overview', $total);
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'));
246 $output .= get_string('hiddencoursecountwithshowall'.$plural, 'block_course_overview', $a);
249 $output .= $this->output
->box_end();
254 * Creates collapsable region
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.
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();
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.
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.
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.
286 protected function collapsible_region_start($classes, $id, $caption, $userpref = '', $default = false) {
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);
292 $collapsed = $default;
297 $classes .= ' collapsed';
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')));
312 * Close a region started with print_collapsible_region_start.
314 * @return string return the HTML as a string, rather than printing it.
316 protected function collapsible_region_end() {
317 $output = '</div></div></div>';
322 * Cretes html for welcome area
324 * @param int $msgcount number of messages
325 * @return string html string for welcome area.
327 public function welcome_area($msgcount) {
329 $output = $this->output
->box_start('welcome_area');
331 $picture = $this->output
->user_picture($USER, array('size' => 75, 'class' => 'welcome_userpicture'));
332 $output .= html_writer
::tag('div', $picture, array('class' => 'profilepicture'));
334 $output .= $this->output
->box_start('welcome_message');
335 $output .= $this->output
->heading(get_string('welcome', 'block_course_overview', $USER->firstname
));
339 $output .= get_string('youhavemessages', 'block_course_overview', $msgcount);
340 if ($msgcount == 1) {
344 $output .= get_string('youhavenomessages', 'block_course_overview');
346 $output .= html_writer
::link(new moodle_url('/message/index.php'), get_string('message'.$plural, 'block_course_overview'));
347 $output .= $this->output
->box_end();
348 $output .= $this->output
->box('', 'flush');
349 $output .= $this->output
->box_end();