Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
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
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
use core_user\output\myprofile\tree;
28
use tool_policy\api;
29
use tool_policy\policy_version;
30
 
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;
45
 
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
    }
50
 
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
    }
61
 
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
    }
70
 
71
    return true;
72
}
73
 
74
/**
75
 * Hooks redirection to policy acceptance pages before sign up.
76
 */
77
function tool_policy_pre_signup_requests() {
78
    global $CFG;
79
 
80
    // Do nothing if we are not set as the site policies handler.
81
    if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
82
        return;
83
    }
84
 
85
    $policies = api::get_current_versions_ids(policy_version::AUDIENCE_LOGGEDIN);
86
    $userpolicyagreed = cache::make('core', 'presignup')->get('tool_policy_userpolicyagreed');
87
    if (!empty($policies) && !$userpolicyagreed) {
88
        // Redirect to "Policy" pages for consenting before creating the user.
89
        cache::make('core', 'presignup')->set('tool_policy_issignup', 1);
90
        redirect(new \moodle_url('/admin/tool/policy/index.php'));
91
    }
92
}
93
 
94
/**
95
 * Serve the embedded files.
96
 *
97
 * @param stdClass $course the course object
98
 * @param stdClass $cm the course module object
99
 * @param stdClass $context the context
100
 * @param string $filearea the name of the file area
101
 * @param array $args extra arguments (itemid, path)
102
 * @param bool $forcedownload whether or not force download
103
 * @param array $options additional options affecting the file serving
104
 * @return bool false if the file not found, just send the file otherwise and do not return anything
105
 */
106
function tool_policy_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
107
    global $CFG, $PAGE;
108
 
109
    // Do not allow access to files if we are not set as the site policy handler.
110
    if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
111
        return false;
112
    }
113
 
114
    if ($context->contextlevel != CONTEXT_SYSTEM) {
115
        return false;
116
    }
117
 
118
    $PAGE->set_context($context);
119
 
120
    if ($filearea !== 'policydocumentsummary' && $filearea !== 'policydocumentcontent') {
121
        return false;
122
    }
123
 
124
    $itemid = array_shift($args);
125
 
126
    $policy = api::get_policy_version($itemid);
127
 
128
    if ($policy->status != policy_version::STATUS_ACTIVE) {
129
        require_login();
130
    }
131
 
132
    if (!api::can_user_view_policy_version($policy)) {
133
        return false;
134
    }
135
 
136
    $filename = array_pop($args);
137
 
138
    if (!$args) {
139
        $filepath = '/';
140
    } else {
141
        $filepath = '/'.implode('/', $args).'/';
142
    }
143
 
144
    $fs = get_file_storage();
145
    $file = $fs->get_file($context->id, 'tool_policy', $filearea, $itemid, $filepath, $filename);
146
 
147
    if (!$file) {
148
        return false;
149
    }
150
 
151
    send_stored_file($file, null, 0, $forcedownload, $options);
152
}
153
 
154
/**
155
 * Map icons for font-awesome themes.
156
 */
157
function tool_policy_get_fontawesome_icon_map() {
158
    return [
159
        'tool_policy:agreed' => 'fa-check text-success',
160
        'tool_policy:declined' => 'fa-times text-danger',
161
        'tool_policy:pending' => 'fa-clock-o text-warning',
162
        'tool_policy:partial' => 'fa-exclamation-triangle text-warning',
163
        'tool_policy:level' => 'fa-level-up fa-rotate-90 text-muted',
164
    ];
165
}
166
 
167
/**
168
 * Serve the new group form as a fragment.
169
 *
170
 * @param array $args List of named arguments for the fragment loader.
171
 * @return string
172
 */
173
function tool_policy_output_fragment_accept_on_behalf($args) {
174
    $args = (object) $args;
175
 
176
    $data = [];
177
    if (!empty($args->jsonformdata)) {
178
        $serialiseddata = json_decode($args->jsonformdata);
179
        parse_str($serialiseddata, $data);
180
    }
181
 
182
    $mform = new \tool_policy\form\accept_policy(null, $data);
183
 
184
    if (!empty($args->jsonformdata)) {
185
        // If we were passed non-empty form data we want the mform to call validation functions and show errors.
186
        $mform->is_validated();
187
    }
188
 
189
    return $mform->render();
190
}