Skip to content

Commit

Permalink
MDL-67278 course: Use autocomplete widget for course category selector
Browse files Browse the repository at this point in the history
  • Loading branch information
jboulen committed Sep 2, 2020
1 parent 38abfb6 commit 93d7220
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 27 deletions.
2 changes: 1 addition & 1 deletion admin/tool/uploadcourse/classes/step2_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function definition () {
$mform->setExpanded('defaultheader', true);

$displaylist = core_course_category::make_categories_list('moodle/course:create');
$mform->addElement('select', 'defaults[category]', get_string('coursecategory'), $displaylist);
$mform->addElement('autocomplete', 'defaults[category]', get_string('coursecategory'), $displaylist);
$mform->addHelpButton('defaults[category]', 'coursecategory');

$choices = array();
Expand Down
2 changes: 1 addition & 1 deletion backup/util/ui/classes/output/copy_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function definition() {
// Always keep current category.
$displaylist[$course->category] = \core_course_category::get($course->category, MUST_EXIST, true)->get_formatted_name();
}
$mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
$mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->addHelpButton('category', 'coursecategory');

// Course visibility.
Expand Down
2 changes: 1 addition & 1 deletion calendar/classes/local/event/forms/eventtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function add_event_type_elements($mform, $eventtypes) {
$categoryoptions[$id] = $category;
}

$mform->addElement('select', 'categoryid', get_string('category'), $categoryoptions);
$mform->addElement('autocomplete', 'categoryid', get_string('category'), $categoryoptions);
$mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
}

Expand Down
16 changes: 8 additions & 8 deletions calendar/tests/behat/calendar.feature
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ Feature: Perform basic calendar functionality
And I am viewing site calendar
And I click on "New event" "button"
And I set the field "Type of event" to "Course"
When I open the autocomplete suggestions list
Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element"
And I should not see "Course 2" in the ".form-autocomplete-suggestions" "css_element"
And I should not see "Course 3" in the ".form-autocomplete-suggestions" "css_element"
When I expand the "Course" autocomplete
Then "Course 1" "autocomplete_suggestions" should exist
And "Course 2" "autocomplete_suggestions" should not exist
And "Course 3" "autocomplete_suggestions" should not exist
And I click on "Close" "button"
And I am on site homepage
And I navigate to "Appearance > Calendar" in site administration
Expand All @@ -211,10 +211,10 @@ Feature: Perform basic calendar functionality
And I am viewing site calendar
And I click on "New event" "button"
And I set the field "Type of event" to "Course"
When I open the autocomplete suggestions list
Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element"
And I should see "Course 2" in the ".form-autocomplete-suggestions" "css_element"
And I should see "Course 3" in the ".form-autocomplete-suggestions" "css_element"
When I expand the "Course" autocomplete
Then "Course 1" "autocomplete_suggestions" should exist
And "Course 2" "autocomplete_suggestions" should exist
And "Course 3" "autocomplete_suggestions" should exist

