diff --git a/mod/data/edit.php b/mod/data/edit.php index d814e240063..906833d06d6 100755 --- a/mod/data/edit.php +++ b/mod/data/edit.php @@ -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 '
'; +echo '
'; if ($rid) { echo ' '; } else { @@ -326,46 +321,6 @@ echo $OUTPUT->box_end(); echo '
'; -/// 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 '
'; - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'.get_string('csvfile', 'data').':'; - echo $OUTPUT->help_icon('csvimport', 'data'); - echo '
'.get_string('fielddelimiter', 'data').':'; - echo get_string('defaultfielddelimiter', 'data').'
'.get_string('fieldenclosure', 'data').':'; - echo get_string('defaultfieldenclosure', 'data').'
'; - echo ''; - echo '
'; - echo '
'; - echo $OUTPUT->box_end(); - } else { - echo '
'; - echo ''.get_string('uploadrecords', 'data').''; - echo '
'; - } -} - - /// Finish the page // Print the stuff that need to come after the form fields. diff --git a/mod/data/import.php b/mod/data/import.php index 646a288eef3..36b4bf8325e 100755 --- a/mod/data/import.php +++ b/mod/data/import.php @@ -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)
\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 '

'; +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; -} - - diff --git a/mod/data/import_form.php b/mod/data/import_form.php new file mode 100644 index 00000000000..7da41db8994 --- /dev/null +++ b/mod/data/import_form.php @@ -0,0 +1,36 @@ +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); + } +} diff --git a/mod/data/lib.php b/mod/data/lib.php index b3a572d8103..efd8adf33e9 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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 = '';