MDL-31006 revert partially @ get_config()
[moodle.git] / group / import.php
blob159cd43674e37cf7f22b2ade54a9bbd6624bb288
1 <?php
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.
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/>.
18 /**
19 * Bulk group creation registration script from a comma separated file
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 require_once('../config.php');
27 require_once($CFG->dirroot.'/course/lib.php');
28 require_once($CFG->dirroot.'/group/lib.php');
29 include_once('import_form.php');
31 $id = required_param('id', PARAM_INT); // Course id
33 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
35 $PAGE->set_url('/group/import.php', array('id'=>$id));
37 require_login($course);
38 $context = get_context_instance(CONTEXT_COURSE, $id);
40 require_capability('moodle/course:managegroups', $context);
42 $strimportgroups = get_string('importgroups', 'core_group');
44 $PAGE->navbar->add($strimportgroups);
45 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
46 $PAGE->set_title("$course->shortname: $strimportgroups");
47 $PAGE->set_heading($course->fullname);
48 $PAGE->set_pagelayout('standard');
50 $returnurl = new moodle_url('/group/index.php', array('id'=>$id));
52 $mform_post = new groups_import_form(null, array('id'=>$id));
54 // If a file has been uploaded, then process it
55 if ($mform_post->is_cancelled()) {
56 redirect($returnurl);
58 } else if ($mform_post->get_data()) {
59 echo $OUTPUT->header();
61 $csv_encode = '/\&\#44/';
62 if (isset($CFG->CSV_DELIMITER)) {
63 $csv_delimiter = $CFG->CSV_DELIMITER;
65 if (isset($CFG->CSV_ENCODE)) {
66 $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
68 } else {
69 $csv_delimiter = ",";
72 $text = $mform_post->get_file_content('userfile');
73 $text = preg_replace('!\r\n?!',"\n",$text);
75 $rawlines = explode("\n", $text);
76 unset($text);
78 // make arrays of valid fields for error checking
79 $required = array("groupname" => 1);
80 $optionalDefaults = array("lang" => 1);
81 $optional = array("coursename" => 1,
82 "idnumber" => 1,
83 "description" => 1,
84 "enrolmentkey" => 1);
86 // --- get header (field names) ---
87 $header = explode($csv_delimiter, array_shift($rawlines));
88 // check for valid field names
89 foreach ($header as $i => $h) {
90 $h = trim($h); $header[$i] = $h; // remove whitespace
91 if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
92 print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h);
94 if (isset($required[$h])) {
95 $required[$h] = 2;
98 // check for required fields
99 foreach ($required as $key => $value) {
100 if ($value < 2) {
101 print_error('fieldrequired', 'error', 'import.php?id='.$id, $key);
104 $linenum = 2; // since header is line 1
106 foreach ($rawlines as $rawline) {
108 $newgroup = new stdClass();//to make Martin happy
109 foreach ($optionalDefaults as $key => $value) {
110 $newgroup->$key = current_language(); //defaults to current language
112 //Note: commas within a field should be encoded as &#44 (for comma separated csv files)
113 //Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
114 $line = explode($csv_delimiter, $rawline);
115 foreach ($line as $key => $value) {
116 //decode encoded commas
117 $record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
119 if ($record[$header[0]]) {
120 // add a new group to the database
122 // add fields to object $user
123 foreach ($record as $name => $value) {
124 // check for required values
125 if (isset($required[$name]) and !$value) {
126 print_error('missingfield', 'error', 'import.php?id='.$id, $name);
127 } else if ($name == "groupname") {
128 $newgroup->name = $value;
129 } else {
130 // normal entry
131 $newgroup->{$name} = $value;
135 if (isset($newgroup->idnumber)){
136 //if idnumber is set, we use that.
137 //unset invalid courseid
138 if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
139 echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
140 unset($newgroup->courseid);//unset so 0 doesn't get written to database
142 $newgroup->courseid = $mycourse->id;
144 } else if (isset($newgroup->coursename)){
145 //else use course short name to look up
146 //unset invalid coursename (if no id)
147 if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
148 echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
149 unset($newgroup->courseid);//unset so 0 doesn't get written to database
151 $newgroup->courseid = $mycourse->id;
153 } else {
154 //else use use current id
155 $newgroup->courseid = $id;
158 //if courseid is set
159 if (isset($newgroup->courseid)) {
160 $linenum++;
161 $groupname = $newgroup->name;
162 $newgrpcoursecontext = get_context_instance(CONTEXT_COURSE, $newgroup->courseid);
164 ///Users cannot upload groups in courses they cannot update.
165 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
166 echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
168 } else {
169 if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
170 echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
171 } else if (groups_create_group($newgroup)) {
172 echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
173 } else {
174 echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
178 unset ($newgroup);
182 echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
183 echo $OUTPUT->footer();
184 die;
187 /// Print the form
188 echo $OUTPUT->header();
189 echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
190 $mform_post ->display();
191 echo $OUTPUT->footer();