This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.php
261 lines (218 loc) · 7.99 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
<?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 version and other meta-data are defined here.
*
* @package tool_policy
* @copyright 2018 David Mudrák <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_user\output\myprofile\tree;
use tool_policy\api;
use tool_policy\policy_version;
/**
* Add nodes to myprofile page.
*
* @param tree $tree Tree object
* @param stdClass $user User object
* @param bool $iscurrentuser
* @param stdClass $course Course object
* @return bool
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function tool_policy_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
global $CFG;
// Do nothing if we are not set as the site policies handler.
if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
return;
}
// Get the Privacy and policies category.
if (!array_key_exists('privacyandpolicies', $tree->__get('categories'))) {
// Create the category.
$categoryname = get_string('privacyandpolicies', 'admin');
$category = new core_user\output\myprofile\category('privacyandpolicies', $categoryname, 'contact');
$tree->add_category($category);
} else {
// Get the existing category.
$category = $tree->__get('categories')['privacyandpolicies'];
}
// Add "Policies and agreements" node only for current user or users who can accept on behalf of current user.
$usercontext = \context_user::instance($user->id);
if ($iscurrentuser || has_capability('tool/policy:acceptbehalf', $usercontext)) {
$url = new moodle_url('/admin/tool/policy/user.php', ['userid' => $user->id]);
$node = new core_user\output\myprofile\node('privacyandpolicies', 'tool_policy',
get_string('policiesagreements', 'tool_policy'), null, $url);
$category->add_node($node);
}
return true;
}
/**
* Load policy message for guests.
*
* @return string The HTML code to insert before the head.
*/
function tool_policy_before_standard_html_head() {
global $CFG, $PAGE, $USER;
$message = null;
if (!empty($CFG->sitepolicyhandler)
&& $CFG->sitepolicyhandler == 'tool_policy'
&& empty($USER->policyagreed)
&& (isguestuser() || !isloggedin())) {
$output = $PAGE->get_renderer('tool_policy');
$page = new \tool_policy\output\guestconsent();
$message = $output->render($page);
}
return $message;
}
/**
* Callback to add footer elements.
*
* @return str valid html footer content
*/
function tool_policy_standard_footer_html() {
global $CFG;
$output = '';
if (!empty($CFG->sitepolicyhandler)
&& $CFG->sitepolicyhandler == 'tool_policy') {
$policies = api::get_current_versions_ids();
if (!empty($policies)) {
$url = (new moodle_url('/admin/tool/policy/viewall.php'))->out();
$output .= html_writer::link($url, get_string('userpolicysettings', 'tool_policy'));
$output = html_writer::div($output, 'policiesfooter');
}
}
return $output;
}
/**
* Hooks redirection to policy acceptance pages before sign up.
*/
function tool_policy_pre_signup_requests() {
global $CFG;
// Do nothing if we are not set as the site policies handler.
if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
return;
}
$policies = api::get_current_versions_ids(policy_version::AUDIENCE_LOGGEDIN);
$userpolicyagreed = cache::make('core', 'presignup')->get('tool_policy_userpolicyagreed');
if (!empty($policies) && !$userpolicyagreed) {
// Redirect to "Policy" pages for consenting before creating the user.
cache::make('core', 'presignup')->set('tool_policy_issignup', 1);
redirect(new \moodle_url('/admin/tool/policy/index.php'));
}
}
/**
* Serve the embedded files.
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file not found, just send the file otherwise and do not return anything
*/
function tool_policy_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $CFG, $PAGE;
// Do not allow access to files if we are not set as the site policy handler.
if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
return false;
}
if ($context->contextlevel != CONTEXT_SYSTEM) {
return false;
}
$PAGE->set_context($context);
if ($filearea !== 'policydocumentsummary' && $filearea !== 'policydocumentcontent') {
return false;
}
$itemid = array_shift($args);
$policy = api::get_policy_version($itemid);
if ($policy->status != policy_version::STATUS_ACTIVE) {
require_login();
}
if (!api::can_user_view_policy_version($policy)) {
return false;
}
$filename = array_pop($args);
if (!$args) {
$filepath = '/';
} else {
$filepath = '/'.implode('/', $args).'/';
}
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'tool_policy', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false;
}
send_stored_file($file, null, 0, $forcedownload, $options);
}
/**
* Map icons for font-awesome themes.
*/
function tool_policy_get_fontawesome_icon_map() {
return [
'tool_policy:agreedno' => 'fa-times text-danger',
'tool_policy:agreedyes' => 'fa-check text-success',
'tool_policy:agreedyesonbehalf' => 'fa-check text-info',
'tool_policy:level' => 'fa-level-up fa-rotate-90 text-muted',
];
}
/**
* Serve the new group form as a fragment.
*
* @param array $args List of named arguments for the fragment loader.
* @return string
*/
function tool_policy_output_fragment_accept_on_behalf($args) {
$args = (object) $args;
$data = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
parse_str($serialiseddata, $data);
}
$mform = new \tool_policy\form\accept_policy(null, $data);
if (!empty($args->jsonformdata)) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$mform->is_validated();
}
return $mform->render();
}
/**
* Custom environment check invoked from environment.xml.
*
* Make sure that the plugin is not present in Moodle that has it already merged into the core.
*
* @param environment_results $result
* @return environment_results|null
*/
function tool_policy_version_check(environment_results $result) {
global $CFG;
if (!empty($CFG->tool_policy_disable_version_check)) {
return null;
}
$version = null;
$branch = null;
require($CFG->dirroot.'/version.php');
if (($branch == 33 && $version >= 2017051507.02) || ($branch == 34 && $version >= 2017111304.05) || $branch >= 35) {
$result->setStatus(false);
return $result;
} else {
return null;
}
}