1 <?php
// $Id: uploadgroups.php, 2005/10/31 19:09:31
3 /// Bulk group creation registration script from a comma separated file
5 require_once('../../../config.php');
6 require_once($CFG->dirroot
.'/course/lib.php');
7 require_once($CFG->dirroot
.'/group/lib.php');
9 $id = required_param('id', PARAM_INT
); // Course id
11 if (! $course = get_record('course', 'id', $id) ) {
12 error("That's an invalid course id");
15 require_login($course->id
);
16 $context = get_context_instance(CONTEXT_COURSE
, $id);
19 if (!has_capability('moodle/course:managegroups', $context)) {
20 error("You do not have the required permissions to manage groups.");
23 //if (!confirm_sesskey()) {
24 // print_error('confirmsesskeybad', 'error');
27 $strimportgroups = get_string("importgroups");
29 $csv_encode = '/\&\#44/';
30 if (isset($CFG->CSV_DELIMITER
)) {
31 $csv_delimiter = '\\' . $CFG->CSV_DELIMITER
;
32 $csv_delimiter2 = $CFG->CSV_DELIMITER
;
34 if (isset($CFG->CSV_ENCODE
)) {
35 $csv_encode = '/\&\#' . $CFG->CSV_ENCODE
. '/';
38 $csv_delimiter = "\,";
39 $csv_delimiter2 = ",";
44 $navlinks[] = array('name' => $course->shortname
,
45 'link' => "$CFG->wwwroot/course/view.php?id=$course->id",
47 $navlinks[] = array('name' => get_string('import'),
48 'link' => "$CFG->wwwroot/course/import.php?id=$course->id",
50 $navlinks[] = array('name' => $strimportgroups, 'link' => null, 'type' => 'misc');
51 $navigation = build_navigation($navlinks);
53 print_header("$course->shortname: $strimportgroups", $course->fullname
, $navigation);
55 /// If a file has been uploaded, then process it
57 require_once($CFG->dirroot
.'/lib/uploadlib.php');
58 $um = new upload_manager('userfile',false,false,null,false,0);
59 if ($um->preprocess_files()) {
60 $filename = $um->files
['userfile']['tmp_name'];
62 //Fix mac/dos newlines
63 $text = my_file_get_contents($filename);
64 $text = preg_replace('!\r\n?!',"\n",$text);
65 $fp = fopen($filename, "w");
69 $fp = fopen($filename, "r");
71 // make arrays of valid fields for error checking
72 $required = array("groupname" => 1, );
73 $optionalDefaults = array("lang" => 1, );
74 $optional = array("coursename" => 1,
80 "hidepicture" => 1, );
82 // --- get header (field names) ---
83 $header = split($csv_delimiter, fgets($fp,1024));
84 // check for valid field names
85 foreach ($header as $i => $h) {
86 $h = trim($h); $header[$i] = $h; // remove whitespace
87 if ( !(isset($required[$h]) or
88 isset($optionalDefaults[$h]) or
89 isset($optional[$h])) ) {
90 print_error('invalidfieldname', 'error', 'index.php?id='.$id.'&sesskey='.$USER->sesskey
, $h);
92 if ( isset($required[$h]) ) {
96 // check for required fields
97 foreach ($required as $key => $value) {
99 print_error('fieldrequired', 'error', 'uploaduser.php?id='.$id.'&sesskey='.$USER->sesskey
, $key);
102 $linenum = 2; // since header is line 1
104 while (!feof ($fp)) {
106 $newgroup = new object();//to make Martin happy
107 foreach ($optionalDefaults as $key => $value) {
108 $newgroup->$key = current_language(); //defaults to current language
110 //Note: commas within a field should be encoded as , (for comma separated csv files)
111 //Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
112 $line = split($csv_delimiter, fgets($fp,1024));
113 foreach ($line as $key => $value) {
114 //decode encoded commas
115 $record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
117 if ($record[$header[0]]) {
118 // add a new group to the database
120 // add fields to object $user
121 foreach ($record as $name => $value) {
122 // check for required values
123 if (isset($required[$name]) and !$value) {
124 error(get_string('missingfield', 'error', $name). " ".
125 get_string('erroronline', 'error', $linenum) .". ".
126 get_string('processingstops', 'error'),
127 'uploaduser.php?sesskey='.$USER->sesskey
);
128 //print_error('missingfield', 'error', 'uploaduser.php?sesskey='.$USER->sesskey, $name);
130 else if ($name == "groupname") {
131 $newgroup->name
= addslashes($value);
135 $newgroup->{$name} = addslashes($value);
138 ///Find the courseid of the course with the given shortname
140 //if idnumber is set, we use that.
141 //unset invalid courseid
142 if (isset($newgroup->idnumber
)){
143 if (!$mycourse = get_record('course', 'idnumber',$newgroup->idnumber
)){
144 notify(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber
));
145 unset($newgroup->courseid
);//unset so 0 doesnt' get written to database
147 $newgroup->courseid
= $mycourse->id
;
149 //else use course short name to look up
150 //unset invalid coursename (if no id)
152 else if (isset($newgroup->coursename
)){
153 if (!$mycourse = get_record('course', 'shortname',$newgroup->coursename
)){
154 notify(get_string('unknowncourse', 'error', $newgroup->coursename
));
155 unset($newgroup->courseid
);//unset so 0 doesnt' get written to database
157 $newgroup->courseid
= $mycourse->id
;
159 //else juse use current id
161 $newgroup->courseid
= $id;
165 if (isset($newgroup->courseid
)){
167 $newgroup->courseid
= (int)$newgroup->courseid
;
168 $newgroup->timecreated
= time();
170 $groupname = $newgroup->name
;
171 $newgrpcoursecontext = get_context_instance(CONTEXT_COURSE
, $newgroup->courseid
);
173 ///Users cannot upload groups in courses they cannot update.
174 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext)){
175 notify(get_string('nopermissionforcreation','group',$groupname));
178 if ( $groupid = groups_get_group_by_name($newgroup->courseid
, $groupname) ||
!($newgroup->id
= groups_create_group($newgroup)) ) {
180 //Record not added - probably because group is already registered
181 //In this case, output groupname from previous registration
183 notify("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
185 notify(get_string('groupnotaddederror', 'error', $groupname));
189 notify(get_string('groupaddedsuccesfully', 'group', $groupname));
192 } //close courseid validity check
194 }//close if ($record[$header[0]])
204 print_footer($course);
206 function my_file_get_contents($filename, $use_include_path = 0) {
207 /// Returns the file as one big long string
210 $file = @fopen
($filename, "rb", $use_include_path);
212 while (!feof($file)) {
213 $data .= fread($file, 1024);