-
Notifications
You must be signed in to change notification settings - Fork 7
/
locallib.php
491 lines (431 loc) · 15.2 KB
/
locallib.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?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/>.
/**
* Plugin administration pages are defined here.
*
* @package local_edwiserreports
* @category admin
* @copyright 2019 wisdmlabs <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . "/cohort/lib.php");
require_once($CFG->dirroot . "/local/edwiserreports/classes/constants.php");
/**
* Get Export Link to export data from blocks and individual page
* @param string $url Url prifix to get export link
* @param object $data Object for additional data
* @return string Moodle link detail
*/
function local_edwiserreports_get_block_exportlinks($url, $data) {
$links = new stdClass();
/* Course Progress Filter Id*/
if (isset($data->firstcourseid)) {
$cpfilter = $data->firstcourseid;
} else {
$cpfilter = false;
}
if (isset($data->firstlpid)) {
$lpfilter = $data->firstlpid;
} else {
$lpfilter = false;
}
$region = "block";
$links->blockactiveusers = local_edwiserreports_get_exportlinks($url, $region, "activeusers", "weekly");
$links->blockactivecourses = local_edwiserreports_get_exportlinks($url, $region, "activecourses");
$links->blockcourseprogress = local_edwiserreports_get_exportlinks($url, $region, "courseprogress", $cpfilter);
$links->blockcertificates = local_edwiserreports_get_exportlinks($url, $region, "certificates");
$links->blockf2fsessions = local_edwiserreports_get_exportlinks($url, $region, "f2fsession");
$links->blocklpstats = local_edwiserreports_get_exportlinks($url, $region, "lpstats", $lpfilter);
$links->blockinactiveusers = local_edwiserreports_get_exportlinks(
$url,
$region,
"inactiveusers",
"never",
false,
false,
"mt-20"
);
return $links;
}
/**
* Get Export Link to export link array
* @param string $url Url url for export link
* @param string $region Region for export
* @param string $blockname Block to export
* @param string $filter Filter for data to export
* @param int $cohortid Cohort id
* @param string $action Action of a page report
* @param string $customclass Custom class
* @return array Array of export link
*/
function local_edwiserreports_get_exportlinks(
$url,
$region,
$blockname,
$filter = false,
$cohortid = false,
$action = false,
$customclass = ''
) {
$params = array(
"region" => $region,
"blockname" => $blockname
);
if ($action !== false) {
$params["action"] = $action;
}
if ($filter !== false) {
$params["filter"] = $filter;
}
if ($cohortid !== false) {
$params["cohortid"] = $cohortid;
}
$out = new stdClass();
$out->export = local_edwiserreports_get_exportlink_array($url, $blockname, $params, $region);
$out->customclass = $customclass;
return $out;
}
/**
* Get Export Link to export link array
* @param string $url Url for export link
* @param string $blockname Block to export
* @param array $params Prameters for link
* @param string $region Block region
* @return array Array of export link
*/
function local_edwiserreports_get_exportlink_array($url, $blockname, $params, $region) {
$context = context_system::instance();
return array(
array(
"name" => get_string("csv", "local_edwiserreports"),
"icon" => "file-o",
"link" => (new moodle_url($url, array_merge(array("format" => "csv"), $params)))->out(),
"action" => 'csv',
"blockname" => $blockname,
"region" => $region,
"contextid" => $context->id
),
array(
"name" => get_string("excel", "local_edwiserreports"),
"icon" => "file-excel-o",
"link" => (new moodle_url($url, array_merge(array("format" => "excel"), $params)))->out(),
"action" => 'excel',
"blockname" => $blockname,
"region" => $region,
"contextid" => $context->id
),
array(
"name" => get_string("pdf", "local_edwiserreports"),
"icon" => "file-pdf-o",
"link" => (new moodle_url($url, array_merge(array("format" => "pdf"), $params)))->out(),
"action" => 'pdf',
"blockname" => $blockname,
"region" => $region,
"contextid" => $context->id
),
array(
"name" => get_string("email", "local_edwiserreports"),
"icon" => "envelope-o",
"link" => (new moodle_url($url, array_merge(array("format" => "email"), $params)))->out(),
"action" => 'email',
"blockname" => $blockname,
"region" => $region,
"contextid" => $context->id
),
array(
"name" => get_string("emailscheduled", "local_edwiserreports"),
"icon" => "envelope-o",
"link" => (new moodle_url($url, array_merge(array("format" => "emailscheduled"), $params)))->out(),
"action" => 'emailscheduled',
"blockname" => $blockname,
"region" => $region,
"contextid" => $context->id,
"sesskey" => sesskey()
)
);
}
/**
* Get Users Filter for filer the data
* @param bool $customfields Custom Fields
* @param bool $cohortfilter Cohort Filters
* @param bool $rangeselector Range Selector
* @return array Array of filters
*/
function local_edwiserreports_get_userfilters($customfields, $cohortfilter, $rangeselector) {
$userfilters = new stdClass();
if ($cohortfilter) {
$userfilters->cohortfilter = local_edwiserreports_get_cohort_filter();
}
if ($rangeselector) {
$userfilters->rangeselector = true;
}
return $userfilters;
}
/**
* Get Cohort filter Filter for filer the data
* @return object Array of Cohort filters
*/
function local_edwiserreports_get_cohort_filter() {
global $DB, $USER;
// Fetch all cohorts
// passing 0,0 -> page_number, number of record, 0 means.
$allcohorts = cohort_get_all_cohorts(0, 0);
$usercontext = context_user::instance($USER->id);
$cohorts = [];
// Users visibility check.
foreach ($allcohorts['cohorts'] as $key => $value) {
if (cohort_can_view_cohort($key, $usercontext)) {
$tempval = $value;
$tempval->name = format_string($value->name, true, ['context' => \context_system::instance()]);
$cohorts[] = $value;
}
}
if (empty($cohorts)) {
// Returning false if no cohorts are present.
return false;
}
$cohortfilter = new stdClass();
$cohortfilter->values = array_merge([['id' => 0, 'name' => get_string('allcohorts', 'local_edwiserreports')]], $cohorts);
return $cohortfilter;
}
/**
* Create individual pageheader
* @param string $blockname Block name
* @param string $coursename Course name
* @return string HTML header string
*/
function local_edwiserreports_create_page_header($blockname, $coursename = false) {
global $CFG;
// Create backurl.
$backurl = $CFG->wwwroot . "/local/edwiserreports/";
$component = "local_edwiserreports";
// Start page header.
$out = html_writer::start_div("d-md-flex mb-10", array("id" => "esr-page-header"));
// Back button link.
$out .= html_writer::start_div("");
$out .= html_writer::link($backurl,
'<i class="icon fa fa-arrow-left"></i>',
array(
"class" => "btn btn-sm btn-default",
"data-toggle" => "tooltip",
"data-original-title" => get_string("back"),
"data-placement" => "bottom"
)
);
$out .= html_writer::end_div();
// If coursename then send as param in getstring.
$params = array();
if ($coursename) {
$params["coursename"] = $coursename;
}
// Create header.
$out .= html_writer::span(get_string($blockname . "header", $component, $params), "px-md-10");
// End pageheader.
$out .= html_writer::end_div();
// Return output.
return $out;
}
/**
* Create back button for each individual page
* @param object $backurl Moodle Url Object
* @return string Html string for back button
*/
function local_edwiserreports_create_back_button($backurl) {
$html = html_writer::div(
html_writer::link(
$backurl,
html_writer::tag(
"i", "", array(
"class" => "icon fa fa-arrow-left",
"aria-hidden" => "true"
)
),
array(
"class" => "btn btn-sm btn-default",
"data-toggle" => "tooltip",
"data-original-title" => get_string("back"),
"data-placement" => "bottom"
)
),
"mb-10", array ("id" => "wdm_reportback_button")
);
return $html;
}
/**
* If the moodle has plugin then return true
* @param string $plugintype Plugin Type
* @param string $pluginname Plugin Name
* @return boolean True|False based on plugin exist
*/
function local_edwiserreports_has_plugin($plugintype, $pluginname) {
$plugins = core_plugin_manager::instance()->get_plugins_of_type($plugintype);
return array_key_exists($pluginname, $plugins);
}
/**
* Prepare export filename
* @param array $params Params to prepare filename
* @return string Filename
*/
function local_edwiserreports_prepare_export_filename($params) {
if (isset($params['filter'])) {
$filter = $params['filter'];
$filter = json_decode($filter, true);
if (is_array($filter)) {
$filtered = [];
foreach ($filter as $key => $value) {
$filtered[] = $key . '-' . $value;
}
$filter = implode(',', $filtered);
$params['filter'] = $filter;
}
}
return "report_" . implode("_", $params);
}
/**
* Get required strings for js
*/
function local_edwiserreports_get_required_strings_for_js() {
global $PAGE, $USER;
$stringman = get_string_manager();
$strings = $stringman->load_component_strings('local_edwiserreports', 'en');
$PAGE->requires->strings_for_js(array_keys($strings), 'local_edwiserreports');
// Require string from role component.
$str = array(
'inherit',
'allow',
'prevent',
'prohibit'
);
$PAGE->requires->strings_for_js($str, 'role');
// Require string from role component.
$str = array(
'loading',
'next',
'previous',
'yes',
'no'
);
$PAGE->requires->strings_for_js($str, 'moodle');
// Require string for column data.
$str = [
"notyetstarted",
"completed",
"inprogress"
];
$PAGE->requires->strings_for_js($str, 'core_completion');
}
/**
* Reset Site Report Page to default
*/
function reset_edwiserreports_page_default() {
global $CFG, $DB, $USER;
$blocks = \local_edwiserreports\utility::get_reports_block();
foreach ($blocks as $block) {
$prefname = 'pref_' . $block->classname;
$DB->delete_records('user_preferences', array('userid' => $USER->id, 'name' => $prefname));
unset($USER->preference[$prefname]);
}
$customreports = $DB->get_records('edwreports_custom_reports');
foreach ($customreports as $block) {
$prefname = 'pref_customreportsblock-' . $block->id;
$DB->delete_records('user_preferences', array('userid' => $USER->id, 'name' => $prefname));
unset($USER->preference[$prefname]);
}
redirect($CFG->wwwroot . '/local/edwiserreports/index.php');
}
/**
* Check if any of the blocks are present in reports dashboard.
*/
function is_block_present_indashboard() {
// Check if any of the block is present.
$hasblock = false;
$blocks = \local_edwiserreports\utility::get_reports_block();
foreach ($blocks as $key => $block) {
if ($block->classname == 'customreportsblock') {
if (can_view_block('customreportsroleallow-' . $block->id)) {
$hasblock = true;
}
} else {
$capname = 'report/edwiserreports_' . $block->classname . ':view';
if (has_capability($capname, context_system::instance()) ||
can_view_block($capname)) {
$hasblock = true;
continue;
}
}
}
return $hasblock;
}
/**
* Check if user has course level role in the system
* @param [int] $userid Users Id
* @param [string] $roleshortname Role Short Name
* @return [boolean] Status
*/
function has_user_role($userid, $roleshortname) {
global $DB;
$roleid = $DB->get_field('role', 'id', array('shortname' => $roleshortname));
return $DB->record_exists('role_assignments', ['userid' => $userid, 'roleid' => $roleid]);
}
/**
* Function to get the users role in any courses
* @param String $capname Capability name
*/
function can_view_block($capname) {
global $DB, $USER;
$canviewblocks = false;
if (strpos($capname, 'customreportsroleallow') !== false) {
$configstr = get_config('local_edwiserreports', $capname);
if ($configstr) {
$roleids = explode(',', $configstr);
list($insql, $inparams) = $DB->get_in_or_equal($roleids, SQL_PARAMS_NAMED);
$sql = 'SELECT * FROM {role} WHERE id ' . $insql;
$allowedrole = $DB->get_records_sql($sql, $inparams);
} else {
$allowedarchetype = array('mamnager', 'coursecreator');
list($insql, $inparams) = $DB->get_in_or_equal($allowedarchetype, SQL_PARAMS_NAMED);
$sql = 'SELECT * FROM {role} WHERE archetype ' . $insql;
$allowedrole = $DB->get_records_sql($sql, $inparams);
}
} else {
$allowedrole = get_roles_with_capability($capname, CAP_ALLOW);
}
foreach ($allowedrole as $role) {
if (has_user_role($USER->id, $role->shortname)) {
$canviewblocks = true;
continue;
}
}
return $canviewblocks;
}
/**
* Checking whether current user can edit capability of block.
* @param String $capname Capability name
*/
function can_edit_capability($capname) {
global $USER;
if (is_siteadmin($USER)) {
return true;
}
$allowedrole = get_roles_with_capability($capname, CAP_ALLOW);
foreach ($allowedrole as $role) {
if (has_user_role($USER->id, $role->shortname)) {
return true;
}
}
return false;
}