Skip to content

Commit

Permalink
Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamara Gunkel committed Dec 8, 2021
1 parent af4a0db commit 103e88e
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:

- name: Moodle Code Checker
if: ${{ always() }}
run: moodle-plugin-ci codechecker --max-warnings 5
run: moodle-plugin-ci codechecker

- name: Moodle PHPDoc Checker
if: ${{ always() }}
Expand Down
2 changes: 2 additions & 0 deletions amd/build/form_dependent_select.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/form_dependent_select.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions amd/src/form_dependent_select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export const init = (choices) => {
var selectseries = document.getElementById('id_series');
var selectepisodes = document.getElementById('id_episode');

selectseries.addEventListener('change', function() {
var series = this.value;
document.getElementById('id_ocinstanceid').value = this.value.substring(this.value.lastIndexOf('_') + 1, this.value.length);
document.getElementById('id_opencastid').value = this.value.substring(0, this.value.lastIndexOf('_'));
document.getElementById('id_opencastid').dispatchEvent(new Event('change'));

Array.from(selectepisodes.options).forEach(function(option) {
if (option.value in choices[series]) {
option.hidden = false;
option.hidden = false;
} else {
option.hidden = true;
option.hidden = true;
}
});
selectepisodes.value = 'allvideos';
});

selectepisodes.addEventListener('change', function() {
document.getElementById('id_opencastid').value = this.value;
document.getElementById('id_opencastid').dispatchEvent(new Event('change'));
});

// Initialize select values.
var initOpencastid = document.getElementById('id_opencastid').value;
if (initOpencastid) {
selectepisodes.value = initOpencastid;
var foundseries = false;
for (const [seriesid, episodes] of Object.entries(choices)) {
// Skip series from wrong Opencast instance.
if (seriesid.substring(seriesid.lastIndexOf('_') + 1, seriesid.length) !==
document.getElementById('id_ocinstanceid').value) {
continue;
}

// ID is series.
if (initOpencastid === seriesid.substring(0, seriesid.lastIndexOf('_'))) {
selectseries.value = seriesid;
selectepisodes.value = 'allvideos';
foundseries = true;
break;
}

if (initOpencastid in episodes) {
selectseries.value = seriesid;
foundseries = true;
break;
}
}
if (!foundseries) {
selectseries.value = 'invalid';
}

} else {
// Use defaults.
selectseries.dispatchEvent(new Event('change'));
}

// Disabled values should also be submitted in this case.
selectseries.form.addEventListener("submit", function() {
document.getElementById('id_opencastid').disabled = false;
document.getElementById('id_ocinstanceid').disabled = false;
});
};
38 changes: 37 additions & 1 deletion classes/local/apibridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace mod_opencast\local;

use tool_opencast\local\api;
use tool_opencast\seriesmapping;

defined('MOODLE_INTERNAL') || die();

