Commit | Line | Data |
---|---|---|
24e27ac0 SH |
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 | ||
18 | /** | |
19 | * Renderer for use with the course section and all the goodness that falls | |
20 | * within it. | |
21 | * | |
22 | * This renderer should contain methods useful to courses, and categories. | |
23 | * | |
24 | * @package moodlecore | |
25 | * @copyright 2010 Sam Hemelryk | |
26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
27 | */ | |
28 | ||
29 | /** | |
30 | * The core course renderer | |
31 | * | |
32 | * Can be retrieved with the following: | |
33 | * $renderer = $PAGE->get_renderer('core','course'); | |
34 | */ | |
35 | class core_course_renderer extends plugin_renderer_base { | |
36 | ||
37 | /** | |
38 | * A cache of strings | |
39 | * @var stdClass | |
40 | */ | |
41 | protected $strings; | |
42 | ||
43 | /** | |
44 | * Override the constructor so that we can initialise the string cache | |
45 | * | |
46 | * @param moodle_page $page | |
47 | * @param string $target | |
48 | */ | |
49 | public function __construct(moodle_page $page, $target) { | |
50 | $this->strings = new stdClass; | |
51 | parent::__construct($page, $target); | |
52 | } | |
53 | ||
cb76fec0 PS |
54 | /** |
55 | * Renders course info box. | |
56 | * | |
57 | * @param stdClass $course | |
58 | * @return string | |
59 | */ | |
60 | public function course_info_box(stdClass $course) { | |
61 | global $CFG; | |
62 | ||
63 | $context = context_course::instance($course->id); | |
64 | ||
65 | $content = ''; | |
66 | $content .= $this->output->box_start('generalbox info'); | |
67 | ||
68 | $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null); | |
69 | $content .= format_text($summary, $course->summaryformat, array('overflowdiv'=>true), $course->id); | |
70 | if (!empty($CFG->coursecontact)) { | |
71 | $coursecontactroles = explode(',', $CFG->coursecontact); | |
72 | foreach ($coursecontactroles as $roleid) { | |
73 | if ($users = get_role_users($roleid, $context, true)) { | |
74 | foreach ($users as $teacher) { | |
75 | $role = new stdClass(); | |
76 | $role->id = $teacher->roleid; | |
77 | $role->name = $teacher->rolename; | |
78 | $role->shortname = $teacher->roleshortname; | |
79 | $role->coursealias = $teacher->rolecoursealias; | |
80 | $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context)); | |
81 | $namesarray[] = role_get_name($role, $context).': <a href="'.$CFG->wwwroot.'/user/view.php?id='. | |
82 | $teacher->id.'&course='.SITEID.'">'.$fullname.'</a>'; | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | if (!empty($namesarray)) { | |
88 | $content .= "<ul class=\"teachers\">\n<li>"; | |
89 | $content .= implode('</li><li>', $namesarray); | |
90 | $content .= "</li></ul>"; | |
91 | } | |
92 | } | |
93 | ||
94 | $content .= $this->output->box_end(); | |
95 | ||
96 | return $content; | |
97 | } | |
98 | ||
24e27ac0 SH |
99 | /** |
100 | * Renderers a structured array of courses and categories into a nice | |
101 | * XHTML tree structure. | |
102 | * | |
103 | * This method was designed initially to display the front page course/category | |
104 | * combo view. The structure can be retrieved by get_course_category_tree() | |
105 | * | |
106 | * @param array $structure | |
107 | * @return string | |
108 | */ | |
109 | public function course_category_tree(array $structure) { | |
24e27ac0 SH |
110 | $this->strings->summary = get_string('summary'); |
111 | ||
112 | // Generate an id and the required JS call to make this a nice widget | |
113 | $id = html_writer::random_id('course_category_tree'); | |
c8ffba95 | 114 | $this->page->requires->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children .category_label', 'collapsed', '.category.with_children')); |
24e27ac0 SH |
115 | |
116 | // Start content generation | |
117 | $content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id)); | |
118 | foreach ($structure as $category) { | |
119 | $content .= $this->course_category_tree_category($category); | |
120 | } | |
121 | $content .= html_writer::start_tag('div', array('class'=>'controls')); | |
122 | $content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall')); | |
123 | $content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall')); | |
124 | $content .= html_writer::end_tag('div'); | |
125 | $content .= html_writer::end_tag('div'); | |
df997f84 | 126 | |
24e27ac0 SH |
127 | // Return the course category tree HTML |
128 | return $content; | |
129 | } | |
130 | ||
131 | /** | |
132 | * Renderers a category for use with course_category_tree | |
133 | * | |
134 | * @param array $category | |
135 | * @param int $depth | |
136 | * @return string | |
137 | */ | |
138 | protected function course_category_tree_category(stdClass $category, $depth=1) { | |
139 | $content = ''; | |
3754f4f3 ARN |
140 | $hassubcategories = (isset($category->categories) && count($category->categories)>0); |
141 | $hascourses = (isset($category->courses) && count($category->courses)>0); | |
24e27ac0 SH |
142 | $classes = array('category'); |
143 | if ($category->parent != 0) { | |
144 | $classes[] = 'subcategory'; | |
145 | } | |
d703f226 SH |
146 | if (empty($category->visible)) { |
147 | $classes[] = 'dimmed_category'; | |
148 | } | |
24e27ac0 SH |
149 | if ($hassubcategories || $hascourses) { |
150 | $classes[] = 'with_children'; | |
151 | if ($depth > 1) { | |
152 | $classes[] = 'collapsed'; | |
153 | } | |
154 | } | |
9a5e297b | 155 | $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id))); |
63390481 | 156 | |
24e27ac0 SH |
157 | $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); |
158 | $content .= html_writer::start_tag('div', array('class'=>'category_label')); | |
63390481 | 159 | $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link')); |
24e27ac0 SH |
160 | $content .= html_writer::end_tag('div'); |
161 | if ($hassubcategories) { | |
162 | $content .= html_writer::start_tag('div', array('class'=>'subcategories')); | |
163 | foreach ($category->categories as $subcategory) { | |
164 | $content .= $this->course_category_tree_category($subcategory, $depth+1); | |
165 | } | |
166 | $content .= html_writer::end_tag('div'); | |
167 | } | |
168 | if ($hascourses) { | |
169 | $content .= html_writer::start_tag('div', array('class'=>'courses')); | |
170 | $coursecount = 0; | |
171 | foreach ($category->courses as $course) { | |
172 | $classes = array('course'); | |
d2fd38be PS |
173 | $linkclass = 'course_link'; |
174 | if (!$course->visible) { | |
175 | $linkclass .= ' dimmed'; | |
176 | } | |
24e27ac0 SH |
177 | $coursecount ++; |
178 | $classes[] = ($coursecount%2)?'odd':'even'; | |
179 | $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); | |
f4c23f03 | 180 | $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass)); |
24e27ac0 SH |
181 | $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix')); |
182 | ||
bf423bb1 PS |
183 | // print enrol info |
184 | if ($icons = enrol_get_course_info_icons($course)) { | |
185 | foreach ($icons as $pix_icon) { | |
186 | $content .= $this->render($pix_icon); | |
187 | } | |
188 | } | |
189 | ||
24e27ac0 SH |
190 | if ($course->summary) { |
191 | $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary)); | |
192 | $content .= html_writer::link(new moodle_url('/course/info.php', array('id'=>$course->id)), $image, array('title'=>$this->strings->summary)); | |
24e27ac0 SH |
193 | } |
194 | $content .= html_writer::end_tag('div'); | |
195 | $content .= html_writer::end_tag('div'); | |
196 | } | |
197 | $content .= html_writer::end_tag('div'); | |
198 | } | |
199 | $content .= html_writer::end_tag('div'); | |
200 | return $content; | |
201 | } | |
01e0e704 ARN |
202 | |
203 | /** | |
204 | * Build the HTML for the module chooser javascript popup | |
205 | * | |
206 | * @param array $modules A set of modules as returned form @see | |
207 | * get_module_metadata | |
208 | * @param object $course The course that will be displayed | |
209 | * @return string The composed HTML for the module | |
210 | */ | |
211 | public function course_modchooser($modules, $course) { | |
212 | global $OUTPUT; | |
213 | ||
214 | // Add the header | |
1edff8c7 | 215 | $header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'), |
255dd8d1 | 216 | array('class' => 'hd choosertitle')); |
01e0e704 ARN |
217 | |
218 | $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'), | |
219 | 'id' => 'chooserform', 'method' => 'post')); | |
220 | $formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv')); | |
221 | $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course', | |
222 | 'name' => 'course', 'value' => $course->id)); | |
223 | $formcontent .= html_writer::tag('input', '', | |
0a2fb910 | 224 | array('type' => 'hidden', 'class' => 'jump', 'name' => 'jump', 'value' => '')); |
01e0e704 ARN |
225 | $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey', |
226 | 'value' => sesskey())); | |
227 | $formcontent .= html_writer::end_tag('div'); | |
228 | ||
229 | // Put everything into one tag 'options' | |
230 | $formcontent .= html_writer::start_tag('div', array('class' => 'options')); | |
231 | $formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'), | |
232 | array('class' => 'instruction')); | |
233 | // Put all options into one tag 'alloptions' to allow us to handle scrolling | |
234 | $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions')); | |
235 | ||
48a5e55e | 236 | // Activities |
01e0e704 ARN |
237 | $activities = array_filter($modules, |
238 | create_function('$mod', 'return ($mod->archetype !== MOD_CLASS_RESOURCE);')); | |
239 | if (count($activities)) { | |
240 | $formcontent .= $this->course_modchooser_title('activities'); | |
241 | $formcontent .= $this->course_modchooser_module_types($activities); | |
242 | } | |
243 | ||
463ccee1 | 244 | // Resources |
48a5e55e ARN |
245 | $resources = array_filter($modules, |
246 | create_function('$mod', 'return ($mod->archetype === MOD_CLASS_RESOURCE);')); | |
247 | if (count($resources)) { | |
248 | $formcontent .= $this->course_modchooser_title('resources'); | |
249 | $formcontent .= $this->course_modchooser_module_types($resources); | |
250 | } | |
251 | ||
01e0e704 ARN |
252 | $formcontent .= html_writer::end_tag('div'); // modoptions |
253 | $formcontent .= html_writer::end_tag('div'); // types | |
254 | ||
255 | $formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons')); | |
af75421c | 256 | $formcontent .= html_writer::tag('input', '', |
0a2fb910 | 257 | array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add'))); |
8ff6c5ee | 258 | $formcontent .= html_writer::tag('input', '', |
0a2fb910 | 259 | array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel'))); |
01e0e704 ARN |
260 | $formcontent .= html_writer::end_tag('div'); |
261 | $formcontent .= html_writer::end_tag('form'); | |
262 | ||
263 | // Wrap the whole form in a div | |
264 | $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform')); | |
265 | ||
266 | // Put all of the content together | |
267 | $content = $formcontent; | |
268 | ||
255dd8d1 ARN |
269 | $content = html_writer::tag('div', $content, array('class' => 'choosercontainer')); |
270 | return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody')); | |
01e0e704 ARN |
271 | } |
272 | ||
273 | /** | |
274 | * Build the HTML for a specified set of modules | |
275 | * | |
276 | * @param array $modules A set of modules as used by the | |
277 | * course_modchooser_module function | |
278 | * @return string The composed HTML for the module | |
279 | */ | |
280 | protected function course_modchooser_module_types($modules) { | |
281 | $return = ''; | |
282 | foreach ($modules as $module) { | |
283 | if (!isset($module->types)) { | |
284 | $return .= $this->course_modchooser_module($module); | |
285 | } else { | |
286 | $return .= $this->course_modchooser_module($module, array('nonoption')); | |
287 | foreach ($module->types as $type) { | |
288 | $return .= $this->course_modchooser_module($type, array('option', 'subtype')); | |
289 | } | |
290 | } | |
291 | } | |
292 | return $return; | |
293 | } | |
294 | ||
295 | /** | |
296 | * Return the HTML for the specified module adding any required classes | |
297 | * | |
298 | * @param object $module An object containing the title, and link. An | |
299 | * icon, and help text may optionally be specified. If the module | |
300 | * contains subtypes in the types option, then these will also be | |
301 | * displayed. | |
302 | * @param array $classes Additional classes to add to the encompassing | |
303 | * div element | |
304 | * @return string The composed HTML for the module | |
305 | */ | |
306 | protected function course_modchooser_module($module, $classes = array('option')) { | |
307 | $output = ''; | |
308 | $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes))); | |
309 | $output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name)); | |
310 | if (!isset($module->types)) { | |
311 | $output .= html_writer::tag('input', '', array('type' => 'radio', | |
312 | 'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link)); | |
313 | } | |
314 | ||
315 | $output .= html_writer::start_tag('span', array('class' => 'modicon')); | |
316 | if (isset($module->icon)) { | |
317 | // Add an icon if we have one | |
318 | $output .= $module->icon; | |
319 | } | |
320 | $output .= html_writer::end_tag('span'); | |
321 | ||
322 | $output .= html_writer::tag('span', $module->title, array('class' => 'typename')); | |
323 | if (!isset($module->help)) { | |
324 | // Add help if found | |
325 | $module->help = get_string('nohelpforactivityorresource', 'moodle'); | |
326 | } | |
327 | ||
328 | // Format the help text using markdown with the following options | |
329 | $options = new stdClass(); | |
330 | $options->trusted = false; | |
331 | $options->noclean = false; | |
332 | $options->smiley = false; | |
333 | $options->filter = false; | |
334 | $options->para = true; | |
335 | $options->newlines = false; | |
336 | $options->overflowdiv = false; | |
337 | $module->help = format_text($module->help, FORMAT_MARKDOWN, $options); | |
338 | $output .= html_writer::tag('span', $module->help, array('class' => 'typesummary')); | |
339 | $output .= html_writer::end_tag('label'); | |
340 | $output .= html_writer::end_tag('div'); | |
341 | ||
342 | return $output; | |
343 | } | |
344 | ||
345 | protected function course_modchooser_title($title, $identifier = null) { | |
346 | $module = new stdClass(); | |
347 | $module->name = $title; | |
348 | $module->types = array(); | |
349 | $module->title = get_string($title, $identifier); | |
350 | $module->help = ''; | |
351 | return $this->course_modchooser_module($module, array('moduletypetitle')); | |
352 | } | |
f4c23f03 | 353 | } |