|
| 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 | +} |
0 commit comments