| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 4 | //
 | 
        
           |  |  | 5 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 6 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 7 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 8 | // (at your option) any later version.
 | 
        
           |  |  | 9 | //
 | 
        
           |  |  | 10 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 13 | // GNU General Public License for more details.
 | 
        
           |  |  | 14 | //
 | 
        
           |  |  | 15 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 16 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * This file is part of the Database module for Moodle
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @copyright 2005 Martin Dougiamas  http://dougiamas.com
 | 
        
           |  |  | 22 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  * @package mod_data
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | require_once('../../config.php');
 | 
        
           |  |  | 27 | require_once('lib.php');
 | 
        
           |  |  | 28 | require_once($CFG->libdir.'/csvlib.class.php');
 | 
        
           |  |  | 29 | require_once('import_form.php');
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | $id              = optional_param('id', 0, PARAM_INT);  // course module id
 | 
        
           |  |  | 32 | $d               = optional_param('d', 0, PARAM_INT);   // database id
 | 
        
           |  |  | 33 | $rid             = optional_param('rid', 0, PARAM_INT); // record id
 | 
        
           |  |  | 34 | $fielddelimiter  = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import
 | 
        
           |  |  | 35 | $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML);   // characters used as record delimiters for csv file import
 | 
        
           |  |  | 36 | $redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back to.
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | $url = new moodle_url('/mod/data/import.php');
 | 
        
           |  |  | 39 | if ($rid !== 0) {
 | 
        
           |  |  | 40 |     $url->param('rid', $rid);
 | 
        
           |  |  | 41 | }
 | 
        
           |  |  | 42 | if ($fielddelimiter !== '') {
 | 
        
           |  |  | 43 |     $url->param('fielddelimiter', $fielddelimiter);
 | 
        
           |  |  | 44 | }
 | 
        
           |  |  | 45 | if ($fieldenclosure !== '') {
 | 
        
           |  |  | 46 |     $url->param('fieldenclosure', $fieldenclosure);
 | 
        
           |  |  | 47 | }
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 | if ($id) {
 | 
        
           |  |  | 50 |     $url->param('id', $id);
 | 
        
           |  |  | 51 |     $PAGE->set_url($url);
 | 
        
           |  |  | 52 |     $cm     = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST);
 | 
        
           |  |  | 53 |     $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 | 
        
           |  |  | 54 |     $data   = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | } else {
 | 
        
           |  |  | 57 |     $url->param('d', $d);
 | 
        
           |  |  | 58 |     $PAGE->set_url($url);
 | 
        
           |  |  | 59 |     $data   = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
 | 
        
           |  |  | 60 |     $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
 | 
        
           |  |  | 61 |     $cm     = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST);
 | 
        
           |  |  | 62 | }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | require_login($course, false, $cm);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | $context = context_module::instance($cm->id);
 | 
        
           |  |  | 67 | require_capability('mod/data:manageentries', $context);
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 | $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'), ['dataid' => $data->id,
 | 
        
           |  |  | 70 |     'backtourl' => $redirectbackto]);
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | if ($form->is_cancelled()) {
 | 
        
           |  |  | 73 |     $redirectbackto = !empty($redirectbackto) ? $redirectbackto :
 | 
        
           |  |  | 74 |         new \moodle_url('/mod/data/view.php', ['d' => $data->id]);
 | 
        
           |  |  | 75 |     redirect($redirectbackto);
 | 
        
           |  |  | 76 | }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 | /// Print the page header
 | 
        
           |  |  | 79 | $pagename = get_string('uploadrecords', 'data');
 | 
        
           |  |  | 80 | $PAGE->navbar->add($pagename);
 | 
        
           | 1441 | ariadna | 81 | $PAGE->add_body_class('limitedwidth');
 | 
        
           | 1 | efrain | 82 | $titleparts = [
 | 
        
           |  |  | 83 |     $pagename,
 | 
        
           |  |  | 84 |     format_string($data->name),
 | 
        
           |  |  | 85 |     format_string($course->fullname),
 | 
        
           |  |  | 86 | ];
 | 
        
           |  |  | 87 | $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
 | 
        
           |  |  | 88 | $PAGE->set_heading($course->fullname);
 | 
        
           |  |  | 89 | $PAGE->set_secondary_active_tab('modulepage');
 | 
        
           |  |  | 90 | $PAGE->activityheader->disable();
 | 
        
           |  |  | 91 | echo $OUTPUT->header();
 | 
        
           |  |  | 92 | echo $OUTPUT->heading_with_help($pagename, 'uploadrecords', 'mod_data');
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 | if ($formdata = $form->get_data()) {
 | 
        
           |  |  | 95 |     $uploadedfilepath = $form->save_temp_file('recordsfile');
 | 
        
           |  |  | 96 |     $filestempdir = null;
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |     if (!$uploadedfilepath) {
 | 
        
           |  |  | 99 |         throw new coding_exception('No file uploaded.');
 | 
        
           |  |  | 100 |     }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |     $importer = new \mod_data\local\importer\csv_entries_importer($uploadedfilepath, $form->get_new_filename('recordsfile'));
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |     if (!$importer->get_data_file_content()) {
 | 
        
           |  |  | 105 |         echo $OUTPUT->notification(get_string('errordatafilenotfound', 'data'),
 | 
        
           |  |  | 106 |             \core\output\notification::NOTIFY_ERROR);
 | 
        
           |  |  | 107 |     } else {
 | 
        
           |  |  | 108 |         $importer->import_csv($cm, $data, $formdata->encoding, $formdata->fielddelimiter);
 | 
        
           |  |  | 109 |         unlink($uploadedfilepath);
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $addedrecordsmessages = $importer->get_added_records_messages();
 | 
        
           |  |  | 112 |         echo html_writer::div(implode('<br/>', $addedrecordsmessages));
 | 
        
           |  |  | 113 |         if (count($addedrecordsmessages) > 0) {
 | 
        
           |  |  | 114 |             echo $OUTPUT->notification(count($addedrecordsmessages) . ' ' . get_string('recordssaved', 'data'),
 | 
        
           |  |  | 115 |                 \core\output\notification::NOTIFY_SUCCESS);
 | 
        
           |  |  | 116 |         } else {
 | 
        
           |  |  | 117 |             echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'),
 | 
        
           |  |  | 118 |                 \core\output\notification::NOTIFY_ERROR);
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 | } else {
 | 
        
           |  |  | 122 |     /// Upload records section. Only for teachers and the admin.
 | 
        
           |  |  | 123 |     echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 | 
        
           |  |  | 124 |     $form->display();
 | 
        
           |  |  | 125 |     echo $OUTPUT->box_end();
 | 
        
           |  |  | 126 | }
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 | /// Finish the page
 | 
        
           |  |  | 129 | echo $OUTPUT->footer();
 |