MDL-62062 tool_policy: Avoid access to consent page after agreeding
[moodle.git] / admin / tool / policy / classes / form / accept_policy.php
CommitLineData
cf398020
MG
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 * Provides {@link tool_policy\form\accept_policy} class.
19 *
20 * @package tool_policy
21 * @copyright 2018 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace tool_policy\form;
26
27use tool_policy\api;
28use tool_policy\policy_version;
29
30defined('MOODLE_INTERNAL') || die();
31
32require_once($CFG->dirroot.'/lib/formslib.php');
33
34/**
35 * Represents the form for accepting a policy.
36 *
37 * @package tool_policy
38 * @copyright 2018 Marina Glancy
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class accept_policy extends \moodleform {
42
43 /**
44 * Defines the form fields.
45 */
46 public function definition() {
47 global $PAGE;
48 $mform = $this->_form;
49
50 if (empty($this->_customdata['userids']) || !is_array($this->_customdata['userids'])) {
51 throw new \moodle_exception('missingparam', 'error', '', 'userids');
52 }
53 if (empty($this->_customdata['versionids']) || !is_array($this->_customdata['versionids'])) {
54 throw new \moodle_exception('missingparam', '', '', 'versionids');
55 }
56 $userids = clean_param_array($this->_customdata['userids'], PARAM_INT);
57 $versionids = clean_param_array($this->_customdata['versionids'], PARAM_INT);
58 $usernames = $this->validate_and_get_users($userids);
59 $versionnames = $this->validate_and_get_versions($versionids);
60
61 foreach ($usernames as $userid => $name) {
62 $mform->addElement('hidden', 'userids['.$userid.']', $userid);
63 $mform->setType('userids['.$userid.']', PARAM_INT);
64 }
65
66 foreach ($versionnames as $versionid => $name) {
67 $mform->addElement('hidden', 'versionids['.$versionid.']', $versionid);
68 $mform->setType('versionids['.$versionid.']', PARAM_INT);
69 }
70
71 $mform->addElement('hidden', 'returnurl');
72 $mform->setType('returnurl', PARAM_LOCALURL);
73
74 $mform->addElement('static', 'user', get_string('acceptanceusers', 'tool_policy'), join(', ', $usernames));
75 $mform->addElement('static', 'policy', get_string('acceptancepolicies', 'tool_policy'),
76 join(', ', $versionnames));
77
78 $mform->addElement('static', 'ack', '', get_string('acceptanceacknowledgement', 'tool_policy'));
79
80 $mform->addElement('textarea', 'note', get_string('acceptancenote', 'tool_policy'));
81 $mform->setType('note', PARAM_NOTAGS);
82
83 if (!empty($this->_customdata['showbuttons'])) {
84 $this->add_action_buttons(true, get_string('iagreetothepolicy', 'tool_policy'));
85 }
86
87 $PAGE->requires->js_call_amd('tool_policy/policyactions', 'init');
88 }
89
90 /**
91 * Validate userids and return usernames
92 *
93 * @param array $userids
94 * @return array (userid=>username)
95 */
96 protected function validate_and_get_users($userids) {
e8cf2ffd 97 global $DB;
cf398020
MG
98 $usernames = [];
99 list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
100 $params['usercontextlevel'] = CONTEXT_USER;
101 $users = $DB->get_records_sql("SELECT u.id, " . get_all_user_name_fields(true, 'u') . ", " .
102 \context_helper::get_preload_record_columns_sql('ctx') .
103 " FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
104 WHERE u.id " . $sql, $params);
105
cf398020
MG
106 foreach ($userids as $userid) {
107 if (!isset($users[$userid])) {
108 throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
109 }
110 $user = $users[$userid];
111 if (isguestuser($user)) {
112 throw new \moodle_exception('noguest');
113 }
e8cf2ffd
MG
114 \context_helper::preload_from_record($user);
115 api::can_accept_policies($userid, true);
cf398020
MG
116 $usernames[$userid] = fullname($user);
117 }
118 return $usernames;
119 }
120
121 /**
122 * Validate versionids and return their names
123 *
124 * @param array $versionids
125 * @return array (versionid=>name)
126 */
127 protected function validate_and_get_versions($versionids) {
128 $versionnames = [];
129 $policies = api::list_policies();
130 foreach ($versionids as $versionid) {
131 $version = api::get_policy_version($versionid, $policies);
132 if ($version->audience == policy_version::AUDIENCE_GUESTS) {
133 throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
134 }
135 $url = new \moodle_url('/admin/tool/policy/view.php', ['versionid' => $version->id]);
136 $policyname = $version->name;
137 if ($version->status != policy_version::STATUS_ACTIVE) {
138 $policyname .= ' ' . $version->revision;
139 }
140 $versionnames[$version->id] = \html_writer::link($url, $policyname,
141 ['data-action' => 'view', 'data-versionid' => $version->id]);
142 }
143 return $versionnames;
144 }
145
146 /**
147 * Process form submission
148 */
149 public function process() {
150 if ($data = $this->get_data()) {
151 foreach ($data->userids as $userid) {
152 \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
153 }
154 }
155 }
156}