PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/data/import.php

http://github.com/moodle/moodle
PHP | 107 lines | 67 code | 15 blank | 25 comment | 9 complexity | 35f5706d58eb1f4e669bc816763e4aaa MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file is part of the Database module for Moodle
  18. *
  19. * @copyright 2005 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package mod_data
  22. */
  23. require_once('../../config.php');
  24. require_once('lib.php');
  25. require_once($CFG->libdir.'/csvlib.class.php');
  26. require_once('import_form.php');
  27. $id = optional_param('id', 0, PARAM_INT); // course module id
  28. $d = optional_param('d', 0, PARAM_INT); // database id
  29. $rid = optional_param('rid', 0, PARAM_INT); // record id
  30. $fielddelimiter = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import
  31. $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML); // characters used as record delimiters for csv file import
  32. $url = new moodle_url('/mod/data/import.php');
  33. if ($rid !== 0) {
  34. $url->param('rid', $rid);
  35. }
  36. if ($fielddelimiter !== '') {
  37. $url->param('fielddelimiter', $fielddelimiter);
  38. }
  39. if ($fieldenclosure !== '') {
  40. $url->param('fieldenclosure', $fieldenclosure);
  41. }
  42. if ($id) {
  43. $url->param('id', $id);
  44. $PAGE->set_url($url);
  45. $cm = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST);
  46. $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  47. $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
  48. } else {
  49. $url->param('d', $d);
  50. $PAGE->set_url($url);
  51. $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
  52. $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
  53. $cm = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST);
  54. }
  55. require_login($course, false, $cm);
  56. $context = context_module::instance($cm->id);
  57. require_capability('mod/data:manageentries', $context);
  58. $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
  59. /// Print the page header
  60. $PAGE->navbar->add(get_string('add', 'data'));
  61. $PAGE->set_title($data->name);
  62. $PAGE->set_heading($course->fullname);
  63. navigation_node::override_active_url(new moodle_url('/mod/data/import.php', array('d' => $data->id)));
  64. echo $OUTPUT->header();
  65. echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data');
  66. /// Groups needed for Add entry tab
  67. $currentgroup = groups_get_activity_group($cm);
  68. $groupmode = groups_get_activity_groupmode($cm);
  69. if (!$formdata = $form->get_data()) {
  70. /// Upload records section. Only for teachers and the admin.
  71. echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
  72. require_once('import_form.php');
  73. $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
  74. $formdata = new stdClass();
  75. $formdata->d = $data->id;
  76. $form->set_data($formdata);
  77. $form->display();
  78. echo $OUTPUT->box_end();
  79. echo $OUTPUT->footer();
  80. die;
  81. } else {
  82. $filecontent = $form->get_file_content('recordsfile');
  83. $recordsadded = data_import_csv($cm, $data, $filecontent, $formdata->encoding, $formdata->fielddelimiter);
  84. }
  85. if ($recordsadded > 0) {
  86. echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
  87. } else {
  88. echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
  89. }
  90. echo $OUTPUT->continue_button('import.php?d='.$data->id);
  91. /// Finish the page
  92. echo $OUTPUT->footer();