Skip to content

Commit

Permalink
MED-48: Add search function
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Jan 23, 2024
1 parent 33e690c commit e477d4a
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,30 +79,24 @@ public function get_listing($path = '', $page = '') {
return [
'manage' => $manageurl->out(),
'nologin' => true,
'nosearch' => true,
'list' => $this->get_mediatime_resources(),
];
}

/**
* 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);
Expand All @@ -120,8 +115,8 @@ private function get_mediatime_resources(): array {
'author' => fullname(core_user::get_user($record->usermodified)),
];
}
return $result;
$rs->close();
return $result;
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e477d4a

Please sign in to comment.