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($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
|
|
|
26 |
|
|
|
27 |
class plugin_reportcolumn extends plugin_base {
|
|
|
28 |
|
|
|
29 |
public $reportcache = array();
|
|
|
30 |
|
|
|
31 |
public function init() {
|
|
|
32 |
$this->fullname = get_string('reportcolumn', 'block_configurable_reports');
|
|
|
33 |
$this->type = 'undefined';
|
|
|
34 |
$this->form = true;
|
|
|
35 |
$this->reporttypes = array('courses', 'users', 'timeline', 'categories');
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function summary($data) {
|
|
|
39 |
return format_string($data->columname);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
public function colformat($data) {
|
|
|
44 |
$align = (isset($data->align)) ? $data->align : '';
|
|
|
45 |
$size = (isset($data->size)) ? $data->size : '';
|
|
|
46 |
$wrap = (isset($data->wrap)) ? $data->wrap : '';
|
|
|
47 |
return array($align, $size, $wrap);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function get_user_reports() {
|
|
|
51 |
global $DB, $USER;
|
|
|
52 |
|
|
|
53 |
$supported = array(
|
|
|
54 |
'courses' => array('users'),
|
|
|
55 |
'users' => array('courses'),
|
|
|
56 |
'timeline' => array('users', 'courses', 'sql'),
|
|
|
57 |
'categories' => array('courses')
|
|
|
58 |
);
|
|
|
59 |
|
|
|
60 |
$reports = cr_get_my_reports($this->report->courseid, $USER->id);
|
|
|
61 |
if ($reports) {
|
|
|
62 |
foreach ($reports as $key => $val) {
|
|
|
63 |
if (!in_array($val->type, $supported[$this->report->type])) {
|
|
|
64 |
unset($reports[$key]);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
return $reports;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function get_current_report($report) {
|
|
|
72 |
$components = cr_unserialize($report->components);
|
|
|
73 |
|
|
|
74 |
if (!is_array($components) || empty($components['columns']['elements'])) {
|
|
|
75 |
return false;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$elements = $components['columns']['elements'];
|
|
|
79 |
foreach ($elements as $e) {
|
|
|
80 |
if ($e['pluginname'] == 'reportcolumn' && $e['formdata']->reportid) {
|
|
|
81 |
return $e['formdata']->reportid;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
return 0;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function get_report_columns($reportid) {
|
|
|
88 |
global $DB;
|
|
|
89 |
|
|
|
90 |
$columns = array();
|
|
|
91 |
if (!$report = $DB->get_record('block_configurable_reports', array('id' => $reportid))) {
|
|
|
92 |
return $columns;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$components = cr_unserialize($report->components);
|
|
|
96 |
|
|
|
97 |
if (!is_array($components) || empty($components['columns']['elements'])) {
|
|
|
98 |
return $columns;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$elements = $components['columns']['elements'];
|
|
|
102 |
foreach ($elements as $e) {
|
|
|
103 |
$columns[] = $e['summary'];
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return $columns;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public function fix_condition_expr($condition, $count) {
|
|
|
110 |
switch($count) {
|
|
|
111 |
case 0:
|
|
|
112 |
return '';
|
|
|
113 |
case 1:
|
|
|
114 |
return '';
|
|
|
115 |
case 2:
|
|
|
116 |
return 'c1 and c2';
|
|
|
117 |
default:
|
|
|
118 |
return $condition." and c$count";
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// Data -> Plugin configuration data.
|
|
|
123 |
// Row -> Complet course/user row c->id, c->fullname, etc...
|
|
|
124 |
public function execute($data, $row, $user, $courseid, $starttime = 0, $endtime = 0) {
|
|
|
125 |
global $DB, $CFG;
|
|
|
126 |
|
|
|
127 |
if (!$report = $DB->get_record('block_configurable_reports', array('id' => $data->reportid))) {
|
|
|
128 |
print_error('reportdoesnotexists', 'block_configurable_reports');
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
132 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
|
|
|
133 |
|
|
|
134 |
if (!isset($this->reportcache[$row->id])) {
|
|
|
135 |
|
|
|
136 |
$reportclassname = 'report_'.$report->type;
|
|
|
137 |
$reportclass = new $reportclassname($report);
|
|
|
138 |
|
|
|
139 |
// Delete conditions - TODO
|
|
|
140 |
// Add new condition
|
|
|
141 |
// User report -> New condition "User courses"
|
|
|
142 |
// Course report -> New condition "Course users".
|
|
|
143 |
if ($this->report->type == 'users') {
|
|
|
144 |
$reportclass->currentuser = $row;
|
|
|
145 |
$reportclass->starttime = $starttime;
|
|
|
146 |
$reportclass->endtime = $endtime;
|
|
|
147 |
|
|
|
148 |
if ($report->type == 'courses') {
|
|
|
149 |
$components = cr_unserialize($reportclass->config->components);
|
|
|
150 |
$newplugin = array(
|
|
|
151 |
'pluginname' => 'currentusercourses',
|
|
|
152 |
'fullname' => 'currentusercourses',
|
|
|
153 |
'formdata' => new \stdclass
|
|
|
154 |
);
|
|
|
155 |
|
|
|
156 |
$components['conditions']['elements'][] = $newplugin;
|
|
|
157 |
if (empty($components['conditions']['config'])) {
|
|
|
158 |
$components['conditions']['config'] = new stdclass();
|
|
|
159 |
$components['conditions']['config']->conditionexpr = '';
|
|
|
160 |
}
|
|
|
161 |
$components['conditions']['config']->conditionexpr = $this->fix_condition_expr($components['conditions']['config']->conditionexpr, count($components['conditions']['elements']));
|
|
|
162 |
$reportclass->config->components = cr_serialize($components);
|
|
|
163 |
}
|
|
|
164 |
} else if ($this->report->type == 'courses') {
|
|
|
165 |
$reportclass->currentcourseid = $row->id;
|
|
|
166 |
$reportclass->starttime = $starttime;
|
|
|
167 |
$reportclass->endtime = $endtime;
|
|
|
168 |
|
|
|
169 |
if ($report->type == 'users') {
|
|
|
170 |
$components = cr_unserialize($reportclass->config->components);
|
|
|
171 |
|
|
|
172 |
$roles = $DB->get_records('role');
|
|
|
173 |
$rolesid = array_keys($roles);
|
|
|
174 |
|
|
|
175 |
$formdata = new stdclass;
|
|
|
176 |
$formdata->roles = $rolesid;
|
|
|
177 |
$newplugin = array(
|
|
|
178 |
'pluginname' => 'usersincurrentcourse',
|
|
|
179 |
'fullname' => 'usersincurrentcourse',
|
|
|
180 |
'formdata' => $formdata
|
|
|
181 |
);
|
|
|
182 |
|
|
|
183 |
$components['conditions']['elements'][] = $newplugin;
|
|
|
184 |
if (!empty($components['conditions']['config'])) {
|
|
|
185 |
$components['conditions']['config']->conditionexpr = $this->fix_condition_expr($components['conditions']['config']->conditionexpr, count($components['conditions']['elements']));
|
|
|
186 |
}
|
|
|
187 |
$reportclass->config->components = cr_serialize($components);
|
|
|
188 |
}
|
|
|
189 |
} else if ($this->report->type == 'timeline') {
|
|
|
190 |
$reportclass->starttime = $row->starttime;
|
|
|
191 |
$reportclass->endtime = $row->endtime;
|
|
|
192 |
} else if ($this->report->type == 'categories') {
|
|
|
193 |
$reportclass->starttime = $starttime;
|
|
|
194 |
$reportclass->endtime = $endtime;
|
|
|
195 |
|
|
|
196 |
if ($report->type == 'courses') {
|
|
|
197 |
$components = cr_unserialize($reportclass->config->components);
|
|
|
198 |
|
|
|
199 |
$formdata = new stdclass;
|
|
|
200 |
$formdata->categoryid = $row->id;
|
|
|
201 |
$newplugin = array('pluginname' => 'coursecategory', 'fullname' => 'coursecategory', 'formdata' => $formdata);
|
|
|
202 |
|
|
|
203 |
$components['conditions']['elements'][] = $newplugin;
|
|
|
204 |
$components['conditions']['config']->conditionexpr = $this->fix_condition_expr($components['conditions']['config']->conditionexpr, count($components['conditions']['elements']));
|
|
|
205 |
$reportclass->config->components = cr_serialize($components);
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
$reportclass->create_report();
|
|
|
210 |
$this->reportcache[$row->id] = $reportclass->finalreport->table->data;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
if (!empty($this->reportcache[$row->id])) {
|
|
|
214 |
$subtable = array();
|
|
|
215 |
foreach ($this->reportcache[$row->id] as $r) {
|
|
|
216 |
$subtable[] = $r[$data->column];
|
|
|
217 |
}
|
|
|
218 |
return $subtable;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
return '';
|
|
|
222 |
}
|
|
|
223 |
}
|