Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / course / import / groups / index.php
blob9ef9b1c4eb1fac7c6839fae5822eca482bb8fb39
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');
25 //}
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 . '/';
37 } else {
38 $csv_delimiter = "\,";
39 $csv_delimiter2 = ",";
42 /// Print the header
43 $navlinks = array();
44 $navlinks[] = array('name' => $course->shortname,
45 'link' => "$CFG->wwwroot/course/view.php?id=$course->id",
46 'type' => 'misc');
47 $navlinks[] = array('name' => get_string('import'),
48 'link' => "$CFG->wwwroot/course/import.php?id=$course->id",
49 'type' => 'misc');
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");
66 fwrite($fp,$text);
67 fclose($fp);
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,
75 "idnumber" =>1,
76 "description" => 1,
77 "enrolmentkey" => 1,
78 "theme" => 1,
79 "picture" => 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.'&amp;sesskey='.$USER->sesskey, $h);
92 if ( isset($required[$h]) ) {
93 $required[$h] = 2;
96 // check for required fields
97 foreach ($required as $key => $value) {
98 if ($value < 2) {
99 print_error('fieldrequired', 'error', 'uploaduser.php?id='.$id.'&amp;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 &#44 (for comma separated csv files)
111 //Note: semicolon within a field should be encoded as &#59 (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);
133 // normal entry
134 else {
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
160 else{
161 $newgroup->courseid = $id;
164 //if courseid is set
165 if (isset($newgroup->courseid)){
167 $newgroup->courseid = (int)$newgroup->courseid;
168 $newgroup->timecreated = time();
169 $linenum++;
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));
177 } else {
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
182 if ($groupid) {
183 notify("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
184 } else {
185 notify(get_string('groupnotaddederror', 'error', $groupname));
188 else {
189 notify(get_string('groupaddedsuccesfully', 'group', $groupname));
192 } //close courseid validity check
193 unset ($newgroup);
194 }//close if ($record[$header[0]])
195 }//close while($fp)
196 fclose($fp);
198 echo '<hr />';
201 /// Print the form
202 require('mod.php');
204 print_footer($course);
206 function my_file_get_contents($filename, $use_include_path = 0) {
207 /// Returns the file as one big long string
209 $data = "";
210 $file = @fopen($filename, "rb", $use_include_path);
211 if ($file) {
212 while (!feof($file)) {
213 $data .= fread($file, 1024);
215 fclose($file);
217 return $data;