Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/group/import.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../config.php');
00027 require_once($CFG->dirroot.'/course/lib.php');
00028 require_once($CFG->dirroot.'/group/lib.php');
00029 include_once('import_form.php');
00030 
00031 $id = required_param('id', PARAM_INT);    // Course id
00032 
00033 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
00034 
00035 $PAGE->set_url('/group/import.php', array('id'=>$id));
00036 
00037 require_login($course);
00038 $context = get_context_instance(CONTEXT_COURSE, $id);
00039 
00040 require_capability('moodle/course:managegroups', $context);
00041 
00042 $strimportgroups = get_string('importgroups', 'core_group');
00043 
00044 $PAGE->navbar->add($strimportgroups);
00045 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
00046 $PAGE->set_title("$course->shortname: $strimportgroups");
00047 $PAGE->set_heading($course->fullname);
00048 $PAGE->set_pagelayout('standard');
00049 
00050 $returnurl = new moodle_url('/group/index.php', array('id'=>$id));
00051 
00052 $mform_post = new groups_import_form(null, array('id'=>$id));
00053 
00054 // If a file has been uploaded, then process it
00055 if ($mform_post->is_cancelled()) {
00056     redirect($returnurl);
00057 
00058 } else if ($mform_post->get_data()) {
00059     echo $OUTPUT->header();
00060 
00061     $csv_encode = '/\&\#44/';
00062     if (isset($CFG->CSV_DELIMITER)) {
00063         $csv_delimiter = $CFG->CSV_DELIMITER;
00064 
00065         if (isset($CFG->CSV_ENCODE)) {
00066             $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
00067         }
00068     } else {
00069         $csv_delimiter = ",";
00070     }
00071 
00072     $text = $mform_post->get_file_content('userfile');
00073     $text = preg_replace('!\r\n?!',"\n",$text);
00074 
00075     $rawlines = explode("\n", $text);
00076     unset($text);
00077 
00078     // make arrays of valid fields for error checking
00079     $required = array("groupname" => 1);
00080     $optionalDefaults = array("lang" => 1);
00081     $optional = array("coursename" => 1,
00082             "idnumber" => 1,
00083             "description" => 1,
00084             "enrolmentkey" => 1);
00085 
00086     // --- get header (field names) ---
00087     $header = explode($csv_delimiter, array_shift($rawlines));
00088     // check for valid field names
00089     foreach ($header as $i => $h) {
00090         $h = trim($h); $header[$i] = $h; // remove whitespace
00091         if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
00092                 print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h);
00093             }
00094         if (isset($required[$h])) {
00095             $required[$h] = 2;
00096         }
00097     }
00098     // check for required fields
00099     foreach ($required as $key => $value) {
00100         if ($value < 2) {
00101             print_error('fieldrequired', 'error', 'import.php?id='.$id, $key);
00102         }
00103     }
00104     $linenum = 2; // since header is line 1
00105 
00106     foreach ($rawlines as $rawline) {
00107 
00108         $newgroup = new stdClass();//to make Martin happy
00109         foreach ($optionalDefaults as $key => $value) {
00110             $newgroup->$key = current_language(); //defaults to current language
00111         }
00112         //Note: commas within a field should be encoded as &#44 (for comma separated csv files)
00113         //Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
00114         $line = explode($csv_delimiter, $rawline);
00115         foreach ($line as $key => $value) {
00116             //decode encoded commas
00117             $record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
00118         }
00119         if ($record[$header[0]]) {
00120             // add a new group to the database
00121 
00122             // add fields to object $user
00123             foreach ($record as $name => $value) {
00124                 // check for required values
00125                 if (isset($required[$name]) and !$value) {
00126                     print_error('missingfield', 'error', 'import.php?id='.$id, $name);
00127                 } else if ($name == "groupname") {
00128                     $newgroup->name = $value;
00129                 } else {
00130                 // normal entry
00131                     $newgroup->{$name} = $value;
00132                 }
00133             }
00134 
00135             if (isset($newgroup->idnumber)){
00136                 //if idnumber is set, we use that.
00137                 //unset invalid courseid
00138                 if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
00139                     echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
00140                     unset($newgroup->courseid);//unset so 0 doesn't get written to database
00141                 }
00142                 $newgroup->courseid = $mycourse->id;
00143 
00144             } else if (isset($newgroup->coursename)){
00145                 //else use course short name to look up
00146                 //unset invalid coursename (if no id)
00147                 if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
00148                     echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
00149                     unset($newgroup->courseid);//unset so 0 doesn't get written to database
00150                 }
00151                 $newgroup->courseid = $mycourse->id;
00152 
00153             } else {
00154                 //else use use current id
00155                 $newgroup->courseid = $id;
00156             }
00157 
00158             //if courseid is set
00159             if (isset($newgroup->courseid)) {
00160                 $linenum++;
00161                 $groupname = $newgroup->name;
00162                 $newgrpcoursecontext = get_context_instance(CONTEXT_COURSE, $newgroup->courseid);
00163 
00165                 if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
00166                     echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
00167 
00168                 } else {
00169                     if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
00170                         echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
00171                     } else if (groups_create_group($newgroup)) {
00172                         echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
00173                     } else {
00174                         echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
00175                     }
00176                 }
00177             }
00178             unset ($newgroup);
00179         }
00180     }
00181 
00182     echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
00183     echo $OUTPUT->footer();
00184     die;
00185 }
00186 
00188 echo $OUTPUT->header();
00189 echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
00190 $mform_post ->display();
00191 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations