Skip to content
This repository was archived by the owner on Sep 24, 2020. It is now read-only.

Commit 64794a8

Browse files
committed
MDL-62460 tool_dataprivacy: Extend autocomplete for compatibility
* For backwards compatibility, we need to extend the MoodleQuickForm_autocomplete element as the valuehtmlcallback is not yet available for 33 and 34.
1 parent 0d84292 commit 64794a8

File tree

2 files changed

+107
-14
lines changed

2 files changed

+107
-14
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
/**
19+
* Filter selector field.
20+
*
21+
* @package tool_dataprivacy
22+
* @copyright 2018 Jun Pataleta
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
namespace tool_dataprivacy\form;
27+
28+
use MoodleQuickForm_autocomplete;
29+
30+
global $CFG;
31+
require_once($CFG->libdir . '/form/autocomplete.php');
32+
33+
/**
34+
* Form field type for choosing a user on the data request creation form.
35+
*
36+
* @package tool_dataprivacy
37+
* @copyright 2018 Jun Pataleta
38+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
*/
40+
class request_user_autocomplete extends MoodleQuickForm_autocomplete {
41+
42+
/**
43+
* Constructor.
44+
*
45+
* @param string $elementName Element name
46+
* @param mixed $elementLabel Label(s) for an element
47+
* @param array $options Options to control the element's display
48+
* Valid options are:
49+
* - multiple bool Whether or not the field accepts more than one values.
50+
*/
51+
public function __construct($elementName = null, $elementLabel = null, $options = array()) {
52+
$validattributes = array(
53+
'ajax' => 'tool_dataprivacy/form-user-selector',
54+
);
55+
if (!empty($options['multiple'])) {
56+
$validattributes['multiple'] = 'multiple';
57+
}
58+
59+
parent::__construct($elementName, $elementLabel, array(), $validattributes);
60+
}
61+
62+
/**
63+
* Set the value of this element.
64+
*
65+
* @param string|array $value The value to set.
66+
* @return boolean
67+
*/
68+
public function setValue($value) {
69+
global $DB;
70+
$values = (array) $value;
71+
$ids = [];
72+
foreach ($values as $onevalue) {
73+
if (!empty($onevalue) && (!$this->optionExists($onevalue)) &&
74+
($onevalue !== '_qf__force_multiselect_submission')) {
75+
array_push($ids, $onevalue);
76+
}
77+
}
78+
79+
if (empty($ids)) {
80+
return $this->setSelected([]);
81+
}
82+
83+
$toselect = [];
84+
list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED);
85+
$allusernames = get_all_user_name_fields(true);
86+
// Exclude admins and guest user.
87+
$excludedusers = array_keys(get_admins()) + [guest_user()->id];
88+
$sort = 'firstname ASC';
89+
$fields = 'id, email, ' . $allusernames;
90+
// Fetch the selected users, exclude the admins and guest users.
91+
$users = get_users(true, '', true, $excludedusers, $sort, '', '', 0, 30, $fields, "id $insql", $inparams);
92+
foreach ($users as $user) {
93+
$userdata = (object)[
94+
'name' => fullname($user),
95+
'email' => $user->email
96+
];
97+
$this->addOption(get_string('nameemail', 'tool_dataprivacy', $userdata), $user->id);
98+
array_push($toselect, $user->id);
99+
}
100+
101+
return $this->setSelected($toselect);
102+
}
103+
}

createdatarequest_form.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
defined('MOODLE_INTERNAL') || die();
2929

3030
require_once($CFG->libdir.'/formslib.php');
31-
31+
\MoodleQuickForm::registerElementType('request_user_autocomplete',
32+
$CFG->dirroot . '/admin/tool/dataprivacy/classes/form/request_user_autocomplete.php',
33+
'\\tool_dataprivacy\\form\\request_user_autocomplete');
3234
/**
3335
* The contact form to the site's Data Protection Officer
3436
*
@@ -54,20 +56,8 @@ public function definition() {
5456
if ($this->manage) {
5557
$options = [
5658
'ajax' => 'tool_dataprivacy/form-user-selector',
57-
'valuehtmlcallback' => function($value) {
58-
global $OUTPUT;
59-
60-
$allusernames = get_all_user_name_fields(true);
61-
$fields = 'id, email, ' . $allusernames;
62-
$user = \core_user::get_user($value, $fields);
63-
$useroptiondata = [
64-
'fullname' => fullname($user),
65-
'email' => $user->email
66-
];
67-
return $OUTPUT->render_from_template('tool_dataprivacy/form-user-selector-suggestion', $useroptiondata);
68-
}
6959
];
70-
$mform->addElement('autocomplete', 'userid', get_string('requestfor', 'tool_dataprivacy'), [], $options);
60+
$mform->addElement('request_user_autocomplete', 'userid', get_string('requestfor', 'tool_dataprivacy'), [], $options);
7161
$mform->addRule('userid', null, 'required', null, 'client');
7262

7363
} else {

0 commit comments

Comments
 (0)