-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
176 lines (159 loc) · 7.09 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
// This file is part of the Moodle block "EJSApp File Browser"
//
// EJSApp File Browser is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EJSApp File Browser is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//
// EJSApp File Browser has been developed by:
// - Luis de la Torre: ldelatorre@dia.uned.es
// - Ruben Heradio: rheradio@issi.uned.es
//
// at the Computer Science and Automatic Control, Spanish Open University
// (UNED), Madrid, Spain.
/**
* Prints the private files tree
*
* @package block_ejsapp_file_browser
* @copyright 2012 Luis de la Torre and Ruben Heradio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Prints the private files tree
* @copyright 2012 Luis de la Torre and Ruben Heradio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_ejsapp_file_browser_renderer extends plugin_renderer_base {
/**
* Prints ejsapp file browser tree view
* @return string Html code that prints the tree view
*/
public function ejsapp_file_browser_tree() {
return $this->render(new ejsapp_file_browser_tree());
}
/**
* Prints ejsapp file browser tree view
* @param ejsapp_file_browser_tree $tree
* @return string Html code that prints the tree view
* @throws dml_exception
* @throws moodle_exception
*/
public function render_ejsapp_file_browser_tree(ejsapp_file_browser_tree $tree) {
global $CFG, $PAGE;
$htmlid = 'ejsapp_file_browser_tree';
$url = $CFG->wwwroot . '/blocks/ejsapp_file_browser/refresh_tree.php';
$this->page->requires->js_init_call('M.block_ejsapp_file_browser.init_reload', array($url, $htmlid));
if (substr_count($PAGE->url, '/mod/ejsapp/view.php') > 0) {
$this->page->requires->js_init_call('M.block_ejsapp_file_browser.init_auto_refresh',
array($url, $htmlid, get_config('block_ejsapp_file_browser', 'Auto_refresh')));
}
$this->page->requires->js_init_call('M.block_ejsapp_file_browser.init_tree', array(false, $htmlid));
$html = htmllize_tree($tree, $tree->dir);
$html = html_writer::div($html, '', array('id' => $htmlid));
return $html;
}
}
/**
* Auxiliary class to print the private files tree
* @copyright 2012 Luis de la Torre and Ruben Heradio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ejsapp_file_browser_tree implements renderable {
/**
* @var $context
*/
public $context;
/**
* @var $dir
*/
public $dir;
/**
* __construct
*/
public function __construct() {
global $USER;
$this->context = context_user::instance($USER->id);
$fs = get_file_storage();
$this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
}
}
/**
* Public function - creates htmls structure suitable for YUI tree
*
* @param ejsapp_file_browser_tree $tree
* @param array $dir
* @return string result
* @throws dml_exception
* @throws moodle_exception
*/
function htmllize_tree($tree, $dir) {
global $CFG, $OUTPUT, $DB, $USER;
$yuiconfig = array();
$yuiconfig['type'] = 'html';
if (empty($dir['subdirs']) and empty($dir['files'])) {
return '';
}
$result = '';
foreach ($dir['subdirs'] as $subdir) {
$image = $OUTPUT->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class' => 'icon'));
$result .= html_writer::tag('li', html_writer::div($image.s($subdir['dirname'])) .
htmllize_tree($tree, $subdir), array('yuiConfig' => json_encode($yuiconfig)));
}
foreach ($dir['files'] as $file) {
$filename = $file->get_filename();
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// Get $ejsappid.
$frecord = $DB->get_record('files', array('filename' => $filename, 'component' => 'user',
'filearea' => 'private', 'userid' => ($USER->id)));
if (!$frecord) {
$source = array();
} else {
preg_match('/ejsappid=(\d+)/', $frecord->source, $source);
}
if (!empty($source) && ($extension == 'json' || $extension == 'rec' || $extension == 'blk')) { // An ejs state, recording or blockly file.
$ejsappid = $source[1];
$ejsapprecord = $DB->get_record('ejsapp', array('id' => $ejsappid));
if ($ejsapprecord) {
if ($extension == 'json') {
$image = '<img class="icon" src="' . $CFG->wwwroot .
'/blocks/ejsapp_file_browser/pix/icon_state.svg' . '"/>';
$url = $CFG->wwwroot . "/mod/ejsapp/view.php?n=" . $ejsappid . "&state_file=" . $frecord->contextid .
"/mod_ejsapp/" . $frecord->filearea . "/" . $frecord->itemid . "/" . $frecord->filename;
} else if ($extension == 'rec') {
$image = '<img class="icon" src="' . $CFG->wwwroot .
'/blocks/ejsapp_file_browser/pix/icon_recording.svg' . '"/>';
$url = $CFG->wwwroot . "/mod/ejsapp/view.php?n=" . $ejsappid . "&rec_file=" . $frecord->contextid .
"/mod_ejsapp/" . $frecord->filearea . "/" . $frecord->itemid . "/" . $frecord->filename;
} else if ($extension == 'blk') {
$image = '<img class="icon" src="' . $CFG->wwwroot .
'/blocks/ejsapp_file_browser/pix/icon_blockly.svg' . '"/>';
$url = $CFG->wwwroot . "/mod/ejsapp/view.php?n=" . $ejsappid . "&blk_file=" . $frecord->contextid .
"/mod_ejsapp/" . $frecord->filearea . "/" . $frecord->itemid . "/" . $frecord->filename;
}
}
} else { // A non-state, non-blockly, non-recording, file.
$image = $OUTPUT->pix_icon(file_file_icon($file), $filename, 'moodle', array('class' => 'icon'));
$url = new moodle_url('/pluginfile.php/' . $tree->context->id . '/user/private' .
$file->get_filepath() . $file->get_filename());
}
if (isset($url) && isset($image)) {
if ($DB->record_exists('block_ejsapp_shared_files', array('sharedfileid' => $frecord->id))) {
$image .= html_writer::tag('i', '', array('class' => 'fa fa-share-alt', 'aria-hidden' => 'true')) . ' ';
}
$result .= html_writer::tag('li', html_writer::tag('div', html_writer::link($url, $image . $filename)),
array('yuiConfig' => json_encode($yuiconfig)));
}
}
$result = html_writer::tag('ul', $result);
return $result;
}