/
lib.php
350 lines (324 loc) · 13.9 KB
/
lib.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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/>.
/**
* The interface library of Learnerscript
*
* @package block_learnerscript
* @copyright 2023 Moodle India Information Solutions Private Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_learnerscript\local\ls;
use block_learnerscript\local\schedule;
/**
* Learnerscript block plugin file
* @param object $course Course data
* @param object $cm Course module data
* @param object $context Context data
* @param string $filearea File filter area
* @param array $args Plugin file arguments
* @param boolean $forcedownload Force download
* @param array $options File options
*/
function block_learnerscript_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
global $CFG;
if ($filearea == 'logo') {
$itemid = (int) array_shift($args);
if ($itemid > 0) {
return false;
}
$fs = get_file_storage();
$filename = array_pop($args);
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
$file = $fs->get_file($context->id, 'block_learnerscript', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false;
}
$filedata = $file->resize_image(200, 200);
\core\session\manager::write_close();
send_stored_file($file, null, 0, 1);
}
send_file_not_found();
}
/**
* PDF export report header image path
* @param boolean $excel Report header
* @return string
*/
function get_reportheader_imagepath($excel = false) {
global $CFG;
$fs = get_file_storage();
$syscontext = context_system::instance();
$reportheaderimagepath = '';
// Now get the full list of stamp files for this instance.
if ($files = $fs->get_area_files($syscontext->id, 'block_learnerscript', 'logo', 0,
'filename', false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
if ($filename !== '.') {
if ($excel) {
$reportheaderimagepath = $CFG->wwwroot . '/blocks/learnerscript/pix/logo.jpg';
} else {
$url = moodle_url::make_pluginfile_url($syscontext->id,
'block_learnerscript', 'logo', 0, '/', $file->get_filename(), false);
$reportheaderimagepath = $url->out();
}
}
}
} else {
$reportheaderimagepath = $CFG->wwwroot . '/blocks/learnerscript/pix/logo.jpg';
}
return $reportheaderimagepath;
}
/**
* Learnerscript reports schedule form
* @param array $args Schedule form arguments
*/
function block_learnerscript_schreportform_ajaxform($args) {
global $CFG, $DB, $OUTPUT, $PAGE, $USER;
$args = (object) $args;
$o = '';
$context = context_system::instance();
$reportid = $args->reportid;
$instance = $args->instance;
$scheduleid = 0;
$ajaxformdata = [];
if (!empty($args->jsonformdata)) {
parse_str($args->jsonformdata, $ajaxformdata);
if (!empty($ajaxformdata)) {
if (isset($ajaxformdata['users_data'])
&& !is_array($ajaxformdata['users_data'])) {
$ajaxformdata['schuserslist'] = $ajaxformdata['users_data'];
}
}
}
if ((has_capability('block/learnerscript:managereports', $context) ||
has_capability('block/learnerscript:manageownreports', $context) ||
is_siteadmin()) && !empty($reportid)) {
require_once($CFG->dirroot . '/blocks/learnerscript/components/scheduler/schedule_form.php');
$roleslist = (new schedule)->reportroles('', $reportid);
$schuserslist = !empty($ajaxformdata['schuserslist']) ? $ajaxformdata['schuserslist'] : [];
list($schusers, $schusersids) = (new schedule)->userslist($reportid, $scheduleid, $schuserslist);
$exportoptions = (new ls)->cr_get_export_plugins();
$frequencyselect = (new schedule)->get_options();
if (!empty($ajaxformdata['frequency']) && $ajaxformdata['frequency']) {
$schedulelist = (new schedule)->getschedule($ajaxformdata['frequency']);
} else {
$schedulelist = [null => get_string('selectall', 'block_reportdashboard')];
}
$scheduleurl = $CFG->wwwroot . '/blocks/learnerscript/components/scheduler/schedule.php';
$scheduleform = new scheduled_reports_form($scheduleurl, ['id' => $reportid,
'scheduleid' => $scheduleid, 'roles_list' => $roleslist,
'schusers' => $schusers, 'schusersids' => $schusersids,
'exportoptions' => $exportoptions,
'schedule_list' => $schedulelist,
'frequencyselect' => $frequencyselect,
'instance' => $instance, ], 'post', '', null, true, $ajaxformdata);
$setdata = new stdClass();
$setdata->schuserslist = $schusersids;
$setdata->users_data = explode(',', $schusersids);
$scheduleform->set_data($setdata);
if (!empty($ajaxformdata) && $scheduleform->is_validated()) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$validated = $scheduleform->is_validated();
$validateddata = $scheduleform->get_data();
if ($validateddata) {
try {
$fromform = new stdClass();
$formrole = explode('_', $ajaxformdata['role']);
$fromform->reportid = $ajaxformdata['reportid'];
$fromform->roleid = $formrole[0];
$fromform->sendinguserid = $ajaxformdata['schuserslist'];
$fromform->exportformat = $ajaxformdata['exportformat'];
$fromform->frequency = $ajaxformdata['frequency'];
$fromform->schedule = $ajaxformdata['schedule'];
$fromform->exporttofilesystem = $ajaxformdata['exporttofilesystem'];
$fromform->userid = $USER->id;
$fromform->nextschedule = (new schedule)->next($fromform);
$fromform->timemodified = time();
$fromform->timecreated = time();
if (array_key_exists(1, $formrole)) {
$fromform->contextlevel = $formrole[1];
} else {
$fromform->contextlevel = 10;
}
$schedule = $DB->insert_record('block_ls_schedule', $fromform);
$event = \block_learnerscript\event\schedule_report::create([
'objectid' => $fromform->reportid,
'context' => $context,
]);
$event->trigger();
return ['error' => false, 'data' => $validateddata];
} catch (dml_exception $ex) {
throw new moodle_exception($ex);
}
}
} else {
$output = [];
if (!empty($ajaxformdata)) {
$scheduleform->is_validated();
$output['formerror'] = true;
}
$OUTPUT->header();
$PAGE->start_collecting_javascript_requirements();
ob_start();
$scheduleform->display();
$o .= ob_get_contents();
ob_end_clean();
$data = $o;
$jsfooter = $PAGE->requires->get_end_code();
$output['error'] = false;
$output['html'] = $data;
$output['javascript'] = $jsfooter;
return $output;
}
}
}
/**
* Learnerscript reports send emails
* @param array $args Send emails arguments
*/
function block_learnerscript_sendreportemail_ajaxform($args) {
global $CFG, $DB, $OUTPUT, $PAGE, $USER;
$args = (object) $args;
$o = '';
$context = context_system::instance();
$reportid = $args->reportid;
$instance = $args->instance;
$scheduleid = 0;
$ajaxformdata = [];
if (!empty($args->jsonformdata)) {
parse_str($args->jsonformdata, $ajaxformdata);
if (!empty($ajaxformdata)) {
if (isset($ajaxformdata['email']) && (!is_array($ajaxformdata['email']) ||
$ajaxformdata['email'] == '_qf__force_multiselect_submission')) {
unset($ajaxformdata['email']);
}
}
}
if ((has_capability('block/learnerscript:managereports', $context) ||
has_capability('block/learnerscript:manageownreports', $context) ||
is_siteadmin()) && !empty($reportid)) {
require_once($CFG->dirroot . '/blocks/reportdashboard/email_form.php');
$emailform = new block_reportdashboard_emailform($CFG->wwwroot . '/blocks/reportdashboard/dashboard.php',
['reportid' => $reportid,
'AjaxForm' => true, 'instance' => $instance, 'ajaxformdata' => $ajaxformdata, ], 'post', '', null, true, $ajaxformdata, );
if (!empty($ajaxformdata) && $emailform->is_validated()) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$validated = $emailform->is_validated();
$validateddata = $emailform->get_data();
if ($validateddata) {
try {
$roleid = 0;
$rolecontext = 0;
if (!empty($SESSION->role)) {
$roleid = $DB->get_field('role', 'id', ['shortname' => $SESSION->role]);
$rolecontext = $SESSION->ls_contextlevel;
}
$data = new stdClass();
$userlist = implode(',', $ajaxformdata['email']);
$data->sendinguserid = $userlist;
$data->exportformat = $ajaxformdata['format'];
$data->frequency = -1;
$data->schedule = 0;
$data->exporttofilesystem = 1;
$data->reportid = $ajaxformdata['reportid'];
$data->timecreated = time();
$data->timemodified = 0;
$data->userid = $USER->id;
$data->roleid = $roleid;
$data->nextschedule = 0;
$data->contextlevel = $rolecontext;
$insert = $DB->insert_record('block_ls_schedule', $data);
return ['error' => false, 'data' => $validateddata];
} catch (dml_exception $ex) {
throw new moodle_exception($ex);
}
}
} else {
$output = [];
if (!empty($ajaxformdata)) {
$emailform->is_validated();
$output['formerror'] = true;
}
$OUTPUT->header();
$PAGE->start_collecting_javascript_requirements();
ob_start();
$emailform->display();
$o .= ob_get_contents();
ob_end_clean();
$data = $o;
$jsfooter = $PAGE->requires->get_end_code();
$output['error'] = false;
$output['html'] = $data;
$output['javascript'] = $jsfooter;
return $output;
}
}
}
/**
* Get roles in context level
* @param int $contextlevel User contextlevel
* @param array $excludedroles Roles to exclude report permissions
*/
function get_roles_in_context($contextlevel, $excludedroles = null) {
global $DB;
if ($contextlevel == 10) {
$contextroles = $DB->get_records_sql("SELECT DISTINCT roleid FROM {role_context_levels}");
list($contextsql, $params) = $DB->get_in_or_equal($contextroles, SQL_PARAMS_NAMED, 'param', false);
$systemroles = array_values(get_roles_for_contextlevels($contextlevel));
$iomadroles = $DB->get_records_sql_menu("SELECT id, shortname
FROM {role}
WHERE 1 = 1 AND id $contextsql", $params);
$allroles = array_merge($systemroles, array_keys($iomadroles));
} else {
$allroles = array_values(get_roles_for_contextlevels($contextlevel));
}
if (!empty($excludedroles)) {
$rolesexcluded = implode(',', array_values($excludedroles));
}
list($rolesql, $params) = $DB->get_in_or_equal($allroles);
list($excludesql, $params) = $DB->get_in_or_equal($excludedroles, SQL_PARAMS_NAMED, 'param', false);
if (!empty($rolesexcluded)) {
$roles = $DB->get_records_sql("SELECT id, shortname, name
FROM {role} WHERE id $rolesql
AND shortname $excludesql", $params);
} else {
$roles = $DB->get_records_sql("SELECT id, shortname, name
FROM {role} WHERE id $rolesql", $params);
}
$userroles = [];
foreach ($roles as $r) {
if ($r->shortname == 'guest' || $r->shortname == 'user' || $r->shortname == 'frontpage') {
continue;
}
if ($contextlevel == CONTEXT_SYSTEM && $r->shortname == 'manager') {
continue;
}
$userroles[$r->id] = role_get_name($r);
}
return $userroles;
}
/**
* Get page variables
*/
function get_pagevariables() {
global $PAGE;
return $PAGE;
}