weekly release 3.10.1+
[moodle.git] / group / import.php
CommitLineData
d9cb06dc 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Bulk group creation registration script from a comma separated file
20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4d8e2417 23 * @package core_group
d9cb06dc 24 */
25
81ed4632 26require_once('../config.php');
d9cb06dc 27require_once($CFG->dirroot.'/course/lib.php');
28require_once($CFG->dirroot.'/group/lib.php');
9bed73fa 29include_once('import_form.php');
d9cb06dc 30
31$id = required_param('id', PARAM_INT); // Course id
32
74df2951 33$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
d9cb06dc 34
81ed4632 35$PAGE->set_url('/group/import.php', array('id'=>$id));
0be6f678 36
81ed4632 37require_login($course);
bf0f06b1 38$context = context_course::instance($id);
0be6f678 39
81ed4632 40require_capability('moodle/course:managegroups', $context);
f8b3c0f4 41
b74ceb3f 42$strimportgroups = get_string('importgroups', 'core_group');
f8b3c0f4 43
d9cb06dc 44$PAGE->navbar->add($strimportgroups);
f672d6a0 45navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
d9cb06dc 46$PAGE->set_title("$course->shortname: $strimportgroups");
47$PAGE->set_heading($course->fullname);
566889aa 48$PAGE->set_pagelayout('admin');
f8b3c0f4 49
81ed4632
PS
50$returnurl = new moodle_url('/group/index.php', array('id'=>$id));
51
97043c76 52$importform = new groups_import_form(null, ['id' => $id]);
9bed73fa
DC
53
54// If a file has been uploaded, then process it
97043c76 55if ($importform->is_cancelled()) {
81ed4632
PS
56 redirect($returnurl);
57
97043c76 58} else if ($formdata = $importform->get_data()) {
81ed4632
PS
59 echo $OUTPUT->header();
60
97043c76
SL
61 $text = $importform->get_file_content('userfile');
62 $text = preg_replace('!\r\n?!', "\n", $text);
f8b3c0f4 63
97043c76
SL
64 $rawlines = explode("\n", $text);
65
66 require_once($CFG->libdir . '/csvlib.class.php');
67 $importid = csv_import_reader::get_new_iid('groupimport');
68 $csvimport = new csv_import_reader($importid, 'groupimport');
69 $delimiter = $formdata->delimiter_name;
70 $encoding = $formdata->encoding;
71 $readcount = $csvimport->load_csv_content($text, $encoding, $delimiter);
72
73 if ($readcount === false) {
74 print_error('csvfileerror', 'error', $PAGE->url, $csvimport->get_error());
75 } else if ($readcount == 0) {
76 print_error('csvemptyfile', 'error', $PAGE->url, $csvimport->get_error());
77 } else if ($readcount == 1) {
78 print_error('csvnodata', 'error', $PAGE->url);
9bed73fa 79 }
d9cb06dc 80
97043c76 81 $csvimport->init();
d9cb06dc 82
81ed4632 83 unset($text);
d9cb06dc 84
85 // make arrays of valid fields for error checking
9bed73fa
DC
86 $required = array("groupname" => 1);
87 $optionalDefaults = array("lang" => 1);
d9cb06dc 88 $optional = array("coursename" => 1,
9bed73fa 89 "idnumber" => 1,
74b714df 90 "groupidnumber" => 1,
9bed73fa 91 "description" => 1,
df3efbfb 92 "enrolmentkey" => 1,
c617a727
JP
93 "groupingname" => 1,
94 "enablemessaging" => 1,
95 );
d9cb06dc 96
97 // --- get header (field names) ---
97043c76 98 $header = explode($csvimport::get_delimiter($delimiter), array_shift($rawlines));
d9cb06dc 99 // check for valid field names
100 foreach ($header as $i => $h) {
101 $h = trim($h); $header[$i] = $h; // remove whitespace
81ed4632 102 if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
97043c76 103 print_error('invalidfieldname', 'error', $PAGE->url, $h);
9bed73fa 104 }
81ed4632 105 if (isset($required[$h])) {
d9cb06dc 106 $required[$h] = 2;
107 }
108 }
109 // check for required fields
110 foreach ($required as $key => $value) {
111 if ($value < 2) {
97043c76 112 print_error('fieldrequired', 'error', $PAGE->url, $key);
f8b3c0f4 113 }
d9cb06dc 114 }
115 $linenum = 2; // since header is line 1
f8b3c0f4 116
97043c76 117 while ($line = $csvimport->next()) {
0be6f678 118
fbaea88f 119 $newgroup = new stdClass();//to make Martin happy
d9cb06dc 120 foreach ($optionalDefaults as $key => $value) {
121 $newgroup->$key = current_language(); //defaults to current language
122 }
d9cb06dc 123 foreach ($line as $key => $value) {
97043c76 124 $record[$header[$key]] = trim($value);
d9cb06dc 125 }
97043c76 126 if (trim(implode($line)) !== '') {
d9cb06dc 127 // add a new group to the database
128
129 // add fields to object $user
130 foreach ($record as $name => $value) {
131 // check for required values
132 if (isset($required[$name]) and !$value) {
97043c76 133 print_error('missingfield', 'error', $PAGE->url, $name);
81ed4632 134 } else if ($name == "groupname") {
d9cb06dc 135 $newgroup->name = $value;
81ed4632 136 } else {
d9cb06dc 137 // normal entry
d9cb06dc 138 $newgroup->{$name} = $value;
61240489 139 }
d9cb06dc 140 }
d9cb06dc 141
b955ad6a 142 if (isset($newgroup->idnumber) && strlen($newgroup->idnumber)) {
81ed4632
PS
143 //if idnumber is set, we use that.
144 //unset invalid courseid
d9cb06dc 145 if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
146 echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
81ed4632 147 unset($newgroup->courseid);//unset so 0 doesn't get written to database
ec43371a
MG
148 } else {
149 $newgroup->courseid = $mycourse->id;
61240489 150 }
0be6f678 151
b955ad6a 152 } else if (isset($newgroup->coursename) && strlen($newgroup->coursename)) {
81ed4632
PS
153 //else use course short name to look up
154 //unset invalid coursename (if no id)
ec43371a 155 if (!$mycourse = $DB->get_record('course', array('shortname' => $newgroup->coursename))) {
d9cb06dc 156 echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
81ed4632 157 unset($newgroup->courseid);//unset so 0 doesn't get written to database
ec43371a
MG
158 } else {
159 $newgroup->courseid = $mycourse->id;
d9cb06dc 160 }
81ed4632
PS
161
162 } else {
163 //else use use current id
d9cb06dc 164 $newgroup->courseid = $id;
165 }
ec43371a
MG
166 unset($newgroup->idnumber);
167 unset($newgroup->coursename);
d9cb06dc 168
169 //if courseid is set
81ed4632 170 if (isset($newgroup->courseid)) {
d9cb06dc 171 $linenum++;
172 $groupname = $newgroup->name;
bf0f06b1 173 $newgrpcoursecontext = context_course::instance($newgroup->courseid);
d9cb06dc 174
175 ///Users cannot upload groups in courses they cannot update.
81ed4632
PS
176 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
177 echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
d9cb06dc 178
179 } else {
74b714df
ARN
180 if (isset($newgroup->groupidnumber)) {
181 // The CSV field for the group idnumber is groupidnumber rather than
182 // idnumber as that field is already in use for the course idnumber.
183 $newgroup->groupidnumber = trim($newgroup->groupidnumber);
184 if (has_capability('moodle/course:changeidnumber', $newgrpcoursecontext)) {
185 $newgroup->idnumber = $newgroup->groupidnumber;
186 if ($existing = groups_get_group_by_idnumber($newgroup->courseid, $newgroup->idnumber)) {
187 // idnumbers must be unique to a course but we shouldn't ignore group creation for duplicates
188 $existing->name = s($existing->name);
189 $existing->idnumber = s($existing->idnumber);
190 $existing->problemgroup = $groupname;
191 echo $OUTPUT->notification(get_string('groupexistforcoursewithidnumber', 'error', $existing));
192 unset($newgroup->idnumber);
193 }
194 }
195 // Always drop the groupidnumber key. It's not a valid database field
196 unset($newgroup->groupidnumber);
197 }
81ed4632
PS
198 if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
199 echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
df3efbfb 200 } else if ($groupid = groups_create_group($newgroup)) {
81ed4632
PS
201 echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
202 } else {
203 echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
df3efbfb
MA
204 continue;
205 }
a932fa43 206
df3efbfb 207 // Add group to grouping
ec43371a 208 if (isset($newgroup->groupingname) && strlen($newgroup->groupingname)) {
df3efbfb
MA
209 $groupingname = $newgroup->groupingname;
210 if (! $groupingid = groups_get_grouping_by_name($newgroup->courseid, $groupingname)) {
211 $data = new stdClass();
212 $data->courseid = $newgroup->courseid;
213 $data->name = $groupingname;
214 if ($groupingid = groups_create_grouping($data)) {
40bbce24 215 echo $OUTPUT->notification(get_string('groupingaddedsuccesfully', 'group', $groupingname), 'notifysuccess');
df3efbfb 216 } else {
40bbce24 217 echo $OUTPUT->notification(get_string('groupingnotaddederror', 'error', $groupingname));
df3efbfb
MA
218 continue;
219 }
220 }
221
222 // if we have reached here we definitely have a groupingid
223 $a = array('groupname' => $groupname, 'groupingname' => $groupingname);
224 try {
225 groups_assign_grouping($groupingid, $groupid);
226 echo $OUTPUT->notification(get_string('groupaddedtogroupingsuccesfully', 'group', $a), 'notifysuccess');
227 } catch (Exception $e) {
228 echo $OUTPUT->notification(get_string('groupnotaddedtogroupingerror', 'error', $a));
229 }
230
d9cb06dc 231 }
232 }
81ed4632 233 }
d9cb06dc 234 unset ($newgroup);
81ed4632
PS
235 }
236 }
f8b3c0f4 237
97043c76 238 $csvimport->close();
81ed4632
PS
239 echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
240 echo $OUTPUT->footer();
241 die;
d9cb06dc 242}
81ed4632
PS
243
244/// Print the form
245echo $OUTPUT->header();
b74ceb3f 246echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
97043c76 247$importform->display();
81ed4632 248echo $OUTPUT->footer();