From 427e3cbcd689cb112df8aff7836e580f25e4f0cb Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Thu, 7 Apr 2016 14:41:59 -0400 Subject: [PATCH] MDL-53167 search: Add ability to limit courses searched --- course/externallib.php | 19 ++++++++++-- lang/en/search.php | 1 + lib/amd/build/form-autocomplete.min.js | 2 +- lib/amd/build/form-course-selector.min.js | 2 +- lib/amd/src/form-autocomplete.js | 9 ++++-- lib/amd/src/form-course-selector.js | 6 +++- lib/form/autocomplete.php | 10 ++++++- lib/form/course.php | 17 ++++++++++- .../form_autocomplete_selection.mustache | 5 ++-- lib/upgrade.txt | 2 ++ search/classes/manager.php | 23 +++++++++++---- search/classes/output/form/search.php | 8 +++++ search/engine/solr/classes/engine.php | 29 ++++++++++++------- search/engine/solr/tests/engine_test.php | 8 +++++ search/index.php | 10 ++++++- .../tests/fixtures/testable_core_search.php | 4 +-- search/tests/manager_test.php | 20 +++++++++++++ 17 files changed, 145 insertions(+), 30 deletions(-) diff --git a/course/externallib.php b/course/externallib.php index 62148282aba6e..c79cb2cfca7da 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -2139,7 +2139,8 @@ public static function search_courses_parameters() { 'requiredcapabilities' => new external_multiple_structure( new external_value(PARAM_CAPABILITY, 'Capability string used to filter courses by permission'), VALUE_OPTIONAL - ) + ), + 'limittoenrolled' => new external_value(PARAM_BOOL, 'limit to enrolled courses', VALUE_DEFAULT, 0), ) ); } @@ -2152,6 +2153,7 @@ public static function search_courses_parameters() { * @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). + * @param int $limittoenrolled Limit to only enrolled courses * @return array of course objects and warnings * @since Moodle 3.0 * @throws moodle_exception @@ -2160,7 +2162,8 @@ public static function search_courses($criterianame, $criteriavalue, $page=0, $perpage=0, - $requiredcapabilities=array()) { + $requiredcapabilities=array(), + $limittoenrolled=0) { global $CFG; require_once($CFG->libdir . '/coursecatlib.php'); @@ -2207,10 +2210,22 @@ public static function search_courses($criterianame, $courses = coursecat::search_courses($searchcriteria, $options, $params['requiredcapabilities']); $totalcount = coursecat::search_courses_count($searchcriteria, $options, $params['requiredcapabilities']); + if (!empty($limittoenrolled)) { + // Get the courses where the current user has access. + $enrolled = enrol_get_my_courses(array('id', 'cacherev')); + } + $finalcourses = array(); $categoriescache = array(); foreach ($courses as $course) { + if (!empty($limittoenrolled)) { + // Filter out not enrolled courses. + if (empty($enrolled[$course->id])) { + $totalcount--; + continue; + } + } $coursecontext = context_course::instance($course->id); diff --git a/lang/en/search.php b/lang/en/search.php index b1e94554dfb3a..be993f38a6abd 100644 --- a/lang/en/search.php +++ b/lang/en/search.php @@ -25,6 +25,7 @@ $string['advancedsearch'] = 'Advanced search'; $string['all'] = 'All'; $string['allareas'] = 'All areas'; +$string['allcourses'] = 'All courses'; $string['author'] = 'Author'; $string['authorname'] = 'Author name'; $string['back'] = 'Back'; diff --git a/lib/amd/build/form-autocomplete.min.js b/lib/amd/build/form-autocomplete.min.js index 76fd2b9cdde65..dedccb74eff1f 100644 --- a/lib/amd/build/form-autocomplete.min.js +++ b/lib/amd/build/form-autocomplete.min.js @@ -1 +1 @@ -define(["jquery","core/log","core/str","core/templates","core/notification"],function(a,b,c,d,e){var f={DOWN:40,ENTER:13,SPACE:32,ESCAPE:27,COMMA:188,UP:38},g=function(b,c){var d=a(document.getElementById(c.selectionId)),e=d.children("[aria-selected=true]").length;for(b%=e;0>b;)b+=e;var f=a(d.children("[aria-selected=true]").get(b)),g=c.selectionId+"-"+b;d.children().attr("data-active-selection",!1).attr("id",""),f.attr("data-active-selection",!0).attr("id",g),d.attr("aria-activedescendant",g)},h=function(b,c,f){var h=[],i=a(document.getElementById(c.selectionId)),j=i.attr("aria-activedescendant"),k=!1;j&&(k=a(document.getElementById(j)).attr("data-value")),f.children("option").each(function(b,c){a(c).prop("selected")&&h.push({label:a(c).html(),value:a(c).attr("value")})});var l=a.extend({items:h},b,c);d.render("core/form_autocomplete_selection",l).done(function(b){i.empty().append(a(b).html()),k!==!1&&i.children("[aria-selected=true]").each(function(b,d){a(d).attr("data-value")===k&&g(b,c)})}).fail(e.exception),"undefined"!=typeof M.core_formchangechecker&&M.core_formchangechecker.set_form_changed(),f.change()},i=function(b,c,d,e){var f=a(d).attr("data-value");b.multiple&&e.children("option").each(function(b,c){a(c).attr("value")==f&&(a(c).prop("selected",!1),a(c).attr("data-iscustom")&&a(c).remove())}),h(b,c,e)},j=function(b,c){var d=a(document.getElementById(c.inputId)),e=a(document.getElementById(c.suggestionsId)),f=e.children("[aria-hidden=false]").length;for(b%=f;0>b;)b+=f;var g=a(e.children("[aria-hidden=false]").get(b)),h=a(e.children("[role=option]")).index(g),i=c.suggestionsId+"-"+h;e.children().attr("aria-selected",!1).attr("id",""),g.attr("aria-selected",!0).attr("id",i),d.attr("aria-activedescendant",i);var j=g.offset().top-e.offset().top+e.scrollTop()-e.height()/2;e.animate({scrollTop:j},100)},k=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);j(e+1,b)},l=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void g(0,b);var e=c.children("[aria-selected=true]").index(d);g(e-1,b)},m=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void g(0,b);var e=c.children("[aria-selected=true]").index(d);g(e+1,b)},n=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);j(e-1,b)},o=function(b){var c=a(document.getElementById(b.inputId)),d=a(document.getElementById(b.suggestionsId));c.attr("aria-expanded",!1).attr("aria-activedescendant",b.selectionId),d.hide().attr("aria-hidden",!0)},p=function(b,f,g,h){var i=a(document.getElementById(f.inputId)),k=a(document.getElementById(f.suggestionsId)),l=!1,m=[];h.children("option").each(function(b,c){a(c).prop("selected")!==!0&&(m[m.length]={label:c.innerHTML,value:a(c).attr("value")})});var n=f.caseSensitive?g:g.toLocaleLowerCase(),o=a.extend({options:m},b,f);d.render("core/form_autocomplete_suggestions",o).done(function(d){k.replaceWith(d),k=a(document.getElementById(f.suggestionsId)),k.show().attr("aria-hidden",!1),k.children().each(function(c,d){d=a(d),b.caseSensitive&&d.text().indexOf(n)>-1||!b.caseSensitive&&d.text().toLocaleLowerCase().indexOf(n)>-1?(d.show().attr("aria-hidden",!1),l=!0):d.hide().attr("aria-hidden",!0)}),i.attr("aria-expanded",!0),l?b.tags||j(0,f):c.get_string("nosuggestions","form").done(function(a){k.html(a)})}).fail(e.exception)},q=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=e.val(),g=f.split(","),i=!1;a.each(g,function(c,e){if(e=e.trim(),""!==e&&(b.multiple||d.children("option").prop("selected",!1),d.children("option").each(function(b,c){a(c).attr("value")==e&&(i=!0,a(c).prop("selected",!0))}),!i)){var f=a("