Skip to content

Commit

Permalink
MED-48: Add search to library
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Jan 23, 2024
1 parent f27a053 commit 4b27220
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 11 deletions.
73 changes: 73 additions & 0 deletions classes/form/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Media Time media search form
*
* @package tool_mediatime
* @copyright 2024 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_mediatime\form;

use context_system;
use moodleform;

/**
* Media Time media search form
*
* @copyright 2024 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class search extends moodleform {

/**
* Definition
*/
public function definition() {
$mform = $this->_form;
require_capability('tool/mediatime:manage', context_system::instance());

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

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

$mform->addElement('text', 'query', get_string('keyword', 'tool_mediatime'));
$mform->setType('query', PARAM_TEXT);

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

/**
* Add tag elements
*/
protected function tag_elements() {
$mform = $this->_form;

$mform->addElement(
'tags',
'tags',
get_string('tags'),
[
'itemtype' => 'media_resources',
'component' => 'tool_mediatime',
]
);
}
}
54 changes: 49 additions & 5 deletions classes/media_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use single_select;
use templatable;
use stdClass;
use recordset;

/**
* Manage Media Time source files
Expand All @@ -47,6 +48,9 @@ class media_manager implements renderable, templatable {
/** @var int $page Paging offset */
protected int $page = 0;

/** @var $page Search form */
protected $search = null;

/** @var ?stdClass $record Media Time resource record */
protected $record;

Expand All @@ -58,7 +62,6 @@ class media_manager implements renderable, templatable {
* @param int $page Paging offset
*/
public function __construct(string $source, ?stdClass $record = null, int $page = 0) {
global $DB;
$this->page = $page;

$this->record = $record;
Expand All @@ -80,12 +83,19 @@ public function __construct(string $source, ?stdClass $record = null, int $page
$this->source = new $classname($this->record);
}
} else {
foreach ($DB->get_records('tool_mediatime', [], 'timecreated DESC') as $media) {
$this->search = new form\search(null, null, 'get', '', [
'class' => 'form-inline',
]);

$rs = self::search((array)$this->search->get_data());
foreach ($rs as $media) {
if (in_array($media->source, $plugins)) {
$media->content = json_decode($media->content);
$this->media[] = $media;
}
}

$rs->close();
}
}

Expand All @@ -96,8 +106,6 @@ public function __construct(string $source, ?stdClass $record = null, int $page
* @return array
*/
public function export_for_template(renderer_base $output): array {
global $DB;

if (!empty($this->source)) {
$context = [
'libraryhome' => (new moodle_url('/admin/tool/mediatime/index.php'))->out(),
Expand Down Expand Up @@ -143,8 +151,44 @@ public function export_for_template(renderer_base $output): array {
$action = '';
}
return [
'media' => array_values($media),
'action' => $action,
'media' => array_values($media),
'search' => $this->search->render(),
];
}

/**
* Return search query
*
* @param array $filters Parameters for search
* @return recordset
*/
public static function search($filters = []) {
global $DB;

$params = [];

if (!$sources = plugininfo\mediatimesrc::get_enabled_plugins()) {
return $result;
}
list($sql, $params) = $DB->get_in_or_equal($sources, SQL_PARAMS_NAMED);

$sql = "source $sql";
$order = 'timecreated DESC';

if ($query = $filters['query'] ?? '') {
$sql .= ' AND ' . $DB->sql_like('content', ':query', false);
$params['query'] = "%$query%";

$params['name'] = "%$query%";
$order = 'CASE WHEN ' . $DB->sql_like('name', ':name', false) . ' THEN 1 ELSE 0 END DESC, timecreated DESC';
}

return $DB->get_recordset_select(
'tool_mediatime',
$sql,
$params,
$order
);
}
}
4 changes: 1 addition & 3 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/mediatime/db" VERSION="20240118" COMMENT="XMLDB file for Moodle admin/tool/mediatime"
<XMLDB PATH="admin/tool/mediatime/db" VERSION="20240122" COMMENT="XMLDB file for Moodle admin/tool/mediatime"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -8,7 +8,6 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="source" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="repository" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand All @@ -18,7 +17,6 @@
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="repository" TYPE="foreign" FIELDS="repository" REFTABLE="repository_instances" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
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 @@ -28,6 +28,7 @@
$string['pluginname'] = 'Media Time';
$string['addnewcontent'] = 'Add new content';
$string['library'] = 'Library';
$string['keyword'] = 'Keyword';
$string['mediatime:create'] = 'Create media in library';
$string['mediatime:manage'] = 'Manage media in library';
$string['mediatime:view'] = 'View media in library';
Expand Down
13 changes: 11 additions & 2 deletions templates/media_manager.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@
}

}}
{{{action}}}
<div class="container-fluid">
<div class="row">
<div class="col col-12 col-lg-3">
{{{ action }}}
</div>
<div class="col col-12{{# action }} col-lg-9{{/ action }}">
{{{ search }}}
</div>
</div>
</div>
<div class="cards container-fluid">
<div class="row">
{{# media }}
<div class="col-sm-6 col-md-4 col-lg-3 m-0 p-2">
<div class="col col-12 col-md-6 col-lg-4 col-xl-3 m-0 p-2">
<div class="card">
<div class="card-header">
{{# content.name }}
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 = 2024010802;
$plugin->version = 2024010803;
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_ALPHA;

0 comments on commit 4b27220

Please sign in to comment.