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 |
* Version details
|
|
|
19 |
*
|
|
|
20 |
* Configurable Reports - A Moodle block for creating customizable reports
|
|
|
21 |
*
|
|
|
22 |
* @package block_configurable_reports
|
|
|
23 |
* @copyright Daniel Neis Araujo <danielneis@gmail.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace block_configurable_reports;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
require_once("$CFG->libdir/externallib.php");
|
|
|
32 |
|
|
|
33 |
use external_api;
|
|
|
34 |
use external_function_parameters;
|
|
|
35 |
use external_value;
|
|
|
36 |
use external_single_structure;
|
|
|
37 |
use external_multiple_structure;
|
|
|
38 |
use external_warnings;
|
|
|
39 |
use context_course;
|
|
|
40 |
use context_system;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* This is the external API for this component.
|
|
|
44 |
*
|
|
|
45 |
* @copyright Daniel Neis Araujo <danielneis@gmail.com>
|
|
|
46 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
47 |
*/
|
|
|
48 |
class external extends external_api {
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* get_report_data parameters.
|
|
|
52 |
*
|
|
|
53 |
* @return external_function_parameters
|
|
|
54 |
*/
|
|
|
55 |
public static function get_report_data_parameters() {
|
|
|
56 |
return new external_function_parameters(
|
|
|
57 |
array(
|
|
|
58 |
'reportid' => new external_value(PARAM_INT, 'The report id', VALUE_REQUIRED),
|
|
|
59 |
'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_OPTIONAL)
|
|
|
60 |
)
|
|
|
61 |
);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Returns data of given report id.
|
|
|
66 |
*
|
|
|
67 |
* @param int $reportid
|
|
|
68 |
* @param int $courseid
|
|
|
69 |
* @return array An array with a 'data' JSON string and a 'warnings' string
|
|
|
70 |
*/
|
|
|
71 |
public static function get_report_data($reportid, $courseid = 1) {
|
|
|
72 |
global $CFG, $DB;
|
|
|
73 |
|
|
|
74 |
$params = self::validate_parameters(
|
|
|
75 |
self::get_report_data_parameters(),
|
|
|
76 |
array('reportid' => $reportid, 'courseid' => $courseid)
|
|
|
77 |
);
|
|
|
78 |
|
|
|
79 |
if ($courseid == SITEID) {
|
|
|
80 |
$context = context_system::instance();
|
|
|
81 |
} else {
|
|
|
82 |
$context = context_course::instance($courseid);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
self::validate_context($context);
|
|
|
86 |
|
|
|
87 |
$json = [];
|
|
|
88 |
$warnings = '';
|
|
|
89 |
if (!$report = $DB->get_record('block_configurable_reports', ['id' => $reportid])) {
|
|
|
90 |
$warnings = get_string('reportdoesnotexists', 'block_configurable_reports');
|
|
|
91 |
} else {
|
|
|
92 |
|
|
|
93 |
require_once($CFG->dirroot.'/blocks/configurable_reports/locallib.php');
|
|
|
94 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
95 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
|
|
|
96 |
|
|
|
97 |
$reportclassname = 'report_'.$report->type;
|
|
|
98 |
$reportclass = new $reportclassname($report);
|
|
|
99 |
if (!$reportclass->check_permissions($USER->id, $context)) {
|
|
|
100 |
$warnings = get_string('badpermissions', 'block_configurable_reports');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$reportclass->create_report();
|
|
|
104 |
$table = $reportclass->finalreport->table;
|
|
|
105 |
$headers = $table->head;
|
|
|
106 |
foreach ($table->data as $data) {
|
|
|
107 |
$jsonobject = [];
|
|
|
108 |
foreach ($data as $index => $value) {
|
|
|
109 |
$jsonobject[$headers[$index]] = $value;
|
|
|
110 |
}
|
|
|
111 |
$json[] = $jsonobject;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return array('data' => json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), 'warnings' => $warnings);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* get_report_data return
|
|
|
120 |
*
|
|
|
121 |
* @return external_description
|
|
|
122 |
*/
|
|
|
123 |
public static function get_report_data_returns() {
|
|
|
124 |
return new external_single_structure(
|
|
|
125 |
array(
|
|
|
126 |
'data' => new external_value(PARAM_RAW, 'JSON-formatted report data'),
|
|
|
127 |
'warnings' => new external_value(PARAM_TEXT, 'Warning message'),
|
|
|
128 |
)
|
|
|
129 |
);
|
|
|
130 |
}
|
|
|
131 |
}
|