From e477d4aad3754d7834318118523fc7c9e871dc49 Mon Sep 17 00:00:00 2001 From: Daniel Thies Date: Mon, 22 Jan 2024 18:53:07 -0600 Subject: [PATCH] MED-48: Add search function --- lib.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lib.php b/lib.php index 8f37695..c836aec 100644 --- a/lib.php +++ b/lib.php @@ -29,6 +29,7 @@ use tool_mediatime\output\media_resource; use tool_mediatime\plugininfo\mediatimesrc; +use tool_mediatime\media_manager; /** * repository_mediatime class is used to share Media Time resources @@ -78,7 +79,6 @@ public function get_listing($path = '', $page = '') { return [ 'manage' => $manageurl->out(), 'nologin' => true, - 'nosearch' => true, 'list' => $this->get_mediatime_resources(), ]; } @@ -86,22 +86,17 @@ public function get_listing($path = '', $page = '') { /** * Only return resouces usable in context * + * @param string $searchtext optional search query * @return array list of mediatime files */ - private function get_mediatime_resources(): array { + private function get_mediatime_resources($searchtext = ''): array { global $USER, $DB, $OUTPUT; - $result = []; - if (!$sources = mediatimesrc::get_enabled_plugins()) { - return $result; + if (!$rs = media_manager::search([ + 'query' => $searchtext, + ])) { + return []; } - list($sql, $params) = $DB->get_in_or_equal($sources); - $rs = $DB->get_recordset_select( - 'tool_mediatime', - "source $sql", - $params, - 'timecreated DESC' - ); foreach ($rs as $record) { $record->content = json_decode($record->content); $resource = new media_resource($record); @@ -120,8 +115,8 @@ private function get_mediatime_resources(): array { 'author' => fullname(core_user::get_user($record->usermodified)), ]; } - return $result; $rs->close(); + return $result; } /** @@ -386,4 +381,19 @@ public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownloa send_file($file, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie); } + + /** + * Search files in repository + * When doing global search, $searchtext will be used as + * keyword. + * + * @param string $searchtext search key word + * @param int $page page + * @return mixed + */ + public function search($searchtext, $page = 0) { + $list = []; + $list['list'] = $this->get_mediatime_resources($searchtext); + return $list; + } }