PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/data/import.php

https://bitbucket.org/moodle/moodle
PHP | 105 lines | 65 code | 16 blank | 24 comment | 10 complexity | dc6df58207ac01b2b300c99cc1d6efff MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. $redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back to.
  33. $url = new moodle_url('/mod/data/import.php');
  34. if ($rid !== 0) {
  35. $url->param('rid', $rid);
  36. }
  37. if ($fielddelimiter !== '') {
  38. $url->param('fielddelimiter', $fielddelimiter);
  39. }
  40. if ($fieldenclosure !== '') {
  41. $url->param('fieldenclosure', $fieldenclosure);
  42. }
  43. if ($id) {
  44. $url->param('id', $id);
  45. $PAGE->set_url($url);
  46. $cm = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST);
  47. $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  48. $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
  49. } else {
  50. $url->param('d', $d);
  51. $PAGE->set_url($url);
  52. $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
  53. $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
  54. $cm = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST);
  55. }
  56. require_login($course, false, $cm);
  57. $context = context_module::instance($cm->id);
  58. require_capability('mod/data:manageentries', $context);
  59. $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'), ['dataid' => $data->id,
  60. 'backtourl' => $redirectbackto]);
  61. if ($form->is_cancelled()) {
  62. $redirectbackto = !empty($redirectbackto) ? $redirectbackto :
  63. new \moodle_url('/mod/data/view.php', ['d' => $data->id]);
  64. redirect($redirectbackto);
  65. }
  66. /// Print the page header
  67. $PAGE->navbar->add(get_string('add', 'data'));
  68. $PAGE->set_title($data->name);
  69. $PAGE->set_heading($course->fullname);
  70. $PAGE->set_secondary_active_tab('modulepage');
  71. echo $OUTPUT->header();
  72. echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data');
  73. if ($formdata = $form->get_data()) {
  74. $filecontent = $form->get_file_content('recordsfile');
  75. $recordsadded = data_import_csv($cm, $data, $filecontent, $formdata->encoding, $formdata->fielddelimiter);
  76. if ($recordsadded > 0) {
  77. echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
  78. } else {
  79. echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
  80. }
  81. echo $OUTPUT->continue_button($redirectbackto);
  82. } else {
  83. /// Upload records section. Only for teachers and the admin.
  84. echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
  85. $form->display();
  86. echo $OUTPUT->box_end();
  87. }
  88. /// Finish the page
  89. echo $OUTPUT->footer();