import.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Bulk group creation registration script from a comma separated file
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. require_once('../config.php');
  24. require_once($CFG->dirroot.'/course/lib.php');
  25. require_once($CFG->dirroot.'/group/lib.php');
  26. include_once('import_form.php');
  27. $id = required_param('id', PARAM_INT); // Course id
  28. $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  29. $PAGE->set_url('/group/import.php', array('id'=>$id));
  30. require_login($course);
  31. $context = context_course::instance($id);
  32. require_capability('moodle/course:managegroups', $context);
  33. $strimportgroups = get_string('importgroups', 'core_group');
  34. $PAGE->navbar->add($strimportgroups);
  35. navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
  36. $PAGE->set_title("$course->shortname: $strimportgroups");
  37. $PAGE->set_heading($course->fullname);
  38. $PAGE->set_pagelayout('admin');
  39. $returnurl = new moodle_url('/group/index.php', array('id'=>$id));
  40. $mform_post = new groups_import_form(null, array('id'=>$id));
  41. // If a file has been uploaded, then process it
  42. if ($mform_post->is_cancelled()) {
  43. redirect($returnurl);
  44. } else if ($mform_post->get_data()) {
  45. echo $OUTPUT->header();
  46. $csv_encode = '/\&\#44/';
  47. if (isset($CFG->CSV_DELIMITER)) {
  48. $csv_delimiter = $CFG->CSV_DELIMITER;
  49. if (isset($CFG->CSV_ENCODE)) {
  50. $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
  51. }
  52. } else {
  53. $csv_delimiter = ",";
  54. }
  55. $text = $mform_post->get_file_content('userfile');
  56. $text = preg_replace('!\r\n?!',"\n",$text);
  57. $rawlines = explode("\n", $text);
  58. unset($text);
  59. // make arrays of valid fields for error checking
  60. $required = array("groupname" => 1);
  61. $optionalDefaults = array("lang" => 1);
  62. $optional = array("coursename" => 1,
  63. "idnumber" => 1,
  64. "groupidnumber" => 1,
  65. "description" => 1,
  66. "enrolmentkey" => 1,
  67. "groupingname" => 1);
  68. // --- get header (field names) ---
  69. $header = explode($csv_delimiter, array_shift($rawlines));
  70. // check for valid field names
  71. foreach ($header as $i => $h) {
  72. $h = trim($h); $header[$i] = $h; // remove whitespace
  73. if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
  74. print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h);
  75. }
  76. if (isset($required[$h])) {
  77. $required[$h] = 2;
  78. }
  79. }
  80. // check for required fields
  81. foreach ($required as $key => $value) {
  82. if ($value < 2) {
  83. print_error('fieldrequired', 'error', 'import.php?id='.$id, $key);
  84. }
  85. }
  86. $linenum = 2; // since header is line 1
  87. foreach ($rawlines as $rawline) {
  88. $newgroup = new stdClass();//to make Martin happy
  89. foreach ($optionalDefaults as $key => $value) {
  90. $newgroup->$key = current_language(); //defaults to current language
  91. }
  92. //Note: commas within a field should be encoded as &#44 (for comma separated csv files)
  93. //Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
  94. $line = explode($csv_delimiter, $rawline);
  95. foreach ($line as $key => $value) {
  96. //decode encoded commas
  97. $record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
  98. }
  99. if ($record[$header[0]]) {
  100. // add a new group to the database
  101. // add fields to object $user
  102. foreach ($record as $name => $value) {
  103. // check for required values
  104. if (isset($required[$name]) and !$value) {
  105. print_error('missingfield', 'error', 'import.php?id='.$id, $name);
  106. } else if ($name == "groupname") {
  107. $newgroup->name = $value;
  108. } else {
  109. // normal entry
  110. $newgroup->{$name} = $value;
  111. }
  112. }
  113. if (isset($newgroup->idnumber)){
  114. //if idnumber is set, we use that.
  115. //unset invalid courseid
  116. if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
  117. echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
  118. unset($newgroup->courseid);//unset so 0 doesn't get written to database
  119. }
  120. $newgroup->courseid = $mycourse->id;
  121. } else if (isset($newgroup->coursename)){
  122. //else use course short name to look up
  123. //unset invalid coursename (if no id)
  124. if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
  125. echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
  126. unset($newgroup->courseid);//unset so 0 doesn't get written to database
  127. }
  128. $newgroup->courseid = $mycourse->id;
  129. } else {
  130. //else use use current id
  131. $newgroup->courseid = $id;
  132. }
  133. //if courseid is set
  134. if (isset($newgroup->courseid)) {
  135. $linenum++;
  136. $groupname = $newgroup->name;
  137. $newgrpcoursecontext = context_course::instance($newgroup->courseid);
  138. ///Users cannot upload groups in courses they cannot update.
  139. if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
  140. echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
  141. } else {
  142. if (isset($newgroup->groupidnumber)) {
  143. // The CSV field for the group idnumber is groupidnumber rather than
  144. // idnumber as that field is already in use for the course idnumber.
  145. $newgroup->groupidnumber = trim($newgroup->groupidnumber);
  146. if (has_capability('moodle/course:changeidnumber', $newgrpcoursecontext)) {
  147. $newgroup->idnumber = $newgroup->groupidnumber;
  148. if ($existing = groups_get_group_by_idnumber($newgroup->courseid, $newgroup->idnumber)) {
  149. // idnumbers must be unique to a course but we shouldn't ignore group creation for duplicates
  150. $existing->name = s($existing->name);
  151. $existing->idnumber = s($existing->idnumber);
  152. $existing->problemgroup = $groupname;
  153. echo $OUTPUT->notification(get_string('groupexistforcoursewithidnumber', 'error', $existing));
  154. unset($newgroup->idnumber);
  155. }
  156. }
  157. // Always drop the groupidnumber key. It's not a valid database field
  158. unset($newgroup->groupidnumber);
  159. }
  160. if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
  161. echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
  162. } else if ($groupid = groups_create_group($newgroup)) {
  163. echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
  164. } else {
  165. echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
  166. continue;
  167. }
  168. // Add group to grouping
  169. if (!empty($newgroup->groupingname) || is_numeric($newgroup->groupingname)) {
  170. $groupingname = $newgroup->groupingname;
  171. if (! $groupingid = groups_get_grouping_by_name($newgroup->courseid, $groupingname)) {
  172. $data = new stdClass();
  173. $data->courseid = $newgroup->courseid;
  174. $data->name = $groupingname;
  175. if ($groupingid = groups_create_grouping($data)) {
  176. echo $OUTPUT->notification(get_string('groupingaddedsuccesfully', 'group', $groupingname), 'notifysuccess');
  177. } else {
  178. echo $OUTPUT->notification(get_string('groupingnotaddederror', 'error', $groupingname));
  179. continue;
  180. }
  181. }
  182. // if we have reached here we definitely have a groupingid
  183. $a = array('groupname' => $groupname, 'groupingname' => $groupingname);
  184. try {
  185. groups_assign_grouping($groupingid, $groupid);
  186. echo $OUTPUT->notification(get_string('groupaddedtogroupingsuccesfully', 'group', $a), 'notifysuccess');
  187. } catch (Exception $e) {
  188. echo $OUTPUT->notification(get_string('groupnotaddedtogroupingerror', 'error', $a));
  189. }
  190. }
  191. }
  192. }
  193. unset ($newgroup);
  194. }
  195. }
  196. echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
  197. echo $OUTPUT->footer();
  198. die;
  199. }
  200. /// Print the form
  201. echo $OUTPUT->header();
  202. echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
  203. $mform_post ->display();
  204. echo $OUTPUT->footer();