MDL-61864 tool_policy: user agreement reports
[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) {
97 global $DB, $USER;
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
106 $acceptany = has_capability('tool/policy:acceptbehalf', \context_system::instance());
107 foreach ($userids as $userid) {
108 if (!isset($users[$userid])) {
109 throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
110 }
111 $user = $users[$userid];
112 if (isguestuser($user)) {
113 throw new \moodle_exception('noguest');
114 }
115 if ($userid == $USER->id) {
116 require_capability('tool/policy:accept', \context_system::instance());
117 } else if (!$acceptany) {
118 \context_helper::preload_from_record($user);
119 require_capability('tool/policy:acceptbehalf', \context_user::instance($userid));
120 }
121 $usernames[$userid] = fullname($user);
122 }
123 return $usernames;
124 }
125
126 /**
127 * Validate versionids and return their names
128 *
129 * @param array $versionids
130 * @return array (versionid=>name)
131 */
132 protected function validate_and_get_versions($versionids) {
133 $versionnames = [];
134 $policies = api::list_policies();
135 foreach ($versionids as $versionid) {
136 $version = api::get_policy_version($versionid, $policies);
137 if ($version->audience == policy_version::AUDIENCE_GUESTS) {
138 throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
139 }
140 $url = new \moodle_url('/admin/tool/policy/view.php', ['versionid' => $version->id]);
141 $policyname = $version->name;
142 if ($version->status != policy_version::STATUS_ACTIVE) {
143 $policyname .= ' ' . $version->revision;
144 }
145 $versionnames[$version->id] = \html_writer::link($url, $policyname,
146 ['data-action' => 'view', 'data-versionid' => $version->id]);
147 }
148 return $versionnames;
149 }
150
151 /**
152 * Process form submission
153 */
154 public function process() {
155 if ($data = $this->get_data()) {
156 foreach ($data->userids as $userid) {
157 \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
158 }
159 }
160 }
161}