Skip to content

Commit 91b6fc9

Browse files
author
Matthias Kollenbroich
committed
Removed Opencast instance settings without instance id (for default Opencast instance)
1 parent db03139 commit 91b6fc9

11 files changed

+891
-250
lines changed

classes/local/api.php

+51-23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace tool_opencast\local;
2828

2929
use local_chunkupload\local\chunkupload_file;
30+
use Matrix\Exception;
3031
use tool_opencast\empty_configuration_exception;
3132

3233
defined('MOODLE_INTERNAL') || die;
@@ -130,54 +131,81 @@ public static function get_courses_series_title($courseid) {
130131
/**
131132
* Returns the real api or test api depending on the environment.
132133
*
133-
* @param null $instanceid Opencast instance id
134+
* @param int|null $instanceid
135+
* Opencast instance id.
136+
*
134137
* @param array $settings
135138
* @param array $customconfigs
139+
*
136140
* @return api|api_testable
141+
*
137142
* @throws \dml_exception
138143
* @throws \moodle_exception
139144
*/
140-
public static function get_instance($instanceid = null, $settings = array(), $customconfigs = array()) {
141-
if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING && get_config('tool_opencast', 'apiurl') == 'http://testapi:8080') {
142-
return new api_testable();
145+
public static function get_instance($instanceid = null,
146+
$settings = array(),
147+
$customconfigs = array()) {
148+
if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING) {
149+
$defaultocinstance = settings_api::get_default_ocinstance();
150+
if ($defaultocinstance === null) {
151+
throw new \dml_exception('dmlreadexception', null,
152+
'No default Opencast instance is defined.');
153+
}
154+
155+
$defaultocinstanceapiurl = settings_api::get_apiurl($defaultocinstance->id);
156+
if ($defaultocinstanceapiurl === false) {
157+
throw new \dml_exception('dmlreadexception', null,
158+
'No api url for the default Opencast instance is defined.');
159+
}
160+
161+
if ($defaultocinstanceapiurl === 'http://testapi:8080') {
162+
return new api_testable();
163+
}
143164
}
165+
144166
return new api($instanceid, $settings, $customconfigs);
145167
}
146168

147169
/**
148170
* Constructor of the Opencast API.
149-
* @param int $instanceid Opencast instance id
150-
* @param array $settings additional curl settings.
151-
* @param array $customconfigs custom api config.
171+
*
172+
* @param int|null $instanceid
173+
* Opencast instance id.
174+
*
175+
* @param array $settings
176+
* Additional curl settings.
177+
*
178+
* @param array $customconfigs
179+
* Custom api config.
180+
*
152181
* @throws \dml_exception
153182
* @throws \moodle_exception
154183
*/
155-
public function __construct($instanceid = null, $settings = array(), $customconfigs = array()) {
184+
public function __construct($instanceid = null,
185+
$settings = array(),
186+
$customconfigs = array()) {
156187
// Allow access to local ips.
157188
$settings['ignoresecurity'] = true;
158189
parent::__construct($settings);
159190

160191
$instanceid = intval($instanceid);
161192

162-
$ocinstances = settings_api::get_ocinstances();
163-
$key = array_search(true, array_column($ocinstances, 'isdefault'));
164-
165193
// If there is no custom configs to set, we go for the stored configs.
166194
if (empty($customconfigs)) {
167-
if (!$instanceid || $ocinstances[$key]->id === $instanceid) {
168-
$this->username = get_config('tool_opencast', 'apiusername');
169-
$this->password = get_config('tool_opencast', 'apipassword');;
170-
$this->timeout = get_config('tool_opencast', 'apitimeout');;
171-
$this->connecttimeout = get_config('tool_opencast', 'apiconnecttimeout');;
172-
$this->baseurl = get_config('tool_opencast', 'apiurl');
173-
} else {
174-
$this->username = get_config('tool_opencast', 'apiusername_' . $instanceid);
175-
$this->password = get_config('tool_opencast', 'apipassword_' . $instanceid);
176-
$this->timeout = get_config('tool_opencast', 'apitimeout_' . $instanceid);
177-
$this->connecttimeout = get_config('tool_opencast', 'apiconnecttimeout_' . $instanceid);
178-
$this->baseurl = get_config('tool_opencast', 'apiurl_' . $instanceid);
195+
$defaultocinstance = settings_api::get_default_ocinstance();
196+
if ($defaultocinstance === null) {
197+
throw new \dml_exception('dmlreadexception', null,
198+
'No default Opencast instance is defined.');
179199
}
180200

201+
$storedconfigocinstanceid = !$instanceid ? $defaultocinstance->id : $instanceid;
202+
203+
$this->username = settings_api::get_apiusername($storedconfigocinstanceid);
204+
$this->password = settings_api::get_apipassword($storedconfigocinstanceid);
205+
$this->timeout = settings_api::get_apitimeout($storedconfigocinstanceid);
206+
$this->connecttimeout = settings_api::get_apiconnecttimeout($storedconfigocinstanceid);
207+
$this->baseurl = settings_api::get_apiurl($storedconfigocinstanceid);
208+
181209
if (empty($this->username)) {
182210
throw new empty_configuration_exception('apiusernameempty', 'tool_opencast');
183211
}

classes/local/opencast_instance.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_opencast\local;
18+
19+
/**
20+
* A class, to represent Opencast instances for Moodle.
21+
*
22+
* An instance of this class represents an Opencast instance for Moodle and has the properties,
23+
* that are given by or are definable with the admin settings of tool_opencast for an Opencast instance.
24+
*
25+
* @package tool_opencast
26+
* @copyright 2022 Matthias Kollenbroich, University of Münster
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class opencast_instance {
30+
31+
/**
32+
* The id of the Opencast instance.
33+
*
34+
* This id identifies the configuration of the Opencast instance for Moodle
35+
* and is not explicitly associated to Opencast.
36+
*
37+
* Note, that a valid id of an Opencast instance is greater than zero.
38+
*
39+
* @var int
40+
*/
41+
public int $id;
42+
43+
/**
44+
* The name of the Opencast instance.
45+
*
46+
* @var string
47+
*/
48+
public string $name;
49+
50+
/**
51+
* The visibility state of the Opencast instance.
52+
*
53+
* @var bool
54+
*/
55+
public bool $isvisible;
56+
57+
/**
58+
* The default state of the Opencast instance.
59+
*
60+
* Exactly one of the for Moodle configured Opencast instances
61+
* is the default Opencast instance.
62+
* For this instance, this property is true.
63+
* For all other instances, this property is false.
64+
*
65+
* @var bool
66+
*/
67+
public bool $isdefault;
68+
69+
/**
70+
* Constructs an instance with the properties of the passed \stdClass instance,
71+
* which are copied.
72+
*
73+
* All properties, that are required for an instance of the class
74+
* opencast_instance, must be defined for the passed \stdClass instance.
75+
*
76+
* @param \stdClass $dynamicobject
77+
*/
78+
public function __construct(\stdClass $dynamicobject) {
79+
$this->id = $dynamicobject->id;
80+
$this->name = $dynamicobject->name;
81+
$this->isvisible = $dynamicobject->isvisible;
82+
$this->isdefault = $dynamicobject->isdefault;
83+
}
84+
85+
}

0 commit comments

Comments
 (0)