| 1 | 
           efrain | 
           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 | 
            * Edit book chapter
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    mod_book
  | 
        
        
            | 
            | 
           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__.'/edit_form.php');
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           $cmid       = required_param('cmid', PARAM_INT);  // Book Course Module ID
  | 
        
        
            | 
            | 
           30 | 
           $chapterid  = optional_param('id', 0, PARAM_INT); // Chapter ID
  | 
        
        
            | 
            | 
           31 | 
           $pagenum    = optional_param('pagenum', 0, PARAM_INT);
  | 
        
        
            | 
            | 
           32 | 
           $subchapter = optional_param('subchapter', 0, PARAM_BOOL);
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           $cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST);
  | 
        
        
            | 
            | 
           35 | 
           $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           36 | 
           $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           require_login($course, false, $cm);
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
           $context = context_module::instance($cm->id);
  | 
        
        
            | 
            | 
           41 | 
           require_capability('mod/book:edit', $context);
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           $PAGE->set_url('/mod/book/edit.php', array('cmid'=>$cmid, 'id'=>$chapterid, 'pagenum'=>$pagenum, 'subchapter'=>$subchapter));
  | 
        
        
            | 
            | 
           44 | 
           $PAGE->set_pagelayout('admin'); // TODO: Something. This is a bloody hack!
  | 
        
        
            | 
            | 
           45 | 
           $PAGE->add_body_class('limitedwidth');
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
           if ($chapterid) {
  | 
        
        
            | 
            | 
           48 | 
               $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           49 | 
               $chapter->tags = core_tag_tag::get_item_tags_array('mod_book', 'book_chapters', $chapter->id);
  | 
        
        
            | 
            | 
           50 | 
           } else {
  | 
        
        
            | 
            | 
           51 | 
               $chapter = new stdClass();
  | 
        
        
            | 
            | 
           52 | 
               $chapter->id         = null;
  | 
        
        
            | 
            | 
           53 | 
               $chapter->subchapter = $subchapter;
  | 
        
        
            | 
            | 
           54 | 
               $chapter->pagenum    = $pagenum + 1;
  | 
        
        
            | 
            | 
           55 | 
           }
  | 
        
        
            | 
            | 
           56 | 
           $chapter->cmid = $cm->id;
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
           // Get the previous page number.
  | 
        
        
            | 
            | 
           59 | 
           $prevpage = $chapter->pagenum - 1;
  | 
        
        
            | 
            | 
           60 | 
           if ($prevpage) {
  | 
        
        
            | 
            | 
           61 | 
               $currentchapter = $DB->get_record('book_chapters', ['pagenum' => $prevpage, 'bookid' => $book->id]);
  | 
        
        
            | 
            | 
           62 | 
               if ($currentchapter) {
  | 
        
        
            | 
            | 
           63 | 
                   $chapter->currentchaptertitle = $currentchapter->title;
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
           }
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
           $options = array('noclean'=>true, 'subdirs'=>true, 'maxfiles'=>-1, 'maxbytes'=>0, 'context'=>$context);
  | 
        
        
            | 
            | 
           68 | 
           $chapter = file_prepare_standard_editor($chapter, 'content', $options, $context, 'mod_book', 'chapter', $chapter->id);
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
           $mform = new book_chapter_edit_form(null, array('chapter'=>$chapter, 'options'=>$options));
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
           // If data submitted, then process and store.
  | 
        
        
            | 
            | 
           73 | 
           if ($mform->is_cancelled()) {
  | 
        
        
            | 
            | 
           74 | 
               // Make sure at least one chapter exists.
  | 
        
        
            | 
            | 
           75 | 
               $chapters = book_preload_chapters($book);
  | 
        
        
            | 
            | 
           76 | 
               if (!$chapters) {
  | 
        
        
           | 1441 | 
           ariadna | 
           77 | 
                   $section = $DB->get_record('course_sections', ['id' => $cm->section]);
  | 
        
        
            | 
            | 
           78 | 
                   redirect(course_get_url($course, $section)); // Back to course view.
  | 
        
        
           | 1 | 
           efrain | 
           79 | 
               }
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
               if (empty($chapter->id)) {
  | 
        
        
            | 
            | 
           82 | 
                   redirect("view.php?id=$cm->id");
  | 
        
        
            | 
            | 
           83 | 
               } else {
  | 
        
        
            | 
            | 
           84 | 
                   redirect("view.php?id=$cm->id&chapterid=$chapter->id");
  | 
        
        
            | 
            | 
           85 | 
               }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           } else if ($data = $mform->get_data()) {
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               if ($data->id) {
  | 
        
        
            | 
            | 
           90 | 
                   // store the files
  | 
        
        
            | 
            | 
           91 | 
                   $data->timemodified = time();
  | 
        
        
            | 
            | 
           92 | 
                   $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
  | 
        
        
            | 
            | 
           93 | 
                   $DB->update_record('book_chapters', $data);
  | 
        
        
            | 
            | 
           94 | 
                   $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
  | 
        
        
            | 
            | 
           95 | 
                   $chapter = $DB->get_record('book_chapters', array('id' => $data->id));
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   core_tag_tag::set_item_tags('mod_book', 'book_chapters', $chapter->id, $context, $data->tags);
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger();
  | 
        
        
            | 
            | 
           100 | 
               } else {
  | 
        
        
            | 
            | 
           101 | 
                   // adding new chapter
  | 
        
        
            | 
            | 
           102 | 
                   $data->bookid        = $book->id;
  | 
        
        
            | 
            | 
           103 | 
                   $data->hidden        = 0;
  | 
        
        
            | 
            | 
           104 | 
                   $data->timecreated   = time();
  | 
        
        
            | 
            | 
           105 | 
                   $data->timemodified  = time();
  | 
        
        
            | 
            | 
           106 | 
                   $data->importsrc     = '';
  | 
        
        
            | 
            | 
           107 | 
                   $data->content       = '';          // updated later
  | 
        
        
            | 
            | 
           108 | 
                   $data->contentformat = FORMAT_HTML; // updated later
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
                   // make room for new page
  | 
        
        
            | 
            | 
           111 | 
                   $sql = "UPDATE {book_chapters}
  | 
        
        
            | 
            | 
           112 | 
                              SET pagenum = pagenum + 1
  | 
        
        
            | 
            | 
           113 | 
                            WHERE bookid = ? AND pagenum >= ?";
  | 
        
        
            | 
            | 
           114 | 
                   $DB->execute($sql, array($book->id, $data->pagenum));
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $data->id = $DB->insert_record('book_chapters', $data);
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                   // store the files
  | 
        
        
            | 
            | 
           119 | 
                   $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
  | 
        
        
            | 
            | 
           120 | 
                   $DB->update_record('book_chapters', $data);
  | 
        
        
            | 
            | 
           121 | 
                   $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
  | 
        
        
            | 
            | 
           122 | 
                   $chapter = $DB->get_record('book_chapters', array('id' => $data->id));
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   core_tag_tag::set_item_tags('mod_book', 'book_chapters', $chapter->id, $context, $data->tags);
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
                   \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger();
  | 
        
        
            | 
            | 
           127 | 
               }
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
               book_preload_chapters($book); // fix structure
  | 
        
        
            | 
            | 
           130 | 
               redirect("view.php?id=$cm->id&chapterid=$data->id");
  | 
        
        
            | 
            | 
           131 | 
           }
  | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
           // Otherwise fill and print the form.
  | 
        
        
            | 
            | 
           134 | 
           $PAGE->set_title($book->name);
  | 
        
        
            | 
            | 
           135 | 
           $PAGE->set_heading($course->fullname);
  | 
        
        
            | 
            | 
           136 | 
           $PAGE->set_secondary_active_tab('modulepage');
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
           if ($chapters = book_preload_chapters($book)) {
  | 
        
        
            | 
            | 
           139 | 
               book_add_fake_block($chapters, $chapter, $book, $cm);
  | 
        
        
            | 
            | 
           140 | 
           }
  | 
        
        
            | 
            | 
           141 | 
           $PAGE->activityheader->set_attrs([
  | 
        
        
            | 
            | 
           142 | 
               "description" => '',
  | 
        
        
            | 
            | 
           143 | 
               "hidecompletion" => true
  | 
        
        
            | 
            | 
           144 | 
           ]);
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
           echo $OUTPUT->header();
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
           $mform->display();
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
           echo $OUTPUT->footer();
  |