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('../../lib.php');
7 require_once($CFG->dirroot
. '/group/lib/basicgrouplib.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 // error(get_string('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 print_header("$course->shortname: $strimportgroups", $course->fullname
,
45 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ".
46 "-> <a href=\"$CFG->wwwroot/course/import.php?id=$course->id\">".get_string('import')."</a> ".
47 "-> $strimportgroups");
49 /// If a file has been uploaded, then process it
51 require_once($CFG->dirroot
.'/lib/uploadlib.php');
52 $um = new upload_manager('userfile',false,false,null,false,0);
53 if ($um->preprocess_files()) {
54 $filename = $um->files
['userfile']['tmp_name'];
56 //Fix mac/dos newlines
57 $text = my_file_get_contents($filename);
58 $text = preg_replace('!\r\n?!',"\n",$text);
59 $fp = fopen($filename, "w");
63 $fp = fopen($filename, "r");
65 // make arrays of valid fields for error checking
66 $required = array("groupname" => 1, );
67 $optionalDefaults = array("lang" => 1, );
68 $optional = array("coursename" => 1,
74 "hidepicture" => 1, );
76 // --- get header (field names) ---
77 $header = split($csv_delimiter, fgets($fp,1024));
78 // check for valid field names
79 foreach ($header as $i => $h) {
80 $h = trim($h); $header[$i] = $h; // remove whitespace
81 if ( !(isset($required[$h]) or
82 isset($optionalDefaults[$h]) or
83 isset($optional[$h])) ) {
84 error(get_string('invalidfieldname', 'error', $h), 'index.php?id='.$id.'&sesskey='.$USER->sesskey
);
86 if ( isset($required[$h]) ) {
90 // check for required fields
91 foreach ($required as $key => $value) {
93 error(get_string('fieldrequired', 'error', $key), 'uploaduser.php?id='.$id.'&sesskey='.$USER->sesskey
);
96 $linenum = 2; // since header is line 1
100 $newgroup = new object();//to make Martin happy
101 foreach ($optionalDefaults as $key => $value) {
102 $newgroup->$key = current_language(); //defaults to current language
104 //Note: commas within a field should be encoded as , (for comma separated csv files)
105 //Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
106 $line = split($csv_delimiter, fgets($fp,1024));
107 foreach ($line as $key => $value) {
108 //decode encoded commas
109 $record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
111 if ($record[$header[0]]) {
112 // add a new group to the database
114 // add fields to object $user
115 foreach ($record as $name => $value) {
116 // check for required values
117 if (isset($required[$name]) and !$value) {
118 error(get_string('missingfield', 'error', $name). " ".
119 get_string('erroronline', 'error', $linenum) .". ".
120 get_string('processingstops', 'error'),
121 'uploaduser.php?sesskey='.$USER->sesskey
);
123 else if ($name == "groupname") {
124 $newgroup->name
= addslashes($value);
128 $newgroup->{$name} = addslashes($value);
131 ///Find the courseid of the course with the given shortname
133 //if idnumber is set, we use that.
134 //unset invalid courseid
135 if (isset($newgroup->idnumber
)){
136 if (!$mycourse = get_record('course', 'idnumber',$newgroup->idnumber
)){
137 notify(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber
));
138 unset($newgroup->courseid
);//unset so 0 doesnt' get written to database
140 $newgroup->courseid
= $mycourse->id
;
142 //else use course short name to look up
143 //unset invalid coursename (if no id)
145 else if (isset($newgroup->coursename
)){
146 if (!$mycourse = get_record('course', 'shortname',$newgroup->coursename
)){
147 notify(get_string('unknowncourse', 'error', $newgroup->coursename
));
148 unset($newgroup->courseid
);//unset so 0 doesnt' get written to database
150 $newgroup->courseid
= $mycourse->id
;
152 //else juse use current id
154 $newgroup->courseid
= $id;
158 if (isset($newgroup->courseid
)){
160 $newgroup->timecreated
= time();
162 $groupname = $newgroup->name
;
163 $newgrpcoursecontext = get_context_instance(CONTEXT_COURSE
, $newgroup->courseid
);
165 ///Users cannot upload groups in courses they cannot update.
166 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext)){
167 notify( get_string('nopermissionforcreation','group',$newgroup->name
) );
169 if ( $group = groups_group_name_exists($newgroup->courseid
, $groupname) ||
!($newgroup->id
= groups_create_group($newgroup->courseid
, $newgroup)) ) {
171 //Record not added - probably because group is already registered
172 //In this case, output groupname from previous registration
174 notify("$newgroup->name :".get_string('groupexistforcourse', 'error', $groupname));
176 notify(get_string('groupnotaddederror', 'error', $groupname));
180 notify( get_string('groupaddedsuccesfully', 'group', $newgroup->name
) );
183 } //close courseid validity check
185 }//close if ($record[$header[0]])
195 print_footer($course);
197 function my_file_get_contents($filename, $use_include_path = 0) {
198 /// Returns the file as one big long string
201 $file = @fopen
($filename, "rb", $use_include_path);
203 while (!feof($file)) {
204 $data .= fread($file, 1024);