[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/group/ -> import.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1