[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * This file is part of the Database module for Moodle 20 * 21 * @copyright 2005 Martin Dougiamas http://dougiamas.com 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 * @package mod_data 24 */ 25 26 require_once('../../config.php'); 27 require_once ('lib.php'); 28 require_once($CFG->libdir.'/csvlib.class.php'); 29 require_once ('import_form.php'); 30 31 $id = optional_param('id', 0, PARAM_INT); // course module id 32 $d = optional_param('d', 0, PARAM_INT); // database id 33 $rid = optional_param('rid', 0, PARAM_INT); // record id 34 $fielddelimiter = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import 35 $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML); // characters used as record delimiters for csv file import 36 37 $url = new moodle_url('/mod/data/import.php'); 38 if ($rid !== 0) { 39 $url->param('rid', $rid); 40 } 41 if ($fielddelimiter !== '') { 42 $url->param('fielddelimiter', $fielddelimiter); 43 } 44 if ($fieldenclosure !== '') { 45 $url->param('fieldenclosure', $fieldenclosure); 46 } 47 48 if ($id) { 49 $url->param('id', $id); 50 $PAGE->set_url($url); 51 $cm = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST); 52 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); 53 $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST); 54 55 } else { 56 $url->param('d', $d); 57 $PAGE->set_url($url); 58 $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST); 59 $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST); 60 $cm = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST); 61 } 62 63 require_login($course, false, $cm); 64 65 $context = context_module::instance($cm->id); 66 require_capability('mod/data:manageentries', $context); 67 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php')); 68 69 /// Print the page header 70 $PAGE->navbar->add(get_string('add', 'data')); 71 $PAGE->set_title($data->name); 72 $PAGE->set_heading($course->fullname); 73 navigation_node::override_active_url(new moodle_url('/mod/data/import.php', array('d' => $data->id))); 74 echo $OUTPUT->header(); 75 echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data'); 76 77 /// Groups needed for Add entry tab 78 $currentgroup = groups_get_activity_group($cm); 79 $groupmode = groups_get_activity_groupmode($cm); 80 81 if (!$formdata = $form->get_data()) { 82 /// Upload records section. Only for teachers and the admin. 83 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); 84 require_once ('import_form.php'); 85 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php')); 86 $formdata = new stdClass(); 87 $formdata->d = $data->id; 88 $form->set_data($formdata); 89 $form->display(); 90 echo $OUTPUT->box_end(); 91 echo $OUTPUT->footer(); 92 die; 93 } else { 94 // Large files are likely to take their time and memory. Let PHP know 95 // that we'll take longer, and that the process should be recycled soon 96 // to free up memory. 97 core_php_time_limit::raise(); 98 raise_memory_limit(MEMORY_EXTRA); 99 100 $iid = csv_import_reader::get_new_iid('moddata'); 101 $cir = new csv_import_reader($iid, 'moddata'); 102 103 $filecontent = $form->get_file_content('recordsfile'); 104 $readcount = $cir->load_csv_content($filecontent, $formdata->encoding, $formdata->fielddelimiter); 105 unset($filecontent); 106 if (empty($readcount)) { 107 print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}"); 108 } else { 109 if (!$fieldnames = $cir->get_columns()) { 110 print_error('cannotreadtmpfile', 'error'); 111 } 112 // check the fieldnames are valid 113 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type'); 114 $errorfield = ''; 115 foreach ($fieldnames as $name) { 116 if (!isset($fields[$name])) { 117 $errorfield .= "'$name' "; 118 } 119 } 120 121 if (!empty($errorfield)) { 122 print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield); 123 } 124 125 $cir->init(); 126 $recordsadded = 0; 127 while ($record = $cir->next()) { 128 if ($recordid = data_add_record($data, 0)) { // add instance to data_record 129 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type'); 130 131 // Insert new data_content fields with NULL contents: 132 foreach ($fields as $field) { 133 $content = new stdClass(); 134 $content->recordid = $recordid; 135 $content->fieldid = $field->id; 136 $DB->insert_record('data_content', $content); 137 } 138 // Fill data_content with the values imported from the CSV file: 139 foreach ($record as $key => $value) { 140 $name = $fieldnames[$key]; 141 $field = $fields[$name]; 142 $content = new stdClass(); 143 $content->fieldid = $field->id; 144 $content->recordid = $recordid; 145 if ($field->type == 'textarea') { 146 // the only field type where HTML is possible 147 $value = clean_param($value, PARAM_CLEANHTML); 148 } else { 149 // remove potential HTML: 150 $patterns[] = '/</'; 151 $replacements[] = '<'; 152 $patterns[] = '/>/'; 153 $replacements[] = '>'; 154 $value = preg_replace($patterns, $replacements, $value); 155 } 156 // for now, only for "latlong" and "url" fields, but that should better be looked up from 157 // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php' 158 // once there is stored how many contents the field can have. 159 if ($field->type == 'latlong') { 160 $values = explode(" ", $value, 2); 161 // The lat, long values might be in a different float format. 162 $content->content = unformat_float($values[0]); 163 $content->content1 = unformat_float($values[1]); 164 } else if ($field->type == 'url') { 165 $values = explode(" ", $value, 2); 166 $content->content = $values[0]; 167 if (!empty($content->content) && (strpos($content->content, '://') === false) 168 && (strpos($content->content, '/') !== 0)) { 169 $content->content = 'http://' . $content->content; 170 } 171 // The url field doesn't always have two values (unforced autolinking). 172 if (count($values) > 1) { 173 $content->content1 = $values[1]; 174 } 175 } else { 176 $content->content = $value; 177 } 178 $oldcontent = $DB->get_record('data_content', array('fieldid'=>$field->id, 'recordid'=>$recordid)); 179 $content->id = $oldcontent->id; 180 $DB->update_record('data_content', $content); 181 } 182 $recordsadded++; 183 print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n"; 184 } 185 } 186 $cir->close(); 187 $cir->cleanup(true); 188 } 189 } 190 191 if ($recordsadded > 0) { 192 echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), ''); 193 } else { 194 echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess'); 195 } 196 197 echo $OUTPUT->continue_button('import.php?d='.$data->id); 198 199 /// Finish the page 200 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |