Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  /**
  18   * Book import form
  19   *
  20   * @package    booktool_importhtml
  21   * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  require_once($CFG->libdir.'/formslib.php');
  28  
  29  class booktool_importhtml_form extends moodleform {
  30  
  31      function definition() {
  32          $mform = $this->_form;
  33          $data  = $this->_customdata;
  34  
  35          $mform->addElement('header', 'general', get_string('import', 'booktool_importhtml'));
  36  
  37          $options = array(
  38                  // '0'=>get_string('typeonefile', 'booktool_importhtml'),
  39                  '1'=>get_string('typezipdirs', 'booktool_importhtml'),
  40                  '2'=>get_string('typezipfiles', 'booktool_importhtml'),
  41          );
  42          $mform->addElement('select', 'type', get_string('type', 'booktool_importhtml'), $options);
  43          $mform->setDefault('type', 2);
  44  
  45          $mform->addElement('filepicker', 'importfile', get_string('ziparchive', 'booktool_importhtml'));
  46          $mform->addHelpButton('importfile', 'ziparchive', 'booktool_importhtml');
  47          $mform->addRule('importfile', null, 'required');
  48  
  49          $mform->addElement('hidden', 'id');
  50          $mform->setType('id', PARAM_INT);
  51  
  52          $mform->addElement('hidden', 'chapterid');
  53          $mform->setType('chapterid', PARAM_INT);
  54  
  55          $this->add_action_buttons(true, get_string('doimport', 'booktool_importhtml'));
  56  
  57          $this->set_data($data);
  58      }
  59  
  60      function validation($data, $files) {
  61          global $USER;
  62  
  63          if ($errors = parent::validation($data, $files)) {
  64              return $errors;
  65          }
  66  
  67          $usercontext = context_user::instance($USER->id);
  68          $fs = get_file_storage();
  69  
  70          if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['importfile'], 'id', false)) {
  71              $errors['importfile'] = get_string('required');
  72              return $errors;
  73          } else {
  74              $file = reset($files);
  75              if ($file->get_mimetype() != 'application/zip') {
  76                  $errors['importfile'] = get_string('invalidfiletype', 'error', $file->get_filename());
  77                  // better delete current file, it is not usable anyway
  78                  $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['importfile']);
  79              } else {
  80                  if (!$chpterfiles = toolbook_importhtml_get_chapter_files($file, $data['type'])) {
  81                      $errors['importfile'] = get_string('errornochapters', 'booktool_importhtml');
  82                  }
  83              }
  84          }
  85  
  86          return $errors;
  87      }
  88  }