Skip to content

Commit

Permalink
MED-48: Add tag filter to library
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Jan 24, 2024
1 parent 8617f3c commit dcf6f2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions classes/form/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function definition() {
$mform->addElement('text', 'query', get_string('keyword', 'tool_mediatime'));
$mform->setType('query', PARAM_TEXT);

$this->tag_elements();

$this->add_action_buttons(false, get_string('search'));
}

Expand Down
13 changes: 13 additions & 0 deletions classes/media_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace tool_mediatime;

use context_system;
use core_tag_tag;
use core_tag_area;
use moodle_exception;
use moodle_url;
use renderable;
Expand Down Expand Up @@ -168,6 +170,7 @@ public static function search($filters = []) {

$params = [];

// Require enabled source plugins.
if (!$sources = plugininfo\mediatimesrc::get_enabled_plugins()) {
return $result;
}
Expand All @@ -176,6 +179,7 @@ public static function search($filters = []) {
$sql = "source $sql";
$order = 'timecreated DESC';

// Filter for text query.
if ($query = $filters['query'] ?? '') {
$sql .= ' AND ' . $DB->sql_like('content', ':query', false);
$params['query'] = "%$query%";
Expand All @@ -184,6 +188,15 @@ public static function search($filters = []) {
$order = 'CASE WHEN ' . $DB->sql_like('name', ':name', false) . ' THEN 1 ELSE 0 END DESC, timecreated DESC';
}

// Filter by tags.
if ($tags = $filters['tags'] ?? '') {
$tagcollid = core_tag_area::get_collection('tool_mediatime', 'media_resources');
$tags = core_tag_tag::get_by_name_bulk($tagcollid, $tags);
list($tagsql, $tagparams) = $DB->get_in_or_equal(array_column($tags, 'id'), SQL_PARAMS_NAMED, 'tagparams');
$sql .= " AND id IN (SELECT itemid FROM {tag_instance} WHERE tagid $tagsql)";
$params += $tagparams;
}

return $DB->get_recordset_select(
'tool_mediatime',
$sql,
Expand Down

0 comments on commit dcf6f2b

Please sign in to comment.