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 30, 2024
1 parent 8619f40 commit 79eecc4
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function tag_elements() {
'tags',
get_string('tags'),
[
'itemtype' => 'media_resources',
'itemtype' => 'tool_mediatime',
'component' => 'tool_mediatime',
]
);
Expand Down
6 changes: 5 additions & 1 deletion classes/form/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ 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'));
$mform->addElement('cancel', 'reset', get_string('reset'));
$mform->disabledIf('reset', 'query', 'eq', '');
}

/**
Expand All @@ -65,7 +69,7 @@ protected function tag_elements() {
'tags',
get_string('tags'),
[
'itemtype' => 'media_resources',
'itemtype' => 'tool_mediatime',
'component' => 'tool_mediatime',
]
);
Expand Down
21 changes: 21 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 @@ -87,6 +89,11 @@ public function __construct(string $source, ?stdClass $record = null, int $page
'class' => 'form-inline',
]);

if ($this->search->is_cancelled()) {
$url = new moodle_url('/admin/tool/mediatime/index.php');
redirect($url);
}

$rs = self::search((array)$this->search->get_data());
foreach ($rs as $media) {
if (in_array($media->source, $plugins)) {
Expand Down Expand Up @@ -168,6 +175,7 @@ public static function search($filters = []) {

$params = [];

// Require enabled source plugins.
if (!$sources = plugininfo\mediatimesrc::get_enabled_plugins()) {
return $result;
}
Expand All @@ -176,6 +184,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 +193,18 @@ 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', 'tool_mediatime');
$tags = core_tag_tag::get_by_name_bulk($tagcollid, $tags);
$tags = array_column($tags, 'id');
if (!empty($tags)) {
list($tagsql, $tagparams) = $DB->get_in_or_equal($tags, 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
2 changes: 1 addition & 1 deletion classes/output/media_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function tags($output) {
return $output->tag_list(
core_tag_tag::get_item_tags(
'tool_mediatime',
'media_resources',
'tool_mediatime',
$this->record->id
),
null,
Expand Down
2 changes: 1 addition & 1 deletion db/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

$tagareas = [
[
'itemtype' => 'media_resources',
'itemtype' => 'tool_mediatime',
'component' => 'tool_mediatime',
'callback' => 'tool_mediatime_get_tagged_resources',
'callbackfile' => '/admin/tool/mediatime/lib.php',
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_mediatime.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
$string['resourcename_help'] = 'The name for a resource is used internally in the library to identify the resource, but not displayed when it is used.';
$string['managemediatimesrcplugins'] = 'Manage source plugins';
$string['subplugin_mediatimesrc_plural'] = 'Media sources';
$string['tagarea_tool_mediatime'] = 'Media resources';
$string['title'] = 'Title';
$string['title_help'] = 'The title may be displayed by the video player when used';
6 changes: 3 additions & 3 deletions source/streamio/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct($record = null) {
$context = context_system::instance();
core_tag_tag::set_item_tags(
'tool_mediatime',
'media_resources',
'tool_meidatime',
$id,
$context,
json_decode($tags)
Expand All @@ -100,7 +100,7 @@ public function __construct($record = null) {
}

if ($edit = optional_param('edit', null, PARAM_INT)) {
$this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'media_resources', $edit);
$this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'tool_mediatime', $edit);

$this->form->set_data([
'edit' => $edit,
Expand Down Expand Up @@ -135,7 +135,7 @@ public function __construct($record = null) {
$context = context_system::instance();
core_tag_tag::set_item_tags(
'tool_mediatime',
'media_resources',
'tool_mediatime',
$data->edit,
$context,
$data->tags
Expand Down
4 changes: 4 additions & 0 deletions source/videotime/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class edit_resource extends \tool_mediatime\form\edit_resource {
public function definition() {
$mform = $this->_form;

$mform->addElement('hidden', 'create');
$mform->setType('create', PARAM_TEXT);
$mform->setDefault('create', '');

$mform->addElement('hidden', 'source');
$mform->setType('source', PARAM_TEXT);
$mform->setDefault('source', 'videotime');
Expand Down
4 changes: 2 additions & 2 deletions source/videotime/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function __construct($record = null) {
]
);

$this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'media_resources', $edit);
$this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'tool_mediatime', $edit);

$this->form->set_data([
'edit' => $edit,
Expand Down Expand Up @@ -156,7 +156,7 @@ public function __construct($record = null) {

core_tag_tag::set_item_tags(
'tool_mediatime',
'media_resources',
'tool_mediatime',
$data->edit,
$context,
$data->tags
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

$plugin->component = 'tool_mediatime';
$plugin->release = '1.0';
$plugin->version = 2024010803;
$plugin->version = 2024010804;
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_ALPHA;

0 comments on commit 79eecc4

Please sign in to comment.