From d379dc9b0c6559a9166c58dfa756065480fb5490 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 25 Mar 2020 09:19:42 +0800 Subject: [PATCH] MDL-67587 course: Add search in recommended activities admin page --- .../local/service/content_item_service.php | 34 ++++++++- .../output/recommendations/activity_list.php | 21 +++++- course/recommendations.php | 10 ++- course/templates/activity_list.mustache | 73 +++++++++++++------ lang/en/course.php | 3 + 5 files changed, 111 insertions(+), 30 deletions(-) diff --git a/course/classes/local/service/content_item_service.php b/course/classes/local/service/content_item_service.php index 76aadb95f74..b1dfddd8457 100644 --- a/course/classes/local/service/content_item_service.php +++ b/course/classes/local/service/content_item_service.php @@ -163,14 +163,44 @@ class content_item_service { * @return array the array of exported content items. */ public function get_all_content_items(\stdClass $user): array { - global $PAGE; $allcontentitems = $this->repository->find_all(); + return $this->export_content_items($user, $allcontentitems); + } + + /** + * Get content items which name matches a certain pattern and may be added to courses, + * irrespective of course caps, for site admin views, etc. + * + * @param \stdClass $user The user object. + * @param string $pattern The search pattern. + * @return array The array of exported content items. + */ + public function get_content_items_by_name_pattern(\stdClass $user, string $pattern): array { + $allcontentitems = $this->repository->find_all(); + + $filteredcontentitems = array_filter($allcontentitems, function($contentitem) use ($pattern) { + return preg_match("/$pattern/i", $contentitem->get_title()->get_value()); + }); + + return $this->export_content_items($user, $filteredcontentitems); + } + + /** + * Export content items. + * + * @param \stdClass $user The user object. + * @param array $contentitems The content items array. + * @return array The array of exported content items. + */ + private function export_content_items(\stdClass $user, $contentitems) { + global $PAGE; + // Export the objects to get the formatted objects for transfer/display. $favourites = $this->get_favourite_content_items_for_user($user); $recommendations = $this->get_recommendations(); $ciexporter = new course_content_items_exporter( - $allcontentitems, + $contentitems, [ 'context' => \context_system::instance(), 'favouriteitems' => $favourites, diff --git a/course/classes/output/recommendations/activity_list.php b/course/classes/output/recommendations/activity_list.php index 0352734f52f..13b2e740eda 100644 --- a/course/classes/output/recommendations/activity_list.php +++ b/course/classes/output/recommendations/activity_list.php @@ -36,13 +36,18 @@ class activity_list implements \renderable, \templatable { /** @var array $modules activities to display in the recommendations page. */ protected $modules; + /** @var string $searchquery The search query. */ + protected $searchquery; + /** * Constructor method. * * @param array $modules Activities to display + * @param string $searchquery The search query if present */ - public function __construct(array $modules) { + public function __construct(array $modules, string $searchquery) { $this->modules = $modules; + $this->searchquery = $searchquery; } /** @@ -63,6 +68,18 @@ class activity_list implements \renderable, \templatable { ]; }, $this->modules); - return ['categories' => ['categoryname' => get_string('activities'), 'categorydata' => $info]]; + return [ + 'categories' => [ + [ + 'categoryname' => get_string('activities'), + 'hascategorydata' => !empty($info), + 'categorydata' => $info + ] + ], + 'search' => [ + 'query' => $this->searchquery, + 'searchresultsnumber' => count($this->modules) + ] + ]; } } diff --git a/course/recommendations.php b/course/recommendations.php index f7c2d1d3836..56b30c3834e 100644 --- a/course/recommendations.php +++ b/course/recommendations.php @@ -24,6 +24,8 @@ require_once("../config.php"); +$search = optional_param('search', '', PARAM_TEXT); + $context = context_system::instance(); $url = new moodle_url('/course/recommendations.php'); @@ -45,9 +47,13 @@ echo $renderer->header(); echo $renderer->heading(get_string('activitychooserrecommendations', 'course')); $manager = \core_course\local\factory\content_item_service_factory::get_content_item_service(); -$modules = $manager->get_all_content_items($USER); +if (!empty($search)) { + $modules = $manager->get_content_items_by_name_pattern($USER, $search); +} else { + $modules = $manager->get_all_content_items($USER); +} -$activitylist = new \core_course\output\recommendations\activity_list($modules); +$activitylist = new \core_course\output\recommendations\activity_list($modules, $search); echo $renderer->render_activity_list($activitylist); diff --git a/course/templates/activity_list.mustache b/course/templates/activity_list.mustache index 43efcd4c646..1670b4bd25d 100644 --- a/course/templates/activity_list.mustache +++ b/course/templates/activity_list.mustache @@ -21,31 +21,56 @@ No example given as the js will fire and create records from the template library page. }} +{{#search}} +
+
+ + +
+ +
+
+
+ {{#query}} +
+ {{#str}} searchresults, course, {{searchresultsnumber}} {{/str}} +
+ {{/query}} +{{/search}} {{#categories}} -

{{categoryname}}

- - - - - - - - - {{#categorydata}} - - - {{#id}} - - {{/id}} - {{^id}} - - {{/id}} - - {{/categorydata}} - -
{{#str}}module, course{{/str}}{{#str}}recommend, course{{/str}}
{{{icon}}}{{name}} - -
+ {{#hascategorydata}} +

{{categoryname}}

+ + + + + + + + + {{#categorydata}} + + + {{#id}} + + {{/id}} + {{^id}} + + {{/id}} + + {{/categorydata}} + +
{{#str}}module, course{{/str}}{{#str}}recommend, course{{/str}}
{{{icon}}}{{name}} + +
+ {{/hascategorydata}} {{/categories}} {{#js}} require([ diff --git a/lang/en/course.php b/lang/en/course.php index ed272bb9b54..c3735150926 100644 --- a/lang/en/course.php +++ b/lang/en/course.php @@ -67,6 +67,9 @@ $string['privacy:metadata:completionsummary'] = 'The course contains completion $string['privacy:metadata:favouritessummary'] = 'The course contains information relating to the course being starred by the user.'; $string['recommend'] = 'Recommend'; $string['recommendcheckbox'] = 'Recommend activity: {$a}'; +$string['searchactivitiesbyname'] = 'Search for activities by name'; +$string['searchresults'] = 'Search results: {$a}'; +$string['submitsearch'] = 'Submit search'; $string['studentsatriskincourse'] = 'Students at risk in {$a} course'; $string['studentsatriskinfomessage'] = 'Hi {$a->userfirstname},

Students in the {$a->coursename} course have been identified as being at risk.

'; -- 2.43.0