add basic revision tracking support
[moodle.git] / tool / exportimscp / index.php
CommitLineData
8d623d14 1<?php
fc9479b8
EL
2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.com //
8// //
9// Copyright (C) 2001-3001 Antonio Vicent http://ludens.es //
10// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
4bc75ecc
PS
26die('Not converted to 2.0 yet, sorry');
27
c6d2eda2 28require('../../config.php');
e355240d 29require_once($CFG->dirroot.'/mod/book/locallib.php');
c6d2eda2
PS
30require_once($CFG->dirroot.'/backup/lib.php');
31require_once($CFG->libdir.'/filelib.php');
fc9479b8 32
dab784a3 33$id = required_param('id', PARAM_INT); // Course Module ID
fc9479b8 34
9d2fe179 35
dab784a3 36if (!$cm = get_coursemodule_from_id('book', $id)) {
fc9479b8
EL
37 error('Course Module ID was incorrect');
38}
39
40if (!$course = get_record('course', 'id', $cm->course)) {
41 error('Course is misconfigured');
42}
43
c6d2eda2
PS
44require_login($course, true, $cm);
45
dab784a3 46$context = get_context_instance(CONTEXT_MODULE, $cm->id);
c6d2eda2
PS
47require_capability('mod/book:read', $context);
48require_capability('mod/book:exportimscp', $context);
49
fc9479b8
EL
50if (!$book = get_record('book', 'id', $cm->instance)) {
51 error('Course module is incorrect');
52}
53
fc9479b8
EL
54$strbooks = get_string('modulenameplural', 'book');
55$strbook = get_string('modulename', 'book');
56$strtop = get_string('top', 'book');
57
58add_to_log($course->id, 'book', 'generateimscp', 'generateimscp.php?id='.$cm->id, $book->id, $cm->id);
59
60/// Get all the chapters
61 $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum');
62
63/// Generate the manifest and all the contents
64 chapters2imsmanifest($chapters, $book, $cm);
65
66/// Now zip everything
67 make_upload_directory('temp');
68 $zipfile = $CFG->dataroot . "/temp/". time() . '.zip';
69 $files = get_directory_list($CFG->dataroot . "/$cm->course/moddata/book/$book->id", basename($zipfile), false, true, true);
70 foreach ($files as $key => $value) {
71 $files[$key] = $CFG->dataroot . "/$cm->course/moddata/book/$book->id/" . $value;
72 }
73 zip_files($files, $zipfile);
74/// Now delete all the temp dirs
75 delete_dir_contents($CFG->dataroot . "/$cm->course/moddata/book/$book->id");
76/// Now serve the file
c6d2eda2 77 send_temp_file($zipfile, clean_filename($book->name) . '.zip');
fc9479b8
EL
78
79/**
80 * This function will create the default imsmanifest plus contents for the book chapters passed as array
81 * Everything will be created under the book moddata file area *
82 */
83function chapters2imsmanifest ($chapters, $book, $cm) {
84
85 global $CFG;
86
87/// Init imsmanifest and others
88 $imsmanifest = '';
89 $imsitems = '';
90 $imsresources = '';
91
92/// Moodle and Book version
93 $moodle_release = $CFG->release;
94 $moodle_version = $CFG->version;
95 $book_version = get_field('modules', 'version', 'name', 'book');
96
97/// Load manifest header
98 $imsmanifest .= '<?xml version="1.0" encoding="UTF-8"?>
99<!-- This package has been created with Moodle ' . $moodle_release . ' (' . $moodle_version . '), Book module version ' . $book_version . ' - http://moodle.org -->
100<!-- One idea and implementation by Eloy Lafuente (stronk7) and Antonio Vicent (C) 2001-3001 -->
101<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="MANIFEST-' . md5($CFG->wwwroot . '-' . $book->course . '-' . $book->id) . '" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd">
102 <organizations default="MOODLE-' . $book->course . '-' . $book->id . '">
103 <organization identifier="MOODLE-' . $book->course . '-' . $book->id . '" structure="hierarchical">
104 <title>' . htmlspecialchars($book->name) . '</title>';
105
106/// Create the temp directory
107 $moddir = "$cm->course/moddata/book/$book->id";
108 make_upload_directory($moddir);
109
110/// For each chapter, create the corresponding directory and save contents there
111
112/// To store the prev level (book only have 0 and 1)
113 $prevlevel = null;
114 foreach ($chapters as $chapter) {
115 /// Calculate current level ((book only have 0 and 1)
116 $currlevel = empty($chapter->subchapter) ? 0 : 1;
117 /// Based upon prevlevel and current one, decide what to close
118 if ($prevlevel !== null) {
119 /// Calculate the number of spaces (for visual xml-text formating)
120 $prevspaces = substr(' ', 0, $currlevel * 2);
121
122 /// Same level, simply close the item
123 if ($prevlevel == $currlevel) {
124 $imsitems .= $prevspaces . ' </item>' . "\n";
125 }
126 /// Bigger currlevel, nothing to close
127 /// Smaller currlevel, close both the current item and the parent one
128 if ($prevlevel > $currlevel) {
129 $imsitems .= ' </item>' . "\n";
130 $imsitems .= ' </item>' . "\n";
131 }
132 }
133 /// Update prevlevel
134 $prevlevel = $currlevel;
135
136 /// Calculate the number of spaces (for visual xml-text formating)
137 $currspaces = substr(' ', 0, $currlevel * 2);
138
139 /// Create the static html file + local attachments (images...)
140 $chapterdir = "$moddir/$chapter->pagenum";
141 make_upload_directory($chapterdir);
142 $chaptercontent = chapter2html($chapter, $book->course, $book->id);
143 file_put_contents($CFG->dataroot . "/" . $chapterdir . "/index.html", $chaptercontent->content);
144 /// Add the imsitems
145 $imsitems .= $currspaces .' <item identifier="ITEM-' . $book->course . '-' . $book->id . '-' . $chapter->pagenum .'" isvisible="true" identifierref="RES-' . $book->course . '-' . $book->id . '-' . $chapter->pagenum . '">
146 ' . $currspaces . ' <title>' . htmlspecialchars($chapter->title) . '</title>' . "\n";
147
148 /// Add the imsresources
149 /// First, check if we have localfiles
150 $localfiles = '';
151 if ($chaptercontent->localfiles) {
152 foreach ($chaptercontent->localfiles as $localfile) {
153 $localfiles .= "\n" . ' <file href="' . $chapter->pagenum . '/' . $localfile . '" />';
154 }
155 }
156 /// Now add the dependency to css
157 $cssdependency = "\n" . ' <dependency identifierref="RES-' . $book->course . '-' . $book->id . '-css" />';
158 /// Now build the resources section
159 $imsresources .= ' <resource identifier="RES-' . $book->course . '-' . $book->id . '-' . $chapter->pagenum . '" type="webcontent" xml:base="' . $chapter->pagenum . '/" href="index.html">
160 <file href="' . $chapter->pagenum . '/index.html" />' . $localfiles . $cssdependency . '
161 </resource>' . "\n";
162 }
163
164/// Close items (the latest chapter)
165/// Level 1, close 1
166 if ($currlevel == 0) {
167 $imsitems .= ' </item>' . "\n";
168 }
169/// Level 2, close 2
170 if ($currlevel == 1) {
171 $imsitems .= ' </item>' . "\n";
172 $imsitems .= ' </item>' . "\n";
173 }
174
175/// Define the css common resource
176$cssresource = ' <resource identifier="RES-' . $book->course . '-' . $book->id . '-css" type="webcontent" xml:base="css/" href="styles.css">
177 <file href="css/styles.css" />
178 </resource>' . "\n";
179
180/// Add imsitems to manifest
181 $imsmanifest .= "\n" . $imsitems;
182/// Close the organization
183 $imsmanifest .= " </organization>
184 </organizations>";
185/// Add resources to manifest
186 $imsmanifest .= "\n <resources>\n" . $imsresources . $cssresource . " </resources>";
187/// Close manifest
188 $imsmanifest .= "\n</manifest>\n";
189
190 file_put_contents($CFG->dataroot . "/" . $moddir . '/imsmanifest.xml', $imsmanifest );
191
192/// Now send the css resource
193 make_upload_directory("$moddir/css");
194 file_put_contents($CFG->dataroot . "/" . $moddir . "/css/styles.css", file_get_contents("$CFG->dirroot/mod/book/generateimscp.css"));
195}
196
197/**
c6d2eda2 198 * This function will create one chaptercontent object, with the contents converted to html and
fc9479b8
EL
199 * one array of local images to be included
200 */
201function chapter2html($chapter, $courseid, $bookid) {
202
203 global $CFG;
204
205 $content = '';
206 $content .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' . "\n";
207 $content .= '<html>' . "\n";
208 $content .= '<head>' . "\n";
209 $content .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />' . "\n";
210 $content .= '<link rel="stylesheet" type="text/css" href="../css/styles.css" />' . "\n";
211 $content .= '<title>' . $chapter->title . '</title>' . "\n";
212 $content .= '</head>' . "\n";
213 $content .= '<body>' . "\n";
214 $content .= '<h1 id="header">' . $chapter->title . '</h1>' ."\n";
dab784a3 215 $options = new object();
fc9479b8
EL
216 $options->noclean = true;
217 $content .= format_text($chapter->content, '', $options, $courseid) . "\n";
218 $content .= '</body>' . "\n";
219 $content .= '</html>' . "\n";
220
221/// Now look for course-files in contents
222 $search = array($CFG->wwwroot.'/file.php/'.$courseid,
223 $CFG->wwwroot.'/file.php?file=/'.$courseid);
224 $replace = array('$@FILEPHP@$','$@FILEPHP@$');
225 $content = str_replace($search, $replace, $content);
226
227 $regexp = '/\$@FILEPHP@\$(.*?)"/is';
228 $localfiles = array();
229 $basefiles = array();
230 preg_match_all($regexp, $content, $list);
231
232 if ($list) {
233 /// Build the array of local files
234 foreach (array_unique($list[1]) as $key => $value) {
235 $localfiles['<#'. $key . '#>'] = $value;
236 $basefiles['<#'. $key . '#>'] = basename($value);
237 /// Copy files to current chapter directory
238 if (file_exists($CFG->dataroot . '/' . $courseid . '/' . $value)) {
239 copy($CFG->dataroot . '/' . $courseid . '/' . $value, $CFG->dataroot . '/' . $courseid . '/moddata/book/' . $bookid . '/' . $chapter->pagenum . '/' . basename ($value));
240 }
241 }
242 /// Replace contents by keys
243 $content = str_replace($localfiles, array_keys($localfiles), $content);
244 /// Replace keys by basefiles
245 $content = str_replace(array_keys($basefiles), $basefiles, $content);
246 /// Delete $@FILEPHP@$
247 $content = str_replace('$@FILEPHP@$', '', $content);
248 }
249
250/// Build the final object needed to have all the info in order to create the manifest
dab784a3 251 $object = new object();
fc9479b8
EL
252 $object->content = $content;
253 $object->localfiles = $basefiles;
254
255 return $object;
256}