|
Moodle
2.2.1
http://www.collinsharper.com
|
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('lib.php'); 00028 require_once($CFG->libdir.'/csvlib.class.php'); 00029 require_once('import_form.php'); 00030 00031 $id = optional_param('id', 0, PARAM_INT); // course module id 00032 $d = optional_param('d', 0, PARAM_INT); // database id 00033 $rid = optional_param('rid', 0, PARAM_INT); // record id 00034 $fielddelimiter = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import 00035 $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML); // characters used as record delimiters for csv file import 00036 00037 $url = new moodle_url('/mod/data/import.php'); 00038 if ($rid !== 0) { 00039 $url->param('rid', $rid); 00040 } 00041 if ($fielddelimiter !== '') { 00042 $url->param('fielddelimiter', $fielddelimiter); 00043 } 00044 if ($fieldenclosure !== '') { 00045 $url->param('fieldenclosure', $fieldenclosure); 00046 } 00047 00048 if ($id) { 00049 $url->param('id', $id); 00050 $PAGE->set_url($url); 00051 $cm = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST); 00052 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); 00053 $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST); 00054 00055 } else { 00056 $url->param('d', $d); 00057 $PAGE->set_url($url); 00058 $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST); 00059 $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST); 00060 $cm = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST); 00061 } 00062 00063 require_login($course, false, $cm); 00064 00065 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00066 require_capability('mod/data:manageentries', $context); 00067 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php')); 00068 00070 $PAGE->navbar->add(get_string('add', 'data')); 00071 $PAGE->set_title($data->name); 00072 $PAGE->set_heading($course->fullname); 00073 navigation_node::override_active_url(new moodle_url('/mod/data/import.php', array('d' => $data->id))); 00074 echo $OUTPUT->header(); 00075 echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data'); 00076 00078 $currentgroup = groups_get_activity_group($cm); 00079 $groupmode = groups_get_activity_groupmode($cm); 00080 00081 if (!$formdata = $form->get_data()) { 00083 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); 00084 require_once('import_form.php'); 00085 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php')); 00086 $formdata = new stdClass(); 00087 $formdata->d = $data->id; 00088 $form->set_data($formdata); 00089 $form->display(); 00090 echo $OUTPUT->box_end(); 00091 echo $OUTPUT->footer(); 00092 die; 00093 } else { 00094 // Large files are likely to take their time and memory. Let PHP know 00095 // that we'll take longer, and that the process should be recycled soon 00096 // to free up memory. 00097 @set_time_limit(0); 00098 raise_memory_limit(MEMORY_EXTRA); 00099 00100 $iid = csv_import_reader::get_new_iid('moddata'); 00101 $cir = new csv_import_reader($iid, 'moddata'); 00102 00103 $readcount = $cir->load_csv_content($form->get_file_content('recordsfile'), $formdata->encoding, $formdata->fielddelimiter); 00104 if (empty($readcount)) { 00105 print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}"); 00106 } else { 00107 if (!$fieldnames = $cir->get_columns()) { 00108 print_error('cannotreadtmpfile', 'error'); 00109 } 00110 // check the fieldnames are valid 00111 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type'); 00112 $errorfield = ''; 00113 foreach ($fieldnames as $name) { 00114 if (!isset($fields[$name])) { 00115 $errorfield .= "'$name' "; 00116 } 00117 } 00118 00119 if (!empty($errorfield)) { 00120 print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield); 00121 } 00122 00123 $cir->init(); 00124 $recordsadded = 0; 00125 while ($record = $cir->next()) { 00126 if ($recordid = data_add_record($data, 0)) { // add instance to data_record 00127 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type'); 00128 00129 // Insert new data_content fields with NULL contents: 00130 foreach ($fields as $field) { 00131 $content = new stdClass(); 00132 $content->recordid = $recordid; 00133 $content->fieldid = $field->id; 00134 $DB->insert_record('data_content', $content); 00135 } 00136 // Fill data_content with the values imported from the CSV file: 00137 foreach ($record as $key => $value) { 00138 $name = $fieldnames[$key]; 00139 $field = $fields[$name]; 00140 $content = new stdClass(); 00141 $content->fieldid = $field->id; 00142 $content->recordid = $recordid; 00143 if ($field->type == 'textarea') { 00144 // the only field type where HTML is possible 00145 $value = clean_param($value, PARAM_CLEANHTML); 00146 } else { 00147 // remove potential HTML: 00148 $patterns[] = '/</'; 00149 $replacements[] = '<'; 00150 $patterns[] = '/>/'; 00151 $replacements[] = '>'; 00152 $value = preg_replace($patterns, $replacements, $value); 00153 } 00154 // for now, only for "latlong" and "url" fields, but that should better be looked up from 00155 // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php' 00156 // once there is stored how many contents the field can have. 00157 if (preg_match("/^(latlong|url)$/", $field->type)) { 00158 $values = explode(" ", $value, 2); 00159 $content->content = $values[0]; 00160 $content->content1 = $values[1]; 00161 } else { 00162 $content->content = $value; 00163 } 00164 $oldcontent = $DB->get_record('data_content', array('fieldid'=>$field->id, 'recordid'=>$recordid)); 00165 $content->id = $oldcontent->id; 00166 $DB->update_record('data_content', $content); 00167 } 00168 $recordsadded++; 00169 print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n"; 00170 } 00171 } 00172 $cir->close(); 00173 $cir->cleanup(true); 00174 } 00175 } 00176 00177 if ($recordsadded > 0) { 00178 echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), ''); 00179 } else { 00180 echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess'); 00181 } 00182 00183 echo $OUTPUT->continue_button('import.php?d='.$data->id); 00184 00186 echo $OUTPUT->footer();