fixed log action inserts
[moodle.git] / import.php
CommitLineData
02039379
PS
1<?PHP // $Id: import.php,v 1.1 2006/03/12 18:39:59 skodak Exp $
2
3require_once('../../config.php');
4require_once('lib.php');
5
6$id = required_param('id', PARAM_INT); // Course Module ID
7$subchapter = optional_param('subchapter', 0, PARAM_BOOL);
8$cancel = optional_param('cancel', 0, PARAM_BOOL);
9
10// =========================================================================
11// security checks START - only teachers edit
12// =========================================================================
13require_login();
14
15if (!$cm = get_record('course_modules', 'id', $id)) {
16 error('Course Module ID was incorrect');
17}
18
19if (!$course = get_record('course', 'id', $cm->course)) {
20 error('Course is misconfigured');
21}
22
23if (!isteacheredit($course->id)) {
24 error('Only editing teachers can edit books!');
25}
26
27if (!$book = get_record('book', 'id', $cm->instance)) {
28 error('Course module is incorrect');
29}
30
31//check all variables
32unset($id);
33
34// =========================================================================
35// security checks END
36// =========================================================================
37
38///cancel pressed, go back to book
39if ($cancel) {
40 redirect('view.php?id='.$cm->id);
41 die;
42}
43
44///prepare the page header
45$strbook = get_string('modulename', 'book');
46$strbooks = get_string('modulenameplural', 'book');
47$strimport = get_string('import', 'book');
48
49if ($course->category) {
50 $navigation = '<a href="../../course/view.php?id='.$course->id.'">'.$course->shortname.'</a> ->';
51} else {
52 $navigation = '';
53}
54
55print_header( "$course->shortname: $book->name",
56 $course->fullname,
57 "$navigation <a href=\"index.php?id=$course->id\">$strbooks</a> -> <a href=\"view.php?id=$cm->id\">$book->name</a> -> $strimport",
58 '',
59 '',
60 true,
61 '',
62 ''
63 );
64
65/// If data submitted, then process, store and relink.
66if (($form = data_submitted()) && (confirm_sesskey())) {
67 $form->reference = stripslashes($form->reference);
68 if ($form->reference != '') { //null path is root
69 $form->reference = book_prepare_link($form->reference);
70 if ($form->reference == '') { //evil characters in $ref!
71 error('Invalid character detected in given path!');
72 }
73 }
74 $coursebase = $CFG->dataroot.'/'.$book->course;
75 if ($form->reference == '') {
76 $base = $coursebase;
77 } else {
78 $base = $coursebase.'/'.$form->reference;
79 }
80
81 //prepare list of html files in $refs
82 $refs = array();
83 $htmlpat = '/\.html$|\.htm$/i';
84 if (is_dir($base)) { //import whole directory
85 $basedir = opendir($base);
86 while ($file = readdir($basedir)) {
87 $path = $base.'/'.$file;
88 if (filetype($path) === 'file' and preg_match($htmlpat, $file)) {
89 $refs[] = str_replace($coursebase, '', $path);
90 }
91 }
92 asort($refs);
93 } else if (is_file($base)) { //import single file
94 $refs[] = '/'.$form->reference;
95 } else { //what is it???
96 error('Incorrect file/directory specified!');
97 }
98
99 //import files
100 echo '<center>';
101 echo '<b>'.get_string('importing', 'book').':</b>';
102 echo '<table cellpadding="2" cellspacing="2" border="1">';
103 book_check_structure($book->id);
104 foreach($refs as $ref) {
105 $chapter = book_read_chapter($coursebase, $ref);
106 if ($chapter) {
107 $chapter->title = addslashes($chapter->title);
108 $chapter->content = addslashes($chapter->content);
109 $chapter->importsrc = addslashes($chapter->importsrc);
110 $chapter->bookid = $book->id;
111 $chapter->pagenum = count_records('book_chapters', 'bookid', $book->id)+1;
112 $chapter->timecreated = time();
113 $chapter->timemodified = time();
114 echo "imsrc:".$chapter->importsrc;
115 if (($subchapter) || preg_match('/_sub\.htm/i', $chapter->importsrc)) { //if filename or directory starts with sub_* treat as subdirecotories
116 $chapter->subchapter = 1;
117 } else {
118 $chapter->subchapter = 0;
119 }
120 if (!$chapter->id = insert_record('book_chapters', $chapter)) {
121 error('Could not update your book');
122 }
123 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
124 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
125 }
126 }
127 echo '</table><br />';
128 echo '<b>'.get_string('relinking', 'book').':</b>';
129 echo '<table cellpadding="2" cellspacing="2" border="1">';
130 //relink whole book = all chapters
131 book_relink($cm->id, $book->id, $course->id);
132 echo '</table><br />';
133 echo '<a href="view.php?id='.$cm->id.'">'.get_string('continue').'</a>';
134 echo '</center>';
135} else {
136/// Otherwise fill and print the form.
137 $strdoimport = get_string('doimport', 'book');
138 $strchoose = get_string('choose');
139 $pageheading = get_string('importingchapters', 'book');
140
141 $icon = '<img align="absmiddle" height="16" width="16" src="icon_chapter.gif" />&nbsp;';
142 print_heading_with_help($pageheading, 'import', 'book', $icon);
143 print_simple_box_start('center', '');
144 ?>
145 <form name="theform" method="post" action="import.php">
146 <table cellpadding="5" align="center">
147 <tr valign="top">
148 <td valign="top" align="right">
149 <b><?php print_string('fileordir', 'book') ?>:</b>
150 </td>
151 <td>
152 <?php
153 echo '<input name="reference" size="40" value="" />&nbsp;';
154 button_to_popup_window ('/mod/book/coursefiles.php?choose=theform.reference&id='.$course->id,
155 'coursefiles', $strchoose, 500, 750, $strchoose);
156 ?>
157 </td>
158 </tr>
159 <tr valign="top">
160 <td valign="top" align="right">
161 <b><?php print_string('subchapter', 'book') ?>:</b>
162 </td>
163 <td>
164 <?php
165 echo '<input name="subchapter" type="checkbox" value="1" />';
166 ?>
167 </td>
168 </tr>
169 <tr valign="top">
170 <td valign="top" align="right">&nbsp;</td>
171 <td><p><?php print_string('importinfo', 'book') ?></p></td>
172 </tr>
173 </table>
174 <center>
175 <input type="submit" value="<?php echo $strdoimport ?>" />
176 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
177 </center>
178 <input type="hidden" name="id" value="<?php p($cm->id) ?>" />
179 <input type="hidden" name="sesskey" value="<?php echo $USER->sesskey ?>" />
180 </form>
181
182 <?php
183 print_simple_box_end();
184}
185
186print_footer($course);
187
188?>