MDL-32888 Grader report: use SESSION object and DB functions
[moodle.git] / user / renderer.php
CommitLineData
82af55d7
MD
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/**
20 * Print private files tree
21 *
22 * @package core_user
23 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30class core_user_renderer extends plugin_renderer_base {
31
32 /**
33 * Prints user files tree view
34 * @return string
35 */
36 public function user_files_tree() {
37 return $this->render(new user_files_tree);
38 }
39
40 public function render_user_files_tree(user_files_tree $tree) {
d0257a09
DC
41 if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
42 $html = $this->output->box(get_string('nofilesavailable', 'repository'));
43 } else {
44 $htmlid = 'user_files_tree_'.uniqid();
45 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
46 $this->page->requires->js_init_call('M.core_user.init_tree', array(false, $htmlid), false, $module);
47 $html = '<div id="'.$htmlid.'">';
48 $html .= $this->htmllize_tree($tree, $tree->dir);
49 $html .= '</div>';
50 }
82af55d7
MD
51 return $html;
52 }
53
54 /**
55 * Internal function - creates htmls structure suitable for YUI tree.
56 */
57 protected function htmllize_tree($tree, $dir) {
58 global $CFG;
59 $yuiconfig = array();
60 $yuiconfig['type'] = 'html';
61
62 if (empty($dir['subdirs']) and empty($dir['files'])) {
63 return '';
64 }
65 $result = '<ul>';
66 foreach ($dir['subdirs'] as $subdir) {
559276b1 67 $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
82af55d7
MD
68 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
69 }
70 foreach ($dir['files'] as $file) {
71 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
72 $filename = $file->get_filename();
559276b1 73 $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
82af55d7
MD
74 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
75 }
76 $result .= '</ul>';
77
78 return $result;
79 }
cbe8e5b3
MA
80
81 /**
82 * Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
83 * Prints a header with a title and the number of users found within that subset
84 * @param string $url the url to return to, complete with any parameters needed for the return
cbe8e5b3
MA
85 * @param string $firstinitial the first initial of the firstname
86 * @param string $lastinitial the first initial of the lastname
87 * @param int $usercount the amount of users meeting the search criteria
88 * @param int $totalcount the amount of users of the set/subset being searched
89 * @param string $heading heading of the subset being searched, default is All Participants
90 * @return string html output
91 */
41877849 92 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
cbe8e5b3
MA
93 global $OUTPUT;
94
95 $strall = get_string('all');
96 $alpha = explode(',', get_string('alphabet', 'langconfig'));
97
98 if (!isset($heading)) {
99 $heading = get_string('allparticipants');
100 }
101
102 $content = html_writer::start_tag('form', array('action' => new moodle_url($url)));
103 $content .= html_writer::start_tag('div');
104
41877849 105 // Search utility heading.
cbe8e5b3
MA
106 $content .= $OUTPUT->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
107
41877849 108 // Bar of first initials.
cbe8e5b3
MA
109 $content .= html_writer::start_tag('div', array('class' => 'initialbar firstinitial'));
110 $content .= html_writer::label(get_string('firstname').' : ', null);
111
112 if (!empty($firstinitial)) {
113 $content .= html_writer::link($url.'&sifirst=', $strall);
114 } else {
115 $content .= html_writer::tag('strong', $strall);
116 }
117
118 foreach ($alpha as $letter) {
119 if ($letter == $firstinitial) {
120 $content .= html_writer::tag('strong', $letter);
121 } else {
122 $content .= html_writer::link($url.'&sifirst='.$letter, $letter);
123 }
124 }
125 $content .= html_writer::end_tag('div');
126
127 // Bar of last initials
128 $content .= html_writer::start_tag('div', array('class' => 'initialbar lastinitial'));
129 $content .= html_writer::label(get_string('lastname').' : ', null);
130
131 if (!empty($lastinitial)) {
132 $content .= html_writer::link($url.'&silast=', $strall);
133 } else {
134 $content .= html_writer::tag('strong', $strall);
135 }
136
137 foreach ($alpha as $letter) {
138 if ($letter == $lastinitial) {
139 $content .= html_writer::tag('strong', $letter);
140 } else {
141 $content .= html_writer::link($url.'&silast='.$letter, $letter);
142 }
143 }
144 $content .= html_writer::end_tag('div');
145
cbe8e5b3
MA
146 $content .= html_writer::end_tag('div');
147 $content .= html_writer::tag('div', '&nbsp');
148 $content .= html_writer::end_tag('form');
149
150 return $content;
151 }
152
82af55d7
MD
153}
154
155class user_files_tree implements renderable {
156 public $context;
157 public $dir;
158 public function __construct() {
159 global $USER;
43731030 160 $this->context = context_user::instance($USER->id);
82af55d7
MD
161 $fs = get_file_storage();
162 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
163 }
164}