MDL-67278 course: Use autocomplete widget for course category selector

This commit is contained in:
Julien Boulen
2020-09-02 15:30:34 +02:00
parent 38abfb6a01
commit 93d7220587
14 changed files with 77 additions and 27 deletions

View File

@@ -83,7 +83,7 @@ class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form {
$mform->setExpanded('defaultheader', true); $mform->setExpanded('defaultheader', true);
$displaylist = core_course_category::make_categories_list('moodle/course:create'); $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'); $mform->addHelpButton('defaults[category]', 'coursecategory');
$choices = array(); $choices = array();

View File

@@ -106,7 +106,7 @@ class copy_form extends \moodleform {
// Always keep current category. // Always keep current category.
$displaylist[$course->category] = \core_course_category::get($course->category, MUST_EXIST, true)->get_formatted_name(); $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'); $mform->addHelpButton('category', 'coursecategory');
// Course visibility. // Course visibility.

View File

@@ -94,7 +94,7 @@ trait eventtype {
$categoryoptions[$id] = $category; $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'); $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
} }

View File

@@ -199,10 +199,10 @@ Feature: Perform basic calendar functionality
And I am viewing site calendar And I am viewing site calendar
And I click on "New event" "button" And I click on "New event" "button"
And I set the field "Type of event" to "Course" And I set the field "Type of event" to "Course"
When I open the autocomplete suggestions list When I expand the "Course" autocomplete
Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element" Then "Course 1" "autocomplete_suggestions" should exist
And I should not see "Course 2" in the ".form-autocomplete-suggestions" "css_element" And "Course 2" "autocomplete_suggestions" should not exist
And I should not see "Course 3" in the ".form-autocomplete-suggestions" "css_element" And "Course 3" "autocomplete_suggestions" should not exist
And I click on "Close" "button" And I click on "Close" "button"
And I am on site homepage And I am on site homepage
And I navigate to "Appearance > Calendar" in site administration 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 am viewing site calendar
And I click on "New event" "button" And I click on "New event" "button"
And I set the field "Type of event" to "Course" And I set the field "Type of event" to "Course"
When I open the autocomplete suggestions list When I expand the "Course" autocomplete
Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element" Then "Course 1" "autocomplete_suggestions" should exist
And I should see "Course 2" in the ".form-autocomplete-suggestions" "css_element" And "Course 2" "autocomplete_suggestions" should exist
And I should see "Course 3" in the ".form-autocomplete-suggestions" "css_element" And "Course 3" "autocomplete_suggestions" should exist
@javascript @javascript
Scenario: Students can only see user event type by default. Scenario: Students can only see user event type by default.

View File

@@ -43,7 +43,7 @@ class cohort_edit_form extends moodleform {
$mform->setType('name', PARAM_TEXT); $mform->setType('name', PARAM_TEXT);
$options = $this->get_category_options($cohort->contextid); $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->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed. $mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.

View File

@@ -21,6 +21,7 @@ Feature: Add cohorts of users
| Description | Test cohort description | | Description | Test cohort description |
And I press "Save changes" And I press "Save changes"
@javascript
Scenario: Add a cohort Scenario: Add a cohort
When I follow "Cohorts" When I follow "Cohorts"
Then I should see "Test cohort name" 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 "Test cohort description"
And I should see "Created manually" And I should see "Created manually"
@javascript
Scenario: Add users to a cohort selecting them from the system users list 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 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 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 contain "Second User (second@example.com)"
And the "Current users" select box should not contain "Forth User (forth@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 Scenario: Add users to a cohort using a bulk user action
When I follow "Accounts" When I follow "Accounts"
And I follow "Bulk user actions" And I follow "Bulk user actions"

View File

@@ -111,7 +111,7 @@ class core_course_deletecategory_form extends moodleform {
} }
if ($displaylist) { 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)) { if (in_array($this->coursecat->parent, $displaylist)) {
$mform->setDefault('newparent', $this->coursecat->parent); $mform->setDefault('newparent', $this->coursecat->parent);
} }

View File

