1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Configurable Reports
|
|
|
19 |
* A Moodle block for creating Configurable Reports
|
|
|
20 |
* @package blocks
|
|
|
21 |
* @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
|
|
|
22 |
* @date: 2009
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once("../../config.php");
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
$id = required_param('id', PARAM_INT);
|
|
|
31 |
$comp = required_param('comp', PARAM_ALPHA);
|
|
|
32 |
$cid = optional_param('cid', '', PARAM_ALPHANUM);
|
|
|
33 |
$pname = optional_param('pname', '', PARAM_ALPHA);
|
|
|
34 |
|
|
|
35 |
$moveup = optional_param('moveup', 0, PARAM_INT);
|
|
|
36 |
$movedown = optional_param('movedown', 0, PARAM_INT);
|
|
|
37 |
$delete = optional_param('delete', 0, PARAM_INT);
|
|
|
38 |
|
|
|
39 |
if (!$pname) {
|
|
|
40 |
redirect(new \moodle_url('/blocks/configurable_reports/editcomp.php', ['id' => $id, 'comp' => $comp]));
|
|
|
41 |
exit;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
if (!$report = $DB->get_record('block_configurable_reports', array('id' => $id))) {
|
|
|
45 |
print_error('reportdoesnotexists');
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if (!$course = $DB->get_record("course", ['id' => $report->courseid])) {
|
|
|
49 |
print_error("No such course id");
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// Force user login in course (SITE or Course).
|
|
|
53 |
if ($course->id == SITEID) {
|
|
|
54 |
require_login();
|
|
|
55 |
$context = context_system::instance();
|
|
|
56 |
} else {
|
|
|
57 |
require_login($course->id);
|
|
|
58 |
$context = context_course::instance($course->id);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$hasmanagereportcap = has_capability('block/configurable_reports:managereports', $context);
|
|
|
62 |
if (!$hasmanagereportcap && !has_capability('block/configurable_reports:manageownreports', $context)) {
|
|
|
63 |
print_error('badpermissions');
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (!$hasmanagereportcap && $report->ownerid != $USER->id) {
|
|
|
67 |
print_error('badpermissions');
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
71 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
|
|
|
72 |
|
|
|
73 |
$reportclassname = 'report_'.$report->type;
|
|
|
74 |
$reportclass = new $reportclassname($report->id);
|
|
|
75 |
|
|
|
76 |
if (!in_array($comp, $reportclass->components)) {
|
|
|
77 |
print_error('badcomponent');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$PAGE->set_context($context);
|
|
|
81 |
$PAGE->set_pagelayout('incourse');
|
|
|
82 |
$PAGE->set_url('/blocks/configurable_reports/editplugin.php', ['id' => $id, 'comp' => $comp, 'cid' => $cid, 'pname' => $pname]);
|
|
|
83 |
|
|
|
84 |
$cdata = null;
|
|
|
85 |
$plugin = '';
|
|
|
86 |
if (!$cid) {
|
|
|
87 |
if (filetype($CFG->dirroot.'/blocks/configurable_reports/components/'.$comp.'/'.$pname) == 'dir') {
|
|
|
88 |
$plugin = $pname;
|
|
|
89 |
}
|
|
|
90 |
} else {
|
|
|
91 |
$components = cr_unserialize($report->components);
|
|
|
92 |
$elements = isset($components[$comp]['elements']) ? $components[$comp]['elements'] : [];
|
|
|
93 |
|
|
|
94 |
if ($elements) {
|
|
|
95 |
foreach ($elements as $e) {
|
|
|
96 |
if ($e['id'] == $cid) {
|
|
|
97 |
$cdata = $e;
|
|
|
98 |
$plugin = $e['pluginname'];
|
|
|
99 |
break;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if (($moveup || $movedown || $delete) && confirm_sesskey()) {
|
|
|
105 |
foreach ($elements as $index => $e) {
|
|
|
106 |
if ($e['id'] == $cid) {
|
|
|
107 |
if ($delete) {
|
|
|
108 |
unset($elements[$index]);
|
|
|
109 |
break;
|
|
|
110 |
}
|
|
|
111 |
$newindex = ($moveup) ? $index - 1 : $index + 1;
|
|
|
112 |
$tmp = $elements[$newindex];
|
|
|
113 |
$elements[$newindex] = $e;
|
|
|
114 |
$elements[$index] = $tmp;
|
|
|
115 |
break;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
$components[$comp]['elements'] = $elements;
|
|
|
119 |
$report->components = cr_serialize($components);
|
|
|
120 |
$DB->update_record('block_configurable_reports', $report);
|
|
|
121 |
redirect(new moodle_url('/blocks/configurable_reports/editcomp.php', ['id' => $id, 'comp' => $comp]));
|
|
|
122 |
exit;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if (!$plugin || $plugin != $pname) {
|
|
|
127 |
print_error('nosuchplugin');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
|
|
|
131 |
require_once($CFG->dirroot.'/blocks/configurable_reports/components/'.$comp.'/'.$pname.'/plugin.class.php');
|
|
|
132 |
$pluginclassname = 'plugin_'.$pname;
|
|
|
133 |
$pluginclass = new $pluginclassname($report);
|
|
|
134 |
|
|
|
135 |
if (isset($pluginclass->form) && $pluginclass->form) {
|
|
|
136 |
require_once($CFG->dirroot.'/blocks/configurable_reports/component.class.php');
|
|
|
137 |
require_once($CFG->dirroot.'/blocks/configurable_reports/components/'.$comp.'/component.class.php');
|
|
|
138 |
$componentclassname = 'component_'.$comp;
|
|
|
139 |
$compclass = new $componentclassname($report->id);
|
|
|
140 |
|
|
|
141 |
require_once($CFG->dirroot.'/blocks/configurable_reports/components/'.$comp.'/'.$pname.'/form.php');
|
|
|
142 |
$classname = $pname.'_form';
|
|
|
143 |
|
|
|
144 |
$formurlparams = array('id' => $id, 'comp' => $comp, 'pname' => $pname);
|
|
|
145 |
if ($cid) {
|
|
|
146 |
$formurlparams['cid'] = $cid;
|
|
|
147 |
}
|
|
|
148 |
$formurl = new moodle_url('/blocks/configurable_reports/editplugin.php', $formurlparams);
|
|
|
149 |
$editform = new $classname($formurl, compact('comp', 'cid', 'id', 'pluginclass', 'compclass', 'report', 'reportclass'));
|
|
|
150 |
|
|
|
151 |
if (!empty($cdata)) {
|
|
|
152 |
$editform->set_data($cdata['formdata']);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if ($editform->is_cancelled()) {
|
|
|
156 |
if (!empty($report)) {
|
|
|
157 |
redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php?id='.$report->id);
|
|
|
158 |
} else {
|
|
|
159 |
redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php');
|
|
|
160 |
}
|
|
|
161 |
} else if ($data = $editform->get_data()) {
|
|
|
162 |
cr_add_to_log($report->courseid, 'configurable_reports', 'edit', '', $report->name);
|
|
|
163 |
if (!empty($cdata)) {
|
|
|
164 |
$cdata['formdata'] = $data;
|
|
|
165 |
$cdata['summary'] = $pluginclass->summary($data);
|
|
|
166 |
$elements = cr_unserialize($report->components);
|
|
|
167 |
$elements = isset($elements[$comp]['elements']) ? $elements[$comp]['elements'] : [];
|
|
|
168 |
|
|
|
169 |
if ($elements) {
|
|
|
170 |
foreach ($elements as $key => $e) {
|
|
|
171 |
if ($e['id'] == $cid) {
|
|
|
172 |
$elements[$key] = $cdata;
|
|
|
173 |
break;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
$allelements = cr_unserialize($report->components);
|
|
|
179 |
$allelements[$comp]['elements'] = $elements;
|
|
|
180 |
|
|
|
181 |
$report->components = cr_serialize($allelements);
|
|
|
182 |
if (!$DB->update_record('block_configurable_reports', $report)) {
|
|
|
183 |
print_error('errorsaving');
|
|
|
184 |
} else {
|
|
|
185 |
redirect(new moodle_url('/blocks/configurable_reports/editcomp.php', array('id' => $id, 'comp' => $comp)));
|
|
|
186 |
exit;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
} else {
|
|
|
190 |
|
|
|
191 |
$allelements = cr_unserialize($report->components);
|
|
|
192 |
|
|
|
193 |
$uniqueid = random_string(15);
|
|
|
194 |
while (strpos($report->components, $uniqueid) !== false) {
|
|
|
195 |
$uniqueid = random_string(15);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$cdata = [
|
|
|
199 |
'id' => $uniqueid,
|
|
|
200 |
'formdata' => $data,
|
|
|
201 |
'pluginname' => $pname,
|
|
|
202 |
'pluginfullname' => $pluginclass->fullname,
|
|
|
203 |
'summary' => $pluginclass->summary($data)
|
|
|
204 |
];
|
|
|
205 |
|
|
|
206 |
$allelements[$comp]['elements'][] = $cdata;
|
|
|
207 |
$report->components = cr_serialize($allelements, false);
|
|
|
208 |
if (!$DB->update_record('block_configurable_reports', $report)) {
|
|
|
209 |
print_error('errorsaving');
|
|
|
210 |
} else {
|
|
|
211 |
redirect(new moodle_url('/blocks/configurable_reports/editcomp.php', array('id' => $id, 'comp' => $comp)));
|
|
|
212 |
exit;
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
} else {
|
|
|
217 |
$allelements = cr_unserialize($report->components);
|
|
|
218 |
|
|
|
219 |
$uniqueid = random_string(15);
|
|
|
220 |
while (strpos($report->components, $uniqueid) !== false) {
|
|
|
221 |
$uniqueid = random_string(15);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
$cdata = [
|
|
|
225 |
'id' => $uniqueid,
|
|
|
226 |
'formdata' => new \stdclass,
|
|
|
227 |
'pluginname' => $pname,
|
|
|
228 |
'pluginfullname' => $pluginclass->fullname,
|
|
|
229 |
'summary' => $pluginclass->summary(new \stdclass)
|
|
|
230 |
];
|
|
|
231 |
|
|
|
232 |
$allelements[$comp]['elements'][] = $cdata;
|
|
|
233 |
$report->components = cr_serialize($allelements);
|
|
|
234 |
if (!$DB->update_record('block_configurable_reports', $report)) {
|
|
|
235 |
print_error('errorsaving');
|
|
|
236 |
} else {
|
|
|
237 |
redirect(new moodle_url('/blocks/configurable_reports/editcomp.php', ['id' => $id, 'comp' => $comp]));
|
|
|
238 |
exit;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
$title = format_string($report->name).' '.get_string($comp, 'block_configurable_reports');
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
$PAGE->navbar->add(get_string('managereports', 'block_configurable_reports'), $CFG->wwwroot.'/blocks/configurable_reports/managereport.php?courseid='.$report->courseid);
|
|
|
246 |
$PAGE->navbar->add($title, $CFG->wwwroot.'/blocks/configurable_reports/editcomp.php?id='.$id.'&comp='.$comp);
|
|
|
247 |
$PAGE->navbar->add(get_string($pname, 'block_configurable_reports'));
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
$PAGE->set_title($title);
|
|
|
251 |
$PAGE->set_heading( $title);
|
|
|
252 |
$PAGE->set_cacheable( true);
|
|
|
253 |
|
|
|
254 |
echo $OUTPUT->header();
|
|
|
255 |
|
|
|
256 |
$currenttab = $comp;
|
|
|
257 |
require('tabs.php');
|
|
|
258 |
|
|
|
259 |
if ($pluginclass->form) {
|
|
|
260 |
$editform->display();
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
echo $OUTPUT->footer();
|
|
|
264 |
|