1 |
efrain |
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 |
declare(strict_types = 1);
|
|
|
18 |
|
|
|
19 |
namespace mod_edusharing;
|
|
|
20 |
|
|
|
21 |
use Exception;
|
|
|
22 |
use JsonException;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Class InstallUpgradeLogic
|
|
|
26 |
*
|
|
|
27 |
* @author Marian Ziegler <ziegler@edu-sharing.net>
|
|
|
28 |
* @package mod_edusharing
|
|
|
29 |
*/
|
|
|
30 |
class InstallUpgradeLogic {
|
|
|
31 |
/**
|
|
|
32 |
* @var PluginRegistration|null
|
|
|
33 |
*/
|
|
|
34 |
private ?PluginRegistration $registrationlogic = null;
|
|
|
35 |
/**
|
|
|
36 |
* @var MetadataLogic|null
|
|
|
37 |
*/
|
|
|
38 |
private ?MetadataLogic $metadatalogic = null;
|
|
|
39 |
/**
|
|
|
40 |
* @var string
|
|
|
41 |
*/
|
|
|
42 |
private string $configpath;
|
|
|
43 |
/**
|
|
|
44 |
* @var array|null
|
|
|
45 |
*/
|
|
|
46 |
private ?array $configdata = null;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* InstallUpgradeLogic constructor
|
|
|
50 |
*
|
|
|
51 |
* @param string $configpath
|
|
|
52 |
*/
|
|
|
53 |
public function __construct(string $configpath = __DIR__ . '/../db/installConfig.json') {
|
|
|
54 |
$this->configpath = $configpath;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Function parse_config_data
|
|
|
59 |
*
|
|
|
60 |
* @throws JsonException
|
|
|
61 |
* @throws Exception
|
|
|
62 |
*/
|
|
|
63 |
public function parse_config_data(): void {
|
|
|
64 |
if (! file_exists($this->configpath)) {
|
|
|
65 |
throw new Exception('Metadata import and plugin registration failed: Missing installConfig.json');
|
|
|
66 |
}
|
|
|
67 |
$jsonstring = file_get_contents($this->configpath);
|
|
|
68 |
$this->configdata = json_decode($jsonstring, true, 512, JSON_THROW_ON_ERROR);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Function perform
|
|
|
73 |
*
|
|
|
74 |
* @param bool $isinstall
|
|
|
75 |
* @return void
|
|
|
76 |
*/
|
|
|
77 |
public function perform(bool $isinstall = true): void {
|
|
|
78 |
global $CFG;
|
|
|
79 |
if (in_array(null, [$this->metadatalogic, $this->registrationlogic, $this->configdata], true)
|
|
|
80 |
|| empty($this->configdata['repoAdmin']) || empty($this->configdata['repoAdminPassword'])
|
|
|
81 |
) {
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
$metadataurl = $this->configdata['repoUrl'] . '/metadata?format=lms&external=true';
|
|
|
85 |
if ($isinstall && $this->configdata['autoAppIdFromUrl']) {
|
|
|
86 |
$this->metadatalogic->set_app_id(basename($CFG->wwwroot));
|
|
|
87 |
}
|
|
|
88 |
if (! empty($this->configdata['wloGuestUser_optional'])) {
|
|
|
89 |
$this->metadatalogic->set_wlo_guest_user($this->configdata['wloGuestUser_optional']);
|
|
|
90 |
}
|
|
|
91 |
if (! empty($this->configdata['hostAliases_optional'])) {
|
|
|
92 |
$this->metadatalogic->set_host_aliases($this->configdata['hostAliases_optional']);
|
|
|
93 |
}
|
|
|
94 |
try {
|
|
|
95 |
$this->metadatalogic->import_metadata($metadataurl, $this->configdata['host'] ?? null);
|
|
|
96 |
$repourl = get_config('edusharing', 'application_cc_gui_url');
|
|
|
97 |
$data = $this->metadatalogic->create_xml_metadata();
|
|
|
98 |
$registrationresult = $this->registrationlogic->register_plugin(
|
|
|
99 |
$repourl,
|
|
|
100 |
$this->configdata['repoAdmin'],
|
|
|
101 |
$this->configdata['repoAdminPassword'],
|
|
|
102 |
$data
|
|
|
103 |
);
|
|
|
104 |
} catch (Exception $exception) {
|
|
|
105 |
debugging($exception->getMessage());
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
if (! isset($registrationresult['appid'])) {
|
|
|
109 |
debugging('Automatic plugin registration could not be performed.');
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Function get_config_data
|
|
|
115 |
*
|
|
|
116 |
* @return array
|
|
|
117 |
*/
|
|
|
118 |
public function get_config_data(): array {
|
|
|
119 |
return $this->configdata ?? [];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Function set_registration_logic
|
|
|
124 |
*
|
|
|
125 |
* @param PluginRegistration $pluginregistration
|
|
|
126 |
* @return void
|
|
|
127 |
*/
|
|
|
128 |
public function set_registration_logic(PluginRegistration $pluginregistration): void {
|
|
|
129 |
$this->registrationlogic = $pluginregistration;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Function set_metadata_logic
|
|
|
134 |
*
|
|
|
135 |
* @param MetadataLogic $metadatalogic
|
|
|
136 |
* @return void
|
|
|
137 |
*/
|
|
|
138 |
public function set_metadata_logic(MetadataLogic $metadatalogic): void {
|
|
|
139 |
$this->metadatalogic = $metadatalogic;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Function discern_app_id
|
|
|
144 |
*
|
|
|
145 |
* During install and upgrade an appId has to be set.
|
|
|
146 |
* This function discerns and returns it.
|
|
|
147 |
* Priority (highest to lowest):
|
|
|
148 |
* - configured preexisting app id (from get_config)
|
|
|
149 |
* - app id provided in installConfig.json
|
|
|
150 |
* - auto generated new app id
|
|
|
151 |
*
|
|
|
152 |
* @return string
|
|
|
153 |
*/
|
|
|
154 |
public function discern_app_id(): string {
|
|
|
155 |
$utils = new UtilityFunctions();
|
|
|
156 |
try {
|
|
|
157 |
$appid = empty($utils->get_config_entry('application_appid')) ? false : $utils->get_config_entry('application_appid');
|
|
|
158 |
} catch (Exception $exception) {
|
|
|
159 |
unset($exception);
|
|
|
160 |
$appid = false;
|
|
|
161 |
}
|
|
|
162 |
if ($appid === false) {
|
|
|
163 |
$appid = $this->get_config_data()['moodleAppId_optional'];
|
|
|
164 |
if (empty($appid)) {
|
|
|
165 |
$appid = uniqid('moodle_');
|
|
|
166 |
}
|
|
|
167 |
} else {
|
|
|
168 |
$appid = (string)$appid;
|
|
|
169 |
}
|
|
|
170 |
return $appid;
|
|
|
171 |
}
|
|
|
172 |
}
|