Skip to content

Commit

Permalink
MDL-67587 course: Add search in recommended activities admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Geshoski committed Apr 9, 2020
1 parent df0e58a commit d379dc9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 30 deletions.
34 changes: 32 additions & 2 deletions course/classes/local/service/content_item_service.php
Expand Up @@ -163,14 +163,44 @@ private function get_content_favourites(string $prefix, \context_user $userconte
* @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,
Expand Down
21 changes: 19 additions & 2 deletions course/classes/output/recommendations/activity_list.php
Expand Up @@ -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;
}

/**
Expand All @@ -63,6 +68,18 @@ public function export_for_template(\renderer_base $output): array {
];
}, $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)
]
];
}
}
10 changes: 8 additions & 2 deletions course/recommendations.php
Expand Up @@ -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');

Expand All @@ -45,9 +47,13 @@
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);

Expand Down
73 changes: 49 additions & 24 deletions course/templates/activity_list.mustache
Expand Up @@ -21,31 +21,56 @@
No example given as the js will fire and create records from the template library page.
}}
{{#search}}
<form class="row">
<div class="input-group pt-4 pb-1 col-md-6">
<label for="search">
<span class="sr-only">{{#str}} searchactivitiesbyname, course {{/str}}</span>
</label>
<input type="text" name="search" id="search" class="form-control rounded-left" autocomplete="off"
placeholder="{{#str}}search, core {{/str}}" {{#query}} value="{{query}}" autofocus {{/query}}
>
<div class="input-group-append">
<button type="submit" class="btn btn-outline-secondary rounded-right" type="button">
<i class="icon fa fa-search fa-fw m-0" aria-hidden="true"></i>
<span class="sr-only">{{#str}}submitsearch, course {{/str}}</span>
</button>
</div>
</div>
</form>
{{#query}}
<div class="pt-1 pb-1">
<span role="alert">{{#str}} searchresults, course, {{searchresultsnumber}} {{/str}}</span>
</div>
{{/query}}
{{/search}}
{{#categories}}
<h3>{{categoryname}}</h3>
<table class="table table-striped table-hover">
<thead>
<tr class="d-flex">
<th scope="col" class="col-7 c0">{{#str}}module, course{{/str}}</th>
<th scope="col" class="col-5 c1">{{#str}}recommend, course{{/str}}</th>
</tr>
</thead>
<tbody>
{{#categorydata}}
<tr class="d-flex">
<td class="col-7 c0"><span>{{{icon}}}</span>{{name}}</td>
{{#id}}
<td class="col-5 c1 colselect">
<input class="activity-recommend-checkbox" type="checkbox" aria-label="{{#str}}recommendcheckbox, course, {{name}}{{/str}}" data-area="{{componentname}}" data-id="{{id}}" {{#recommended}}checked="checked"{{/recommended}} />
</td>
{{/id}}
{{^id}}
<td class="col-5"></td>
{{/id}}
</tr>
{{/categorydata}}
</tbody>
</table>
{{#hascategorydata}}
<h3 class="pt-4">{{categoryname}}</h3>
<table class="table table-striped table-hover">
<thead>
<tr class="d-flex">
<th scope="col" class="col-7 c0">{{#str}}module, course{{/str}}</th>
<th scope="col" class="col-5 c1">{{#str}}recommend, course{{/str}}</th>
</tr>
</thead>
<tbody>
{{#categorydata}}
<tr class="d-flex">
<td class="col-7 c0"><span>{{{icon}}}</span>{{name}}</td>
{{#id}}
<td class="col-5 c1 colselect">
<input class="activity-recommend-checkbox" type="checkbox" aria-label="{{#str}}recommendcheckbox, course, {{name}}{{/str}}" data-area="{{componentname}}" data-id="{{id}}" {{#recommended}}checked="checked"{{/recommended}} />
</td>
{{/id}}
{{^id}}
<td class="col-5"></td>
{{/id}}
</tr>
{{/categorydata}}
</tbody>
</table>
{{/hascategorydata}}
{{/categories}}
{{#js}}
require([
Expand Down
3 changes: 3 additions & 0 deletions lang/en/course.php
Expand Up @@ -67,6 +67,9 @@
$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},
<p>Students in the {$a->coursename} course have been identified as being at risk.</p>';
Expand Down

0 comments on commit d379dc9

Please sign in to comment.