-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditdef.php
More file actions
executable file
·127 lines (113 loc) · 4.66 KB
/
editdef.php
File metadata and controls
executable file
·127 lines (113 loc) · 4.66 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?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/>.
/**
* The SA-Tool edit project definition page.
*
* @package local_satool
* @copyright 2021 Jeremy Funke
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot.'/local/satool/classes/editdef_form.php');
require_once($CFG->dirroot.'/local/satool/lib.php');
require_once($CFG->libdir.'/filelib.php');
// Check for capabilities and if user is logged in.
require_login();
!isguestuser($USER->id) || print_error('noguest');
// Get params.
$id = optional_param('id', -1, PARAM_INT);
// Get database objects.
$course = array_reverse($DB->get_records('local_satool_courses'))[0];
// Check for existing project.
if ($id == -1) {
$project = new stdClass();
$project->id = -1;
$project->status = 0;
$student1 = $DB->get_record('local_satool_students', ['courseid' => $course->id, 'userid' => $USER->id]);
} else {
$project = $DB->get_record('local_satool_projects', ['id' => $id]);
$projdef = json_decode($project->definition);
$student1 = $DB->get_record('local_satool_students', ['courseid' => $course->id, 'userid' => $projdef->student1]);
}
// Check if user is allowed to view page.
if (!$student1) {
print_error('accessdenied', 'admin');
}
// Check if user is owner of the project.
if ($id != -1 && $student1->userid != $USER->id) {
print_error('accessdenied', 'admin');
}
// Set Page variables.
$PAGE->set_url(new moodle_url('/local/satool/editdef.php', ['id' => $id]));
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('title', 'local_satool'));
$PAGE->set_heading(get_string('title', 'local_satool'));
// Set additional values.
$returnurl = new moodle_url('/local/satool');
$manageroptions = array(
'maxfiles' => 1,
'accepted_types' => array('.svg', '.jpg', '.png')
);
$html = '';
// Check for existing project.
if ($id == -1) {
$project = new stdClass();
$project->id = -1;
$project->status = 0;
} else {
$project = $DB->get_record('local_satool_projects', ['id' => $id]);
$projdef = json_decode($project->definition);
}
// Prepare Filemanager if project exists.
if ($project->id !== -1) {
$projdef = file_prepare_standard_filemanager($projdef, 'projsketch', $manageroptions, $PAGE->context,
'local_satool', 'document', $course->id * 1000000 + $project->id * 100);
}
$projectform = new local_satool_editdef_form(new moodle_url($PAGE->url), array('project' => $project));
// Deal with form submission.
if ($projectform->is_cancelled()) {
redirect($returnurl);
} else if ($projdefnew = $projectform->get_data()) {
// Check if project is new and save accordingly if it is.
if ($id == -1) {
unset($projdefnew->id);
unset($projdefnew->submitbutton);
$projectnew = local_satool_create_projdef($projdefnew, $course->id);
$projdefnew = json_decode($projectnew->definition);
$projectsave = file_save_draft_area_files($projdefnew->projsketch_filemanager, $PAGE->context->id,
'local_satool', 'document', $course->id * 1000000 + $projectnew->id * 100,
array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 1,
'accepted_types' => array('.svg', '.jpg', '.png')
));
local_satool_update_projdef($projdefnew, $course->id, $projectnew);
} else {
$projectnew = $DB->get_record('local_satool_projects', ['id' => $projdefnew->id]);
unset($projdefnew->id);
unset($projdefnew->submitbutton);
$projectsave = file_save_draft_area_files($projdefnew->projsketch_filemanager, $PAGE->context->id,
'local_satool', 'document', $course->id * 1000000 + $projectnew->id * 100,
array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 1,
'accepted_types' => array('.svg', '.jpg', '.png')
));
local_satool_update_projdef($projdefnew, $course->id, $projectnew);
}
redirect($returnurl);
}
// Output page.
echo $OUTPUT->header();
$projectform->display();
echo $OUTPUT->footer();