2009-09-24 04:25:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is part of the Database module for Moodle
|
|
|
|
*
|
|
|
|
* @copyright 2005 Martin Dougiamas http://dougiamas.com
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @package mod-data
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('../../config.php');
|
|
|
|
require_once('lib.php');
|
|
|
|
require_once($CFG->libdir.'/uploadlib.php');
|
|
|
|
|
|
|
|
require_login();
|
|
|
|
|
|
|
|
$id = optional_param('id', 0, PARAM_INT); // course module id
|
|
|
|
$d = optional_param('d', 0, PARAM_INT); // database id
|
|
|
|
$rid = optional_param('rid', 0, PARAM_INT); // record id
|
|
|
|
$fielddelimiter = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import
|
|
|
|
$fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML); // characters used as record delimiters for csv file import
|
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/mod/data/import.php');
|
2009-09-24 04:25:40 +00:00
|
|
|
if ($rid !== 0) {
|
|
|
|
$url->param('rid', $rid);
|
|
|
|
}
|
|
|
|
if ($fielddelimiter !== '') {
|
|
|
|
$url->param('fielddelimiter', $fielddelimiter);
|
|
|
|
}
|
|
|
|
if ($fieldenclosure !== '') {
|
|
|
|
$url->param('fieldenclosure', $fieldenclosure);
|
|
|
|
}
|
2006-03-03 14:44:54 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
if ($id) {
|
|
|
|
$url->param('id', $id);
|
|
|
|
$PAGE->set_url($url);
|
|
|
|
if (! $cm = get_coursemodule_from_id('data', $id)) {
|
|
|
|
print_error('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
|
|
|
|
print_error('coursemisconf');
|
|
|
|
}
|
|
|
|
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
|
|
|
|
print_error('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$url->param('d', $d);
|
|
|
|
$PAGE->set_url($url);
|
|
|
|
if (! $data = $DB->get_record('data', array('id'=>$d))) {
|
|
|
|
print_error('invalidid', 'data');
|
|
|
|
}
|
|
|
|
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
|
|
|
|
print_error('coursemisconf');
|
|
|
|
}
|
|
|
|
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
|
|
|
|
print_error('coursemisconf');
|
2006-03-03 14:44:54 +00:00
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
}
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
require_login($course, false, $cm);
|
2007-08-17 12:49:28 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
|
|
|
require_capability('mod/data:manageentries', $context);
|
2006-03-03 14:44:54 +00:00
|
|
|
|
|
|
|
/// Print the page header
|
2009-09-24 04:25:40 +00:00
|
|
|
$strdata = get_string('modulenameplural','data');
|
|
|
|
|
|
|
|
$PAGE->set_title($data->name);
|
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading(format_string($data->name));
|
2006-03-03 14:44:54 +00:00
|
|
|
|
2006-12-10 20:16:03 +00:00
|
|
|
/// Groups needed for Add entry tab
|
2009-09-24 04:25:40 +00:00
|
|
|
$currentgroup = groups_get_activity_group($cm);
|
|
|
|
$groupmode = groups_get_activity_groupmode($cm);
|
2006-12-10 20:16:03 +00:00
|
|
|
|
2006-03-03 14:44:54 +00:00
|
|
|
/// Print the tabs
|
2009-09-24 04:25:40 +00:00
|
|
|
$currenttab = 'add';
|
|
|
|
include('tabs.php');
|
2006-12-13 20:26:11 +00:00
|
|
|
|
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
$um = new upload_manager('recordsfile', false, false, null, false, 0);
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
if ($um->preprocess_files() && confirm_sesskey()) {
|
|
|
|
$filename = $um->files['recordsfile']['tmp_name'];
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
// Large files are likely to take their time and memory. Let PHP know
|
|
|
|
// that we'll take longer, and that the process should be recycled soon
|
|
|
|
// to free up memory.
|
|
|
|
@set_time_limit(0);
|
|
|
|
@raise_memory_limit("96M");
|
|
|
|
if (function_exists('apache_child_terminate')) {
|
|
|
|
@apache_child_terminate();
|
|
|
|
}
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2010-04-08 18:16:26 +00:00
|
|
|
// Fix mac/dos newlines
|
|
|
|
// TODO: Switch to cvslib when possible
|
|
|
|
$textlib = textlib_get_instance();
|
2009-09-24 04:25:40 +00:00
|
|
|
$text = my_file_get_contents($filename);
|
|
|
|
$text = preg_replace('!\r\n?!',"\n",$text);
|
2010-04-08 18:16:26 +00:00
|
|
|
$text = $textlib->trim_utf8_bom($text); // remove Unicode BOM from first line
|
2009-09-24 04:25:40 +00:00
|
|
|
$fp = fopen($filename, "w");
|
|
|
|
fwrite($fp, $text);
|
|
|
|
fclose($fp);
|
2009-03-12 14:45:13 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
$recordsadded = 0;
|
|
|
|
|
|
|
|
if (!$records = data_get_records_csv($filename, $fielddelimiter, $fieldenclosure)) {
|
|
|
|
print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
|
|
|
|
} else {
|
|
|
|
$fieldnames = array_shift($records);
|
|
|
|
|
|
|
|
// check the fieldnames are valid
|
|
|
|
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
|
|
|
|
$errorfield = '';
|
|
|
|
foreach ($fieldnames as $name) {
|
|
|
|
if (!isset($fields[$name])) {
|
|
|
|
$errorfield .= "'$name' ";
|
2009-03-12 14:45:13 +00:00
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errorfield)) {
|
|
|
|
print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
|
|
|
|
}
|
2009-03-12 14:45:13 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
foreach ($records as $record) {
|
|
|
|
if ($recordid = data_add_record($data, 0)) { // add instance to data_record
|
|
|
|
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
// Insert new data_content fields with NULL contents:
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$content = new object();
|
|
|
|
$content->recordid = $recordid;
|
|
|
|
$content->fieldid = $field->id;
|
|
|
|
$DB->insert_record('data_content', $content);
|
|
|
|
}
|
|
|
|
// Fill data_content with the values imported from the CSV file:
|
|
|
|
foreach ($record as $key => $value) {
|
|
|
|
$name = $fieldnames[$key];
|
|
|
|
$field = $fields[$name];
|
|
|
|
$content = new object();
|
|
|
|
$content->fieldid = $field->id;
|
|
|
|
$content->recordid = $recordid;
|
|
|
|
if ($field->type == 'textarea') {
|
|
|
|
// the only field type where HTML is possible
|
|
|
|
$value = clean_param($value, PARAM_CLEANHTML);
|
|
|
|
} else {
|
|
|
|
// remove potential HTML:
|
|
|
|
$patterns[] = '/</';
|
|
|
|
$replacements[] = '<';
|
|
|
|
$patterns[] = '/>/';
|
|
|
|
$replacements[] = '>';
|
|
|
|
$value = preg_replace($patterns, $replacements, $value);
|
2006-03-21 04:36:36 +00:00
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
// for now, only for "latlong" and "url" fields, but that should better be looked up from
|
|
|
|
// $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php'
|
|
|
|
// once there is stored how many contents the field can have.
|
|
|
|
if (preg_match("/^(latlong|url)$/", $field->type)) {
|
|
|
|
$values = explode(" ", $value, 2);
|
|
|
|
$content->content = $values[0];
|
|
|
|
$content->content1 = $values[1];
|
|
|
|
} else {
|
|
|
|
$content->content = $value;
|
2006-03-21 04:36:36 +00:00
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
$oldcontent = $DB->get_record('data_content', array('fieldid'=>$field->id, 'recordid'=>$recordid));
|
|
|
|
$content->id = $oldcontent->id;
|
|
|
|
$DB->update_record('data_content', $content);
|
2006-03-21 04:36:36 +00:00
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
$recordsadded++;
|
|
|
|
print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
|
2008-05-13 02:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-24 04:25:40 +00:00
|
|
|
}
|
2006-03-03 14:44:54 +00:00
|
|
|
|
2009-09-24 04:25:40 +00:00
|
|
|
if ($recordsadded > 0) {
|
|
|
|
echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'));
|
|
|
|
} else {
|
|
|
|
echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'));
|
|
|
|
}
|
|
|
|
echo '<p />';
|
2006-03-03 14:44:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Finish the page
|
2009-09-24 04:25:40 +00:00
|
|
|
echo $OUTPUT->footer();
|
2006-03-03 14:44:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
function my_file_get_contents($filename, $use_include_path = 0) {
|
|
|
|
/// Returns the file as one big long string
|
2006-03-03 14:44:54 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
$data = "";
|
|
|
|
$file = @fopen($filename, "rb", $use_include_path);
|
|
|
|
if ($file) {
|
|
|
|
while (!feof($file)) {
|
|
|
|
$data .= fread($file, 1024);
|
|
|
|
}
|
|
|
|
fclose($file);
|
2006-03-03 14:44:54 +00:00
|
|
|
}
|
2006-03-21 04:36:36 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2006-03-03 14:44:54 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
|
|
|
|
// Read the records from the given file.
|
|
|
|
// Perform a simple field count check for each record.
|
|
|
|
function data_get_records_csv($filename, $fielddelimiter=',', $fieldenclosure="\n") {
|
2008-05-31 22:18:41 +00:00
|
|
|
global $DB;
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2009-03-12 14:45:13 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
if (empty($fielddelimiter)) {
|
|
|
|
$fielddelimiter = ',';
|
|
|
|
}
|
|
|
|
if (empty($fieldenclosure)) {
|
|
|
|
$fieldenclosure = "\n";
|
|
|
|
}
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
if (!$fp = fopen($filename, "r")) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error('get_records_csv failed to open '.$filename);
|
2006-03-21 04:36:36 +00:00
|
|
|
}
|
|
|
|
$fieldnames = array();
|
2006-03-03 14:44:54 +00:00
|
|
|
$rows = array();
|
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
$fieldnames = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure);
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
if (empty($fieldnames)) {
|
|
|
|
fclose($fp);
|
2006-03-03 14:44:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$rows[] = $fieldnames;
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
while (($data = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure)) !== false) {
|
|
|
|
if (count($data) > count($fieldnames)) {
|
|
|
|
// For any given record, we can't have more data entities than the number of fields.
|
|
|
|
fclose($fp);
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-03 14:44:54 +00:00
|
|
|
$rows[] = $data;
|
|
|
|
}
|
2006-12-13 20:26:11 +00:00
|
|
|
|
2006-03-21 04:36:36 +00:00
|
|
|
fclose($fp);
|
2006-03-03 14:44:54 +00:00
|
|
|
return $rows;
|
|
|
|
}
|
2006-03-21 04:36:36 +00:00
|
|
|
|
2009-11-01 14:55:15 +00:00
|
|
|
|