forked from intelliboard/intellidata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
executable file
·147 lines (130 loc) · 4.17 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
<?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/>.
/**
* Extra library for intellidata plugin.
*
* @package local_intellidata
* @subpackage intellidata
* @copyright 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_intellidata\api\apilib;
use local_intellidata\helpers\DBHelper;
use local_intellidata\helpers\ParamsHelper;
use local_intellidata\helpers\TrackingHelper;
use local_intellidata\helpers\SettingsHelper;
use local_intellidata\services\encryption_service;
/**
* Return pluginfile URL.
*
* @param $course
* @param $cm
* @param $context
* @param $filearea
* @param $args
* @param $forcedownload
* @param array $options
* @return false|void
* @throws coding_exception
* @throws moodle_exception
* @throws require_login_exception
* @throws required_capability_exception
*/
function local_intellidata_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
global $CFG, $PAGE;
require_once($CFG->dirroot . '/repository/lib.php');
// Additional auth validation.
if (stristr($PAGE->url, '/webservice/pluginfile.php')) {
try {
apilib::check_auth();
} catch (\moodle_exception $e) {
send_file_not_found();
}
} else {
require_login();
require_capability('local/intellidata:viewlogs', $context);
}
$itemid = array_shift($args);
$filename = array_shift($args);
$filepath = '/';
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'local_intellidata', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // The file does not exist.
}
if (stristr($PAGE->url, '/webservice/pluginfile.php')) {
send_stored_file($file, 86400, 0, $forcedownload, $options);
} else {
$encryptionservice = new encryption_service();
$enczipfile = $file->copy_content_to_temp();
// Prepare temp area.
$tempfolder = make_temp_directory('local_intellidata');
$tempfile = $tempfolder . '/' . $file->get_filename();
$encryptionservice->decrypt_file($enczipfile, $tempfile);
// Rename file to human friendly.
$zip = new ZipArchive;
$zip->open($tempfile);
$zip->renameIndex( 0, $filearea . '.csv');
$zip->close();
send_file($tempfile, $filearea . '_' . $filename, 86400, 0, false, $forcedownload, '', true, $options);
unlink($enczipfile);
unlink($tempfile);
die();
}
}
/**
* Add IntelliData LTI menu to the navigation.
*
* @param global_navigation $nav
* @throws dml_exception
*/
function local_intellidata_extend_navigation(global_navigation $nav) {
(new \local_intellidata\helpers\CustomMenuHelper())->setup($nav);
local_intellidata_tracking_init();
}
/**
* Return custom sidebar icon.
*
* @return string[]
*/
function local_intellidata_get_fontawesome_icon_map() {
return [
'local_intellidata:i/area_chart' => 'fa-area-chart',
];
}
/**
* Init IntelliBoard tracking.
*
* @throws dml_exception
*/
function local_intellidata_tracking_init() {
if (TrackingHelper::tracking_enabled()) {
$tracking = new \local_intellidata\services\tracking_service();
$tracking->track();
}
}
/**
* Allow plugins to callback as soon possible after setup.php is loaded.
*
* @return void
* @throws dml_exception
*/
function local_intellidata_after_config() {
global $DB;
if (TrackingHelper::new_tracking_enabled()) {
$DB = DBHelper::get_db_client(DBHelper::PENETRATION_TYPE_EXTERNAL);
}
}