PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/group/import.php

https://bitbucket.org/ngmares/moodle
PHP | 210 lines | 134 code | 31 blank | 45 comment | 31 complexity | ceab570a1207db870b20528417c2969c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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 = get_context_instance(CONTEXT_COURSE, $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('standard');
  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. // --- get header (field names) ---
  68. $header = explode($csv_delimiter, array_shift($rawlines));
  69. // check for valid field names
  70. foreach ($header as $i => $h) {
  71. $h = trim($h); $header[$i] = $h; // remove whitespace
  72. if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
  73. print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h);
  74. }
  75. if (isset($required[$h])) {
  76. $required[$h] = 2;
  77. }
  78. }
  79. // check for required fields
  80. foreach ($required as $key => $value) {
  81. if ($value < 2) {
  82. print_error('fieldrequired', 'error', 'import.php?id='.$id, $key);
  83. }
  84. }
  85. $linenum = 2; // since header is line 1
  86. foreach ($rawlines as $rawline) {
  87. $newgroup = new stdClass();//to make Martin happy
  88. foreach ($optionalDefaults as $key => $value) {
  89. $newgroup->$key = current_language(); //defaults to current language
  90. }
  91. //Note: commas within a field should be encoded as &#44 (for comma separated csv files)
  92. //Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
  93. $line = explode($csv_delimiter, $rawline);
  94. foreach ($line as $key => $value) {
  95. //decode encoded commas
  96. $record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
  97. }
  98. if ($record[$header[0]]) {
  99. // add a new group to the database
  100. // add fields to object $user
  101. foreach ($record as $name => $value) {
  102. // check for required values
  103. if (isset($required[$name]) and !$value) {
  104. print_error('missingfield', 'error', 'import.php?id='.$id, $name);
  105. } else if ($name == "groupname") {
  106. $newgroup->name = $value;
  107. } else {
  108. // normal entry
  109. $newgroup->{$name} = $value;
  110. }
  111. }
  112. if (isset($newgroup->idnumber)){
  113. //if idnumber is set, we use that.
  114. //unset invalid courseid
  115. if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
  116. echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
  117. unset($newgroup->courseid);//unset so 0 doesn't get written to database
  118. }
  119. $newgroup->courseid = $mycourse->id;
  120. } else if (isset($newgroup->coursename)){
  121. //else use course short name to look up
  122. //unset invalid coursename (if no id)
  123. if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
  124. echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
  125. unset($newgroup->courseid);//unset so 0 doesn't get written to database
  126. }
  127. $newgroup->courseid = $mycourse->id;
  128. } else {
  129. //else use use current id
  130. $newgroup->courseid = $id;
  131. }
  132. //if courseid is set
  133. if (isset($newgroup->courseid)) {
  134. $linenum++;
  135. $groupname = $newgroup->name;
  136. $newgrpcoursecontext = get_context_instance(CONTEXT_COURSE, $newgroup->courseid);
  137. ///Users cannot upload groups in courses they cannot update.
  138. if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
  139. echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
  140. } else {
  141. if (isset($newgroup->groupidnumber)) {
  142. // The CSV field for the group idnumber is groupidnumber rather than
  143. // idnumber as that field is already in use for the course idnumber.
  144. $newgroup->groupidnumber = trim($newgroup->groupidnumber);
  145. if (has_capability('moodle/course:changeidnumber', $newgrpcoursecontext)) {
  146. $newgroup->idnumber = $newgroup->groupidnumber;
  147. if ($existing = groups_get_group_by_idnumber($newgroup->courseid, $newgroup->idnumber)) {
  148. // idnumbers must be unique to a course but we shouldn't ignore group creation for duplicates
  149. $existing->name = s($existing->name);
  150. $existing->idnumber = s($existing->idnumber);
  151. $existing->problemgroup = $groupname;
  152. echo $OUTPUT->notification(get_string('groupexistforcoursewithidnumber', 'error', $existing));
  153. unset($newgroup->idnumber);
  154. }
  155. }
  156. // Always drop the groupidnumber key. It's not a valid database field
  157. unset($newgroup->groupidnumber);
  158. }
  159. if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
  160. echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
  161. } else if (groups_create_group($newgroup)) {
  162. echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
  163. } else {
  164. echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
  165. }
  166. }
  167. }
  168. unset ($newgroup);
  169. }
  170. }
  171. echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
  172. echo $OUTPUT->footer();
  173. die;
  174. }
  175. /// Print the form
  176. echo $OUTPUT->header();
  177. echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
  178. $mform_post ->display();
  179. echo $OUTPUT->footer();