MDL-22574, move import form to import.php, use csv_import_reader class to process csv file, use filepicker to replace legacy upload form.
This commit is contained in:
+4
-49
@@ -30,7 +30,6 @@ require_once("$CFG->libdir/rsslib.php");
|
||||
$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
|
||||
$import = optional_param('import', 0, PARAM_INT); // show import form
|
||||
$cancel = optional_param('cancel', '', PARAM_RAW); // cancel an add
|
||||
$mode ='addtemplate'; //define the mode for this page, only 1 mode available
|
||||
|
||||
@@ -38,9 +37,6 @@ $url = new moodle_url('/mod/data/edit.php');
|
||||
if ($rid !== 0) {
|
||||
$url->param('rid', $rid);
|
||||
}
|
||||
if ($import !== 0) {
|
||||
$url->param('import', $import);
|
||||
}
|
||||
if ($cancel !== '') {
|
||||
$url->param('cancel', $cancel);
|
||||
}
|
||||
@@ -124,7 +120,7 @@ if ($data->jstemplate) {
|
||||
$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
|
||||
|
||||
foreach ($possiblefields as $field) {
|
||||
if ($field->type == 'file') {
|
||||
if ($field->type == 'file' || $field->type == 'picture') {
|
||||
require_once($CFG->dirroot.'/repository/lib.php');
|
||||
break;
|
||||
}
|
||||
@@ -135,8 +131,6 @@ $strdata = get_string('modulenameplural','data');
|
||||
|
||||
if ($rid) {
|
||||
$PAGE->navbar->add(get_string('editentry', 'data'));
|
||||
} else if ($import) {
|
||||
$PAGE->navbar->add(get_string('add', 'data'));
|
||||
}
|
||||
|
||||
$PAGE->set_title($data->name);
|
||||
@@ -167,7 +161,6 @@ if ($rid) {
|
||||
}
|
||||
include('tabs.php');
|
||||
|
||||
|
||||
/// Process incoming data for adding/updating records
|
||||
|
||||
if ($datarecord = data_submitted() and confirm_sesskey()) {
|
||||
@@ -276,6 +269,8 @@ if ($datarecord = data_submitted() and confirm_sesskey()) {
|
||||
}
|
||||
} // End of form processing
|
||||
|
||||
|
||||
|
||||
/// Print the browsing interface
|
||||
|
||||
$patterns = array(); //tags to replace
|
||||
@@ -315,7 +310,7 @@ if ($data->addtemplate){
|
||||
}
|
||||
|
||||
echo $newtext;
|
||||
echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
|
||||
echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
|
||||
if ($rid) {
|
||||
echo ' <input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
|
||||
} else {
|
||||
@@ -326,46 +321,6 @@ echo $OUTPUT->box_end();
|
||||
echo '</div></form>';
|
||||
|
||||
|
||||
/// Upload records section. Only for teachers and the admin.
|
||||
|
||||
if (has_capability('mod/data:manageentries',$context)) {
|
||||
if ($import) {
|
||||
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
|
||||
echo $OUTPUT->heading(get_string('uploadrecords', 'data'), 3);
|
||||
|
||||
$maxuploadsize = get_max_upload_file_size();
|
||||
echo '<div style="text-align:center">';
|
||||
echo '<form enctype="multipart/form-data" action="import.php" method="post">';
|
||||
echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
|
||||
echo '<input name="d" value="'.$data->id.'" type="hidden" />';
|
||||
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
|
||||
echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
|
||||
echo '<tr>';
|
||||
echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
|
||||
echo '<td><input type="file" name="recordsfile" size="30" />';
|
||||
echo $OUTPUT->help_icon('csvimport', 'data');
|
||||
echo '</td><tr>';
|
||||
echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
|
||||
echo '<td><input type="text" name="fielddelimiter" size="6" />';
|
||||
echo get_string('defaultfielddelimiter', 'data').'</td>';
|
||||
echo '</tr>';
|
||||
echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
|
||||
echo '<td><input type="text" name="fieldenclosure" size="6" />';
|
||||
echo get_string('defaultfieldenclosure', 'data').'</td>';
|
||||
echo '</tr>';
|
||||
echo '</table>';
|
||||
echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
|
||||
echo '</form>';
|
||||
echo '</div>';
|
||||
echo $OUTPUT->box_end();
|
||||
} else {
|
||||
echo '<div style="text-align:center">';
|
||||
echo '<a href="edit.php?d='.$data->id.'&import=1">'.get_string('uploadrecords', 'data').'</a>';
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Finish the page
|
||||
|
||||
// Print the stuff that need to come after the form fields.
|
||||
|
||||
+32
-94
@@ -25,9 +25,8 @@
|
||||
|
||||
require_once('../../config.php');
|
||||
require_once('lib.php');
|
||||
require_once($CFG->libdir.'/uploadlib.php');
|
||||
|
||||
require_login();
|
||||
require_once($CFG->libdir.'/csvlib.class.php');
|
||||
require_once('import_form.php');
|
||||
|
||||
$id = optional_param('id', 0, PARAM_INT); // course module id
|
||||
$d = optional_param('d', 0, PARAM_INT); // database id
|
||||
@@ -77,10 +76,9 @@ require_login($course, false, $cm);
|
||||
|
||||
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
||||
require_capability('mod/data:manageentries', $context);
|
||||
$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
|
||||
|
||||
/// Print the page header
|
||||
$strdata = get_string('modulenameplural','data');
|
||||
|
||||
$PAGE->navbar->add(get_string('add', 'data'));
|
||||
$PAGE->set_title($data->name);
|
||||
$PAGE->set_heading($course->fullname);
|
||||
@@ -91,16 +89,20 @@ echo $OUTPUT->heading(format_string($data->name));
|
||||
$currentgroup = groups_get_activity_group($cm);
|
||||
$groupmode = groups_get_activity_groupmode($cm);
|
||||
|
||||
/// Print the tabs
|
||||
$currenttab = 'add';
|
||||
include('tabs.php');
|
||||
|
||||
|
||||
$um = new upload_manager('recordsfile', false, false, null, false, 0);
|
||||
|
||||
if ($um->preprocess_files() && confirm_sesskey()) {
|
||||
$filename = $um->files['recordsfile']['tmp_name'];
|
||||
|
||||
if (!$formdata = $form->get_data()) {
|
||||
/// Upload records section. Only for teachers and the admin.
|
||||
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
|
||||
require_once('import_form.php');
|
||||
echo $OUTPUT->heading(get_string('uploadrecords', 'data'), 3);
|
||||
$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
|
||||
$formdata = new stdclass;
|
||||
$formdata->d = $data->id;
|
||||
$form->set_data($formdata);
|
||||
$form->display();
|
||||
echo $OUTPUT->box_end();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
} else {
|
||||
// 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.
|
||||
@@ -110,23 +112,16 @@ if ($um->preprocess_files() && confirm_sesskey()) {
|
||||
@apache_child_terminate();
|
||||
}
|
||||
|
||||
// Fix mac/dos newlines
|
||||
// TODO: Switch to cvslib when possible
|
||||
$textlib = textlib_get_instance();
|
||||
$text = my_file_get_contents($filename);
|
||||
$text = preg_replace('!\r\n?!',"\n",$text);
|
||||
$text = $textlib->trim_utf8_bom($text); // remove Unicode BOM from first line
|
||||
$fp = fopen($filename, "w");
|
||||
fwrite($fp, $text);
|
||||
fclose($fp);
|
||||
$iid = csv_import_reader::get_new_iid('moddata');
|
||||
$cir = new csv_import_reader($iid, 'moddata');
|
||||
|
||||
$recordsadded = 0;
|
||||
|
||||
if (!$records = data_get_records_csv($filename, $fielddelimiter, $fieldenclosure)) {
|
||||
$readcount = $cir->load_csv_content($form->get_file_content('recordsfile'), $formdata->encoding, $formdata->fielddelimiter);
|
||||
if (empty($readcount)) {
|
||||
print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
|
||||
} else {
|
||||
$fieldnames = array_shift($records);
|
||||
|
||||
if (!$fieldnames = $cir->get_columns()) {
|
||||
print_error('cannotreadtmpfile', 'error');
|
||||
}
|
||||
// check the fieldnames are valid
|
||||
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
|
||||
$errorfield = '';
|
||||
@@ -140,7 +135,9 @@ if ($um->preprocess_files() && confirm_sesskey()) {
|
||||
print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
|
||||
}
|
||||
|
||||
foreach ($records as $record) {
|
||||
$cir->init();
|
||||
$recordsadded = 0;
|
||||
while ($record = $cir->next()) {
|
||||
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');
|
||||
|
||||
@@ -187,77 +184,18 @@ if ($um->preprocess_files() && confirm_sesskey()) {
|
||||
print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
|
||||
}
|
||||
}
|
||||
$cir->close();
|
||||
$cir->cleanup(true);
|
||||
}
|
||||
}
|
||||
|
||||
if ($recordsadded > 0) {
|
||||
echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'));
|
||||
echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
|
||||
} else {
|
||||
echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'));
|
||||
echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
|
||||
}
|
||||
echo '<p />';
|
||||
|
||||
echo $OUTPUT->continue_button('import.php?d='.$data->id);
|
||||
|
||||
/// Finish the page
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
|
||||
|
||||
|
||||
function my_file_get_contents($filename, $use_include_path = 0) {
|
||||
/// Returns the file as one big long string
|
||||
|
||||
$data = "";
|
||||
$file = @fopen($filename, "rb", $use_include_path);
|
||||
if ($file) {
|
||||
while (!feof($file)) {
|
||||
$data .= fread($file, 1024);
|
||||
}
|
||||
fclose($file);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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") {
|
||||
global $DB;
|
||||
|
||||
|
||||
if (empty($fielddelimiter)) {
|
||||
$fielddelimiter = ',';
|
||||
}
|
||||
if (empty($fieldenclosure)) {
|
||||
$fieldenclosure = "\n";
|
||||
}
|
||||
|
||||
if (!$fp = fopen($filename, "r")) {
|
||||
print_error('get_records_csv failed to open '.$filename);
|
||||
}
|
||||
$fieldnames = array();
|
||||
$rows = array();
|
||||
|
||||
$fieldnames = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure);
|
||||
|
||||
if (empty($fieldnames)) {
|
||||
fclose($fp);
|
||||
return false;
|
||||
}
|
||||
$rows[] = $fieldnames;
|
||||
|
||||
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;
|
||||
}
|
||||
$rows[] = $data;
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
||||
}
|
||||
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
require_once($CFG->libdir.'/csvlib.class.php');
|
||||
|
||||
class mod_data_import_form extends moodleform {
|
||||
|
||||
function definition() {
|
||||
global $CFG;
|
||||
$mform =& $this->_form;
|
||||
$cmid = $this->_customdata['id'];
|
||||
|
||||
$mform->addElement('filepicker', 'recordsfile', get_string('csvfile', 'data'));
|
||||
|
||||
$delimiters = csv_import_reader::get_delimiter_list();
|
||||
$mform->addElement('select', 'fielddelimiter', get_string('fielddelimiter', 'data'), $delimiters);
|
||||
$mform->setDefault('fielddelimiter', 'comma');
|
||||
|
||||
$mform->addElement('text', 'fieldenclosure', get_string('fieldenclosure', 'data'));
|
||||
|
||||
$textlib = textlib_get_instance();
|
||||
$choices = $textlib->get_encodings();
|
||||
$mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
|
||||
$mform->setDefault('encoding', 'UTF-8');
|
||||
|
||||
$submit_string = get_string('submit');
|
||||
// data id
|
||||
$mform->addElement('hidden', 'd');
|
||||
$mform->setType('d', PARAM_INT);
|
||||
|
||||
$this->add_action_buttons(false, $submit_string);
|
||||
}
|
||||
}
|
||||
@@ -2838,6 +2838,9 @@ function data_extend_settings_navigation(settings_navigation $settings, navigati
|
||||
// and should be weaker than those required to edit Templates, Fields and Presets.
|
||||
$datanode->add(get_string('export', 'data'), new moodle_url('/mod/data/export.php', array('d'=>$data->id)));
|
||||
}
|
||||
if (has_capability('mod/data:manageentries', $PAGE->cm->context)) {
|
||||
$datanode->add(get_string('import'), new moodle_url('/mod/data/import.php', array('d'=>$data->id)));
|
||||
}
|
||||
|
||||
if (has_capability('mod/data:managetemplates', $PAGE->cm->context)) {
|
||||
$currenttab = '';
|
||||
|
||||
Reference in New Issue
Block a user