From 6f657ac554e8534f7076672d327c8585a26d61fb Mon Sep 17 00:00:00 2001 From: Justus Dieckmann Date: Mon, 9 Oct 2023 15:20:10 +0200 Subject: [PATCH] Add possibility to sort videos by title --- classes/local/apibridge.php | 11 +++++++++-- classes/local/output_helper.php | 13 ++++++++----- db/install.xml | 3 ++- db/upgrade.php | 15 +++++++++++++++ lang/en/opencast.php | 7 +++++++ mod_form.php | 8 ++++++++ version.php | 2 +- view.php | 2 +- 8 files changed, 51 insertions(+), 10 deletions(-) diff --git a/classes/local/apibridge.php b/classes/local/apibridge.php index da646db..e4e51da 100644 --- a/classes/local/apibridge.php +++ b/classes/local/apibridge.php @@ -70,11 +70,18 @@ public static function get_instance($ocinstanceid, $forcenewinstance = false) { /** * Get all events in the specified series. * @param string $seriesid + * @param int|null $sortseriesby 1 for sorting by title. Everything else for sorting by date. * @return false|mixed */ - public function get_episodes_in_series($seriesid) { + public function get_episodes_in_series($seriesid, int|null $sortseriesby = null) { + + $sortstring = 'start_date:DESC,title:ASC'; + if ($sortseriesby == 1) { + $sortstring = 'title:ASC,start_date:DESC'; + } + $api = new api($this->ocinstanceid); - $resource = "/api/events?filter=is_part_of:$seriesid&withpublications=true&sort=start_date:DESC,title:ASC&sign=true"; + $resource = "/api/events?filter=is_part_of:$seriesid&withpublications=true&sort=$sortstring&sign=true"; $response = $api->oc_get($resource); if ($api->get_http_code() != 200) { diff --git a/classes/local/output_helper.php b/classes/local/output_helper.php index f6522dc..73f1ffe 100644 --- a/classes/local/output_helper.php +++ b/classes/local/output_helper.php @@ -41,16 +41,19 @@ class output_helper { /** * Prints output for series view. - * @param int $ocinstanceid Opencast instance id - * @param string $seriesid opencast id of series. - * @param string $activityname name of Activity. + * @param stdClass $moduleinstance * @throws \coding_exception */ - public static function output_series($ocinstanceid, $seriesid, $activityname): void { + public static function output_series(stdClass $moduleinstance): void { global $OUTPUT, $PAGE; + $ocinstanceid = $moduleinstance->ocinstanceid; + $seriesid = $moduleinstance->opencastid; + $activityname = $moduleinstance->name; + $sortseriesby = $moduleinstance->sortseriesby; + $api = apibridge::get_instance($ocinstanceid); - $response = $api->get_episodes_in_series($seriesid); + $response = $api->get_episodes_in_series($seriesid, $sortseriesby); if ($response === false) { throw new \exception('There was a problem reaching opencast!'); diff --git a/db/install.xml b/db/install.xml index 1948849..38af77f 100755 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -19,6 +19,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index 7026e21..f792ffc 100755 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -122,5 +122,20 @@ function xmldb_opencast_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023052300, 'opencast'); } + if ($oldversion < 2023100900) { + + // Define field sortseriesby to be added to opencast. + $table = new xmldb_table('opencast'); + $field = new xmldb_field('sortseriesby', XMLDB_TYPE_INTEGER, '2', null, null, null, null, 'allowdownload'); + + // Conditionally launch add field sortseriesby. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Opencast savepoint reached. + upgrade_mod_savepoint(true, 2023100900, 'opencast'); + } + return true; } diff --git a/lang/en/opencast.php b/lang/en/opencast.php index 35725c7..43c281c 100755 --- a/lang/en/opencast.php +++ b/lang/en/opencast.php @@ -92,3 +92,10 @@ $string['uploadinprogress'] = 'Uploading video ({$a}) is in progress, please try again later.'; $string['uploadedvideoisbeingprocesses'] = 'This video ({$a}) is already uploaded and is being processed by Opencast, please wait!'; $string['uploadjobmissing'] = 'There was an error fetching upload data for this video, please try uploading a new one. Due to insufficient data this module is deleted.'; + +// Strings for mod form. +$string['sortseriesby'] = 'Order videos by'; +$string['sortseriesby_help'] = 'Only affects series'; +$string['uploaddate'] = 'Upload date'; +$string['videotitle'] = 'Video title'; +$string['advancedsettings'] = 'Advanced settings'; diff --git a/mod_form.php b/mod_form.php index 5e01945..a8363f3 100755 --- a/mod_form.php +++ b/mod_form.php @@ -123,6 +123,14 @@ public function definition() { $mform->setType('allowdownload', PARAM_INT); $mform->setDefault('allowdownload', get_config('mod_opencast', 'download_default_' . $ocinstanceid)); } + + $mform->addElement('header', 'advancedsettings', get_string('advancedsettings', 'mod_opencast')); + + $mform->addElement('select', 'sortseriesby', get_string('sortseriesby', 'mod_opencast'), [ + 0 => get_string('uploaddate', 'mod_opencast'), + 1 => get_string('videotitle', 'mod_opencast') + ]); + $mform->addHelpButton('sortseriesby', 'sortseriesby', 'mod_opencast'); } $mform->addElement('hidden', 'type'); diff --git a/version.php b/version.php index 6721268..2ff8bd4 100755 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'mod_opencast'; $plugin->release = 'development-version'; -$plugin->version = 2023052300; +$plugin->version = 2023100900; $plugin->requires = 2020061500; // Requires Moodle 3.9+. $plugin->maturity = MATURITY_ALPHA; $plugin->dependencies = array( diff --git a/view.php b/view.php index 5ac21bc..5be2079 100755 --- a/view.php +++ b/view.php @@ -92,7 +92,7 @@ if ($episode) { output_helper::output_episode($moduleinstance->ocinstanceid, $episode, $moduleinstance->id, $moduleinstance->opencastid); } else { - output_helper::output_series($moduleinstance->ocinstanceid, $moduleinstance->opencastid, $moduleinstance->name); + output_helper::output_series($moduleinstance); } } else if ($moduleinstance->type == opencasttype::UPLOAD) { // Redirect to the upload video page in the mod_opencast by default.