forked from appalachianstate/moodle-local_userenrols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.php
171 lines (140 loc) · 7.58 KB
/
import.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
164
165
166
167
168
169
170
171
<?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/>.
/**
* local_userenrols
*
* This plugin will import user enrollments and group assignments
* from a delimited text file. It does not create new user accounts
* in Moodle, it will only enroll existing users in a course.
*
* @author Fred Woolard <[email protected]>
* @copyright (c) 2013 Appalachian State Universtiy, Boone, NC
* @license GNU General Public License version 3
* @package local_userenrols
*/
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
require_once(__DIR__ . '/import_form.php');
// Fetch the course id from query string
$course_id = required_param('id', PARAM_INT);
// No anonymous access for this page, and this will
// handle bogus course id values as well
require_login($course_id);
// $PAGE, $USER, $COURSE, and other globals now set
// up, check the capabilities
require_capability('enrol/manual:enrol', $PAGE->context);
// Determine if they can manage groups
$canmanagegroups = has_capability('moodle/course:managegroups', $PAGE->context);
$user_context = context_user::instance($USER->id);
// Want this for subsequent print_error() calls
$course_url = new moodle_url("{$CFG->wwwroot}/course/view.php", array('id' => $COURSE->id));
$groups_url = new moodle_url("{$CFG->wwwroot}/group/index.php", array('id' => $COURSE->id));
$enrol_url = new moodle_url("{$CFG->wwwroot}/user/index.php", array('id' => $COURSE->id));
$page_head_title = get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME) . ' : ' . $COURSE->shortname;
$PAGE->set_title($page_head_title);
$PAGE->set_heading($page_head_title);
$PAGE->set_pagelayout('incourse');
$PAGE->set_url(new moodle_url("{$CFG->wwwroot}/local/userenrols/import.php", array('id' => $COURSE->id)));
$PAGE->set_cacheable(false);
// Fix up the form. Have not determined yet whether this is a
// GET or POST, but the form will be used in either case.
// Fix up our customdata object to pass to the form constructor
$data = new stdClass();
$data->course = $COURSE;
$data->context = $PAGE->context;
$data->user_id_field_options
= local_userenrols_plugin::get_user_id_field_options();
$data->metacourse = false;
$data->default_role_id = 0;
$data->canmanagegroups = $canmanagegroups;
// Iterate the list of active enrol plugins looking for
// the manual course plugin, deal breaker if not found
$manual_enrol_instance = null;
$enrols_enabled = enrol_get_instances($COURSE->id, true);
foreach($enrols_enabled as $enrol) {
if ($enrol->enrol == 'manual') {
$manual_enrol_instance = $enrol;
$data->default_role_id = $enrol->roleid;
break;
}
}
// Deal breaker
if (null == $manual_enrol_instance) {
print_error('ERR_NO_MANUAL_ENROL', local_userenrols_plugin::PLUGIN_NAME, $course_url);
}
// Iterate the list of active enrol plugins looking for
// the meta course plugin
reset($enrols_enabled);
foreach($enrols_enabled as $enrol) {
if ($enrol->enrol == 'meta') {
$data->metacourse = true;
$data->default_role_id = 0;
break;
}
}
// Set some options for the filepicker
$file_picker_options = array(
'accepted_types' => array('.csv','.txt'),
'maxbytes' => local_userenrols_plugin::MAXFILESIZE);
$formdata = null;
$mform = new local_userenrols_index_form($PAGE->url->out(), array('data' => $data, 'options' => $file_picker_options));
if ($mform->is_cancelled()) {
// POST request, but cancel button clicked, or formdata not
// valid. Either event, clear out draft file area to remove
// unused uploads, then send back to course view
get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', file_get_submitted_draft_itemid(local_userenrols_plugin::FORMID_FILES));
redirect($course_url);
} elseif (!$mform->is_submitted() || null == ($formdata = $mform->get_data())) {
// GET request, or POST request where data did not
// pass validation, either case display the form
echo $OUTPUT->header();
echo $OUTPUT->heading_with_help(get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_IMPORT', local_userenrols_plugin::PLUGIN_NAME);
// Display the form with a filepicker
echo $OUTPUT->container_start();
$mform->display();
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
} else {
// POST request, submit button clicked and formdata
// passed validation, first check session spoofing
require_sesskey();
// Collect the input
$user_id_field = empty($formdata->{local_userenrols_plugin::FORMID_USER_ID_FIELD})
? '' : $formdata->{local_userenrols_plugin::FORMID_USER_ID_FIELD};
$role_id = $data->metacourse
? 0 : (empty($formdata->{local_userenrols_plugin::FORMID_ROLE_ID})
? 0 : intval($formdata->{local_userenrols_plugin::FORMID_ROLE_ID}));
$group_assign = empty($formdata->{local_userenrols_plugin::FORMID_GROUP})
? 0 : intval($formdata->{local_userenrols_plugin::FORMID_GROUP});
$group_id = empty($formdata->{local_userenrols_plugin::FORMID_GROUP_ID})
? 0 : intval($formdata->{local_userenrols_plugin::FORMID_GROUP_ID});
$group_create = empty($formdata->{local_userenrols_plugin::FORMID_GROUP_CREATE})
? 0 : intval($formdata->{local_userenrols_plugin::FORMID_GROUP_CREATE});
// Leave the file in the user's draft area since we
// will not plan to keep it after processing
$area_files = get_file_storage()->get_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}, null, false);
$result = local_userenrols_plugin::import_file($COURSE, $manual_enrol_instance,
$user_id_field, $role_id, $canmanagegroups ? (boolean)$group_assign : false,
$group_id, (boolean)$group_create, array_shift($area_files));
// Clean up the file area
get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES});
echo $OUTPUT->header();
echo $OUTPUT->heading_with_help(get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_IMPORT', local_userenrols_plugin::PLUGIN_NAME);
// Output the processing result
echo $OUTPUT->box(nl2br($result));
echo $OUTPUT->continue_button($canmanagegroups && $group_assign ? $groups_url : $enrol_url);
echo $OUTPUT->footer();
}