2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Plugin version and other meta-data are defined here.
20 * @package tool_policy
21 * @copyright 2018 David Mudrák <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 use core_user\output\myprofile\tree;
29 use tool_policy\policy_version;
32 * Add nodes to myprofile page.
34 * @param tree $tree Tree object
35 * @param stdClass $user User object
36 * @param bool $iscurrentuser
37 * @param stdClass $course Course object
39 * @throws coding_exception
40 * @throws dml_exception
41 * @throws moodle_exception
43 function tool_policy_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
46 // Do nothing if we are not set as the site policies handler.
47 if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
51 // Get the Privacy and policies category.
52 if (!array_key_exists('privacyandpolicies', $tree->__get('categories'))) {
53 // Create the category.
54 $categoryname = get_string('privacyandpolicies', 'admin');
55 $category = new core_user\output\myprofile\category('privacyandpolicies', $categoryname, 'contact');
56 $tree->add_category($category);
58 // Get the existing category.
59 $category = $tree->__get('categories')['privacyandpolicies'];
62 // Add "Policies and agreements" node only for current user or users who can accept on behalf of current user.
63 $usercontext = \context_user::instance($user->id);
64 if ($iscurrentuser || has_capability('tool/policy:acceptbehalf', $usercontext)) {
65 $url = new moodle_url('/admin/tool/policy/user.php', ['userid' => $user->id]);
66 $node = new core_user\output\myprofile\node('privacyandpolicies', 'tool_policy',
67 get_string('policiesagreements', 'tool_policy'), null, $url);
68 $category->add_node($node);
75 * Load policy message for guests.
77 * @return string The HTML code to insert before the head.
79 function tool_policy_before_standard_html_head() {
80 global $CFG, $PAGE, $USER;
83 if (!empty($CFG->sitepolicyhandler)
84 && $CFG->sitepolicyhandler == 'tool_policy'
85 && empty($USER->policyagreed)
86 && (isguestuser() || !isloggedin())) {
87 $output = $PAGE->get_renderer('tool_policy');
88 $page = new \tool_policy\output\guestconsent();
90 $message = $output->render($page);
97 * Callback to add footer elements.
99 * @return str valid html footer content
101 function tool_policy_standard_footer_html() {
105 if (!empty($CFG->sitepolicyhandler)
106 && $CFG->sitepolicyhandler == 'tool_policy') {
107 $policies = api::get_current_versions_ids();
108 if (!empty($policies)) {
109 $url = (new moodle_url('/admin/tool/policy/viewall.php'))->out();
110 $output .= html_writer::link($url, get_string('userpolicysettings', 'tool_policy'));
111 $output = html_writer::div($output, 'policiesfooter');
119 * Hooks redirection to policy acceptance pages before sign up.
121 function tool_policy_pre_signup_requests() {
124 // Do nothing if we are not set as the site policies handler.
125 if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
129 $policies = api::get_current_versions_ids(policy_version::AUDIENCE_LOGGEDIN);
130 $userpolicyagreed = cache::make('core', 'presignup')->get('tool_policy_userpolicyagreed');
131 if (!empty($policies) && !$userpolicyagreed) {
132 // Redirect to "Policy" pages for consenting before creating the user.
133 cache::make('core', 'presignup')->set('tool_policy_issignup', 1);
134 redirect(new \moodle_url('/admin/tool/policy/index.php'));
139 * Serve the embedded files.
141 * @param stdClass $course the course object
142 * @param stdClass $cm the course module object
143 * @param stdClass $context the context
144 * @param string $filearea the name of the file area
145 * @param array $args extra arguments (itemid, path)
146 * @param bool $forcedownload whether or not force download
147 * @param array $options additional options affecting the file serving
148 * @return bool false if the file not found, just send the file otherwise and do not return anything
150 function tool_policy_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
153 // Do not allow access to files if we are not set as the site policy handler.
154 if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
158 if ($context->contextlevel != CONTEXT_SYSTEM) {
162 $PAGE->set_context($context);
164 if ($filearea !== 'policydocumentsummary' && $filearea !== 'policydocumentcontent') {
168 $itemid = array_shift($args);
170 $policy = api::get_policy_version($itemid);
172 if ($policy->status != policy_version::STATUS_ACTIVE) {
176 if (!api::can_user_view_policy_version($policy)) {
180 $filename = array_pop($args);
185 $filepath = '/'.implode('/', $args).'/';
188 $fs = get_file_storage();
189 $file = $fs->get_file($context->id, 'tool_policy', $filearea, $itemid, $filepath, $filename);
195 send_stored_file($file, null, 0, $forcedownload, $options);
199 * Map icons for font-awesome themes.
201 function tool_policy_get_fontawesome_icon_map() {
203 'tool_policy:agreedno' => 'fa-times text-danger',
204 'tool_policy:agreedyes' => 'fa-check text-success',
205 'tool_policy:agreedyesonbehalf' => 'fa-check text-info',
206 'tool_policy:level' => 'fa-level-up fa-rotate-90 text-muted',
211 * Serve the new group form as a fragment.
213 * @param array $args List of named arguments for the fragment loader.
216 function tool_policy_output_fragment_accept_on_behalf($args) {
217 $args = (object) $args;
220 if (!empty($args->jsonformdata)) {
221 $serialiseddata = json_decode($args->jsonformdata);
222 parse_str($serialiseddata, $data);
225 $mform = new \tool_policy\form\accept_policy(null, $data);
227 if (!empty($args->jsonformdata)) {
228 // If we were passed non-empty form data we want the mform to call validation functions and show errors.
229 $mform->is_validated();
232 return $mform->render();