@@ -63,7 +63,7 @@ class core_course_editcategory_form extends moodleform {
$strsubmit = get_string('createcategory'); $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->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
$mform->addRule('name', get_string('required'), 'required', null); $mform->addRule('name', get_string('required'), 'required', null);

View File

@@ -77,7 +77,7 @@ class course_edit_form extends moodleform {
if (empty($course->id)) { if (empty($course->id)) {
if (has_capability('moodle/course:create', $categorycontext)) { if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = core_course_category::make_categories_list('moodle/course:create'); $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->addHelpButton('category', 'coursecategory');
$mform->setDefault('category', $category->id); $mform->setDefault('category', $category->id);
} else { } else {
@@ -93,7 +93,7 @@ class course_edit_form extends moodleform {
$displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true) $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
->get_formatted_name(); ->get_formatted_name();
} }
$mform->addElement('select', 'category', get_string('coursecategory'), $displaylist); $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->addHelpButton('category', 'coursecategory'); $mform->addHelpButton('category', 'coursecategory');
} else { } else {
//keep current //keep current

View File

@@ -70,7 +70,7 @@ class course_request_form extends moodleform {
if (empty($CFG->lockrequestcategory)) { if (empty($CFG->lockrequestcategory)) {
$displaylist = core_course_category::make_categories_list('moodle/course:request'); $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->setDefault('category', $CFG->defaultrequestcategory);
$mform->addHelpButton('category', 'coursecategory'); $mform->addHelpButton('category', 'coursecategory');
} }

View File

@@ -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 "#category-listing" "css_element"
And I should see "Category 1 (edited)" in the "#course-listing h3" "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. Scenario: Test deleting a categories through the management interface.
Given the following "categories" exist: Given the following "categories" exist:
| name | category | idnumber | | name | category | idnumber |
@@ -227,9 +228,9 @@ Feature: Test category management actions
And I should see "Delete category: Cat 1" And I should see "Delete category: Cat 1"
And I should see "Contents of Cat 1" And I should see "Contents of Cat 1"
And "What to do" "select" should exist And "What to do" "select" should exist
And "Move into" "select" should exist And I expand the "Move into" autocomplete
And the "Move into" select box should not contain "Cat 2" And "Cat 2" "autocomplete_suggestions" should not exist
And the "Move into" select box should contain "Miscellaneous" And "Miscellaneous" "autocomplete_selection" should be visible
And I set the field "What to do" to "Delete all - cannot be undone" And I set the field "What to do" to "Delete all - cannot be undone"
And "Move into" "select" should not be visible And "Move into" "select" should not be visible
And I press "Cancel" And I press "Cancel"

View File

@@ -76,7 +76,7 @@ Feature: Users can request and approve courses
And I am on course index And I am on course index
And I follow "English category" And I follow "English category"
And I press "Request a course" 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: And I set the following fields to these values:
| Course full name | My new course | | Course full name | My new course |
| Course short name | Mynewcourse | | Course short name | Mynewcourse |

View File

@@ -36,7 +36,7 @@ Feature: Tagging courses
And I am on "Course 1" course homepage And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration And I navigate to "Edit settings" in current page administration
And I expand all fieldsets 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: And I set the following fields to these values:
| Tags | Algebra | | Tags | Algebra |
And I press "Save and display" And I press "Save and display"

View File

@@ -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 * @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 * Calls parent::__construct with specific arguments
*/ */
public function __construct($name, $visiblename, $description, $defaultsetting) { public function __construct($name, $visiblename, $description, $defaultsetting = 1) {
parent::__construct($name, $visiblename, $description, $defaultsetting, NULL); parent::__construct($name, $visiblename, $description, $defaultsetting, $choices = null);
} }
/** /**
@@ -5021,8 +5021,6 @@ class admin_settings_coursecat_select extends admin_setting_configselect {
* @return bool * @return bool
*/ */
public function load_choices() { public function load_choices() {
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
if (is_array($this->choices)) { if (is_array($this->choices)) {
return true; 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. * Dropdown menu with an advanced checkbox, that controls a additional $name.'_adv' setting.