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

Commit

Permalink
MDL-62460 tool_dataprivacy: Extend autocomplete for compatibility
Browse files Browse the repository at this point in the history
* 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
junpataleta committed Jul 9, 2018
1 parent 0d84292 commit 64794a8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 14 deletions.
103 changes: 103 additions & 0 deletions classes/form/request_user_autocomplete.php
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);
}
}
18 changes: 4 additions & 14 deletions createdatarequest_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/formslib.php');

\MoodleQuickForm::registerElementType('request_user_autocomplete',
$CFG->dirroot . '/admin/tool/dataprivacy/classes/form/request_user_autocomplete.php',
'\\tool_dataprivacy\\form\\request_user_autocomplete');
/**
* The contact form to the site's Data Protection Officer
*
Expand All @@ -54,20 +56,8 @@ public function definition() {
if ($this->manage) {
$options = [
'ajax' => 'tool_dataprivacy/form-user-selector',
'valuehtmlcallback' => function($value) {
global $OUTPUT;

$allusernames = get_all_user_name_fields(true);
$fields = 'id, email, ' . $allusernames;
$user = \core_user::get_user($value, $fields);
$useroptiondata = [
'fullname' => fullname($user),
'email' => $user->email
];
return $OUTPUT->render_from_template('tool_dataprivacy/form-user-selector-suggestion', $useroptiondata);
}
];
$mform->addElement('autocomplete', 'userid', get_string('requestfor', 'tool_dataprivacy'), [], $options);
$mform->addElement('request_user_autocomplete', 'userid', get_string('requestfor', 'tool_dataprivacy'), [], $options);
$mform->addRule('userid', null, 'required', null, 'client');

} else {
Expand Down

0 comments on commit 64794a8

Please sign in to comment.