1 |
efrain |
1 |
<?php
|
|
|
2 |
defined('MOODLE_INTERNAL') || die();
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
global $CFG;
|
|
|
8 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
9 |
require_once $CFG->libdir . '/completionlib.php';
|
|
|
10 |
require_once $CFG->dirroot. '/completion/classes/progress.php';
|
|
|
11 |
|
|
|
12 |
require_once(__DIR__ . '/locallib.php');
|
|
|
13 |
|
|
|
14 |
require_once $CFG->libdir . '/phpspreadsheet/vendor/autoload.php';
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
class local_cesa_reportes_ajax_external extends \external_api
|
|
|
19 |
{
|
|
|
20 |
const _STATUS_PENDING = 0;
|
|
|
21 |
const _STATUS_PROCESSING = 1;
|
|
|
22 |
const _STATUS_READY = 2;
|
|
|
23 |
const _STATUS_ERROR = 3;
|
|
|
24 |
|
|
|
25 |
public static function report_for_one_course_parameters()
|
|
|
26 |
{
|
|
|
27 |
return new \external_function_parameters([
|
|
|
28 |
'courseid' => new \external_value(PARAM_INT, 'ID del curso', VALUE_DEFAULT, 0),
|
|
|
29 |
]);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public static function report_for_one_course($courseid)
|
|
|
33 |
{
|
|
|
34 |
global $CFG, $DB, $OUTPUT, $PAGE;
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
require_login();
|
|
|
38 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
39 |
$PAGE->set_context(context_system::instance());
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
$filter_userids = [];
|
|
|
43 |
$filter_courseids = [$courseid];
|
|
|
44 |
$filter_by_company_active = true;
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
$extra_fields = local_cesa_reportes_get_user_extra_field_names();
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
$context['fields'] = [];
|
|
|
52 |
array_push($context['fields'], ['field' => 'Apellido']);
|
|
|
53 |
array_push($context['fields'], ['field' => 'Nombre']);
|
|
|
54 |
array_push($context['fields'], ['field' => 'Email']);
|
|
|
55 |
array_push($context['fields'], ['field' => 'Curso']);
|
|
|
56 |
array_push($context['fields'], ['field' => 'Categoria']);
|
|
|
57 |
array_push($context['fields'], ['field' => 'Matriculación']);
|
|
|
58 |
array_push($context['fields'], ['field' => 'Fecha finalización']);
|
|
|
59 |
array_push($context['fields'], ['field' => 'Actividades asignadas']);
|
|
|
60 |
array_push($context['fields'], ['field' => 'Actividades completadas']);
|
|
|
61 |
array_push($context['fields'], ['field' => 'Progreso %']);
|
|
|
62 |
array_push($context['fields'], ['field' => 'Primera actividad']);
|
|
|
63 |
array_push($context['fields'], ['field' => 'Ultima actividad']);
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
foreach($extra_fields as $extra_field)
|
|
|
67 |
{
|
|
|
68 |
array_push($context['fields'], ['field' => $extra_field->name]);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
$context['items'] = [];
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
$include_modules = false;
|
|
|
77 |
$page = 0;
|
|
|
78 |
$data = local_cesa_reportes_get_data_for_report_or_excel($filter_by_company_active, $filter_userids, $filter_courseids, $include_modules, $page);
|
|
|
79 |
$records = $data['items'];
|
|
|
80 |
|
|
|
81 |
$context['pagination'] = $data['pagination'];
|
|
|
82 |
|
|
|
83 |
foreach($records as $record)
|
|
|
84 |
{
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
if(!$record->courses) {
|
|
|
88 |
continue;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
foreach($record->courses as $course)
|
|
|
95 |
{
|
|
|
96 |
|
|
|
97 |
$item = [];
|
|
|
98 |
array_push($item, ['value' => $record->lastname ]);
|
|
|
99 |
array_push($item, ['value' => $record->firstname ]);
|
|
|
100 |
array_push($item, ['value' => $record->email ]);
|
|
|
101 |
array_push($item, ['value' => $course->name ]);
|
|
|
102 |
array_push($item, ['value' => $course->category ]);
|
|
|
103 |
array_push($item, ['value' => empty($course->timeenrolled) ? '' : date('d/m/Y h:i a', $course->timeenrolled) ]);
|
|
|
104 |
array_push($item, ['value' => empty($course->timecompleted) ? '' : date('d/m/Y h:i a', $course->timecompleted) ]);
|
|
|
105 |
array_push($item, ['value' => $course->activities_assigned, 'align-right' => true ]);
|
|
|
106 |
array_push($item, ['value' => $course->activities_completed, 'align-right' => true ]);
|
|
|
107 |
array_push($item, ['value' => number_format($course->percentage, 2), 'align-right' => true ]);
|
|
|
108 |
array_push($item, ['value' => empty($course->time_activities_first) ? '' : date('d/m/Y h:i a', $course->time_activities_first) ]);
|
|
|
109 |
array_push($item, ['value' => empty($course->time_activities_last) ? '' : date('d/m/Y h:i a', $course->time_activities_last) ]);
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
foreach($extra_fields as $extra_field)
|
|
|
113 |
{
|
|
|
114 |
$value = '';
|
|
|
115 |
|
|
|
116 |
foreach($record->extras as $extra)
|
|
|
117 |
{
|
|
|
118 |
if($extra->id == $extra_field->id) {
|
|
|
119 |
$value = $extra->value;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
array_push($item, ['value' => $value ]);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
array_push($context['items'], ['item' => $item]);
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
$data = $OUTPUT->render_from_template('local_cesa_reportes/report_one_course', $context);
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
return json_encode(['success' =>true, 'data' => $data]);
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
public static function report_for_one_course_returns()
|
|
|
150 |
{
|
|
|
151 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
152 |
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
public static function report_for_one_user_parameters()
|
|
|
158 |
{
|
|
|
159 |
return new \external_function_parameters([
|
|
|
160 |
'userid' => new \external_value(PARAM_INT, 'ID del usuario', VALUE_DEFAULT, 0),
|
|
|
161 |
]);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
public static function report_for_one_user($userid)
|
|
|
165 |
{
|
|
|
166 |
|
|
|
167 |
global $CFG, $DB, $OUTPUT, $PAGE;
|
|
|
168 |
|
|
|
169 |
require_login();
|
|
|
170 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
171 |
$PAGE->set_context(context_system::instance());
|
|
|
172 |
|
|
|
173 |
$filter_userids = [$userid];
|
|
|
174 |
$filter_courseids = [];
|
|
|
175 |
$filter_by_company_active = true;
|
|
|
176 |
|
|
|
177 |
$extra_fields = local_cesa_reportes_get_user_extra_field_names();
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
$context['fields'] = [];
|
|
|
181 |
array_push($context['fields'], ['field' => 'Apellido']);
|
|
|
182 |
array_push($context['fields'], ['field' => 'Nombre']);
|
|
|
183 |
array_push($context['fields'], ['field' => 'Email']);
|
|
|
184 |
array_push($context['fields'], ['field' => 'Curso']);
|
|
|
185 |
array_push($context['fields'], ['field' => 'Categoria']);
|
|
|
186 |
array_push($context['fields'], ['field' => 'Matriculación']);
|
|
|
187 |
array_push($context['fields'], ['field' => 'Fecha finalización']);
|
|
|
188 |
array_push($context['fields'], ['field' => 'Actividades asignadas']);
|
|
|
189 |
array_push($context['fields'], ['field' => 'Actividades completadas']);
|
|
|
190 |
array_push($context['fields'], ['field' => 'Progreso %']);
|
|
|
191 |
array_push($context['fields'], ['field' => 'Primera actividad']);
|
|
|
192 |
array_push($context['fields'], ['field' => 'Ultima actividad']);
|
|
|
193 |
|
|
|
194 |
foreach($extra_fields as $extra_field)
|
|
|
195 |
{
|
|
|
196 |
array_push($context['fields'], ['field' => $extra_field->name]);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
$context['items'] = [];
|
|
|
200 |
|
|
|
201 |
$include_modules = false;
|
|
|
202 |
$page = 0;
|
|
|
203 |
$data = local_cesa_reportes_get_data_for_report_or_excel($filter_by_company_active, $filter_userids, $filter_courseids, $include_modules, $page);
|
|
|
204 |
$records = $data['items'];
|
|
|
205 |
|
|
|
206 |
$context['pagination'] = $data['pagination'];
|
|
|
207 |
|
|
|
208 |
foreach($records as $record)
|
|
|
209 |
{
|
|
|
210 |
if(!$record->courses) {
|
|
|
211 |
continue;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
foreach($record->courses as $course)
|
|
|
215 |
{
|
|
|
216 |
|
|
|
217 |
$item = [];
|
|
|
218 |
array_push($item, ['value' => $record->lastname ]);
|
|
|
219 |
array_push($item, ['value' => $record->firstname ]);
|
|
|
220 |
array_push($item, ['value' => $record->email ]);
|
|
|
221 |
array_push($item, ['value' => $course->name ]);
|
|
|
222 |
array_push($item, ['value' => $course->category ]);
|
|
|
223 |
array_push($item, ['value' => empty($course->timeenrolled) ? '' : date('d/m/Y h:i a', $course->timeenrolled) ]);
|
|
|
224 |
array_push($item, ['value' => empty($course->timecompleted) ? '' : date('d/m/Y h:i a', $course->timecompleted) ]);
|
|
|
225 |
array_push($item, ['value' => $course->activities_assigned, 'align-right' => true ]);
|
|
|
226 |
array_push($item, ['value' => $course->activities_completed, 'align-right' => true ]);
|
|
|
227 |
array_push($item, ['value' => number_format($course->percentage, 2), 'align-right' => true ]);
|
|
|
228 |
array_push($item, ['value' => empty($course->time_activities_first) ? '' : date('d/m/Y h:i a', $course->time_activities_first) ]);
|
|
|
229 |
array_push($item, ['value' => empty($course->time_activities_last) ? '' : date('d/m/Y h:i a', $course->time_activities_last) ]);
|
|
|
230 |
|
|
|
231 |
|
|
|
232 |
foreach($extra_fields as $extra_field)
|
|
|
233 |
{
|
|
|
234 |
$value = '';
|
|
|
235 |
|
|
|
236 |
foreach($record->extras as $extra)
|
|
|
237 |
{
|
|
|
238 |
if($extra->id == $extra_field->id) {
|
|
|
239 |
$value = $extra->value;
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
array_push($item, ['value' => $value ]);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
array_push($context['items'], ['item' => $item]);
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
$data = $OUTPUT->render_from_template('local_cesa_reportes/report_one_course', $context);
|
|
|
250 |
return json_encode(['success' => true, 'data' => $data]);
|
|
|
251 |
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
public static function report_for_one_user_returns()
|
|
|
256 |
{
|
|
|
257 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
258 |
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
public static function excel_last_seven_days_parameters()
|
|
|
262 |
{
|
|
|
263 |
return new \external_function_parameters([
|
|
|
264 |
'fakeparam' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
265 |
]);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
public static function excel_last_seven_days($fakeparam)
|
|
|
269 |
{
|
|
|
270 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
271 |
|
|
|
272 |
require_login();
|
|
|
273 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
274 |
$PAGE->set_context(context_system::instance());
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
$data = [
|
|
|
278 |
'request_userid' => $USER->id,
|
|
|
279 |
'request_time' => time(),
|
|
|
280 |
'generate_time' => 0,
|
|
|
281 |
'status' => self::_STATUS_PENDING,
|
|
|
282 |
'parameters' => json_encode([
|
|
|
283 |
'type' => 'los_ultimos_siete_dias',
|
|
|
284 |
]),
|
|
|
285 |
'filename' => '',
|
|
|
286 |
];
|
|
|
287 |
|
|
|
288 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
289 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
290 |
} else {
|
|
|
291 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
public static function excel_last_seven_days_returns()
|
|
|
296 |
{
|
|
|
297 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
298 |
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public static function excel_for_one_course_parameters()
|
|
|
302 |
{
|
|
|
303 |
return new \external_function_parameters([
|
|
|
304 |
'courseid' => new \external_value(PARAM_INT, 'ID del usuario', VALUE_DEFAULT, 0),
|
|
|
305 |
]);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
public static function excel_for_one_course($courseid)
|
|
|
309 |
{
|
|
|
310 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
311 |
|
|
|
312 |
require_login();
|
|
|
313 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
314 |
$PAGE->set_context(context_system::instance());
|
|
|
315 |
|
|
|
316 |
$data = [
|
|
|
317 |
'request_userid' => $USER->id,
|
|
|
318 |
'request_time' => time(),
|
|
|
319 |
'generate_time' => 0,
|
|
|
320 |
'status' => self::_STATUS_PENDING,
|
|
|
321 |
'parameters' => json_encode([
|
|
|
322 |
'type' => 'todos_los_usuarios_de_un_curso',
|
|
|
323 |
'courseid' => $courseid,
|
|
|
324 |
]),
|
|
|
325 |
'filename' => '',
|
|
|
326 |
];
|
|
|
327 |
|
|
|
328 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
329 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
330 |
} else {
|
|
|
331 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
public static function excel_for_one_course_returns()
|
|
|
337 |
{
|
|
|
338 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
339 |
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
public static function excel_for_one_user_parameters()
|
|
|
343 |
{
|
|
|
344 |
return new \external_function_parameters([
|
|
|
345 |
'userid' => new \external_value(PARAM_INT, 'ID del usuario', VALUE_DEFAULT, 0),
|
|
|
346 |
]);
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
public static function excel_for_one_user($userid)
|
|
|
350 |
{
|
|
|
351 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
352 |
|
|
|
353 |
require_login();
|
|
|
354 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
355 |
$PAGE->set_context(context_system::instance());
|
|
|
356 |
|
|
|
357 |
$data = [
|
|
|
358 |
'request_userid' => $USER->id,
|
|
|
359 |
'request_time' => time(),
|
|
|
360 |
'generate_time' => 0,
|
|
|
361 |
'status' => self::_STATUS_PENDING,
|
|
|
362 |
'parameters' => json_encode([
|
|
|
363 |
'type' => 'todos_los_cursos_de_un_usuario',
|
|
|
364 |
'userid' => $userid,
|
|
|
365 |
]),
|
|
|
366 |
'filename' => '',
|
|
|
367 |
];
|
|
|
368 |
|
|
|
369 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
370 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
371 |
} else {
|
|
|
372 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
373 |
}
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
public static function excel_for_one_user_returns()
|
|
|
378 |
{
|
|
|
379 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
380 |
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
public static function excel_all_courses_parameters()
|
|
|
384 |
{
|
|
|
385 |
return new \external_function_parameters([
|
|
|
386 |
'fakeparam' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
387 |
]);
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
public static function excel_all_courses($fakeparam)
|
|
|
391 |
{
|
|
|
392 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
393 |
|
|
|
394 |
require_login();
|
|
|
395 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
396 |
$PAGE->set_context(context_system::instance());
|
|
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
$data = [
|
|
|
401 |
'request_userid' => $USER->id,
|
|
|
402 |
'request_time' => time(),
|
|
|
403 |
'generate_time' => 0,
|
|
|
404 |
'status' => self::_STATUS_PENDING,
|
|
|
405 |
'parameters' => json_encode([
|
|
|
406 |
'type' => 'todos_los_cursos',
|
|
|
407 |
]),
|
|
|
408 |
'filename' => '',
|
|
|
409 |
];
|
|
|
410 |
|
|
|
411 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
412 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
413 |
} else {
|
|
|
414 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
415 |
}
|
|
|
416 |
}
|
|
|
417 |
|
|
|
418 |
public static function excel_all_courses_returns()
|
|
|
419 |
{
|
|
|
420 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
421 |
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
public static function excel_all_courses_with_activites_parameters()
|
|
|
425 |
{
|
|
|
426 |
return new \external_function_parameters([
|
|
|
427 |
'fakeparam' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
428 |
]);
|
|
|
429 |
}
|
|
|
430 |
|
|
|
431 |
public static function excel_all_courses_with_activites($fakeparam)
|
|
|
432 |
{
|
|
|
433 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
require_login();
|
|
|
437 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
438 |
$PAGE->set_context(context_system::instance());
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
$data = [
|
|
|
442 |
'request_userid' => $USER->id,
|
|
|
443 |
'request_time' => time(),
|
|
|
444 |
'generate_time' => 0,
|
|
|
445 |
'status' => self::_STATUS_PENDING,
|
|
|
446 |
'parameters' => json_encode([
|
|
|
447 |
'type' => 'todos_los_cursos_con_actividades',
|
|
|
448 |
]),
|
|
|
449 |
'filename' => '',
|
|
|
450 |
];
|
|
|
451 |
|
|
|
452 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
453 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
454 |
} else {
|
|
|
455 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
456 |
}
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
|
|
|
460 |
public static function excel_all_courses_with_activites_returns()
|
|
|
461 |
{
|
|
|
462 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
463 |
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
|
|
|
467 |
public static function all_files_parameters()
|
|
|
468 |
{
|
|
|
469 |
return new \external_function_parameters([
|
|
|
470 |
'fakeparam' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
471 |
]);
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
public static function all_files($fakeparam)
|
|
|
475 |
{
|
|
|
476 |
global $CFG, $DB, $PAGE, $OUTPUT;
|
|
|
477 |
|
|
|
478 |
require_login();
|
|
|
479 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
480 |
$PAGE->set_context(context_system::instance());
|
|
|
481 |
|
|
|
482 |
$context['items'] = [];
|
|
|
483 |
|
|
|
484 |
|
|
|
485 |
$users = [];
|
|
|
486 |
$records = $DB->get_records('local_cesa_reportes', [], 'request_time DESC');
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
foreach($records as $record)
|
|
|
491 |
{
|
|
|
492 |
if(isset($users[$record->request_userid])) {
|
|
|
493 |
$user = $users[$record->request_userid];
|
|
|
494 |
} else {
|
|
|
495 |
$user = $DB->get_record('user', ['id' => $record->request_userid]);
|
|
|
496 |
$users[$record->request_userid] = $user;
|
|
|
497 |
//select id, username, firstname, lastname, email from db_moodle4_dev.mdl_user
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
$item = [
|
|
|
501 |
'id' => $record->id,
|
|
|
502 |
'download' => false,
|
|
|
503 |
'delete' => false,
|
|
|
504 |
'values' => [],
|
|
|
505 |
|
|
|
506 |
];
|
|
|
507 |
array_push($item['values'], ['value' => date('d/m/Y h:i a', $record->request_time)]);
|
|
|
508 |
array_push($item['values'], ['value' => $user->firstname . ' ' . $user->lastname ]);
|
|
|
509 |
|
|
|
510 |
|
|
|
511 |
switch($record->status)
|
|
|
512 |
{
|
|
|
513 |
case 1 :
|
|
|
514 |
array_push($item['values'], ['value' => 'Procesando' ]);
|
|
|
515 |
break;
|
|
|
516 |
|
|
|
517 |
case 2 :
|
|
|
518 |
array_push($item['values'], ['value' => 'Generado' ]);
|
|
|
519 |
$item['delete'] = true;
|
|
|
520 |
$item['download'] = true;
|
|
|
521 |
break;
|
|
|
522 |
|
|
|
523 |
case 3 :
|
|
|
524 |
array_push($item['values'], ['value' => 'Error' ]);
|
|
|
525 |
$item['delete'] = true;
|
|
|
526 |
break;
|
|
|
527 |
|
|
|
528 |
default :
|
|
|
529 |
array_push($item['values'], ['value' => 'Pendiente']);
|
|
|
530 |
$item['delete'] = true;
|
|
|
531 |
break;
|
|
|
532 |
|
|
|
533 |
}
|
|
|
534 |
|
|
|
535 |
|
|
|
536 |
array_push($item['values'], ['value' => $record->filename] );
|
|
|
537 |
array_push($item['values'], ['value' => empty($record->generate_time) ? '' : date('d/m/Y h:i a', $record->generate_time)]);
|
|
|
538 |
|
|
|
539 |
|
|
|
540 |
array_push($context['items'],['item' => $item]);
|
|
|
541 |
|
|
|
542 |
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
$data = $OUTPUT->render_from_template('local_cesa_reportes/all_files', $context);
|
|
|
546 |
return json_encode(['success' => true, 'data' => $data]);
|
|
|
547 |
|
|
|
548 |
|
|
|
549 |
}
|
|
|
550 |
|
|
|
551 |
public static function all_files_returns()
|
|
|
552 |
{
|
|
|
553 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
554 |
|
|
|
555 |
}
|
|
|
556 |
|
|
|
557 |
public static function delete_file_parameters()
|
|
|
558 |
{
|
|
|
559 |
return new \external_function_parameters([
|
|
|
560 |
'id' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
561 |
]);
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
public static function delete_file($id)
|
|
|
565 |
{
|
|
|
566 |
global $CFG, $DB, $PAGE, $OUTPUT;
|
|
|
567 |
|
|
|
568 |
require_login();
|
|
|
569 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
570 |
$PAGE->set_context(context_system::instance());
|
|
|
571 |
|
|
|
572 |
|
|
|
573 |
$record = $DB->get_record('local_cesa_reportes', ['id' => $id]);
|
|
|
574 |
if($record) {
|
|
|
575 |
|
|
|
576 |
$full_filename = __DIR__ . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $record->filename ;
|
|
|
577 |
if(file_exists($full_filename)) {
|
|
|
578 |
|
|
|
579 |
@unlink($full_filename);
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
$DB->delete_records('local_cesa_reportes', ['id' => $id]);
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
|
|
|
586 |
|
|
|
587 |
$context['items'] = [];
|
|
|
588 |
|
|
|
589 |
|
|
|
590 |
$users = [];
|
|
|
591 |
$records = $DB->get_records('local_cesa_reportes', [], 'request_time DESC');
|
|
|
592 |
|
|
|
593 |
|
|
|
594 |
|
|
|
595 |
foreach($records as $record)
|
|
|
596 |
{
|
|
|
597 |
if(isset($users[$record->request_userid])) {
|
|
|
598 |
$user = $users[$record->request_userid];
|
|
|
599 |
} else {
|
|
|
600 |
$user = $DB->get_record('user', ['id' => $record->request_userid]);
|
|
|
601 |
$users[$record->request_userid] = $user;
|
|
|
602 |
//select id, username, firstname, lastname, email from db_moodle4_dev.mdl_user
|
|
|
603 |
}
|
|
|
604 |
|
|
|
605 |
$item = [
|
|
|
606 |
'id' => $record->id,
|
|
|
607 |
'download' => false,
|
|
|
608 |
'delete' => false,
|
|
|
609 |
'values' => [],
|
|
|
610 |
|
|
|
611 |
];
|
|
|
612 |
array_push($item['values'], ['value' => date('d/m/Y h:i a', $record->request_time)]);
|
|
|
613 |
array_push($item['values'], ['value' => $user->firstname . ' ' . $user->lastname ]);
|
|
|
614 |
|
|
|
615 |
|
|
|
616 |
|
|
|
617 |
switch($record->status)
|
|
|
618 |
{
|
|
|
619 |
case 1 :
|
|
|
620 |
array_push($item['values'], ['value' => 'Procesando' ]);
|
|
|
621 |
break;
|
|
|
622 |
|
|
|
623 |
case 2 :
|
|
|
624 |
array_push($item['values'], ['value' => 'Generado' ]);
|
|
|
625 |
$item['download'] = true;
|
|
|
626 |
$item['delete'] = true;
|
|
|
627 |
break;
|
|
|
628 |
|
|
|
629 |
case 3 :
|
|
|
630 |
array_push($item['values'], ['value' => 'Error' ]);
|
|
|
631 |
$item['delete'] = true;
|
|
|
632 |
break;
|
|
|
633 |
|
|
|
634 |
default :
|
|
|
635 |
array_push($item['values'], ['value' => 'Pendiente']);
|
|
|
636 |
$item['delete'] = true;
|
|
|
637 |
break;
|
|
|
638 |
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
|
|
|
642 |
array_push($item['values'], ['value' => $record->filename] );
|
|
|
643 |
array_push($item['values'], ['value' => empty($record->generate_time) ? '' : date('d/m/Y h:i a', $record->generate_time)]);
|
|
|
644 |
|
|
|
645 |
|
|
|
646 |
array_push($context['items'],['item' => $item]);
|
|
|
647 |
|
|
|
648 |
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
$data = $OUTPUT->render_from_template('local_cesa_reportes/all_files', $context);
|
|
|
652 |
return json_encode(['success' => true, 'data' => $data]);
|
|
|
653 |
|
|
|
654 |
|
|
|
655 |
}
|
|
|
656 |
|
|
|
657 |
public static function delete_file_returns()
|
|
|
658 |
{
|
|
|
659 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
660 |
|
|
|
661 |
}
|
|
|
662 |
|
|
|
663 |
public static function download_file_parameters()
|
|
|
664 |
{
|
|
|
665 |
return new \external_function_parameters([
|
|
|
666 |
'id' => new \external_value(PARAM_INT, 'fakeparam', VALUE_DEFAULT, 0),
|
|
|
667 |
]);
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
public static function download_file($id)
|
|
|
671 |
{
|
|
|
672 |
global $CFG, $DB, $PAGE, $OUTPUT;
|
|
|
673 |
|
|
|
674 |
require_login();
|
|
|
675 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
676 |
$PAGE->set_context(context_system::instance());
|
|
|
677 |
|
|
|
678 |
|
|
|
679 |
$record = $DB->get_record('local_cesa_reportes', ['id' => $id]);
|
|
|
680 |
if($record) {
|
|
|
681 |
|
|
|
682 |
$full_filename = __DIR__ . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $record->filename ;
|
|
|
683 |
if(file_exists($full_filename)) {
|
|
|
684 |
|
|
|
685 |
$data = base64_encode(file_get_contents($full_filename));
|
|
|
686 |
|
|
|
687 |
return json_encode(['success' => true, 'data' =>['basename' => $record->filename, 'content' => $data]]);
|
|
|
688 |
}
|
|
|
689 |
|
|
|
690 |
|
|
|
691 |
}
|
|
|
692 |
|
|
|
693 |
|
|
|
694 |
return json_encode(['success' => false, 'data' => get_string('cesa_reportes_error_file_not_found', 'local_cesa_reportes')]);
|
|
|
695 |
|
|
|
696 |
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
public static function download_file_returns()
|
|
|
700 |
{
|
|
|
701 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
702 |
|
|
|
703 |
}
|
|
|
704 |
|
|
|
705 |
|
|
|
706 |
|
|
|
707 |
public static function report_custom_parameters()
|
|
|
708 |
{
|
|
|
709 |
return new \external_function_parameters([
|
|
|
710 |
'filter_from' => new \external_value(PARAM_ALPHANUMEXT, 'filter_from', VALUE_DEFAULT, ''),
|
|
|
711 |
'filter_to' => new \external_value(PARAM_ALPHANUMEXT, 'filter_to', VALUE_DEFAULT, ''),
|
|
|
712 |
'filter_userkeyword' => new \external_value(PARAM_RAW, 'filter_userkeyword', VALUE_DEFAULT, ''),
|
|
|
713 |
]);
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
public static function report_custom($filter_from, $filter_to, $filter_userkeyword)
|
|
|
717 |
{
|
|
|
718 |
global $CFG, $DB, $PAGE, $OUTPUT;
|
|
|
719 |
|
|
|
720 |
require_login();
|
|
|
721 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
722 |
$PAGE->set_context(context_system::instance());
|
|
|
723 |
|
|
|
724 |
$filter_userkeyword = filter_var($filter_userkeyword, FILTER_SANITIZE_STRING);
|
|
|
725 |
|
|
|
726 |
|
|
|
727 |
$extra_fields = local_cesa_reportes_get_user_extra_field_names();
|
|
|
728 |
|
|
|
729 |
$context['fields'] = [];
|
|
|
730 |
array_push($context['fields'], ['field' => 'Apellido']);
|
|
|
731 |
array_push($context['fields'], ['field' => 'Nombre']);
|
|
|
732 |
array_push($context['fields'], ['field' => 'Email']);
|
|
|
733 |
array_push($context['fields'], ['field' => 'Curso']);
|
|
|
734 |
array_push($context['fields'], ['field' => 'Categoria']);
|
|
|
735 |
array_push($context['fields'], ['field' => 'Matriculación']);
|
|
|
736 |
array_push($context['fields'], ['field' => 'Fecha finalización']);
|
|
|
737 |
array_push($context['fields'], ['field' => 'Actividades asignadas ']);
|
|
|
738 |
array_push($context['fields'], ['field' => 'Actividades completadas ']);
|
|
|
739 |
array_push($context['fields'], ['field' => 'Progreso %']);
|
|
|
740 |
array_push($context['fields'], ['field' => 'Primera actividad']);
|
|
|
741 |
array_push($context['fields'], ['field' => 'Ultima actividad']);
|
|
|
742 |
array_push($context['fields'], ['field' => 'Actividad']);
|
|
|
743 |
array_push($context['fields'], ['field' => 'Completado']);
|
|
|
744 |
|
|
|
745 |
|
|
|
746 |
foreach($extra_fields as $extra_field)
|
|
|
747 |
{
|
|
|
748 |
array_push($context['fields'], ['field' => $extra_field->name]);
|
|
|
749 |
}
|
|
|
750 |
|
|
|
751 |
|
|
|
752 |
$filter_userids = [];
|
|
|
753 |
$filter_by_company_active = false;
|
|
|
754 |
|
|
|
755 |
|
|
|
756 |
$dt = \DateTime::createFromFormat('Y-m-d', $filter_from);
|
|
|
757 |
$dt->setTime(0, 0, 0);
|
|
|
758 |
$filter_from = $dt ? $dt->getTimestamp() : strtotime('-1 Month');
|
|
|
759 |
|
|
|
760 |
|
|
|
761 |
$dt = \DateTime::createFromFormat('Y-m-d', $filter_to);
|
|
|
762 |
$dt->setTime(23, 59, 59);
|
|
|
763 |
$filter_to = $dt ? $dt->getTimestamp() : time();
|
|
|
764 |
|
|
|
765 |
$context['items'] = [];
|
|
|
766 |
$context['filter_from'] = $filter_from;
|
|
|
767 |
$context['filter_to'] = $filter_to;
|
|
|
768 |
|
|
|
769 |
$filter_courseids = local_cesa_reportes_get_course_with_activites_range($filter_from, $filter_to);
|
|
|
770 |
$filter_by_company_active = true;
|
|
|
771 |
|
|
|
772 |
|
|
|
773 |
$include_modules = false;
|
|
|
774 |
$data = local_cesa_reportes_get_data_for_report_or_excel_custom($filter_by_company_active, $filter_userids, $filter_courseids, $filter_userkeyword, $filter_from, $filter_to, $include_modules, $page = 1);
|
|
|
775 |
$records = $data['items'];
|
|
|
776 |
|
|
|
777 |
$context['pagination'] = $data['pagination'];
|
|
|
778 |
if($context['pagination']['page'] > 1) {
|
|
|
779 |
$context['pagination']['previous_page'] = $context['pagination']['page'] - 1;
|
|
|
780 |
|
|
|
781 |
|
|
|
782 |
}
|
|
|
783 |
if($context['pagination']['page'] < $context['pagination']['max_pages']) {
|
|
|
784 |
$context['pagination']['next_page'] = $context['pagination']['page'] + 1;
|
|
|
785 |
|
|
|
786 |
|
|
|
787 |
}
|
|
|
788 |
|
|
|
789 |
|
|
|
790 |
|
|
|
791 |
foreach($records as $record)
|
|
|
792 |
{
|
|
|
793 |
if(!$record->courses) {
|
|
|
794 |
continue;
|
|
|
795 |
}
|
|
|
796 |
|
|
|
797 |
foreach($record->courses as $course)
|
|
|
798 |
{
|
|
|
799 |
if(isset($course->activities)) {
|
|
|
800 |
|
|
|
801 |
foreach($course->activities as $activity)
|
|
|
802 |
{
|
|
|
803 |
$item = [];
|
|
|
804 |
array_push($item, ['value' => $record->lastname ]);
|
|
|
805 |
array_push($item, ['value' => $record->firstname ]);
|
|
|
806 |
|
|
|
807 |
array_push($item, ['value' => $record->email ]);
|
|
|
808 |
array_push($item, ['value' => $course->name ]);
|
|
|
809 |
array_push($item, ['value' => $course->category ]);
|
|
|
810 |
array_push($item, ['value' => empty($course->timeenrolled) ? '' : date('d/m/Y h:i a', $course->timeenrolled)]);
|
|
|
811 |
array_push($item, ['value' => empty($course->timecompleted) ? '' : date('d/m/Y h:i a', $course->timecompleted)]);
|
|
|
812 |
array_push($item, ['value' => $course->activities_assigned, 'align-right' => true ]);
|
|
|
813 |
array_push($item, ['value' => $course->activities_completed, 'align-right' => true ]);
|
|
|
814 |
array_push($item, ['value' => number_format($course->percentage, 2), 'align-right' => true ]);
|
|
|
815 |
array_push($item, ['value' => empty($course->time_activities_first) ? '' : date('d/m/Y h:i a', $course->time_activities_first) ]);
|
|
|
816 |
array_push($item, ['value' => empty($course->time_activities_last) ? '' : date('d/m/Y h:i a', $course->time_activities_last) ]);
|
|
|
817 |
|
|
|
818 |
|
|
|
819 |
array_push($item, ['value' => $activity->module ]);
|
|
|
820 |
array_push($item, ['value' => empty($activity->timecompleted) ? '' : date('d/m/Y h:i a', $activity->timecompleted) ]);
|
|
|
821 |
|
|
|
822 |
|
|
|
823 |
foreach($extra_fields as $extra_field)
|
|
|
824 |
{
|
|
|
825 |
$value = '';
|
|
|
826 |
|
|
|
827 |
foreach($record->extras as $extra)
|
|
|
828 |
{
|
|
|
829 |
if($extra->id == $extra_field->id) {
|
|
|
830 |
$value = $extra->value;
|
|
|
831 |
}
|
|
|
832 |
}
|
|
|
833 |
array_push($item, ['value' => $value ]);
|
|
|
834 |
}
|
|
|
835 |
|
|
|
836 |
array_push($context['items'], ['item' => $item]);
|
|
|
837 |
}
|
|
|
838 |
} else {
|
|
|
839 |
$item = [];
|
|
|
840 |
array_push($item, ['value' => $record->lastname ]);
|
|
|
841 |
array_push($item, ['value' => $record->firstname ]);
|
|
|
842 |
|
|
|
843 |
array_push($item, ['value' => $record->email ]);
|
|
|
844 |
array_push($item, ['value' => $course->name ]);
|
|
|
845 |
array_push($item, ['value' => $course->category ]);
|
|
|
846 |
array_push($item, ['value' => empty($course->timeenrolled) ? '' : date('d/m/Y h:i a', $course->timeenrolled)]);
|
|
|
847 |
array_push($item, ['value' => empty($course->timecompleted) ? '' : date('d/m/Y h:i a', $course->timecompleted)]);
|
|
|
848 |
array_push($item, ['value' => $course->activities_assigned, 'align-right' => true ]);
|
|
|
849 |
array_push($item, ['value' => $course->activities_completed, 'align-right' => true ]);
|
|
|
850 |
array_push($item, ['value' => number_format($course->percentage, 2), 'align-right' => true ]);
|
|
|
851 |
array_push($item, ['value' => empty($course->time_activities_first) ? '' : date('d/m/Y h:i a', $course->time_activities_first) ]);
|
|
|
852 |
array_push($item, ['value' => empty($course->time_activities_last) ? '' : date('d/m/Y h:i a', $course->time_activities_last) ]);
|
|
|
853 |
|
|
|
854 |
foreach($extra_fields as $extra_field)
|
|
|
855 |
{
|
|
|
856 |
$value = '';
|
|
|
857 |
|
|
|
858 |
foreach($record->extras as $extra)
|
|
|
859 |
{
|
|
|
860 |
|
|
|
861 |
|
|
|
862 |
if($extra->id == $extra_field->id) {
|
|
|
863 |
$value = $extra->value;
|
|
|
864 |
}
|
|
|
865 |
}
|
|
|
866 |
array_push($item, ['value' => $value ]);
|
|
|
867 |
}
|
|
|
868 |
|
|
|
869 |
array_push($context['items'], ['item' => $item]);
|
|
|
870 |
}
|
|
|
871 |
}
|
|
|
872 |
}
|
|
|
873 |
|
|
|
874 |
|
|
|
875 |
$data = $OUTPUT->render_from_template('local_cesa_reportes/report_custom', $context);
|
|
|
876 |
return json_encode(['success' => true, 'data' => $data]);
|
|
|
877 |
|
|
|
878 |
}
|
|
|
879 |
|
|
|
880 |
public static function report_custom_returns()
|
|
|
881 |
{
|
|
|
882 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
883 |
|
|
|
884 |
}
|
|
|
885 |
|
|
|
886 |
public static function report_excel_custom_parameters()
|
|
|
887 |
{
|
|
|
888 |
return new \external_function_parameters([
|
|
|
889 |
'filter_from' => new \external_value(PARAM_ALPHANUMEXT, 'filter_from', VALUE_DEFAULT, ''),
|
|
|
890 |
'filter_to' => new \external_value(PARAM_ALPHANUMEXT, 'filter_to', VALUE_DEFAULT, ''),
|
|
|
891 |
'filter_userkeyword' => new \external_value(PARAM_RAW, 'filter_userkeyword', VALUE_DEFAULT, ''),
|
|
|
892 |
]);
|
|
|
893 |
}
|
|
|
894 |
|
|
|
895 |
public static function report_excel_custom($filter_from, $filter_to, $filter_userkeyword)
|
|
|
896 |
{
|
|
|
897 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
898 |
|
|
|
899 |
|
|
|
900 |
require_login();
|
|
|
901 |
require_capability('local/cesa_reportes:view', context_system::instance());
|
|
|
902 |
$PAGE->set_context(context_system::instance());
|
|
|
903 |
|
|
|
904 |
|
|
|
905 |
$data = [
|
|
|
906 |
'request_userid' => $USER->id,
|
|
|
907 |
'request_time' => time(),
|
|
|
908 |
'generate_time' => 0,
|
|
|
909 |
'status' => self::_STATUS_PENDING,
|
|
|
910 |
'parameters' => json_encode([
|
|
|
911 |
'type' => 'custom',
|
|
|
912 |
'filter_from' => $filter_from,
|
|
|
913 |
'filter_to' => $filter_to,
|
|
|
914 |
'filter_userkeyword' => $filter_userkeyword,
|
|
|
915 |
]),
|
|
|
916 |
'filename' => '',
|
|
|
917 |
];
|
|
|
918 |
|
|
|
919 |
if($DB->insert_record('local_cesa_reportes', $data)) {
|
|
|
920 |
return json_encode(['success' => true, 'data' => 'La petición de generación del reporte ha sido agregada']);
|
|
|
921 |
} else {
|
|
|
922 |
return json_encode(['success' => false, 'data' => 'No se pudo agregar la petición de generación del reporte']);
|
|
|
923 |
}
|
|
|
924 |
|
|
|
925 |
}
|
|
|
926 |
|
|
|
927 |
public static function report_excel_custom_returns()
|
|
|
928 |
{
|
|
|
929 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
930 |
|
|
|
931 |
}
|
|
|
932 |
|
|
|
933 |
}
|