This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
0d84292
commit 64794a8
Showing
2 changed files
with
107 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
/** | ||
* Filter selector field. | ||
* | ||
* @package tool_dataprivacy | ||
* @copyright 2018 Jun Pataleta | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_dataprivacy\form; | ||
|
||
use MoodleQuickForm_autocomplete; | ||
|
||
global $CFG; | ||
require_once($CFG->libdir . '/form/autocomplete.php'); | ||
|
||
/** | ||
* Form field type for choosing a user on the data request creation form. | ||
* | ||
* @package tool_dataprivacy | ||
* @copyright 2018 Jun Pataleta | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class request_user_autocomplete extends MoodleQuickForm_autocomplete { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $elementName Element name | ||
* @param mixed $elementLabel Label(s) for an element | ||
* @param array $options Options to control the element's display | ||
* Valid options are: | ||
* - multiple bool Whether or not the field accepts more than one values. | ||
*/ | ||
public function __construct($elementName = null, $elementLabel = null, $options = array()) { | ||
$validattributes = array( | ||
'ajax' => 'tool_dataprivacy/form-user-selector', | ||
); | ||
if (!empty($options['multiple'])) { | ||
$validattributes['multiple'] = 'multiple'; | ||
} | ||
|
||
parent::__construct($elementName, $elementLabel, array(), $validattributes); | ||
} | ||
|
||
/** | ||
* Set the value of this element. | ||
* | ||
* @param string|array $value The value to set. | ||
* @return boolean | ||
*/ | ||
public function setValue($value) { | ||
global $DB; | ||
$values = (array) $value; | ||
$ids = []; | ||
foreach ($values as $onevalue) { | ||
if (!empty($onevalue) && (!$this->optionExists($onevalue)) && | ||
($onevalue !== '_qf__force_multiselect_submission')) { | ||
array_push($ids, $onevalue); | ||
} | ||
} | ||
|
||
if (empty($ids)) { | ||
return $this->setSelected([]); | ||
} | ||
|
||
$toselect = []; | ||
list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); | ||
$allusernames = get_all_user_name_fields(true); | ||
// Exclude admins and guest user. | ||
$excludedusers = array_keys(get_admins()) + [guest_user()->id]; | ||
$sort = 'firstname ASC'; | ||
$fields = 'id, email, ' . $allusernames; | ||
// Fetch the selected users, exclude the admins and guest users. | ||
$users = get_users(true, '', true, $excludedusers, $sort, '', '', 0, 30, $fields, "id $insql", $inparams); | ||
foreach ($users as $user) { | ||
$userdata = (object)[ | ||
'name' => fullname($user), | ||
'email' => $user->email | ||
]; | ||
$this->addOption(get_string('nameemail', 'tool_dataprivacy', $userdata), $user->id); | ||
array_push($toselect, $user->id); | ||
} | ||
|
||
return $this->setSelected($toselect); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters