Merge branch 'MDL-62670-master' of git://github.com/sarjona/moodle
[moodle.git] / admin / tool / policy / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Plugin version and other meta-data are defined here.
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die();
27 use core_user\output\myprofile\tree;
28 use tool_policy\api;
29 use tool_policy\policy_version;
31 /**
32  * Add nodes to myprofile page.
33  *
34  * @param tree $tree Tree object
35  * @param stdClass $user User object
36  * @param bool $iscurrentuser
37  * @param stdClass $course Course object
38  * @return bool
39  * @throws coding_exception
40  * @throws dml_exception
41  * @throws moodle_exception
42  */
43 function tool_policy_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
44     global $CFG;
46     // Do nothing if we are not set as the site policies handler.
47     if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
48         return;
49     }
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);
57     } else {
58         // Get the existing category.
59         $category = $tree->__get('categories')['privacyandpolicies'];
60     }
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);
69     }
71     return true;
72 }
74 /**
75  * Load policy message for guests.
76  *
77  * @return string The HTML code to insert before the head.
78  */
79 function tool_policy_before_standard_html_head() {
80     global $CFG, $PAGE, $USER;
82     $message = null;
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);
91     }
93     return $message;
94 }
96 /**
97  * Callback to add footer elements.
98  *
99  * @return str valid html footer content
100  */
101 function tool_policy_standard_footer_html() {
102     global $CFG;
104     $output = '';
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');
112         }
113     }
115     return $output;
118 /**
119  * Hooks redirection to policy acceptance pages before sign up.
120  */
121 function tool_policy_pre_signup_requests() {
122     global $CFG;
124     // Do nothing if we are not set as the site policies handler.
125     if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
126         return;
127     }
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'));
135     }
138 /**
139  * Serve the embedded files.
140  *
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
149  */
150 function tool_policy_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
151     global $CFG, $PAGE;
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') {
155         return false;
156     }
158     if ($context->contextlevel != CONTEXT_SYSTEM) {
159         return false;
160     }
162     $PAGE->set_context($context);
164     if ($filearea !== 'policydocumentsummary' && $filearea !== 'policydocumentcontent') {
165         return false;
166     }
168     $itemid = array_shift($args);
170     $policy = api::get_policy_version($itemid);
172     if ($policy->status != policy_version::STATUS_ACTIVE) {
173         require_login();
174     }
176     if (!api::can_user_view_policy_version($policy)) {
177         return false;
178     }
180     $filename = array_pop($args);
182     if (!$args) {
183         $filepath = '/';
184     } else {
185         $filepath = '/'.implode('/', $args).'/';
186     }
188     $fs = get_file_storage();
189     $file = $fs->get_file($context->id, 'tool_policy', $filearea, $itemid, $filepath, $filename);
191     if (!$file) {
192         return false;
193     }
195     send_stored_file($file, null, 0, $forcedownload, $options);
198 /**
199  * Map icons for font-awesome themes.
200  */
201 function tool_policy_get_fontawesome_icon_map() {
202     return [
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',
207     ];
210 /**
211  * Serve the new group form as a fragment.
212  *
213  * @param array $args List of named arguments for the fragment loader.
214  * @return string
215  */
216 function tool_policy_output_fragment_accept_on_behalf($args) {
217     $args = (object) $args;
219     $data = [];
220     if (!empty($args->jsonformdata)) {
221         $serialiseddata = json_decode($args->jsonformdata);
222         parse_str($serialiseddata, $data);
223     }
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();
230     }
232     return $mform->render();