-
Notifications
You must be signed in to change notification settings - Fork 3
/
view_editor.php
110 lines (91 loc) · 3.58 KB
/
view_editor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Editor template creation page.
*
* @package qtype_shortmath
* @author André Storhaug <[email protected]>
* @copyright 2020 NTNU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use qtype_shortmath\form\view\editor_template_form;
use qtype_shortmath\external\editor_template;
use qtype_shortmath\output\editor_page;
use qtype_shortmath\shortmath_urls;
require_once(__DIR__ . '/../../../config.php');
require_once('lib.php');
require_login();
$context = context_system::instance();
$pageurl = new moodle_url(shortmath_urls::$editorpath);
$title = get_string('create_templates', 'qtype_shortmath');
$id = optional_param('templateid', null, PARAM_INT);
if ($id > 0) {
template_require_capability_on($id, 'edit');
$title = get_string('edit_template', 'qtype_shortmath');
$pageurl->param('templateid', $id);
} else {
require_capability('qtype/shortmath:add', $context);
}
$returnurl = optional_param('returnurl', null, PARAM_RAW);
if (!is_null($returnurl)) {
$pageurl->param('returnurl', $returnurl);
}
$PAGE->set_context($context);
$PAGE->set_url($pageurl);
$pagetitle = get_string('template_editor', 'qtype_shortmath');
$managerpath = new \moodle_url(shortmath_urls::$editormanagerpath, ['returnurl' => $returnurl]);
//$PAGE->navbar->ignore_active();
$managernode = $PAGE->navbar->add(
get_string('editor_manager', 'qtype_shortmath'),
$managerpath
);
$editornode = $managernode->add(get_string('template_editor_link', 'qtype_shortmath'));
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pagetitle);
$PAGE->set_pagelayout('admin');
$PAGE->requires->css('/question/type/shortmath/visualmathinput/mathquill.css');
$PAGE->requires->css('/question/type/shortmath/visualmathinput/visual-math-input.css');
$PAGE->requires->css('/question/type/shortmath/editor/editor.css');
// Process form actions.
$systemcontext = context_system::instance();
$mform = new editor_template_form($pageurl);
if ($mform->is_cancelled()) {
redirect($managerpath);
} else if ($data = $mform->get_data()) {
$templateid = $data->templateid;
$templateobj = new stdClass();
$templateobj->name = $data->templatename;
$templateobj->description = $data->templatedescription;
$templateobj->template = $data->templatestring;
$templateobj->contextid = $systemcontext->id;
$time = time();
if ($templateid > 0) {
$templateobj->id = $templateid;
$templateobj->usermodified = $USER->id;
$templateobj->timemodified = $time;
$DB->update_record('qtype_shortmath_templates', $templateobj);
} else {
$templateobj->timecreated = $time;
$templateobj->userid = $USER->id;
$DB->insert_record('qtype_shortmath_templates', $templateobj);
}
redirect($managerpath);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$editorpage = new editor_page($mform, $id);
echo $OUTPUT->render($editorpage);
echo $OUTPUT->footer();