@javascript
Scenario: Students can only see user event type by default.
Expand Down
2 changes: 1 addition & 1 deletion cohort/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function definition() {
$mform->setType('name', PARAM_TEXT);

$options = $this->get_category_options($cohort->contextid);
$mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
$mform->addElement('autocomplete', 'contextid', get_string('context', 'role'), $options);

$mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
Expand Down
3 changes: 3 additions & 0 deletions cohort/tests/behat/add_cohort.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ Feature: Add cohorts of users
| Description | Test cohort description |
And I press "Save changes"

@javascript
Scenario: Add a cohort
When I follow "Cohorts"
Then I should see "Test cohort name"
And I should see "333"
And I should see "Test cohort description"
And I should see "Created manually"

@javascript
Scenario: Add users to a cohort selecting them from the system users list
When I add "First User ([email protected])" user to "333" cohort members
And I add "Second User ([email protected])" user to "333" cohort members
Expand All @@ -37,6 +39,7 @@ Feature: Add cohorts of users
And the "Current users" select box should contain "Second User ([email protected])"
And the "Current users" select box should not contain "Forth User ([email protected])"

@javascript
Scenario: Add users to a cohort using a bulk user action
When I follow "Accounts"
And I follow "Bulk user actions"
Expand Down
2 changes: 1 addition & 1 deletion course/classes/deletecategory_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function definition() {
}

if ($displaylist) {
$mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
$mform->addElement('autocomplete', 'newparent', get_string('movecategorycontentto'), $displaylist);
if (in_array($this->coursecat->parent, $displaylist)) {
$mform->setDefault('newparent', $this->coursecat->parent);
}
Expand Down
2 changes: 1 addition & 1 deletion course/classes/editcategory_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function definition() {
$strsubmit = get_string('createcategory');
}

$mform->addElement('select', 'parent', get_string('parentcategory'), $options);
$mform->addElement('autocomplete', 'parent', get_string('parentcategory'), $options);

$mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
$mform->addRule('name', get_string('required'), 'required', null);
Expand Down
4 changes: 2 additions & 2 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function definition() {
if (empty($course->id)) {
if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = core_course_category::make_categories_list('moodle/course:create');
$mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
$mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->addHelpButton('category', 'coursecategory');
$mform->setDefault('category', $category->id);
} else {
Expand All @@ -93,7 +93,7 @@ function definition() {
$displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
->get_formatted_name();
}
$mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
$mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->addHelpButton('category', 'coursecategory');
} else {
//keep current
Expand Down
2 changes: 1 addition & 1 deletion course/request_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function definition() {

if (empty($CFG->lockrequestcategory)) {
$displaylist = core_course_category::make_categories_list('moodle/course:request');
$mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
$mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->setDefault('category', $CFG->defaultrequestcategory);
$mform->addHelpButton('category', 'coursecategory');
}
Expand Down
7 changes: 4 additions & 3 deletions course/tests/behat/category_management.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Feature: Test category management actions
And I should see "Category 1 (edited)" in the "#category-listing" "css_element"
And I should see "Category 1 (edited)" in the "#course-listing h3" "css_element"

@javascript
Scenario: Test deleting a categories through the management interface.
Given the following "categories" exist:
| name | category | idnumber |
Expand Down Expand Up @@ -227,9 +228,9 @@ Feature: Test category management actions
And I should see "Delete category: Cat 1"
And I should see "Contents of Cat 1"
And "What to do" "select" should exist
And "Move into" "select" should exist
And the "Move into" select box should not contain "Cat 2"
And the "Move into" select box should contain "Miscellaneous"
And I expand the "Move into" autocomplete
And "Cat 2" "autocomplete_suggestions" should not exist
And "Miscellaneous" "autocomplete_selection" should be visible
And I set the field "What to do" to "Delete all - cannot be undone"
And "Move into" "select" should not be visible
And I press "Cancel"
Expand Down
2 changes: 1 addition & 1 deletion course/tests/behat/course_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Feature: Users can request and approve courses
And I am on course index
And I follow "English category"
And I press "Request a course"
And the field "Course category" matches value "English category"
And the "Course category" select box should contain "English category"
And I set the following fields to these values:
| Course full name | My new course |
| Course short name | Mynewcourse |
Expand Down
2 changes: 1 addition & 1 deletion course/tests/behat/coursetags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Feature: Tagging courses
And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
Then I should see "Mathematics" in the ".form-autocomplete-selection" "css_element"
Then "Mathematics" "autocomplete_suggestions" should exist
And I set the following fields to these values:
| Tags | Algebra |
And I press "Save and display"
Expand Down
56 changes: 51 additions & 5 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5007,12 +5007,12 @@ public function load_choices() {
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_settings_coursecat_select extends admin_setting_configselect {
class admin_settings_coursecat_select extends admin_setting_configselect_autocomplete {
/**
* Calls parent::__construct with specific arguments
*/
public function __construct($name, $visiblename, $description, $defaultsetting) {
parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
public function __construct($name, $visiblename, $description, $defaultsetting = 1) {
parent::__construct($name, $visiblename, $description, $defaultsetting, $choices = null);
}

/**
Expand All @@ -5021,8 +5021,6 @@ public function __construct($name, $visiblename, $description, $defaultsetting)
* @return bool
*/
public function load_choices() {
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
if (is_array($this->choices)) {
return true;
}
Expand Down Expand Up @@ -5435,6 +5433,54 @@ public function __construct($name, $visiblename, $description, $defaultsetting,

}

/**
* Autocomplete as you type form element.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_configselect_autocomplete extends admin_setting_configselect {
/** @var boolean $tags Should we allow typing new entries to the field? */
protected $tags = false;
/** @var string $ajax Name of an AMD module to send/process ajax requests. */
protected $ajax = '';
/** @var string $placeholder Placeholder text for an empty list. */
protected $placeholder = '';
/** @var bool $casesensitive Whether the search has to be case-sensitive. */
protected $casesensitive = false;
/** @var bool $showsuggestions Show suggestions by default - but this can be turned off. */
protected $showsuggestions = true;
/** @var string $noselectionstring String that is shown when there are no selections. */
protected $noselectionstring = '';

/**
* Returns XHTML select field and wrapping div(s)
*
* @see output_select_html()
*
* @param string $data the option to show as selected
* @param string $query
* @return string XHTML field and wrapping div
*/
public function output_html($data, $query='') {
global $PAGE;

$html = parent::output_html($data, $query);

if ($html === '') {
return $html;
}

$this->placeholder = get_string('search');

$params = array('#' . $this->get_id(), $this->tags, $this->ajax,
$this->placeholder, $this->casesensitive, $this->showsuggestions, $this->noselectionstring);

// Load autocomplete wrapper for select2 library.
$PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params);

return $html;
}
}

/**
* Dropdown menu with an advanced checkbox, that controls a additional $name.'_adv' setting.
Expand Down

0 comments on commit 93d7220

Please sign in to comment.