diff --git a/admin/tool/uploadcourse/classes/step2_form.php b/admin/tool/uploadcourse/classes/step2_form.php index 8854abbd90a..9f1a79927aa 100644 --- a/admin/tool/uploadcourse/classes/step2_form.php +++ b/admin/tool/uploadcourse/classes/step2_form.php @@ -83,7 +83,7 @@ class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form { $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(); diff --git a/backup/util/ui/classes/output/copy_form.php b/backup/util/ui/classes/output/copy_form.php index ff7a55dea63..34794a870e7 100644 --- a/backup/util/ui/classes/output/copy_form.php +++ b/backup/util/ui/classes/output/copy_form.php @@ -106,7 +106,7 @@ class copy_form extends \moodleform { // 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. diff --git a/calendar/classes/local/event/forms/eventtype.php b/calendar/classes/local/event/forms/eventtype.php index 8475e294124..6488acb3419 100644 --- a/calendar/classes/local/event/forms/eventtype.php +++ b/calendar/classes/local/event/forms/eventtype.php @@ -94,7 +94,7 @@ trait eventtype { $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'); } diff --git a/calendar/tests/behat/calendar.feature b/calendar/tests/behat/calendar.feature index 5f1ffaf5043..abdf3edea2c 100644 --- a/calendar/tests/behat/calendar.feature +++ b/calendar/tests/behat/calendar.feature @@ -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 @@ -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. diff --git a/cohort/edit_form.php b/cohort/edit_form.php index 7dbe18bc73a..77599fd1dc2 100644 --- a/cohort/edit_form.php +++ b/cohort/edit_form.php @@ -43,7 +43,7 @@ class cohort_edit_form extends moodleform { $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. diff --git a/cohort/tests/behat/add_cohort.feature b/cohort/tests/behat/add_cohort.feature index 1a5955da067..b93d41fb983 100644 --- a/cohort/tests/behat/add_cohort.feature +++ b/cohort/tests/behat/add_cohort.feature @@ -21,6 +21,7 @@ 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" @@ -28,6 +29,7 @@ Feature: Add cohorts of users 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 (first@example.com)" user to "333" cohort members And I add "Second User (second@example.com)" user to "333" cohort members @@ -37,6 +39,7 @@ Feature: Add cohorts of users And the "Current users" select box should contain "Second User (second@example.com)" And the "Current users" select box should not contain "Forth User (forth@example.com)" + @javascript Scenario: Add users to a cohort using a bulk user action When I follow "Accounts" And I follow "Bulk user actions" diff --git a/course/classes/deletecategory_form.php b/course/classes/deletecategory_form.php index ddbd43396ca..906837e505d 100644 --- a/course/classes/deletecategory_form.php +++ b/course/classes/deletecategory_form.php @@ -111,7 +111,7 @@ class core_course_deletecategory_form extends moodleform { } 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); } diff --git a/course/classes/editcategory_form.php b/course/classes/editcategory_form.php index f11d28c0b41..236fbad0901 100644 --- a/course/classes/editcategory_form.php +++ b/course/classes/editcategory_form.php @@ -63,7 +63,7 @@ class core_course_editcategory_form extends moodleform { $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); diff --git a/course/edit_form.php b/course/edit_form.php index 1b9cfc40284..8a289cc7258 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -77,7 +77,7 @@ class course_edit_form extends moodleform { 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 { @@ -93,7 +93,7 @@ class course_edit_form extends moodleform { $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 diff --git a/course/request_form.php b/course/request_form.php index 31075846250..d9179700119 100644 --- a/course/request_form.php +++ b/course/request_form.php @@ -70,7 +70,7 @@ class course_request_form extends moodleform { 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'); } diff --git a/course/tests/behat/category_management.feature b/course/tests/behat/category_management.feature index eaeeeecdbd3..14ad0b238dd 100644 --- a/course/tests/behat/category_management.feature +++ b/course/tests/behat/category_management.feature @@ -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 | @@ -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" diff --git a/course/tests/behat/course_request.feature b/course/tests/behat/course_request.feature index c50671f4ef2..9a9c6e87932 100644 --- a/course/tests/behat/course_request.feature +++ b/course/tests/behat/course_request.feature @@ -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 | diff --git a/course/tests/behat/coursetags.feature b/course/tests/behat/coursetags.feature index 0883b524732..c2655fc48a6 100644 --- a/course/tests/behat/coursetags.feature +++ b/course/tests/behat/coursetags.feature @@ -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" diff --git a/lib/adminlib.php b/lib/adminlib.php index c6a3219fc61..b93d393d210 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -5007,12 +5007,12 @@ class admin_settings_num_course_sections extends admin_setting_configselect { * * @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); } /** @@ -5021,8 +5021,6 @@ class admin_settings_coursecat_select extends admin_setting_configselect { * @return bool */ public function load_choices() { - global $CFG; - require_once($CFG->dirroot.'/course/lib.php'); if (is_array($this->choices)) { return true; } @@ -5435,6 +5433,54 @@ class admin_setting_configcheckbox_with_lock extends admin_setting_configcheckbo } +/** + * 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.