MDL-19430 My Moodle: Merged Netspot My Course Overview Block for Moodle 2
[moodle.git] / blocks / course_overview / renderer.php
CommitLineData
83ea0cc1
AO
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18defined('MOODLE_INTERNAL') || die;
19
20require_once($CFG->dirroot.'/message/lib.php');
21
22class block_course_overview_renderer extends plugin_renderer_base {
23
24 public function course_overview($courses, $overviews, $moving = 0) {
25 global $OUTPUT;
26 $html = '';
27 $config = get_config('block_course_overview');
28
29 $html .= html_writer::start_tag('div', array('id' => 'course_list'));
30 $weight = 0;
31 $keylist = array_keys($courses);
32 if ($this->page->user_is_editing() && $moving && $keylist[0] != $moving) {
33 $html .= $this->course_move_target($moving, $weight);
34 $weight++;
35 }
36 foreach ($courses as $course) {
37 $cursor = ajaxenabled() && !$moving ? ' cursor' : '';
38 $html .= $OUTPUT->box_start('coursebox', "course-{$course->id}");
39 $html .= html_writer::start_tag('div', array('class' => 'course_title'));
40 if ($this->page->user_is_editing()) {
41 // Move icon.
42 $icon = ajaxenabled() ? 'i/move_2d' : 't/move';
43 $control = array('url' => new moodle_url('/my/index.php', array('course_moveid' => $course->id)),
44 'icon' => $icon, 'caption' => get_string('move'));
45 if (ajaxenabled()) {
46 $html .= html_writer::tag('div',
47 html_writer::empty_tag('img',
48 array('src' => $this->pix_url($control['icon'])->out(false),
49 'alt' => $control['caption'], 'class' => 'icon cursor',
50 'title' => $control['caption'])
51 ), array('class' => 'move')
52 );
53 } else {
54 $html .= html_writer::tag('a',
55 html_writer::empty_tag('img', array('src' => $this->pix_url($control['icon'])->out(false), 'alt' => $control['caption'])),
56 array('class' => 'icon move','title' => $control['caption'], 'href' => $control['url']));
57 }
58 }
59 $link = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $course->fullname);
60 $html .= $OUTPUT->heading($link, 2, 'title');
61 $html .= $OUTPUT->box('', 'flush');
62 $html .= html_writer::end_tag('div');
63
64 if (isset($config->showchildren) && $config->showchildren) {
65 //list children here
66 if ($children = block_course_overview_get_child_shortnames($course->id)) {
67 $html .= html_writer::tag('span', $children, array('class' => 'coursechildren'));
68 }
69 }
70
71 if (isset($overviews[$course->id])) {
72 $html .= $this->activity_display($course->id, $overviews[$course->id]);
73 }
74
75 $html .= $OUTPUT->box('', 'flush');
76 $html .= $OUTPUT->box_end();
77
78 if ($this->page->user_is_editing() && $moving && $course->id != $moving) {
79 //check if next is the course we're moving
80 $okay = true;
81 if (isset($keylist[$weight])) {
82 if ($courses[$keylist[$weight]]->id == $moving) {
83 $okay = false;
84 }
85 }
86 if ($okay) {
87 $html .= $this->course_move_target($moving, $weight);
88 }
89 }
90 $weight++;
91 }
92 $html .= html_writer::end_tag('div');
93
94 return $html;
95 }
96
97 protected function course_move_target($cid, $weight) {
98 $url = new moodle_url('/blocks/course_overview/move.php', array('source' => $cid, 'target' => $weight));
99 return html_writer::tag('a', html_writer::tag('span', get_string('movecoursehere', 'block_course_overview'),
100 array('class' => 'accesshide')), array('href' => $url, 'class' => 'coursemovetarget'));
101 }
102
103 protected function activity_display($cid, $overview) {
104 global $OUTPUT;
105 $output = html_writer::start_tag('div', array('class' => 'activity_info'));
106 foreach (array_keys($overview) as $module) {
107 $output .= html_writer::start_tag('div', array('class' => 'activity_overview'));
108 $url = new moodle_url("/mod/$module/index.php", array('id' => $cid));
109 $icontext = html_writer::link($url, $OUTPUT->pix_icon('icon', get_string('modulename', $module), 'mod_'.$module, array('class'=>'icon')).' ');
110 if (get_string_manager()->string_exists("activity_$module", 'block_course_overview')) {
111 $icontext .= get_string("activity_$module", 'block_course_overview');
112 } else {
113 $icontext .= get_string("activityoverview", 'block_course_overview', $module);
114 }
115
116 //add collapsible region with overview text in it
117 $output .= $this->collapsible_region($overview[$module], '', 'region_'.$cid.'_'.$module, $icontext, '', true);
118
119 $output .= html_writer::end_tag('div');
120 }
121 $output .= html_writer::end_tag('div');
122 return $output;
123 }
124
125 public function editing_bar_head($max = 0) {
126 global $OUTPUT, $USER;
127 $output = $OUTPUT->box_start('notice');
128
129 $options = array('0' => get_string('alwaysshowall', 'block_course_overview'));
130 for ($i = 1; $i <= $max; $i++) {
131 $options[$i] = $i;
132 }
133 $url = new moodle_url('/my/index.php');
134 $select = new single_select($url, 'mynumber', $options, $USER->profile['mynumber'], array());
135 $select->set_label(get_string('numtodisplay', 'block_course_overview'));
136 $output .= $OUTPUT->render($select);
137
138 $output .= $OUTPUT->box_end();
139 return $output;
140 }
141
142 public function hidden_courses($total) {
143 global $OUTPUT;
144 if ($total <= 0) {
145 return;
146 }
147 $output = $OUTPUT->box_start('notice');
148 $plural = $total > 1 ? 'plural' : '';
149 $output .= get_string('hiddencoursecount'.$plural, 'block_course_overview', $total);
150 $output .= $OUTPUT->box_end();
151 return $output;
152 }
153
154 protected function collapsible_region($contents, $classes, $id, $caption, $userpref = '', $default = false) {
155 $output = $this->collapsible_region_start($classes, $id, $caption, $userpref, $default);
156 $output .= $contents;
157 $output .= $this->collapsible_region_end();
158
159 return $output;
160 }
161
162 /**
163 * Print (or return) the start of a collapsible region, that has a caption that can
164 * be clicked to expand or collapse the region. If JavaScript is off, then the region
165 * will always be expanded.
166 *
167 * @param string $classes class names added to the div that is output.
168 * @param string $id id added to the div that is output. Must not be blank.
169 * @param string $caption text displayed at the top. Clicking on this will cause the region to expand or contract.
170 * @param string $userpref the name of the user preference that stores the user's preferred default state.
171 * (May be blank if you do not wish the state to be persisted.
172 * @param boolean $default Initial collapsed state to use if the user_preference it not set.
173 * @param boolean $return if true, return the HTML as a string, rather than printing it.
174 */
175 protected function collapsible_region_start($classes, $id, $caption, $userpref = '', $default = false) {
176 global $CFG, $PAGE, $OUTPUT;
177
178 // Work out the initial state.
179 if (!empty($userpref) and is_string($userpref)) {
180 user_preference_allow_ajax_update($userpref, PARAM_BOOL);
181 $collapsed = get_user_preferences($userpref, $default);
182 } else {
183 $collapsed = $default;
184 $userpref = false;
185 }
186
187 if ($collapsed) {
188 $classes .= ' collapsed';
189 }
190
191 $output = '';
192 $output .= '<div id="' . $id . '" class="collapsibleregion ' . $classes . '">';
193 $output .= '<div id="' . $id . '_sizer">';
194 $output .= '<div id="' . $id . '_caption" class="collapsibleregioncaption">';
195 $output .= $caption . ' ';
196 $output .= '</div><div id="' . $id . '_inner" class="collapsibleregioninner">';
197 $this->page->requires->js_init_call('M.block_course_overview.collapsible', array($id, $userpref, get_string('clicktohideshow')));
198
199 return $output;
200 }
201
202 /**
203 * Close a region started with print_collapsible_region_start.
204 *
205 * @param boolean $return if true, return the HTML as a string, rather than printing it.
206 */
207 protected function collapsible_region_end() {
208 $output = '</div></div></div>';
209 return $output;
210 }
211
212 public function welcome_area() {
213 global $OUTPUT, $USER;
214 $output = $OUTPUT->box_start('welcome_area');
215
216 $picture = $OUTPUT->user_picture($USER, array('size' => 75, 'class' => 'welcome_userpicture'));
217 $output .= html_writer::tag('div', $picture, array('class' => 'profilepicture'));
218
219 $output .= $OUTPUT->box_start('welcome_message');
220 $output .= $OUTPUT->heading(get_string('welcome', 'block_course_overview', $USER->firstname));
221
222 //messages
223 $count = message_count_unread_messages($USER);
224 $plural = 's';
225 if ($count > 0) {
226 $output .= get_string('you_have_messages', 'block_course_overview', $count);
227 } else {
228 $output .= get_string('you_have_no_messages', 'block_course_overview');
229 if ($count == 1) {
230 $plural = '';
231 }
232 }
233 $output .= html_writer::link(new moodle_url('/message/index.php'), get_string('message'.$plural, 'block_course_overview'));
234 $output .= $OUTPUT->box_end();
235 $output .= $OUTPUT->box('', 'flush');
236 $output .= $OUTPUT->box_end();
237
238 return $output;
239 }
240}