Expand All @@ -35,7 +36,8 @@
* @copyright 2020 Justus Dieckmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class apibridge {
class apibridge
{

/** @var int Opencast instance id */
private $ocinstanceid;
Expand Down Expand Up @@ -139,6 +141,40 @@ public function get_episode($episodeid, $ensureseries = null) {
return $response;
}

/**
* Creates an array with all available series in the course and an array with all episodes,
* independent of the Opencast instance.
* @param int $courseid Course id
* @return array[] First item: series, second item: episodes
* @throws \coding_exception
*/
public static function get_course_series_and_episodes($courseid) {
$series = seriesmapping::get_records(array('courseid' => $courseid));
$serieschoices = array();
$episodechoices = array();

foreach ($series as $s) {
$apibridge = new apibridge($s->get('ocinstanceid'));
$ocseries = $apibridge->get_series($s->get('series'));
if ($ocseries) {
$serieskey = $ocseries->identifier . '_' . $s->get('ocinstanceid');

$serieschoices[$serieskey] = $ocseries->title;
$episodes = $apibridge->get_episodes_in_series($ocseries->identifier);

if ($episodes) {
$episodechoices[$serieskey] = array('allvideos' => get_string('allvideos', 'mod_opencast'));

foreach ($episodes as $episode) {
$episodechoices[$serieskey][$episode->identifier] = $episode->title;
}
}
}
}

return [$serieschoices, $episodechoices];
}

/**
* Finds out, if a opencastid specifies an episode, a series, or nothing.
* @param string $id opencastid
Expand Down
5 changes: 5 additions & 0 deletions lang/en/opencast.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@

defined('MOODLE_INTERNAL') || die();

$string['allvideos'] = 'All videos';
$string['allowdownload'] = 'Allow students to download the video(s)';

$string['date'] = 'Date';
$string['downloadvideo'] = 'Download video';
$string['duration'] = 'Duration';

$string['episode'] = 'Opencast episode';
$string['errorfetchingvideo'] = 'There was a problem fetching the video.';

$string['gridview'] = 'View as grid';

$string['listview'] = 'View as list';

$string['manualocid'] = 'Directly enter the Opencast ID of the series/episode';
$string['modulename'] = 'Video (Opencast)';
$string['modulenameplural'] = 'Videos (Opencast)';

$string['ocinstance'] = 'Opencast instance';
$string['opencastname'] = 'Opencast Video Provider: {$a}';
$string['opencast:addinstance'] = 'Add a new Video (Opencast) instance';
$string['opencastid'] = 'Opencast ID';
Expand All @@ -60,6 +64,7 @@
$string['settings:global_download'] = 'Force student download';
$string['settings:global_download_desc'] = 'Allow globally that students can download videos. Teachers cannot overwrite this setting.';

$string['series'] = 'Opencast series';
$string['seriesisempty'] = 'This series is currently empty.';

$string['title'] = 'Title';
Expand Down
33 changes: 28 additions & 5 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class mod_opencast_mod_form extends moodleform_mod
* Defines forms elements
*/
public function definition() {
global $CFG;
global $CFG, $PAGE;

$mform = $this->_form;

Expand All @@ -66,10 +66,35 @@ public function definition() {

$this->standard_intro_elements();

$mform->addElement('advcheckbox', 'manualocid', get_string('manualocid', 'mod_opencast'));
$mform->setType('manualocid', PARAM_BOOL);
$mform->setDefault('manualocid', '0');

$choices = apibridge::get_course_series_and_episodes($this->get_course()->id);
$mform->addElement('select', 'series', get_string('series', 'mod_opencast'), $choices[0]);
$mform->setType('series', PARAM_ALPHANUMEXT);
$mform->hideIf('series', 'manualocid', 'eq', '1');

$mform->addElement('select', 'episode', get_string('episode', 'mod_opencast'), array_merge(...array_values($choices[1])));
$mform->setType('episode', PARAM_ALPHANUMEXT);
$mform->hideIf('episode', 'manualocid', 'eq', '1');

$ocoptions = array();
foreach (\tool_opencast\local\settings_api::get_ocinstances() as $oci) {
$ocoptions[$oci->id] = $oci->name;
}

$mform->addElement('select', 'ocinstanceid', get_string('ocinstance', 'mod_opencast'), $ocoptions);
$mform->setType('ocinstanceid', PARAM_INT);
$mform->setDefault('ocinstanceid', \tool_opencast\local\settings_api::get_default_ocinstance()->id);
$mform->addRule('ocinstanceid', get_string('required'), 'required');
$mform->hideIf('ocinstanceid', 'manualocid', 'eq', '0');

$mform->addElement('text', 'opencastid', get_string('opencastid', 'mod_opencast'),
array('size' => 64));
$mform->setType('opencastid', PARAM_ALPHANUMEXT);
$mform->addRule('opencastid', get_string('required'), 'required');
$mform->hideIf('opencastid', 'manualocid', 'eq', '0');

if (get_config('mod_opencast', 'global_download_' . $ocinstanceid)) {
$mform->addElement('hidden', 'allowdownload');
Expand All @@ -81,10 +106,6 @@ public function definition() {
$mform->setDefault('allowdownload', get_config('mod_opencast', 'download_default_' . $ocinstanceid));
}

$mform->addElement('hidden', 'ocinstanceid');
$mform->setType('ocinstanceid', PARAM_INT);
$mform->setDefault('ocinstanceid', \tool_opencast\local\settings_api::get_default_ocinstance()->id);

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

Expand All @@ -93,6 +114,8 @@ public function definition() {

// Add standard buttons.
$this->add_action_buttons();

$PAGE->requires->js_call_amd('mod_opencast/form_dependent_select', 'init', array($choices[1]));
}

/**
Expand Down
16 changes: 8 additions & 8 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@
use mod_opencast\local\opencasttype;
use mod_opencast\local\output_helper;

require(__DIR__.'/../../config.php');
require_once(__DIR__.'/lib.php');
require(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');

global $OUTPUT, $DB, $PAGE;

// Course_module ID, or ...
$id = optional_param('id', 0, PARAM_INT);

// ... module instance id.
$o = optional_param('o', 0, PARAM_INT);
$o = optional_param('o', 0, PARAM_INT);

$episode = optional_param('e', null, PARAM_ALPHANUMEXT);

if ($id) {
$cm = get_coursemodule_from_id('opencast', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_id('opencast', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$moduleinstance = $DB->get_record('opencast', array('id' => $cm->instance), '*', MUST_EXIST);
} else if ($o) {
$moduleinstance = $DB->get_record('opencast', array('id' => $o), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('opencast', $moduleinstance->id, $course->id, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('opencast', $moduleinstance->id, $course->id, false, MUST_EXIST);
} else {
print_error(get_string('missingidandcmid', 'mod_opencast'));
throw new moodle_exception('missingidandcmid', 'mod_opencast');
}

require_login($course, true, $cm);
Expand Down

0 comments on commit 103e88e

Please sign in to comment.