Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 23 * @package core_group 24 */ 25 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'); 30 31 $id = required_param('id', PARAM_INT); // Course id 32 33 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); 34 35 $PAGE->set_url('/group/import.php', array('id'=>$id)); 36 37 require_login($course); 38 $context = context_course::instance($id); 39 40 require_capability('moodle/course:managegroups', $context); 41 42 $strimportgroups = get_string('importgroups', 'core_group'); 43 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('admin'); 49 50 $returnurl = new moodle_url('/group/index.php', array('id'=>$id)); 51 52 $importform = new groups_import_form(null, ['id' => $id]); 53 54 // If a file has been uploaded, then process it 55 if ($importform->is_cancelled()) { 56 redirect($returnurl); 57 58 } else if ($formdata = $importform->get_data()) { 59 echo $OUTPUT->header(); 60 61 $text = $importform->get_file_content('userfile'); 62 $text = preg_replace('!\r\n?!', "\n", $text); 63 64 require_once($CFG->libdir . '/csvlib.class.php'); 65 $importid = csv_import_reader::get_new_iid('groupimport'); 66 $csvimport = new csv_import_reader($importid, 'groupimport'); 67 $delimiter = $formdata->delimiter_name; 68 $encoding = $formdata->encoding; 69 $readcount = $csvimport->load_csv_content($text, $encoding, $delimiter); 70 71 if ($readcount === false) { 72 throw new \moodle_exception('csvfileerror', 'error', $PAGE->url, $csvimport->get_error()); 73 } else if ($readcount == 0) { 74 throw new \moodle_exception('csvemptyfile', 'error', $PAGE->url, $csvimport->get_error()); 75 } else if ($readcount == 1) { 76 throw new \moodle_exception('csvnodata', 'error', $PAGE->url); 77 } 78 79 $csvimport->init(); 80 81 unset($text); 82 83 // make arrays of valid fields for error checking 84 $required = array("groupname" => 1); 85 $optionaldefaults = array("lang" => 1); 86 $optional = array("coursename" => 1, 87 "idnumber" => 1, 88 "groupidnumber" => 1, 89 "description" => 1, 90 "enrolmentkey" => 1, 91 "groupingname" => 1, 92 "enablemessaging" => 1, 93 ); 94 // Check custom fields from group and grouping. 95 $customfields = \core_group\customfield\group_handler::create()->get_fields(); 96 $customfieldnames = []; 97 foreach ($customfields as $customfield) { 98 $controller = \core_customfield\data_controller::create(0, null, $customfield); 99 $customfieldnames['customfield_' . $customfield->get('shortname')] = 1; 100 } 101 $customfields = \core_group\customfield\grouping_handler::create()->get_fields(); 102 $groupingcustomfields = []; 103 foreach ($customfields as $customfield) { 104 $controller = \core_customfield\data_controller::create(0, null, $customfield); 105 $groupingcustomfieldname = 'grouping_customfield_' . $customfield->get('shortname'); 106 $customfieldnames[$groupingcustomfieldname] = 1; 107 $groupingcustomfields[$groupingcustomfieldname] = 'customfield_' . $customfield->get('shortname'); 108 } 109 110 // --- get header (field names) --- 111 // Using get_columns() ensures the Byte Order Mark is removed. 112 $header = $csvimport->get_columns(); 113 114 // Check for valid field names. 115 foreach ($header as $i => $h) { 116 // Remove whitespace. 117 $h = trim($h); 118 $header[$i] = $h; 119 if (!isset($required[$h]) && !isset($optionaldefaults[$h]) && !isset($optional[$h]) && !isset($customfieldnames[$h])) { 120 throw new \moodle_exception('invalidfieldname', 'error', $PAGE->url, $h); 121 } 122 if (isset($required[$h])) { 123 $required[$h] = 2; 124 } 125 } 126 // check for required fields 127 foreach ($required as $key => $value) { 128 if ($value < 2) { 129 throw new \moodle_exception('fieldrequired', 'error', $PAGE->url, $key); 130 } 131 } 132 $linenum = 2; // since header is line 1 133 134 while ($line = $csvimport->next()) { 135 136 $newgroup = new stdClass();//to make Martin happy 137 foreach ($optionaldefaults as $key => $value) { 138 $newgroup->$key = current_language(); //defaults to current language 139 } 140 foreach ($line as $key => $value) { 141 $record[$header[$key]] = trim($value); 142 } 143 if (trim(implode($line)) !== '') { 144 // add a new group to the database 145 146 // add fields to object $user 147 foreach ($record as $name => $value) { 148 // check for required values 149 if (isset($required[$name]) and !$value) { 150 throw new \moodle_exception('missingfield', 'error', $PAGE->url, $name); 151 } else if ($name == "groupname") { 152 $newgroup->name = $value; 153 } else { 154 // normal entry 155 $newgroup->{$name} = $value; 156 } 157 } 158 159 if (isset($newgroup->idnumber) && strlen($newgroup->idnumber)) { 160 //if idnumber is set, we use that. 161 //unset invalid courseid 162 if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) { 163 echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber)); 164 unset($newgroup->courseid);//unset so 0 doesn't get written to database 165 } else { 166 $newgroup->courseid = $mycourse->id; 167 } 168 169 } else if (isset($newgroup->coursename) && strlen($newgroup->coursename)) { 170 //else use course short name to look up 171 //unset invalid coursename (if no id) 172 if (!$mycourse = $DB->get_record('course', array('shortname' => $newgroup->coursename))) { 173 echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename)); 174 unset($newgroup->courseid);//unset so 0 doesn't get written to database 175 } else { 176 $newgroup->courseid = $mycourse->id; 177 } 178 179 } else { 180 //else use use current id 181 $newgroup->courseid = $id; 182 } 183 unset($newgroup->idnumber); 184 unset($newgroup->coursename); 185 186 //if courseid is set 187 if (isset($newgroup->courseid)) { 188 $linenum++; 189 $groupname = $newgroup->name; 190 $newgrpcoursecontext = context_course::instance($newgroup->courseid); 191 192 ///Users cannot upload groups in courses they cannot update. 193 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) { 194 echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname)); 195 196 } else { 197 if (isset($newgroup->groupidnumber)) { 198 // The CSV field for the group idnumber is groupidnumber rather than 199 // idnumber as that field is already in use for the course idnumber. 200 $newgroup->groupidnumber = trim($newgroup->groupidnumber); 201 if (has_capability('moodle/course:changeidnumber', $newgrpcoursecontext)) { 202 $newgroup->idnumber = $newgroup->groupidnumber; 203 if ($existing = groups_get_group_by_idnumber($newgroup->courseid, $newgroup->idnumber)) { 204 // idnumbers must be unique to a course but we shouldn't ignore group creation for duplicates 205 $existing->name = s($existing->name); 206 $existing->idnumber = s($existing->idnumber); 207 $existing->problemgroup = $groupname; 208 echo $OUTPUT->notification(get_string('groupexistforcoursewithidnumber', 'error', $existing)); 209 unset($newgroup->idnumber); 210 } 211 } 212 // Always drop the groupidnumber key. It's not a valid database field 213 unset($newgroup->groupidnumber); 214 } 215 if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) { 216 echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname)); 217 } else if ($groupid = groups_create_group($newgroup)) { 218 echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess'); 219 } else { 220 echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname)); 221 continue; 222 } 223 224 // Add group to grouping 225 if (isset($newgroup->groupingname) && strlen($newgroup->groupingname)) { 226 $groupingname = $newgroup->groupingname; 227 if (! $groupingid = groups_get_grouping_by_name($newgroup->courseid, $groupingname)) { 228 $data = new stdClass(); 229 $data->courseid = $newgroup->courseid; 230 $data->name = $groupingname; 231 // Add customfield if exists. 232 foreach ($header as $fieldname) { 233 if (isset($customfieldnames[$fieldname]) && isset($newgroup->$fieldname)) { 234 $data->{$groupingcustomfields[$groupingcustomfieldname]} = $newgroup->$fieldname; 235 } 236 } 237 if ($groupingid = groups_create_grouping($data)) { 238 echo $OUTPUT->notification(get_string('groupingaddedsuccesfully', 'group', $groupingname), 'notifysuccess'); 239 } else { 240 echo $OUTPUT->notification(get_string('groupingnotaddederror', 'error', $groupingname)); 241 continue; 242 } 243 } 244 245 // if we have reached here we definitely have a groupingid 246 $a = array('groupname' => $groupname, 'groupingname' => $groupingname); 247 try { 248 groups_assign_grouping($groupingid, $groupid); 249 echo $OUTPUT->notification(get_string('groupaddedtogroupingsuccesfully', 'group', $a), 'notifysuccess'); 250 } catch (Exception $e) { 251 echo $OUTPUT->notification(get_string('groupnotaddedtogroupingerror', 'error', $a)); 252 } 253 254 } 255 } 256 } 257 unset ($newgroup); 258 } 259 } 260 261 $csvimport->close(); 262 echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get'); 263 echo $OUTPUT->footer(); 264 die; 265 } 266 267 /// Print the form 268 echo $OUTPUT->header(); 269 echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group'); 270 $importform->display(); 271 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body