view.php 7.12 KB
Newer Older
1
<?php
Petr Skoda's avatar
Petr Skoda committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This file is part of Book module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Book view page
 *
 * @package    mod
 * @subpackage book
Petr Skoda's avatar
Petr Skoda committed
22
 * @copyright  2004-2011 Petr Skoda  {@link http://skodak.org}
Petr Skoda's avatar
Petr Skoda committed
23
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
25

26
require('../../config.php');
Petr Skoda's avatar
Petr Skoda committed
27
require_once($CFG->dirroot.'/mod/book/locallib.php');
Petr Skoda's avatar
Petr Skoda committed
28

Petr Skoda's avatar
Petr Skoda committed
29
30
$id        = optional_param('id', 0, PARAM_INT);        // Course Module ID
$bid       = optional_param('b', 0, PARAM_INT);         // Book id
Petr Skoda's avatar
Petr Skoda committed
31
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
32
$edit      = optional_param('edit', -1, PARAM_BOOL);    // Edit mode
Petr Skoda's avatar
Petr Skoda committed
33
34
35
36

// =========================================================================
// security checks START - teachers edit; students view
// =========================================================================
Petr Skoda's avatar
Petr Skoda committed
37
38
39
40
41
42
if ($id) {
    $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
    $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
} else {
    $book = $DB->get_record('book', array('id'=>$bid), '*', MUST_EXIST);
43
    $cm = get_coursemodule_from_instance('book', $book->id, 0, false, MUST_EXIST);
Petr Skoda's avatar
Petr Skoda committed
44
45
46
    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
    $id = $cm->id;
}
Petr Skoda's avatar
Petr Skoda committed
47

48
49
50
require_course_login($course, true, $cm);

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
51
52
require_capability('mod/book:read', $context);

53
54
$allowedit  = has_capability('mod/book:edit', $context);
$viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
55
56

if ($allowedit) {
Petr Skoda's avatar
Petr Skoda committed
57
    if ($edit != -1 and confirm_sesskey()) {
Petr Skoda's avatar
Petr Skoda committed
58
59
60
61
62
63
64
65
66
67
68
69
70
        $USER->editing = $edit;
    } else {
        if (isset($USER->editing)) {
            $edit = $USER->editing;
        } else {
            $edit = 0;
        }
    }
} else {
    $edit = 0;
}

/// read chapters
71
$chapters = book_preload_chapters($book->id);
Petr Skoda's avatar
Petr Skoda committed
72

73
74
if ($allowedit and !$chapters) {
    redirect('edit.php?cmid='.$cm->id); //no chapters - add new one
Petr Skoda's avatar
Petr Skoda committed
75
76
77
78
}
/// check chapterid and read chapter data
if ($chapterid == '0') { // go to first chapter if no given
    foreach($chapters as $ch) {
Petr Skoda's avatar
Petr Skoda committed
79
        if ($allowedit and !empty($USER->editing)) {
Petr Skoda's avatar
Petr Skoda committed
80
81
82
83
84
85
86
87
88
89
            $chapterid = $ch->id;
            break;
        }
        if (!$ch->hidden) {
            $chapterid = $ch->id;
            break;
        }
    }
}

90
91
92
93
94
95
96
97
if (!$chapterid or !$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
    print_error('errorchapter', 'mod_book', new moodle_url('/course/view.php', array('id'=>$course->id)));
}

/// chapter is hidden for students
if ($chapter->hidden and !$viewhidden) {
    print_error('errorchapter', 'mod_book', new moodle_url('/course/view.php', array('id'=>$course->id)));
}
98

Petr Skoda's avatar
Petr Skoda committed
99
$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
Petr Skoda's avatar
Petr Skoda committed
100
101


102
//unset all page parameters
Petr Skoda's avatar
Petr Skoda committed
103
unset($id);
Petr Skoda's avatar
Petr Skoda committed
104
unset($bid);
Petr Skoda's avatar
Petr Skoda committed
105
106
107
108
109
110
111
unset($chapterid);


// =========================================================================
// security checks  END
// =========================================================================

112
add_to_log($course->id, 'book', 'view', 'view.php?id='.$cm->id.'&amp;chapterid='.$chapter->id, $book->id, $cm->id);
Petr Skoda's avatar
Petr Skoda committed
113
114
115


///read standard strings
116
117
118
$strbooks = get_string('modulenameplural', 'mod_book');
$strbook  = get_string('modulename', 'mod_book');
$strtoc   = get_string('toc', 'mod_book');
Petr Skoda's avatar
Petr Skoda committed
119
120

/// prepare header
Petr Skoda's avatar
Petr Skoda committed
121
122
123
$PAGE->set_title(format_string($book->name));
$PAGE->add_body_class('mod_book');
$PAGE->set_heading(format_string($course->fullname));
124
125
126

book_add_fake_block($chapters, $chapter, $book, $cm, $edit);

Petr Skoda's avatar
Petr Skoda committed
127
echo $OUTPUT->header();
Petr Skoda's avatar
Petr Skoda committed
128
129
130
131

/// prepare chapter navigation icons
$previd = null;
$nextid = null;
Petr Skoda's avatar
Petr Skoda committed
132
$last = null;
Petr Skoda's avatar
Petr Skoda committed
133
foreach ($chapters as $ch) {
Petr Skoda's avatar
Petr Skoda committed
134
135
    if ($allowedit and empty($USER->editing) and $ch->hidden) {
        continue;
Petr Skoda's avatar
Petr Skoda committed
136
    }
Petr Skoda's avatar
Petr Skoda committed
137
138
139
    if ($last == $chapter->id) {
        $nextid = $ch->id;
        break;
Petr Skoda's avatar
Petr Skoda committed
140
    }
Petr Skoda's avatar
Petr Skoda committed
141
    if ($ch->id != $chapter->id) {
Petr Skoda's avatar
Petr Skoda committed
142
143
        $previd = $ch->id;
    }
Petr Skoda's avatar
Petr Skoda committed
144
    $last = $ch->id;
Petr Skoda's avatar
Petr Skoda committed
145
}
Petr Skoda's avatar
Petr Skoda committed
146

Petr Skoda's avatar
Petr Skoda committed
147
148
$chnavigation = '';
if ($previd) {
Petr Skoda's avatar
Petr Skoda committed
149
    $chnavigation .= '<a title="'.get_string('navprev', 'book').'" href="view.php?id='.$cm->id.'&amp;chapterid='.$previd.'"><img src="'.$OUTPUT->pix_url('nav_prev', 'mod_book').'" class="bigicon" alt="'.get_string('navprev', 'book').'"/></a>';
Petr Skoda's avatar
Petr Skoda committed
150
} else {
Petr Skoda's avatar
Petr Skoda committed
151
    $chnavigation .= '<img src="'.$OUTPUT->pix_url('nav_prev_dis', 'mod_book').'" class="bigicon" alt="" />';
Petr Skoda's avatar
Petr Skoda committed
152
153
}
if ($nextid) {
Petr Skoda's avatar
Petr Skoda committed
154
    $chnavigation .= '<a title="'.get_string('navnext', 'book').'" href="view.php?id='.$cm->id.'&amp;chapterid='.$nextid.'"><img src="'.$OUTPUT->pix_url('nav_next', 'mod_book').'" class="bigicon" alt="'.get_string('navnext', 'book').'" /></a>';
Petr Skoda's avatar
Petr Skoda committed
155
156
} else {
    $sec = '';
Petr Skoda's avatar
Petr Skoda committed
157
    if ($section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
Petr Skoda's avatar
Petr Skoda committed
158
159
        $sec = $section->section;
    }
Petr Skoda's avatar
Petr Skoda committed
160
    $chnavigation .= '<a title="'.get_string('navexit', 'book').'" href="../../course/view.php?id='.$course->id.'#section-'.$sec.'"><img src="'.$OUTPUT->pix_url('nav_exit', 'mod_book').'" class="bigicon" alt="'.get_string('navexit', 'book').'" /></a>';
Petr Skoda's avatar
Petr Skoda committed
161
162
163
}

// prepare $toc and $currtitle, $currsubtitle
Petr Skoda's avatar
Petr Skoda committed
164
list($toc, $currtitle, $currsubtitle, $titles) = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
Petr Skoda's avatar
Petr Skoda committed
165
166
167
168
169
170

// =====================================================
// Book display HTML code
// =====================================================

?>
171
<table class="booktable" width="100%" cellspacing="0" cellpadding="2">
Petr Skoda's avatar
Petr Skoda committed
172
173
174

<!-- subchapter title and upper navigation row //-->
<tr>
175
176
    <td>
        <div class="booknav"><?php echo $chnavigation ?></div>
Petr Skoda's avatar
Petr Skoda committed
177
178
179
180
    </td>
</tr>

<!-- toc and chapter row //-->
Petr Skoda's avatar
Petr Skoda committed
181
182
<tr class="tocandchapter" valign="top">
    <td align="right" valign="top"><div class="clearer">&nbsp;</div>
Petr Skoda's avatar
Petr Skoda committed
183
        <?php
Petr Skoda's avatar
Petr Skoda committed
184
        echo $OUTPUT->box_start('generalbox');
Petr Skoda's avatar
Petr Skoda committed
185
        echo '<div class="book_content">';
Petr Skoda's avatar
Petr Skoda committed
186
187
        if (!$book->customtitles) {
          if ($currsubtitle == '&nbsp;') {
Petr Skoda's avatar
Petr Skoda committed
188
              echo '<p class="book_chapter_title">'.$currtitle.'</p>';
Petr Skoda's avatar
Petr Skoda committed
189
          } else {
Petr Skoda's avatar
Petr Skoda committed
190
              echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
Petr Skoda's avatar
Petr Skoda committed
191
192
          }
        }
Petr Skoda's avatar
Petr Skoda committed
193
194
        $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
        echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
Petr Skoda's avatar
Petr Skoda committed
195
        echo '</div>';
Petr Skoda's avatar
Petr Skoda committed
196
        echo $OUTPUT->box_end();
Petr Skoda's avatar
Petr Skoda committed
197
        /// lower navigation
198
        echo '<div class="booknav">'.$chnavigation.'</div>';
Petr Skoda's avatar
Petr Skoda committed
199
200
201
202
203
204
        ?>
    </td>
</tr>
</table>

<?php
Petr Skoda's avatar
Petr Skoda committed
205

Petr Skoda's avatar
Petr Skoda committed
206
echo $OUTPUT->footer();
207