Skip to content

Commit

Permalink
MDL-51324 forms: Add a new course selector
Browse files Browse the repository at this point in the history
This is a squashed commit containing a number of changes:

This is an ajax driven course selector that has searching etc. It can select single, or multiple courses.
Make course selector accept a list of courses to exclude
courseselector - lookup coursename on setValue
Use the get_course_display_name_in_list function to generate the course names
Add a throttle to auto-complete to reduce spamming the server
Do a single query to fetch all the courses in the mform element when validation fails
Fix core course search function to return results when there are less than 2 chars in the query.
Handle setData with an empty array in new course selector
  • Loading branch information
Damyon Wiese committed Mar 9, 2016
1 parent 9502c7f commit 235ef57
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 25 deletions.
27 changes: 20 additions & 7 deletions course/externallib.php
Expand Up @@ -2135,7 +2135,11 @@ public static function search_courses_parameters() {
(search, modulelist (only admins), blocklist (only admins), tagid)'),
'criteriavalue' => new external_value(PARAM_RAW, 'criteria value'),
'page' => new external_value(PARAM_INT, 'page number (0 based)', VALUE_DEFAULT, 0),
'perpage' => new external_value(PARAM_INT, 'items per page', VALUE_DEFAULT, 0)
'perpage' => new external_value(PARAM_INT, 'items per page', VALUE_DEFAULT, 0),
'requiredcapabilities' => new external_multiple_structure(
new external_value(PARAM_CAPABILITY, 'Capability string used to filter courses by permission'),
VALUE_OPTIONAL
)
)
);
}
Expand All @@ -2147,11 +2151,16 @@ public static function search_courses_parameters() {
* @param string $criteriavalue Criteria value
* @param int $page Page number (for pagination)
* @param int $perpage Items per page
* @param array $requiredcapabilities Optional list of required capabilities (used to filter the list).
* @return array of course objects and warnings
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function search_courses($criterianame, $criteriavalue, $page=0, $perpage=0) {
public static function search_courses($criterianame,
$criteriavalue,
$page=0,
$perpage=0,
$requiredcapabilities=array()) {
global $CFG;
require_once($CFG->libdir . '/coursecatlib.php');

Expand All @@ -2161,7 +2170,8 @@ public static function search_courses($criterianame, $criteriavalue, $page=0, $p
'criterianame' => $criterianame,
'criteriavalue' => $criteriavalue,
'page' => $page,
'perpage' => $perpage
'perpage' => $perpage,
'requiredcapabilities' => $requiredcapabilities
);
$params = self::validate_parameters(self::search_courses_parameters(), $parameters);

Expand Down Expand Up @@ -2194,8 +2204,8 @@ public static function search_courses($criterianame, $criteriavalue, $page=0, $p
}

// Search the courses.
$courses = coursecat::search_courses($searchcriteria, $options);
$totalcount = coursecat::search_courses_count($searchcriteria);
$courses = coursecat::search_courses($searchcriteria, $options, $params['requiredcapabilities']);
$totalcount = coursecat::search_courses_count($searchcriteria, $options, $params['requiredcapabilities']);

$finalcourses = array();
$categoriescache = array();
Expand Down Expand Up @@ -2244,10 +2254,12 @@ public static function search_courses($criterianame, $criteriavalue, $page=0, $p
list($summary, $summaryformat) =
external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null);

$displayname = get_course_display_name_for_list($course);
$coursereturns = array();
$coursereturns['id'] = $course->id;
$coursereturns['fullname'] = $course->get_formatted_fullname();
$coursereturns['shortname'] = $course->get_formatted_shortname();
$coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id);
$coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id);
$coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id);
$coursereturns['categoryid'] = $course->category;
$coursereturns['categoryname'] = $category->name;
$coursereturns['summary'] = $summary;
Expand Down Expand Up @@ -2281,6 +2293,7 @@ public static function search_courses_returns() {
array(
'id' => new external_value(PARAM_INT, 'course id'),
'fullname' => new external_value(PARAM_TEXT, 'course full name'),
'displayname' => new external_value(PARAM_TEXT, 'course display name'),
'shortname' => new external_value(PARAM_TEXT, 'course short name'),
'categoryid' => new external_value(PARAM_INT, 'category id'),
'categoryname' => new external_value(PARAM_TEXT, 'category name'),
Expand Down

0 comments on commit 235ef57

Please sign in to comment.