MDL-43266 testing: avoid assoc arrays for json
[moodle.git] / mod / wiki / renderer.php
blob48f72efb633d3de82d00fa4e5dede895dddadd9e
1 <?php
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.
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/>.
18 /**
19 * Moodle Wiki 2.0 Renderer
21 * @package mod-wiki
22 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 class mod_wiki_renderer extends plugin_renderer_base {
29 public function page_index() {
30 global $CFG;
31 $output = '';
32 // Checking wiki instance
33 if (!$wiki = wiki_get_wiki($this->page->cm->instance)) {
34 return false;
37 // @TODO: Fix call to wiki_get_subwiki_by_group
38 $gid = groups_get_activity_group($this->page->cm);
39 $gid = !empty($gid) ? $gid : 0;
40 if (!$subwiki = wiki_get_subwiki_by_group($this->page->cm->instance, $gid)) {
41 return false;
43 $swid = $subwiki->id;
44 $pages = wiki_get_page_list($swid);
45 $selectoptions = array();
46 foreach ($pages as $page) {
47 $selectoptions[$page->id] = format_string($page->title, true, array('context' => $this->page->context));
49 $label = get_string('pageindex', 'wiki') . ': ';
50 $select = new single_select(new moodle_url('/mod/wiki/view.php'), 'pageid', $selectoptions);
51 $select->label = $label;
52 return $this->output->container($this->output->render($select), 'wiki_index');
55 public function search_result($records, $subwiki) {
56 global $CFG, $PAGE;
57 $table = new html_table();
58 $context = context_module::instance($PAGE->cm->id);
59 $strsearchresults = get_string('searchresult', 'wiki');
60 $totalcount = count($records);
61 $html = $this->output->heading("$strsearchresults $totalcount");
62 foreach ($records as $page) {
63 $table->head = array('title' => format_string($page->title) . ' (' . html_writer::link($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, get_string('view', 'wiki')) . ')');
64 $table->align = array('title' => 'left');
65 $table->width = '100%';
66 $table->data = array(array(file_rewrite_pluginfile_urls(format_text($page->cachedcontent, FORMAT_HTML), 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $subwiki->id)));
67 $table->colclasses = array('wikisearchresults');
68 $html .= html_writer::table($table);
70 $html = html_writer::tag('div', $html, array('class'=>'no-overflow'));
71 return $this->output->container($html);
74 public function diff($pageid, $old, $new, $options = array()) {
75 global $CFG;
76 if (!empty($options['total'])) {
77 $total = $options['total'];
78 } else {
79 $total = 0;
81 $diff1 = format_text($old->diff, FORMAT_HTML, array('overflowdiv'=>true));
82 $diff2 = format_text($new->diff, FORMAT_HTML, array('overflowdiv'=>true));
83 $strdatetime = get_string('strftimedatetime', 'langconfig');
85 $olduser = $old->user;
86 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $old->id));
87 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $pageid, 'versionid' => $old->id));
88 $userlink = new moodle_url('/user/view.php', array('id' => $olduser->id));
89 // view version link
90 $oldversionview = ' ';
91 $oldversionview .= html_writer::link($versionlink->out(false), get_string('view', 'wiki'), array('class' => 'wiki_diffview'));
92 $oldversionview .= ' ';
93 // restore version link
94 $oldversionview .= html_writer::link($restorelink->out(false), get_string('restore', 'wiki'), array('class' => 'wiki_diffview'));
96 // userinfo container
97 $oldheading = $this->output->container_start('wiki_diffuserleft');
98 // username
99 $oldheading .= html_writer::link($CFG->wwwroot . '/user/view.php?id=' . $olduser->id, fullname($olduser)) . '&nbsp;';
100 // user picture
101 $oldheading .= html_writer::link($userlink->out(false), $this->output->user_picture($olduser, array('popup' => true)), array('class' => 'notunderlined'));
102 $oldheading .= $this->output->container_end();
104 // version number container
105 $oldheading .= $this->output->container_start('wiki_diffversion');
106 $oldheading .= get_string('version') . ' ' . $old->version . $oldversionview;
107 $oldheading .= $this->output->container_end();
108 // userdate container
109 $oldheading .= $this->output->container_start('wiki_difftime');
110 $oldheading .= userdate($old->timecreated, $strdatetime);
111 $oldheading .= $this->output->container_end();
113 $newuser = $new->user;
114 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $new->id));
115 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $pageid, 'versionid' => $new->id));
116 $userlink = new moodle_url('/user/view.php', array('id' => $newuser->id));
118 $newversionview = ' ';
119 $newversionview .= html_writer::link($versionlink->out(false), get_string('view', 'wiki'), array('class' => 'wiki_diffview'));
120 // new user info
121 $newheading = $this->output->container_start('wiki_diffuserright');
122 $newheading .= $this->output->user_picture($newuser, array('popup' => true));
124 $newheading .= html_writer::link($userlink->out(false), fullname($newuser), array('class' => 'notunderlined'));
125 $newheading .= $this->output->container_end();
127 // version
128 $newheading .= $this->output->container_start('wiki_diffversion');
129 $newheading .= get_string('version') . '&nbsp;' . $new->version . $newversionview;
130 $newheading .= $this->output->container_end();
131 // userdate
132 $newheading .= $this->output->container_start('wiki_difftime');
133 $newheading .= userdate($new->timecreated, $strdatetime);
134 $newheading .= $this->output->container_end();
136 $oldheading = html_writer::tag('div', $oldheading, array('class'=>'wiki-diff-heading header clearfix'));
137 $newheading = html_writer::tag('div', $newheading, array('class'=>'wiki-diff-heading header clearfix'));
139 $output = '';
140 $output .= html_writer::start_tag('div', array('class'=>'wiki-diff-container clearfix'));
141 $output .= html_writer::tag('div', $oldheading.$diff1, array('class'=>'wiki-diff-leftside'));
142 $output .= html_writer::tag('div', $newheading.$diff2, array('class'=>'wiki-diff-rightside'));
143 $output .= html_writer::end_tag('div');
145 if (!empty($total)) {
146 $output .= '<div class="wiki_diff_paging">';
147 $output .= $this->output->container($this->diff_paging_bar(1, $new->version - 1, $old->version, $CFG->wwwroot . '/mod/wiki/diff.php?pageid=' . $pageid . '&amp;comparewith=' . $new->version . '&amp;', 'compare', false, true), 'wiki_diff_oldpaging');
148 $output .= $this->output->container($this->diff_paging_bar($old->version + 1, $total, $new->version, $CFG->wwwroot . '/mod/wiki/diff.php?pageid=' . $pageid . '&amp;compare=' . $old->version . '&amp;', 'comparewith', false, true), 'wiki_diff_newpaging');
149 $output .= '</div>';
152 return $output;
156 * Prints a single paging bar to provide access to other versions
158 * @param int $minpage First page to be displayed in the bar
159 * @param int $maxpage Last page to be displayed in the bar
160 * @param int $page The page you are currently viewing
161 * @param mixed $baseurl If this is a string then it is the url which will be appended with $pagevar, an equals sign and the page number.
162 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
163 * @param string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
164 * @param bool $nocurr do not display the current page as a link
165 * @param bool $return whether to return an output string or echo now
166 * @return bool or string
168 public function diff_paging_bar($minpage, $maxpage, $page, $baseurl, $pagevar = 'page', $nocurr = false) {
169 $totalcount = $maxpage - $minpage;
170 $maxdisplay = 2;
171 $output = '';
173 if ($totalcount > 0) {
174 $output .= '<div class="paging">';
175 $output .= get_string('version', 'wiki') . ':';
176 if ($page - $minpage > 0) {
177 $pagenum = $page - 1;
178 if (!is_a($baseurl, 'moodle_url')) {
179 $output .= '&nbsp;(<a class="previous" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string('previous') . '</a>)&nbsp;';
180 } else {
181 $output .= '&nbsp;(<a class="previous" href="' . $baseurl->out(false, array($pagevar => $pagenum)) . '">' . get_string('previous') . '</a>)&nbsp;';
185 if ($page - $minpage > 4) {
186 $startpage = $page - 3;
187 if (!is_a($baseurl, 'moodle_url')) {
188 $output .= '&nbsp;<a href="' . $baseurl . $pagevar . '=' . $minpage . '">' . $minpage . '</a>&nbsp;...';
189 } else {
190 $output .= '&nbsp;<a href="' . $baseurl->out(false, array($pagevar => $minpage)) . '">' . $minpage . '</a>&nbsp;...';
192 } else {
193 $startpage = $minpage;
195 $currpage = $startpage;
196 $displaycount = 0;
197 while ($displaycount < $maxdisplay and $currpage <= $maxpage) {
198 if ($page == $currpage && empty($nocurr)) {
199 $output .= '&nbsp;&nbsp;' . $currpage;
200 } else {
201 if (!is_a($baseurl, 'moodle_url')) {
202 $output .= '&nbsp;&nbsp;<a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $currpage . '</a>';
203 } else {
204 $output .= '&nbsp;&nbsp;<a href="' . $baseurl->out(false, array($pagevar => $currpage)) . '">' . $currpage . '</a>';
208 $displaycount++;
209 $currpage++;
211 if ($currpage < $maxpage) {
212 if (!is_a($baseurl, 'moodle_url')) {
213 $output .= '&nbsp;...<a href="' . $baseurl . $pagevar . '=' . $maxpage . '">' . $maxpage . '</a>&nbsp;';
214 } else {
215 $output .= '&nbsp;...<a href="' . $baseurl->out(false, array($pagevar => $maxpage)) . '">' . $maxpage . '</a>&nbsp;';
217 } else if ($currpage == $maxpage) {
218 if (!is_a($baseurl, 'moodle_url')) {
219 $output .= '&nbsp;&nbsp;<a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $currpage . '</a>';
220 } else {
221 $output .= '&nbsp;&nbsp;<a href="' . $baseurl->out(false, array($pagevar => $currpage)) . '">' . $currpage . '</a>';
224 $pagenum = $page + 1;
225 if ($page != $maxpage) {
226 if (!is_a($baseurl, 'moodle_url')) {
227 $output .= '&nbsp;&nbsp;(<a class="next" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string('next') . '</a>)';
228 } else {
229 $output .= '&nbsp;&nbsp;(<a class="next" href="' . $baseurl->out(false, array($pagevar => $pagenum)) . '">' . get_string('next') . '</a>)';
232 $output .= '</div>';
235 return $output;
237 public function wiki_info() {
238 global $PAGE;
239 return $this->output->box(format_module_intro('wiki', $this->page->activityrecord, $PAGE->cm->id), 'generalbox', 'intro');
242 public function tabs($page, $tabitems, $options) {
243 $tabs = array();
244 $context = context_module::instance($this->page->cm->id);
246 $pageid = null;
247 if (!empty($page)) {
248 $pageid = $page->id;
251 $selected = $options['activetab'];
253 // make specific tab linked even it is active
254 if (!empty($options['linkedwhenactive'])) {
255 $linked = $options['linkedwhenactive'];
256 } else {
257 $linked = '';
260 if (!empty($options['inactivetabs'])) {
261 $inactive = $options['inactivetabs'];
262 } else {
263 $inactive = array();
266 foreach ($tabitems as $tab) {
267 if ($tab == 'edit' && !has_capability('mod/wiki:editpage', $context)) {
268 continue;
270 if ($tab == 'comments' && !has_capability('mod/wiki:viewcomment', $context)) {
271 continue;
273 if ($tab == 'files' && !has_capability('mod/wiki:viewpage', $context)) {
274 continue;
276 if (($tab == 'view' || $tab == 'map' || $tab == 'history') && !has_capability('mod/wiki:viewpage', $context)) {
277 continue;
279 if ($tab == 'admin' && !has_capability('mod/wiki:managewiki', $context)) {
280 continue;
282 $link = new moodle_url('/mod/wiki/'. $tab. '.php', array('pageid' => $pageid));
283 if ($linked == $tab) {
284 $tabs[] = new tabobject($tab, $link, get_string($tab, 'wiki'), '', true);
285 } else {
286 $tabs[] = new tabobject($tab, $link, get_string($tab, 'wiki'));
290 return $this->tabtree($tabs, $selected, $inactive);
293 public function prettyview_link($page) {
294 $html = '';
295 $link = new moodle_url('/mod/wiki/prettyview.php', array('pageid' => $page->id));
296 $html .= $this->output->container_start('wiki_right');
297 $html .= $this->output->action_link($link, get_string('prettyprint', 'wiki'), new popup_action('click', $link));
298 $html .= $this->output->container_end();
299 return $html;
302 public function wiki_print_subwiki_selector($wiki, $subwiki, $page, $pagetype = 'view') {
303 global $CFG, $USER;
304 require_once($CFG->dirroot . '/user/lib.php');
305 switch ($pagetype) {
306 case 'files':
307 $baseurl = new moodle_url('/mod/wiki/files.php');
308 break;
309 case 'view':
310 default:
311 $baseurl = new moodle_url('/mod/wiki/view.php');
312 break;
315 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
316 $context = context_module::instance($cm->id);
317 // @TODO: A plenty of duplicated code below this lines.
318 // Create private functions.
319 switch (groups_get_activity_groupmode($cm)) {
320 case NOGROUPS:
321 if ($wiki->wikimode == 'collaborative') {
322 // No need to print anything
323 return;
324 } else if ($wiki->wikimode == 'individual') {
325 // We have private wikis here
327 $view = has_capability('mod/wiki:viewpage', $context);
328 $manage = has_capability('mod/wiki:managewiki', $context);
330 // Only people with these capabilities can view all wikis
331 if ($view && $manage) {
332 // @TODO: Print here a combo that contains all users.
333 $users = get_enrolled_users($context);
334 $options = array();
335 foreach ($users as $user) {
336 $options[$user->id] = fullname($user);
339 echo $this->output->container_start('wiki_right');
340 $params = array('wid' => $wiki->id, 'title' => $page->title);
341 if ($pagetype == 'files') {
342 $params['pageid'] = $page->id;
344 $baseurl->params($params);
345 $name = 'uid';
346 $selected = $subwiki->userid;
347 echo $this->output->single_select($baseurl, $name, $options, $selected);
348 echo $this->output->container_end();
350 return;
351 } else {
352 // error
353 return;
355 case SEPARATEGROUPS:
356 if ($wiki->wikimode == 'collaborative') {
357 // We need to print a select to choose a course group
359 $params = array('wid'=>$wiki->id, 'title'=>$page->title);
360 if ($pagetype == 'files') {
361 $params['pageid'] = $page->id;
363 $baseurl->params($params);
365 echo $this->output->container_start('wiki_right');
366 groups_print_activity_menu($cm, $baseurl);
367 echo $this->output->container_end();
368 return;
369 } else if ($wiki->wikimode == 'individual') {
370 // @TODO: Print here a combo that contains all users of that subwiki.
371 $view = has_capability('mod/wiki:viewpage', $context);
372 $manage = has_capability('mod/wiki:managewiki', $context);
374 // Only people with these capabilities can view all wikis
375 if ($view && $manage) {
376 $users = get_enrolled_users($context);
377 $options = array();
378 foreach ($users as $user) {
379 $groups = groups_get_all_groups($cm->course, $user->id);
380 if (!empty($groups)) {
381 foreach ($groups as $group) {
382 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user);
384 } else {
385 $name = get_string('notingroup', 'wiki');
386 $options[0][$name]['0' . '-' . $user->id] = fullname($user);
389 } else {
390 $group = groups_get_group($subwiki->groupid);
391 if (!$group) {
392 return;
394 $users = groups_get_members($subwiki->groupid);
395 foreach ($users as $user) {
396 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user);
399 echo $this->output->container_start('wiki_right');
400 $params = array('wid' => $wiki->id, 'title' => $page->title);
401 if ($pagetype == 'files') {
402 $params['pageid'] = $page->id;
404 $baseurl->params($params);
405 $name = 'groupanduser';
406 $selected = $subwiki->groupid . '-' . $subwiki->userid;
407 echo $this->output->single_select($baseurl, $name, $options, $selected);
408 echo $this->output->container_end();
410 return;
412 } else {
413 // error
414 return;
416 CASE VISIBLEGROUPS:
417 if ($wiki->wikimode == 'collaborative') {
418 // We need to print a select to choose a course group
419 // moodle_url will take care of encoding for us
420 $params = array('wid'=>$wiki->id, 'title'=>$page->title);
421 if ($pagetype == 'files') {
422 $params['pageid'] = $page->id;
424 $baseurl->params($params);
426 echo $this->output->container_start('wiki_right');
427 groups_print_activity_menu($cm, $baseurl);
428 echo $this->output->container_end();
429 return;
431 } else if ($wiki->wikimode == 'individual') {
432 $users = get_enrolled_users($context);
433 $options = array();
434 foreach ($users as $user) {
435 $groups = groups_get_all_groups($cm->course, $user->id);
436 if (!empty($groups)) {
437 foreach ($groups as $group) {
438 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user);
440 } else {
441 $name = get_string('notingroup', 'wiki');
442 $options[0][$name]['0' . '-' . $user->id] = fullname($user);
446 echo $this->output->container_start('wiki_right');
447 $params = array('wid' => $wiki->id, 'title' => $page->title);
448 if ($pagetype == 'files') {
449 $params['pageid'] = $page->id;
451 $baseurl->params($params);
452 $name = 'groupanduser';
453 $selected = $subwiki->groupid . '-' . $subwiki->userid;
454 echo $this->output->single_select($baseurl, $name, $options, $selected);
455 echo $this->output->container_end();
457 return;
459 } else {
460 // error
461 return;
463 default:
464 // error
465 return;
471 function menu_map($pageid, $currentselect) {
472 $options = array('contributions', 'links', 'orphaned', 'pageindex', 'pagelist', 'updatedpages');
473 $items = array();
474 foreach ($options as $opt) {
475 $items[] = get_string($opt, 'wiki');
477 $selectoptions = array();
478 foreach ($items as $key => $item) {
479 $selectoptions[$key + 1] = $item;
481 $select = new single_select(new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid)), 'option', $selectoptions, $currentselect);
482 $select->label = get_string('mapmenu', 'wiki') . ': ';
483 return $this->output->container($this->output->render($select), 'midpad');
485 public function wiki_files_tree($context, $subwiki) {
486 return $this->render(new wiki_files_tree($context, $subwiki));
488 public function render_wiki_files_tree(wiki_files_tree $tree) {
489 if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
490 $html = $this->output->box(get_string('nofilesavailable', 'repository'));
491 } else {
492 $htmlid = 'wiki_files_tree_'.uniqid();
493 $module = array('name'=>'mod_wiki', 'fullpath'=>'/mod/wiki/module.js');
494 $this->page->requires->js_init_call('M.mod_wiki.init_tree', array(false, $htmlid), false, $module);
495 $html = '<div id="'.$htmlid.'">';
496 $html .= $this->htmllize_tree($tree, $tree->dir);
497 $html .= '</div>';
499 return $html;
502 function menu_admin($pageid, $currentselect) {
503 $options = array('removepages', 'deleteversions');
504 $items = array();
505 foreach ($options as $opt) {
506 $items[] = get_string($opt, 'wiki');
508 $selectoptions = array();
509 foreach ($items as $key => $item) {
510 $selectoptions[$key + 1] = $item;
512 $select = new single_select(new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid)), 'option', $selectoptions, $currentselect);
513 $select->label = get_string('adminmenu', 'wiki') . ': ';
514 return $this->output->container($this->output->render($select), 'midpad');
518 * Internal function - creates htmls structure suitable for YUI tree.
520 protected function htmllize_tree($tree, $dir) {
521 global $CFG;
522 $yuiconfig = array();
523 $yuiconfig['type'] = 'html';
525 if (empty($dir['subdirs']) and empty($dir['files'])) {
526 return '';
528 $result = '<ul>';
529 foreach ($dir['subdirs'] as $subdir) {
530 $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
531 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
533 foreach ($dir['files'] as $file) {
534 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/mod_wiki/attachments/' . $tree->subwiki->id . '/'. $file->get_filepath() . $file->get_filename(), true);
535 $filename = $file->get_filename();
536 $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
537 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
539 $result .= '</ul>';
541 return $result;
545 class wiki_files_tree implements renderable {
546 public $context;
547 public $dir;
548 public $subwiki;
549 public function __construct($context, $subwiki) {
550 $fs = get_file_storage();
551 $this->context = $context;
552 $this->subwiki = $subwiki;
553 $this->dir = $fs->get_area_tree($context->id, 'mod_wiki', 'attachments', $subwiki->id);