Skip to content

Commit

Permalink
CONTRIB-8227 autocomplete boxes loaded via web service
Browse files Browse the repository at this point in the history
  • Loading branch information
bostelm committed Aug 14, 2024
1 parent 8389c9b commit 44cf5f4
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 12 deletions.
10 changes: 10 additions & 0 deletions amd/build/studentid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/studentid.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions amd/src/studentid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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/>.

/**
* Potential user selector module.
*
* @module mod_scheduler/studentid
* @copyright 2022 University of Glasgow
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {

return /** @alias module:mod_scheduler/studentid */ {

processResults: function(selector, results) {
var users = [];
$.each(results, function(index, user) {
users.push({
value: user.id,
label: user._label
});
});
return users;
},

transport: function(selector, query, success, failure) {
var promise;

let scheduler = $(selector).attr('scheduler') || null;
let groupids = $(selector).attr('groupids') || null;
promise = Ajax.call([{
methodname: 'mod_scheduler_studentid',
args: {
query: query,
scheduler: scheduler,
groupids: groupids
}
}]);

promise[0].then(function(results) {
var promises = [],
i = 0;

// Render the label.
$.each(results, function(index, user) {
promises.push(Templates.render('mod_scheduler/studentid', user));
});

// Apply the label to the results.
return $.when.apply($.when, promises).then(function() {
var args = arguments;
$.each(results, function(index, user) {
user._label = args[i];
i++;
});
success(results);
return;
});

}).fail(failure);
}

};

});
118 changes: 118 additions & 0 deletions classes/external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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/>.

/**
* This is the external API for this component.
*
* @package mod_scheduler
* @copyright 2022 University of Glasgow
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_scheduler;

defined('MOODLE_INTERNAL') || die();

require_once("$CFG->libdir/externallib.php");

use external_api;
use external_function_parameters;
use external_value;
use external_single_structure;
use external_multiple_structure;

use \mod_scheduler\model\scheduler;

/**
* This is the external API for this component.
*
* @copyright 2022 University of Glasgow
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends external_api {

/**
* studentid parameters
*
* @return external_function_parameters
*/
public static function studentid_parameters() {
return new external_function_parameters([
'query' => new external_value(PARAM_TEXT, 'The search query', VALUE_REQUIRED),
'scheduler' => new external_value(PARAM_INT, 'The scheduler id', VALUE_REQUIRED),
'groupids' => new external_value(PARAM_INT, 'The group ids', VALUE_DEFAULT)
]);
}

/**
* Fetch the details of a user's data request.
*
* @since Moodle 3.5
* @param string $query The search query.
* @param string $scheduler The scheduler id.
* @param string $groupids The group ids.
* @return array
* @throws required_capability_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws restricted_context_exception
*/

public static function studentid($query, $scheduler, $groupids) {
$params = external_api::validate_parameters(self::studentid_parameters(), [
'query' => $query,
'scheduler' => $scheduler,
'groupids' => $groupids
]);
$query = $params['query'];
$scheduler = $params['scheduler'];
$groupids = $params['groupids'];

$scheduler = scheduler::load_by_id($scheduler);
$availablestudents = $scheduler->get_available_students();

$students = [];
$i = 0;
foreach ($availablestudents as $id => $student) {
$fullname = fullname($student);

if (mb_stripos($fullname, $query) !== false) {
$students[] = ['id' => $id, 'fullname' => fullname($student)];
$i++;
}
}

return $students;

}

/**
* Parameter description for get_users().
*
* @since Moodle 3.5
* @return external_description
* @throws coding_exception
*/
public static function studentid_returns() {
return new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'User ID'),
'fullname' => new external_value(PARAM_NOTAGS, 'User fullname')
])
);
}

}
37 changes: 37 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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/>.

/**
* Mod Scheduler webservice definitions
*
* @package mod_scheduler
* @copyright 2022 University of Glasgow
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$functions = array(

'mod_scheduler_studentid' => array(
'classname' => 'mod_scheduler\external',
'methodname' => 'studentid',
'description' => 'Retrieve the list of potential studentids.',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);
12 changes: 12 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ function scheduler_add_instance($data, $mform = null) {
return $data->id;
}

function scheduler_student_autocomplete_callback($value) {
global $OUTPUT;

$fields = 'id ' . \core_user\fields::for_name()->get_sql()->selects;
$user = \core_user::get_user($value, $fields);
$useroptiondata = [
'fullname' => fullname($user)
];

return $OUTPUT->render_from_template('mod_scheduler/studentid', $useroptiondata);
}

/**
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
Expand Down
14 changes: 6 additions & 8 deletions slotforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,12 @@ protected function definition() {
$repeatarray[] = $mform->createElement('header', 'appointhead', get_string('appointmentno', 'scheduler', '{no}'));

// Choose student.
$students = $this->scheduler->get_available_students($this->usergroups);
$studentchoices = array();
if ($students) {
foreach ($students as $astudent) {
$studentchoices[$astudent->id] = fullname($astudent);
}
}
$grouparray[] = $mform->createElement('searchableselector', 'studentid', '', $studentchoices);
$options = [
'ajax' => 'mod_scheduler/studentid',
'valuehtmlcallback' => 'scheduler_student_autocomplete_callback',
'scheduler' => $this->scheduler->id
];
$grouparray[] = $mform->createElement('autocomplete', 'studentid', '', [], $options);
$grouparray[] = $mform->createElement('hidden', 'appointid', 0);

// Seen tickbox.
Expand Down
52 changes: 52 additions & 0 deletions templates/studentid.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{{!
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/>.
}}
{{!
@template mod_scheduler/studentid
Moodle template for the list of valid options in an autocomplate form element.
Classes required for JS:
* none
Data attributes required for JS:
* none
Context variables required for this template:
* fullname string Users full name
* email string user email field
Example context (json):
{
"fullname": "Admin User",
"extrafields": [
{
"name": "email",
"value": "admin@example.com"
},
{
"name": "phone1",
"value": "0123456789"
}
]
}
}}
<span>
<span>{{fullname}}</span>
{{#extrafields}}
<span><small>{{{value}}}</small></span>
{{/extrafields}}
</span>
6 changes: 3 additions & 3 deletions tests/behat/behat_mod_scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ public function i_click_on_item_in_the_nth_autocomplete_list($item, $listnumber)
$downarrowtarget = "(//span[contains(@class,'form-autocomplete-downarrow')])[$listnumber]";
$this->execute('behat_general::i_click_on', [$downarrowtarget, 'xpath_element']);

$xpathtarget = "(//ul[@class='form-autocomplete-suggestions']//*[contains(concat('|', string(.), '|'),'|" .
$item . "|')])[$listnumber]";
$this->execute('behat_general::i_click_on', [$xpathtarget, 'xpath_element']);
$xpathtarget = "(//descendant::ul[@class='form-autocomplete-suggestions'][$listnumber]//"
."*[contains(concat('|', string(.), '|'),'|$item|')])";
$this->execute('behat_general::i_click_on', [$xpathtarget, 'xpath_element']);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

$plugin->component = 'mod_scheduler'; // Full name of the plugin (used for diagnostics).
$plugin->version = 2024080102; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2024080103; // The current module version (Date: YYYYMMDDXX).
$plugin->release = '4.x dev'; // Human-friendly version name.
$plugin->requires = 2023100905; // Requires Moodle 4.3.
$plugin->maturity = MATURITY_ALPHA; // Development release - not for production use.

0 comments on commit 44cf5f4

Please sign in to comment.