Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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));
  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  $PAGE->activityheader->set_attrs([
  54      'hidecompletion' => true,
  55      'description' => ''
  56  ]);
  57  // Prepare the page header.
  58  $strbook = get_string('modulename', 'mod_book');
  59  $strbooks = get_string('modulenameplural', 'mod_book');
  60  
  61  $mform = new booktool_importhtml_form(null, array('id'=>$id, 'chapterid'=>$chapterid));
  62  
  63  // If data submitted, then process and store.
  64  if ($mform->is_cancelled()) {
  65      if (empty($chapter->id)) {
  66          redirect($CFG->wwwroot."/mod/book/view.php?id=$cm->id");
  67      } else {
  68          redirect($CFG->wwwroot."/mod/book/view.php?id=$cm->id&chapterid=$chapter->id");
  69      }
  70  
  71  } else if ($data = $mform->get_data()) {
  72      echo $OUTPUT->header();
  73      echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'), 3);
  74  
  75      // this is a bloody hack - children do not try this at home!
  76      $fs = get_file_storage();
  77      $draftid = file_get_submitted_draft_itemid('importfile');
  78      if (!$files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false)) {
  79          redirect($PAGE->url);
  80      }
  81      $file = reset($files);
  82      toolbook_importhtml_import_chapters($file, $data->type, $book, $context);
  83  
  84      echo $OUTPUT->continue_button(new moodle_url('/mod/book/view.php', array('id'=>$id)));
  85      echo $OUTPUT->footer();
  86      die;
  87  }
  88  
  89  echo $OUTPUT->header();
  90  
  91  $mform->display();
  92  
  93  echo $OUTPUT->footer();