3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Renderer for use with the course section and all the goodness that falls
22 * This renderer should contain methods useful to courses, and categories.
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * The core course renderer
32 * Can be retrieved with the following:
33 * $renderer = $PAGE->get_renderer('core','course');
35 class core_course_renderer extends plugin_renderer_base {
44 * Override the constructor so that we can initialise the string cache
46 * @param moodle_page $page
47 * @param string $target
49 public function __construct(moodle_page $page, $target) {
50 $this->strings = new stdClass;
51 parent::__construct($page, $target);
55 * Renders course info box.
57 * @param stdClass $course
60 public function course_info_box(stdClass $course) {
63 $context = context_course::instance($course->id);
66 $content .= $this->output->box_start('generalbox info');
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>';
87 if (!empty($namesarray)) {
88 $content .= "<ul class=\"teachers\">\n<li>";
89 $content .= implode('</li><li>', $namesarray);
90 $content .= "</li></ul>";
94 $content .= $this->output->box_end();
100 * Renderers a structured array of courses and categories into a nice
101 * XHTML tree structure.
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()
106 * @param array $structure
109 public function course_category_tree(array $structure) {
110 $this->strings->summary = get_string('summary');
112 // Generate an id and the required JS call to make this a nice widget
113 $id = html_writer::random_id('course_category_tree');
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'));
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);
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');
127 // Return the course category tree HTML
132 * Renderers a category for use with course_category_tree
134 * @param array $category
138 protected function course_category_tree_category(stdClass $category, $depth=1) {
140 $hassubcategories = (isset($category->categories) && count($category->categories)>0);
141 $hascourses = (isset($category->courses) && count($category->courses)>0);
142 $classes = array('category');
143 if ($category->parent != 0) {
144 $classes[] = 'subcategory';
146 if (empty($category->visible)) {
147 $classes[] = 'dimmed_category';
149 if ($hassubcategories || $hascourses) {
150 $classes[] = 'with_children';
152 $classes[] = 'collapsed';
155 $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
157 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
158 $content .= html_writer::start_tag('div', array('class'=>'category_label'));
159 $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link'));
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);
166 $content .= html_writer::end_tag('div');
169 $content .= html_writer::start_tag('div', array('class'=>'courses'));
171 $strinfo = new lang_string('info');
172 foreach ($category->courses as $course) {
173 $classes = array('course');
174 $linkclass = 'course_link';
175 if (!$course->visible) {
176 $linkclass .= ' dimmed';
179 $classes[] = ($coursecount%2)?'odd':'even';
180 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
181 $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass));
182 $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix'));
185 if ($icons = enrol_get_course_info_icons($course)) {
186 foreach ($icons as $pix_icon) {
187 $content .= $this->render($pix_icon);
191 if ($course->summary) {
192 $url = new moodle_url('/course/info.php', array('id' => $course->id));
193 $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary));
194 $content .= $this->action_link($url, $image, new popup_action('click', $url, 'courseinfo'), array('title' => $this->strings->summary));
196 $content .= html_writer::end_tag('div');
197 $content .= html_writer::end_tag('div');
199 $content .= html_writer::end_tag('div');
201 $content .= html_writer::end_tag('div');
206 * Build the HTML for the module chooser javascript popup
208 * @param array $modules A set of modules as returned form @see
209 * get_module_metadata
210 * @param object $course The course that will be displayed
211 * @return string The composed HTML for the module
213 public function course_modchooser($modules, $course) {
214 static $isdisplayed = false;
220 // Add the module chooser
221 $this->page->requires->yui_module('moodle-course-modchooser',
222 'M.course.init_chooser',
223 array(array('courseid' => $course->id, 'closeButtonTitle' => get_string('close', 'editor')))
225 $this->page->requires->strings_for_js(array(
226 'addresourceoractivity',
232 $header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
233 array('class' => 'hd choosertitle'));
235 $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
236 'id' => 'chooserform', 'method' => 'post'));
237 $formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
238 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
239 'name' => 'course', 'value' => $course->id));
240 $formcontent .= html_writer::tag('input', '',
241 array('type' => 'hidden', 'class' => 'jump', 'name' => 'jump', 'value' => ''));
242 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
243 'value' => sesskey()));
244 $formcontent .= html_writer::end_tag('div');
246 // Put everything into one tag 'options'
247 $formcontent .= html_writer::start_tag('div', array('class' => 'options'));
248 $formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
249 array('class' => 'instruction'));
250 // Put all options into one tag 'alloptions' to allow us to handle scrolling
251 $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
254 $activities = array_filter($modules, function($mod) {
255 return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);
257 if (count($activities)) {
258 $formcontent .= $this->course_modchooser_title('activities');
259 $formcontent .= $this->course_modchooser_module_types($activities);
263 $resources = array_filter($modules, function($mod) {
264 return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);
266 if (count($resources)) {
267 $formcontent .= $this->course_modchooser_title('resources');
268 $formcontent .= $this->course_modchooser_module_types($resources);
271 $formcontent .= html_writer::end_tag('div'); // modoptions
272 $formcontent .= html_writer::end_tag('div'); // types
274 $formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
275 $formcontent .= html_writer::tag('input', '',
276 array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add')));
277 $formcontent .= html_writer::tag('input', '',
278 array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel')));
279 $formcontent .= html_writer::end_tag('div');
280 $formcontent .= html_writer::end_tag('form');
282 // Wrap the whole form in a div
283 $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
285 // Put all of the content together
286 $content = $formcontent;
288 $content = html_writer::tag('div', $content, array('class' => 'choosercontainer'));
289 return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody'));
293 * Build the HTML for a specified set of modules
295 * @param array $modules A set of modules as used by the
296 * course_modchooser_module function
297 * @return string The composed HTML for the module
299 protected function course_modchooser_module_types($modules) {
301 foreach ($modules as $module) {
302 if (!isset($module->types)) {
303 $return .= $this->course_modchooser_module($module);
305 $return .= $this->course_modchooser_module($module, array('nonoption'));
306 foreach ($module->types as $type) {
307 $return .= $this->course_modchooser_module($type, array('option', 'subtype'));
315 * Return the HTML for the specified module adding any required classes
317 * @param object $module An object containing the title, and link. An
318 * icon, and help text may optionally be specified. If the module
319 * contains subtypes in the types option, then these will also be
321 * @param array $classes Additional classes to add to the encompassing
323 * @return string The composed HTML for the module
325 protected function course_modchooser_module($module, $classes = array('option')) {
327 $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
328 $output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
329 if (!isset($module->types)) {
330 $output .= html_writer::tag('input', '', array('type' => 'radio',
331 'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
334 $output .= html_writer::start_tag('span', array('class' => 'modicon'));
335 if (isset($module->icon)) {
336 // Add an icon if we have one
337 $output .= $module->icon;
339 $output .= html_writer::end_tag('span');
341 $output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
342 if (!isset($module->help)) {
344 $module->help = get_string('nohelpforactivityorresource', 'moodle');
347 // Format the help text using markdown with the following options
348 $options = new stdClass();
349 $options->trusted = false;
350 $options->noclean = false;
351 $options->smiley = false;
352 $options->filter = false;
353 $options->para = true;
354 $options->newlines = false;
355 $options->overflowdiv = false;
356 $module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
357 $output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
358 $output .= html_writer::end_tag('label');
359 $output .= html_writer::end_tag('div');
364 protected function course_modchooser_title($title, $identifier = null) {
365 $module = new stdClass();
366 $module->name = $title;
367 $module->types = array();
368 $module->title = get_string($title, $identifier);
370 return $this->course_modchooser_module($module, array('moduletypetitle'));
374 * Renders HTML for displaying the sequence of course module editing buttons
376 * @see course_get_cm_edit_actions()
378 * @param array $actions array of action_link or pix_icon objects
381 public function course_section_cm_edit_actions($actions) {
382 $output = html_writer::start_tag('span', array('class' => 'commands'));
383 foreach ($actions as $action) {
384 if ($action instanceof renderable) {
385 $output .= $this->output->render($action);
390 $output .= html_writer::end_tag('span');