MDL-68293 mobile: Update language strings to mention new app plans
[moodle.git] / analytics / classes / model_config.php
blob75b31f1128b20489d33a4e54620fde1cec1b5131
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.
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/>.
17 /**
18 * Model configuration manager.
20 * @package core_analytics
21 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_analytics;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Model configuration manager.
32 * @package core_analytics
33 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class model_config {
38 /**
39 * @var \core_analytics\model
41 private $model = null;
43 /**
44 * The name of the file where config is held.
46 const CONFIG_FILE_NAME = 'model-config.json';
48 /**
49 * Constructor.
51 * @param \core_analytics\model|null $model
53 public function __construct(?model $model = null) {
54 $this->model = $model;
57 /**
58 * Exports a model to a zip using the provided file name.
60 * @param string $zipfilename
61 * @param bool $includeweights Include the model weights if available
62 * @return string
64 public function export(string $zipfilename, bool $includeweights = true) : string {
66 if (!$this->model) {
67 throw new \coding_exception('No model object provided.');
70 if (!$this->model->can_export_configuration()) {
71 throw new \moodle_exception('errornoexportconfigrequirements', 'analytics');
74 $zip = new \zip_packer();
75 $zipfiles = [];
77 // Model config in JSON.
78 $modeldata = $this->export_model_data();
80 $exporttmpdir = make_request_directory('analyticsexport');
81 $jsonfilepath = $exporttmpdir . DIRECTORY_SEPARATOR . 'model-config.json';
82 if (!file_put_contents($jsonfilepath, json_encode($modeldata))) {
83 print_error('errornoexportconfig', 'analytics');
85 $zipfiles[self::CONFIG_FILE_NAME] = $jsonfilepath;
87 // ML backend.
88 if ($includeweights && $this->model->is_trained()) {
89 $processor = $this->model->get_predictions_processor(true);
90 $outputdir = $this->model->get_output_dir(array('execution'));
91 $mlbackenddir = $processor->export($this->model->get_unique_id(), $outputdir);
92 $mlbackendfiles = get_directory_list($mlbackenddir);
93 foreach ($mlbackendfiles as $mlbackendfile) {
94 $fullpath = $mlbackenddir . DIRECTORY_SEPARATOR . $mlbackendfile;
95 // Place the ML backend files inside a mlbackend/ dir.
96 $zipfiles['mlbackend/' . $mlbackendfile] = $fullpath;
100 $zipfilepath = $exporttmpdir . DIRECTORY_SEPARATOR . $zipfilename;
101 $zip->archive_to_pathname($zipfiles, $zipfilepath);
103 return $zipfilepath;
107 * Imports the provided model configuration into a new model.
109 * Note that this method assumes that self::check_dependencies has already been called.
111 * @param string $zipfilepath Path to the zip file to import
112 * @return \core_analytics\model
114 public function import(string $zipfilepath) : \core_analytics\model {
116 list($modeldata, $mlbackenddir) = $this->extract_import_contents($zipfilepath);
118 $target = \core_analytics\manager::get_target($modeldata->target);
119 $indicators = [];
120 foreach ($modeldata->indicators as $indicatorclass) {
121 $indicator = \core_analytics\manager::get_indicator($indicatorclass);
122 $indicators[$indicator->get_id()] = $indicator;
124 $model = \core_analytics\model::create($target, $indicators, $modeldata->timesplitting, $modeldata->processor);
126 // Import them disabled.
127 $model->update(false, false, false, false);
129 if ($mlbackenddir) {
130 $modeldir = $model->get_output_dir(['execution']);
131 if (!$model->get_predictions_processor(true)->import($model->get_unique_id(), $modeldir, $mlbackenddir)) {
132 throw new \moodle_exception('errorimport', 'analytics');
134 $model->mark_as_trained();
137 return $model;
141 * Check that the provided model configuration can be deployed in this site.
143 * @param \stdClass $modeldata
144 * @param bool $ignoreversionmismatches
145 * @return string|null Error string or null if all good.
147 public function check_dependencies(\stdClass $modeldata, bool $ignoreversionmismatches) : ?string {
149 $siteversions = \core_component::get_all_versions();
151 // Possible issues.
152 $missingcomponents = [];
153 $versionmismatches = [];
154 $missingclasses = [];
156 // We first check that this site has the required dependencies and the required versions.
157 foreach ($modeldata->dependencies as $component => $importversion) {
159 if (empty($siteversions[$component])) {
161 if ($component === 'core') {
162 $component = 'Moodle';
164 $missingcomponents[$component] = $component . ' (' . $importversion . ')';
165 continue;
168 if ($siteversions[$component] == $importversion) {
169 // All good here.
170 continue;
173 if (!$ignoreversionmismatches) {
174 if ($component === 'core') {
175 $component = 'Moodle';
177 $versionmismatches[$component] = $component . ' (' . $importversion . ')';
181 // Checking that each of the components is available.
182 if (!$target = manager::get_target($modeldata->target)) {
183 $missingclasses[] = $modeldata->target;
186 if (!$timesplitting = manager::get_time_splitting($modeldata->timesplitting)) {
187 $missingclasses[] = $modeldata->timesplitting;
190 // Indicators.
191 foreach ($modeldata->indicators as $indicatorclass) {
192 if (!$indicator = manager::get_indicator($indicatorclass)) {
193 $missingclasses[] = $indicatorclass;
197 // ML backend.
198 if (!empty($modeldata->processor)) {
199 if (!$processor = \core_analytics\manager::get_predictions_processor($modeldata->processor, false)) {
200 $missingclasses[] = $indicatorclass;
204 if (!empty($missingcomponents)) {
205 return get_string('errorimportmissingcomponents', 'analytics', join(', ', $missingcomponents));
208 if (!empty($versionmismatches)) {
209 return get_string('errorimportversionmismatches', 'analytics', implode(', ', $versionmismatches));
212 if (!empty($missingclasses)) {
213 $a = (object)[
214 'missingclasses' => implode(', ', $missingclasses),
216 return get_string('errorimportmissingclasses', 'analytics', $a);
219 // No issues found.
220 return null;
224 * Returns the component the class belongs to.
226 * Note that this method does not work for global space classes.
228 * @param string $fullclassname Qualified name including the namespace.
229 * @return string|null Frankenstyle component
231 public static function get_class_component(string $fullclassname) : ?string {
233 // Strip out leading backslash.
234 $fullclassname = ltrim($fullclassname, '\\');
236 $nextbackslash = strpos($fullclassname, '\\');
237 if ($nextbackslash === false) {
238 // Global space.
239 return 'core';
241 $component = substr($fullclassname, 0, $nextbackslash);
243 // All core subsystems use core's version.php.
244 if (strpos($component, 'core_') === 0) {
245 $component = 'core';
248 return $component;
252 * Extracts the import zip contents.
254 * @param string $zipfilepath Zip file path
255 * @return array [0] => \stdClass, [1] => string
257 public function extract_import_contents(string $zipfilepath) : array {
259 $importtempdir = make_request_directory('analyticsimport' . microtime(false));
261 $zip = new \zip_packer();
262 $filelist = $zip->extract_to_pathname($zipfilepath, $importtempdir);
264 if (empty($filelist[self::CONFIG_FILE_NAME])) {
265 // Missing required file.
266 throw new \moodle_exception('errorimport', 'analytics');
269 $jsonmodeldata = file_get_contents($importtempdir . DIRECTORY_SEPARATOR . self::CONFIG_FILE_NAME);
271 if (!$modeldata = json_decode($jsonmodeldata)) {
272 throw new \moodle_exception('errorimport', 'analytics');
275 if (empty($modeldata->target) || empty($modeldata->timesplitting) || empty($modeldata->indicators)) {
276 throw new \moodle_exception('errorimport', 'analytics');
279 $mlbackenddir = $importtempdir . DIRECTORY_SEPARATOR . 'mlbackend';
280 if (!is_dir($mlbackenddir)) {
281 $mlbackenddir = false;
284 return [$modeldata, $mlbackenddir];
287 * Exports the configuration of the model.
288 * @return \stdClass
290 protected function export_model_data() : \stdClass {
292 $versions = \core_component::get_all_versions();
294 $data = new \stdClass();
296 // Target.
297 $data->target = $this->model->get_target()->get_id();
298 $requiredclasses[] = $data->target;
300 // Time splitting method.
301 $data->timesplitting = $this->model->get_time_splitting()->get_id();
302 $requiredclasses[] = $data->timesplitting;
304 // Model indicators.
305 $data->indicators = [];
306 foreach ($this->model->get_indicators() as $indicator) {
307 $indicatorid = $indicator->get_id();
308 $data->indicators[] = $indicatorid;
309 $requiredclasses[] = $indicatorid;
312 // Return the predictions processor this model is using, even if no predictions processor
313 // was explicitly selected.
314 $predictionsprocessor = $this->model->get_predictions_processor();
315 $data->processor = '\\' . get_class($predictionsprocessor);
316 $requiredclasses[] = $data->processor;
318 // Add information for versioning.
319 $data->dependencies = [];
320 foreach ($requiredclasses as $fullclassname) {
321 $component = $this->get_class_component($fullclassname);
322 $data->dependencies[$component] = $versions[$component];
325 return $data;