Skip to content

Commit

Permalink
Fix #83 - Use core autocomplete form element instead of custom dialog…
Browse files Browse the repository at this point in the history
…ue JS.
  • Loading branch information
danmarsden committed Jul 23, 2021
1 parent 48c66fc commit 48172a2
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 776 deletions.
2 changes: 2 additions & 0 deletions amd/build/form-user-selector.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/form-user-selector.min.js.map

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

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

/**
* Enrolled user selector module - copied from mod_forum.
*
* @module mod_dialogue/form-user-selector
* @class form-user-selector
* @package mod_dialogue
* @copyright 2019 Shamim Rezaie
* @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_dialogue/form-user-selector */ {
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;
var courseid = $(selector).attr('courseid');

promise = Ajax.call([{
methodname: 'core_enrol_search_users',
args: {
courseid: courseid,
search: query,
searchanywhere: true,
page: 0,
perpage: 30
}
}]);

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

// Render the label.
$.each(results, function(index, user) {
promises.push(Templates.render('mod_dialogue/form-user-selector-suggestion', 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);
}

};

});
26 changes: 4 additions & 22 deletions classes/conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function load_participants() {
* @throws \moodle_exception
*/
public function initialise_form() {
global $CFG, $USER, $PAGE;
global $CFG, $USER;
require_once($CFG->dirroot . '/mod/dialogue/formlib.php');

// Form can only be initialise if in draft state.
Expand All @@ -308,27 +308,9 @@ public function initialise_form() {
} else {
$form->set_data(array('action' => 'edit'));
}
// Setup nonjs person selector.
$options = array();
$selected = array();
// Get participants - todo.
$participants = $this->participants; // Insure loaded by using magic.
if ($participants) {
foreach ($participants as $participant) {
$options[$participant->id] = fullname($participant);
$selected[] = $participant->id;
}
$optiongroup = array('' => $options); // Cause formslib selectgroup is stupid.
} else {
$optiongroup = array(get_string('usesearch', 'dialogue') => array('' => '')); // Cause formslib selectgroup is stupid.
}

$json = json_encode($participants);

$PAGE->requires->yui_module('moodle-mod_dialogue-autocomplete',
'M.mod_dialogue.autocomplete.init', array($cm->id, $json));

$form->update_selectgroup('p_select', $optiongroup, $selected);
$form->set_data(['useridsselected' => array_keys($participants)]);

// Set bulk open bulk.
$bulkopenrule = $this->bulkopenrule; // Insure loaded by using magic.
Expand Down Expand Up @@ -578,8 +560,8 @@ public function save_form_data() {

// Shortcut set of participants for now todo - make better.
$this->clear_participants();
if (!empty($data->people)) {
$participants = (array) $data->people; // May be single value.
if (!empty($data->useridsselected)) {
$participants = (array) $data->useridsselected; // May be single value.
foreach ($participants as $userid) {
$this->add_participant($userid);
}
Expand Down
131 changes: 19 additions & 112 deletions formlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,51 +242,32 @@ class mod_dialogue_conversation_form extends mod_dialogue_message_form {
* @throws dml_exception
*/
protected function definition() {
global $PAGE, $OUTPUT;
global $PAGE, $OUTPUT, $COURSE;

$mform = $this->_form;
$cm = $PAGE->cm;
$context = $PAGE->context;

$mform->addElement('header', 'openwithsection', get_string('openwith', 'dialogue'));

// Autocomplete javascript.
$html = '';
$html .= html_writer::start_tag('div', array('class' => 'fitem fitem_ftext'));
$html .= html_writer::start_tag('div', array( 'class' => 'fitemtitle'));
$html .= html_writer::tag('label', get_string('people', 'dialogue'), array('for' => 'people_autocomplete_input'));
$html .= html_writer::end_tag('div');
$html .= html_writer::start_tag('div', array('class' => 'felement ftext'));
$html .= html_writer::start_tag('div', array('id' => 'participant_autocomplete_field',
'class' => 'js-control yui3-aclist-field'));
$html .= html_writer::tag('input', '',
array('id' => 'participant_autocomplete_input', 'class' => 'input-xxlarge',
'placeholder' => get_string('searchpotentials', 'dialogue')));
$html .= html_writer::tag('span', '', array('class' => 'drop-down-arrow'));
$html .= html_writer::end_tag('div');
$html .= html_writer::end_tag('div');
$html .= html_writer::end_tag('div');
// Add to form.
$mform->addElement('html', $html);

// Non javascript.
$mform->addElement('html', '<div class="nonjs-control">'); // Non-js wrapper.
$mform->addElement('text', 'p_query'); // Person search.
$mform->setType('p_query', PARAM_RAW);
$psearchbuttongroup = array();
$psearchbuttongroup[] = $mform->createElement('submit', 'p_search', get_string('search'));
$mform->registerNoSubmitButton('p_search');
$psearchbuttongroup[] = $mform->createElement('submit', 'p_clear', get_string('clear'));
$mform->registerNoSubmitButton('p_clear');
$mform->addGroup($psearchbuttongroup, 'psearchbuttongroup', '', ' ', false);
$attributes = array();
$attributes['size'] = 5;

$mform->addElement('selectgroups', 'p_select', get_string('people', 'dialogue'),
array(),
$attributes);

$mform->addElement('html', '</div>'); // End non-js wrapper.
$options = [
'ajax' => 'mod_dialogue/form-user-selector',
'multiple' => true,
'courseid' => $COURSE->id,
'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),
];
return $OUTPUT->render_from_template('mod_dialogue/form-user-selector-suggestion', $useroptiondata);
}
];
$mform->addElement('autocomplete', 'useridsselected', get_string('users'), [], $options);

// Bulk open rule section.
if (has_capability('mod/dialogue:bulkopenrulecreate', $context)) {
Expand Down Expand Up @@ -325,46 +306,6 @@ protected function definition() {
parent::definition();
}

/**
* Definition after data
* @return boolean
*/
public function definition_after_data() {
global $PAGE;
$mform = $this->_form;

$q = optional_param('p_query', '', PARAM_TEXT);
if (!empty($q)) {
$dialogue = new \mod_dialogue\dialogue($PAGE->cm, $PAGE->course, $PAGE->activityrecord);
$results = dialogue_search_potentials($dialogue, $q);
if (empty($results[0])) {
$people = array(get_string('nomatchingpeople', 'dialogue', $q) => array(''));
} else {
$options = array();
foreach ($results[0] as $person) {
$options[$person->id] = fullname($person);
}
$people = array(get_string('matchingpeople', 'dialogue', count($options)) => $options);
if ($mform->getElement('p_select')->getMultiple()) {
$selected = optional_param_array('p_select', array(), PARAM_INT);
} else {
$selected = optional_param('p_select', array(), PARAM_INT);
}
$this->update_selectgroup('p_select', $people, $selected);
}
}
// Clear out query string and selectgroup form data.
if (optional_param('p_clear', false, PARAM_BOOL)) {
$mform = $this->_form;
$pquery = $mform->getElement('p_query');
$pquery->setValue('');
$this->update_selectgroup('p_select',
array(get_string('usesearch', 'dialogue') => array('' => '')));

}
return true;
}

/**
* Validation
* @param array $data
Expand All @@ -374,25 +315,6 @@ public function definition_after_data() {
public function validation($data, $files) {

$errors = parent::validation($data, $files);
if (optional_param_array('p', array(), PARAM_INT)) {
// JS people search.
$people = optional_param_array('p', array(), PARAM_INT);
if (isset($people['clear'])) {
$data['people'] = array();
} else {
$data['people'] = $people;
}
} else if (optional_param('p_select', array(), PARAM_INT)) {
// Non-js people search select.
$data['people'] = optional_param('p_select', array(), PARAM_INT);
} else {
$data['people'] = array();
}
if (empty($data['groupinformation'])) {
if (empty($data['people'])) {
$errors['participant_autocomplete_field'] = get_string('errornoparticipant', 'dialogue');
}
}
if (empty($data['subject'])) {
$errors['subject'] = get_string('erroremptysubject', 'dialogue');
}
Expand Down Expand Up @@ -435,21 +357,6 @@ public function get_submitted_data() {
unset($data->includefuturemembers);
unset($data->groupinformation);

if (optional_param_array('p', array(), PARAM_INT)) {
// Js people search.
$people = optional_param_array('p', array(), PARAM_INT);
if (isset($people['clear'])) {
$data->people = array();
} else {
$data->people = $people;
}
} else if (optional_param('p_select', array(), PARAM_INT)) {
// Non-js people search select.
$data->people = optional_param('p_select', array(), PARAM_INT);
} else {
$data->people = array();
}

return $data;
}
}
Loading

0 comments on commit 48172a2

Please sign in to comment.