Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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
  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  require(__DIR__.'/../../../../config.php');
  26  require_once (__DIR__.'/locallib.php');
  27  require_once (__DIR__.'/import_form.php');
  28  
  29  $id        = required_param('id', PARAM_INT);           // Course Module ID
  30  $chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
  31  
  32  $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
  33  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  34  $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
  35  
  36  require_login($course, false, $cm);
  37  
  38  $context = context_module::instance($cm->id);
  39  require_capability('booktool/importhtml:import', $context);
  40  
  41  $PAGE->set_url('/mod/book/tool/importhtml/index.php', array('id'=>$id, 'chapterid'=>$chapterid));
  42  
  43  if ($chapterid) {
  44      if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
  45          $chapterid = 0;
  46      }
  47  } else {
  48      $chapter = false;
  49  }
  50  
  51  $PAGE->set_title($book->name);
  52  $PAGE->set_heading($course->fullname);
  53  
  54  // Prepare the page header.
  55  $strbook = get_string('modulename', 'mod_book');
  56  $strbooks = get_string('modulenameplural', 'mod_book');
  57  
  58  $mform = new booktool_importhtml_form(null, array('id'=>$id, 'chapterid'=>$chapterid));
  59  
  60  // If data submitted, then process and store.
  61  if ($mform->is_cancelled()) {
  62      if (empty($chapter->id)) {
  63          redirect($CFG->wwwroot."/mod/book/view.php?id=$cm->id");
  64      } else {
  65          redirect($CFG->wwwroot."/mod/book/view.php?id=$cm->id&chapterid=$chapter->id");
  66      }
  67  
  68  } else if ($data = $mform->get_data()) {
  69      echo $OUTPUT->header();
  70      echo $OUTPUT->heading(format_string($book->name));
  71      echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'), 3);
  72  
  73      // this is a bloody hack - children do not try this at home!
  74      $fs = get_file_storage();
  75      $draftid = file_get_submitted_draft_itemid('importfile');
  76      if (!$files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false)) {
  77          redirect($PAGE->url);
  78      }
  79      $file = reset($files);
  80      toolbook_importhtml_import_chapters($file, $data->type, $book, $context);
  81  
  82      echo $OUTPUT->continue_button(new moodle_url('/mod/book/view.php', array('id'=>$id)));
  83      echo $OUTPUT->footer();
  84      die;
  85  }
  86  
  87  echo $OUTPUT->header();
  88  echo $OUTPUT->heading(format_string($book->name));
  89  
  90  $mform->display();
  91  
  92  echo $OUTPUT->footer();