-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit.php
143 lines (119 loc) · 4.89 KB
/
edit.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
<?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/>.
/**
* Add new instance of UCSF Student Information System enrolment plugin.
*
* @package enrol_ucsfsis
* @copyright 2016 The Regents of the University of California
* @author Carson Tam <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once('edit_form.php');
require_once('../../group/lib.php');
$courseid = required_param('courseid', PARAM_INT);
$instanceid = optional_param('id', 0, PARAM_INT);
$submittedtermid = optional_param('selectterm', null, PARAM_ALPHANUM);
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
require_login($course);
require_capability('moodle/course:enrolconfig', $context);
require_capability('enrol/ucsfsis:config', $context);
$PAGE->set_url('/enrol/ucsfsis/edit.php', ['courseid' => $course->id, 'id' => $instanceid]);
$PAGE->set_pagelayout('admin');
$returnurl = new moodle_url('/enrol/instances.php', ['id' => $course->id]);
if (!enrol_is_enabled('ucsfsis')) {
redirect($returnurl);
}
$enrol = enrol_get_plugin('ucsfsis');
// Allow only one instance for each course.
if ($instances = $DB->get_records('enrol', ['courseid' => $course->id, 'enrol' => 'ucsfsis'], 'id ASC')) {
$instance = array_shift($instances);
if ($instances) {
// Oh - we allow only one instance per course!!
foreach ($instances as $del) {
$enrol->delete_instance($del);
}
}
} else {
// No instance yet, we have to add new instance.
if (!$enrol->get_newinstance_link($course->id)) {
redirect($returnurl);
}
navigation_node::override_active_url(new moodle_url('/enrol/instances.php', ['id' => $course->id]));
$instance = new stdClass();
$instance->id = null;
$instance->courseid = $course->id;
$instance->enrol = 'ucsfsis';
$instance->status = ENROL_INSTANCE_ENABLED;
$instance->customint1 = null; // UCSF SIS course ID.
}
// Tell $mform->definition() that we are loading a known term.
if (!empty($submittedtermid)) {
$instance->submitted_termid = $submittedtermid;
}
// Edit form to be shown here.
$mform = new enrol_ucsfsis_edit_form(null, [$instance, $enrol, $course]);
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
// We are here only because the form is submitted.
/*
* KLUDGE!
* Moodle won't let us get values of form elements that weren't in the original form definition.
* Since we're loading courses into the corresponding dropdown via XHR callbacks, the form submission handler may
* disregard them.
* So we have to dig deeper and check the raw, submitted values.
* [ST 2018/08/21]
*/
$selectcourse = null;
if (object_property_exists($data, 'selectcourse')) {
$selectcourse = $data->selectcourse;
} else {
$selectcourse = (int) $mform->get_submit_value('selectcourse');
}
if ($instance->id) {
$instance->roleid = $data->roleid;
$instance->customint1 = $selectcourse;
// Clear SIS course id if exists.
$instance->customchar1 = '';
$instance->customtext1 = ''; // Get the descriptive course name here.
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
// Use standard API to update instance status.
if ($instance->status != $data->status) {
$instance = $DB->get_record('enrol', ['id' => $instance->id]);
$enrol->update_status($instance, $data->status);
$context->mark_dirty();
}
} else {
$fields = [
'status' => $data->status,
'customint1' => $selectcourse,
'roleid' => $data->roleid];
$enrol->add_instance($course, $fields);
}
$trace = new null_progress_trace();
$enrol->sync($trace, $course->id);
$trace->finished();
redirect($returnurl);
}
$PAGE->set_title(get_string('pluginname', 'enrol_ucsfsis'));
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'enrol_ucsfsis'));
$mform->display();
echo $OUTPUT->footer();