weekly release 2.4dev
[moodle.git] / course / renderer.php
CommitLineData
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 */
35class 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
54 /**
55 * Renderers a structured array of courses and categories into a nice
56 * XHTML tree structure.
57 *
58 * This method was designed initially to display the front page course/category
59 * combo view. The structure can be retrieved by get_course_category_tree()
60 *
61 * @param array $structure
62 * @return string
63 */
64 public function course_category_tree(array $structure) {
24e27ac0
SH
65 $this->strings->summary = get_string('summary');
66
67 // Generate an id and the required JS call to make this a nice widget
68 $id = html_writer::random_id('course_category_tree');
c8ffba95 69 $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
70
71 // Start content generation
72 $content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id));
73 foreach ($structure as $category) {
74 $content .= $this->course_category_tree_category($category);
75 }
76 $content .= html_writer::start_tag('div', array('class'=>'controls'));
77 $content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall'));
78 $content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall'));
79 $content .= html_writer::end_tag('div');
80 $content .= html_writer::end_tag('div');
df997f84 81
24e27ac0
SH
82 // Return the course category tree HTML
83 return $content;
84 }
85
86 /**
87 * Renderers a category for use with course_category_tree
88 *
89 * @param array $category
90 * @param int $depth
91 * @return string
92 */
93 protected function course_category_tree_category(stdClass $category, $depth=1) {
94 $content = '';
3754f4f3
ARN
95 $hassubcategories = (isset($category->categories) && count($category->categories)>0);
96 $hascourses = (isset($category->courses) && count($category->courses)>0);
24e27ac0
SH
97 $classes = array('category');
98 if ($category->parent != 0) {
99 $classes[] = 'subcategory';
100 }
d703f226
SH
101 if (empty($category->visible)) {
102 $classes[] = 'dimmed_category';
103 }
24e27ac0
SH
104 if ($hassubcategories || $hascourses) {
105 $classes[] = 'with_children';
106 if ($depth > 1) {
107 $classes[] = 'collapsed';
108 }
109 }
63390481
SH
110 $categoryname = format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id)));
111
24e27ac0
SH
112 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
113 $content .= html_writer::start_tag('div', array('class'=>'category_label'));
63390481 114 $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link'));
24e27ac0
SH
115 $content .= html_writer::end_tag('div');
116 if ($hassubcategories) {
117 $content .= html_writer::start_tag('div', array('class'=>'subcategories'));
118 foreach ($category->categories as $subcategory) {
119 $content .= $this->course_category_tree_category($subcategory, $depth+1);
120 }
121 $content .= html_writer::end_tag('div');
122 }
123 if ($hascourses) {
124 $content .= html_writer::start_tag('div', array('class'=>'courses'));
125 $coursecount = 0;
126 foreach ($category->courses as $course) {
127 $classes = array('course');
d2fd38be
PS
128 $linkclass = 'course_link';
129 if (!$course->visible) {
130 $linkclass .= ' dimmed';
131 }
24e27ac0
SH
132 $coursecount ++;
133 $classes[] = ($coursecount%2)?'odd':'even';
134 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
f4c23f03 135 $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass));
24e27ac0
SH
136 $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix'));
137
bf423bb1
PS
138 // print enrol info
139 if ($icons = enrol_get_course_info_icons($course)) {
140 foreach ($icons as $pix_icon) {
141 $content .= $this->render($pix_icon);
142 }
143 }
144
24e27ac0
SH
145 if ($course->summary) {
146 $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary));
147 $content .= html_writer::link(new moodle_url('/course/info.php', array('id'=>$course->id)), $image, array('title'=>$this->strings->summary));
24e27ac0
SH
148 }
149 $content .= html_writer::end_tag('div');
150 $content .= html_writer::end_tag('div');
151 }
152 $content .= html_writer::end_tag('div');
153 }
154 $content .= html_writer::end_tag('div');
155 return $content;
156 }
01e0e704
ARN
157
158 /**
159 * Build the HTML for the module chooser javascript popup
160 *
161 * @param array $modules A set of modules as returned form @see
162 * get_module_metadata
163 * @param object $course The course that will be displayed
164 * @return string The composed HTML for the module
165 */
166 public function course_modchooser($modules, $course) {
167 global $OUTPUT;
168
169 // Add the header
1edff8c7 170 $header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
255dd8d1 171 array('class' => 'hd choosertitle'));
01e0e704
ARN
172
173 $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
174 'id' => 'chooserform', 'method' => 'post'));
175 $formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
176 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
177 'name' => 'course', 'value' => $course->id));
178 $formcontent .= html_writer::tag('input', '',
179 array('type' => 'hidden', 'id' => 'jump', 'name' => 'jump', 'value' => ''));
180 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
181 'value' => sesskey()));
182 $formcontent .= html_writer::end_tag('div');
183
184 // Put everything into one tag 'options'
185 $formcontent .= html_writer::start_tag('div', array('class' => 'options'));
186 $formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
187 array('class' => 'instruction'));
188 // Put all options into one tag 'alloptions' to allow us to handle scrolling
189 $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
190
48a5e55e 191 // Activities
01e0e704
ARN
192 $activities = array_filter($modules,
193 create_function('$mod', 'return ($mod->archetype !== MOD_CLASS_RESOURCE);'));
194 if (count($activities)) {
195 $formcontent .= $this->course_modchooser_title('activities');
196 $formcontent .= $this->course_modchooser_module_types($activities);
197 }
198
463ccee1 199 // Resources
48a5e55e
ARN
200 $resources = array_filter($modules,
201 create_function('$mod', 'return ($mod->archetype === MOD_CLASS_RESOURCE);'));
202 if (count($resources)) {
203 $formcontent .= $this->course_modchooser_title('resources');
204 $formcontent .= $this->course_modchooser_module_types($resources);
205 }
206
01e0e704
ARN
207 $formcontent .= html_writer::end_tag('div'); // modoptions
208 $formcontent .= html_writer::end_tag('div'); // types
209
210 $formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
af75421c
ARN
211 $formcontent .= html_writer::tag('input', '',
212 array('type' => 'submit', 'name' => 'submitbutton', 'id' => 'submitbutton', 'value' => get_string('add')));
8ff6c5ee
ARN
213 $formcontent .= html_writer::tag('input', '',
214 array('type' => 'submit', 'name' => 'addcancel', 'id' => 'addcancel', 'value' => get_string('cancel')));
01e0e704
ARN
215 $formcontent .= html_writer::end_tag('div');
216 $formcontent .= html_writer::end_tag('form');
217
218 // Wrap the whole form in a div
219 $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
220
221 // Put all of the content together
222 $content = $formcontent;
223
255dd8d1
ARN
224 $content = html_writer::tag('div', $content, array('class' => 'choosercontainer'));
225 return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody'));
01e0e704
ARN
226 }
227
228 /**
229 * Build the HTML for a specified set of modules
230 *
231 * @param array $modules A set of modules as used by the
232 * course_modchooser_module function
233 * @return string The composed HTML for the module
234 */
235 protected function course_modchooser_module_types($modules) {
236 $return = '';
237 foreach ($modules as $module) {
238 if (!isset($module->types)) {
239 $return .= $this->course_modchooser_module($module);
240 } else {
241 $return .= $this->course_modchooser_module($module, array('nonoption'));
242 foreach ($module->types as $type) {
243 $return .= $this->course_modchooser_module($type, array('option', 'subtype'));
244 }
245 }
246 }
247 return $return;
248 }
249
250 /**
251 * Return the HTML for the specified module adding any required classes
252 *
253 * @param object $module An object containing the title, and link. An
254 * icon, and help text may optionally be specified. If the module
255 * contains subtypes in the types option, then these will also be
256 * displayed.
257 * @param array $classes Additional classes to add to the encompassing
258 * div element
259 * @return string The composed HTML for the module
260 */
261 protected function course_modchooser_module($module, $classes = array('option')) {
262 $output = '';
263 $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
264 $output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
265 if (!isset($module->types)) {
266 $output .= html_writer::tag('input', '', array('type' => 'radio',
267 'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
268 }
269
270 $output .= html_writer::start_tag('span', array('class' => 'modicon'));
271 if (isset($module->icon)) {
272 // Add an icon if we have one
273 $output .= $module->icon;
274 }
275 $output .= html_writer::end_tag('span');
276
277 $output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
278 if (!isset($module->help)) {
279 // Add help if found
280 $module->help = get_string('nohelpforactivityorresource', 'moodle');
281 }
282
283 // Format the help text using markdown with the following options
284 $options = new stdClass();
285 $options->trusted = false;
286 $options->noclean = false;
287 $options->smiley = false;
288 $options->filter = false;
289 $options->para = true;
290 $options->newlines = false;
291 $options->overflowdiv = false;
292 $module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
293 $output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
294 $output .= html_writer::end_tag('label');
295 $output .= html_writer::end_tag('div');
296
297 return $output;
298 }
299
300 protected function course_modchooser_title($title, $identifier = null) {
301 $module = new stdClass();
302 $module->name = $title;
303 $module->types = array();
304 $module->title = get_string($title, $identifier);
305 $module->help = '';
306 return $this->course_modchooser_module($module, array('moduletypetitle'));
307 }
f4c23f03 308}