MDL-60944 analytics: Add base support for import / export
[moodle.git] / admin / tool / analytics / importmodel.php
CommitLineData
349c4412
AA
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/**
18 * Import models tool frontend.
19 *
20 * @package tool_analytics
21 * @copyright 2017 onwards Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir . '/adminlib.php');
27
28admin_externalpage_setup('analyticsmodelimport', '', null, '', array('pagelayout' => 'report'));
29echo $OUTPUT->header();
30
31$form = new tool_analytics\import_model_form();
32if ($data = $form->get_data()) {
33 $content = json_decode($form->get_file_content('modelfile'));
34 if (empty($content->moodleversion)) {
35 // Should never happen.
36 echo $OUTPUT->notification(get_string('missingmoodleversion', 'tool_analytics'), 'error');
37 } else {
38 if ($content->moodleversion != $CFG->version) {
39 $a = new stdClass();
40 $a->importedversion = $content->moodleversion;
41 $a->version = $CFG->version;
42 echo $OUTPUT->notification(get_string('versionnotsame', 'tool_analytics', $a), 'warning');
43 }
44 $model = \core_analytics\model::create_from_json($content);
45 if ($model) {
46 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
47 } else {
48 echo $OUTPUT->notification(get_string('error'), 'error');
49 }
50 }
51 echo $OUTPUT->single_button(new moodle_url("$CFG->wwwroot/$CFG->admin/tool/analytics/index.php"),
52 get_string('continue'), 'get');
53} else {
54 $form->display();
55}
56
57echo $OUTPUT->footer();