-
Notifications
You must be signed in to change notification settings - Fork 0
/
editcourse.php
executable file
·163 lines (147 loc) · 6.2 KB
/
editcourse.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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 course 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/editcourse_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');
require_capability('local/satool:editcourse', context_system::instance());
// Get params.
$id = optional_param('id', -1, PARAM_INT);
$unassignedteacherids = optional_param_array('teacherunassignedselect', [], PARAM_INT);
$assignedteacherids = optional_param_array('teacherassignedselect', [], PARAM_INT);
$unassignedstudentids = optional_param_array('studentunassignedselect', [], PARAM_INT);
$assignedstudentids = optional_param_array('studentassignedselect', [], PARAM_INT);
// Set Page variables.
$PAGE->set_url(new moodle_url('/local/satool/editcourse.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'));
$PAGE->navbar->add('SA-Tool', new moodle_url('/local/satool'));
// Set additional values.
$returnurl = new moodle_url('/local/satool');
$manageroptions = array(
'maxfiles' => 5
);
$html = '';
// Check for existing course.
if ($id == -1) {
$course = new stdClass();
$course->id = -1;
$PAGE->navbar->add(get_string('newcourse', 'local_satool'));
} else {
$course = $DB->get_record('local_satool_courses', ['id' => $id]);
if (!$course) {
print_error('accessdenied', 'admin');
}
$PAGE->navbar->add($course->name);
}
// Add teacher to course.
if (optional_param('teacheradd', false, PARAM_BOOL) && count($unassignedteacherids) && confirm_sesskey()) {
foreach ($unassignedteacherids as $teacherid) {
$teacher = new stdClass();
$teacher->courseid = $course->id;
$teacher->userid = $teacherid;
$DB->insert_record('local_satool_teachers', $teacher);
}
}
// Remove teacher from course.
if (optional_param('teacherremove', false, PARAM_BOOL) && count($assignedteacherids) && confirm_sesskey()) {
foreach ($assignedteacherids as $teacherid) {
$DB->delete_records('local_satool_teachers', ['userid' => $teacherid]);
}
}
// Add student to course.
if (optional_param('studentadd', false, PARAM_BOOL) && count($unassignedstudentids) && confirm_sesskey()) {
foreach ($unassignedstudentids as $studentid) {
$student = $DB->get_record('local_satool_students',
['courseid' => $course->id, 'userid' => $studentid]);
if ($student) {
$student->status = 1;
$DB->update_record('local_satool_students', $student);
} else {
$student = new stdClass();
$student->courseid = $course->id;
$student->userid = $studentid;
$student->status = 1;
$DB->insert_record('local_satool_students', $student);
}
}
}
// Remove student from course.
if (optional_param('studentremove', false, PARAM_BOOL) && count($assignedstudentids) && confirm_sesskey()) {
foreach ($assignedstudentids as $studentid) {
$student = $DB->get_record('local_satool_students',
['courseid' => $course->id, 'userid' => $studentid]);
$student->status = 0;
$DB->update_record('local_satool_students', $student);
}
}
// Prepare Filemanager if course exists.
if ($course->id !== -1) {
$course = file_prepare_standard_filemanager($course, 'coursefiles', $manageroptions, $PAGE->context,
'local_satool', 'document', $course->id * 1000000);
}
$courseform = new local_satool_editcourse_form(new moodle_url($PAGE->url), array('course' => $course));
// Deal with form submission.
if ($courseform->is_cancelled()) {
redirect($returnurl);
} else if ($coursenew = $courseform->get_data()) {
$coursecreated = false;
$coursenew->id = $id;
// Check if course is new and save accordingly if it is.
if ($coursenew->id == -1) {
unset($coursenew->id);
$coursenewid = local_satool_create_course($coursenew);
$coursenew->id = $coursenewid;
$coursesave = file_save_draft_area_files($coursenew->coursefiles_filemanager, $PAGE->context->id,
'local_satool', 'document', $coursenew->id * 1000000,
array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 5));
local_satool_update_course($coursenew);
} else {
$coursesave = file_save_draft_area_files($coursenew->coursefiles_filemanager, $PAGE->context->id,
'local_satool', 'document', $coursenew->id * 1000000,
array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 5));
local_satool_update_course($coursenew);
}
redirect($returnurl);
}
// Load both course forms if class exists in database.
if ($course->id != -1) {
$teacherform = local_satool_load_courseteacherform($course);
$studentform = local_satool_load_coursestudentform($course);
$formtext = $teacherform . '<br>' . $studentform;
} else {
$formtext = html_writer::tag('p', get_string('createcoursetounlock', 'local_satool'));
}
// Prepare html.
$html = '<br>' . html_writer::tag('form', $formtext,
['action' => new moodle_url($PAGE->url, ['sesskey' => sesskey()]), 'method' => 'post', 'class' => 'courseform']);
// Output the page.
echo $OUTPUT->header();
$courseform->display();
echo $html;
echo $OUTPUT->footer();