view.php 8.7 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
22
23
24
// 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
 * @copyright  2004-2010 Petr Skoda  {@link http://skodak.org}
 * @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
29
30

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

// =========================================================================
// security checks START - teachers edit; students view
// =========================================================================
Petr Skoda's avatar
Petr Skoda committed
36
37
38
$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);
Petr Skoda's avatar
Petr Skoda committed
39

40
41
42
require_course_login($course, true, $cm);

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
43
44
45
46
47
48
49
require_capability('mod/book:read', $context);

$allowedit   = has_capability('mod/book:edit', $context);
$allowimport = has_capability('mod/book:import', $context);
$allowprint  = has_capability('mod/book:print', $context) and !$book->disableprinting;
$allowexport = has_capability('mod/book:exportimscp', $context);
$viewhidden  = has_capability('mod/book:viewhiddenchapters', $context);
50
51
52

if ($allowedit) {
    if ($edit != -1) {
Petr Skoda's avatar
Petr Skoda committed
53
54
55
56
57
58
59
60
61
62
63
64
65
        $USER->editing = $edit;
    } else {
        if (isset($USER->editing)) {
            $edit = $USER->editing;
        } else {
            $edit = 0;
        }
    }
} else {
    $edit = 0;
}

/// read chapters
Petr Skoda's avatar
Petr Skoda committed
66
67
$select = $viewhidden ? array('bookid' => $book->id) : array('bookid' => $book->id, 'hidden' => 0);
$chapters = $DB->get_records('book_chapters', $select, 'pagenum', 'id, pagenum, subchapter, title, hidden');
Petr Skoda's avatar
Petr Skoda committed
68
69

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


Petr Skoda's avatar
Petr Skoda committed
92
if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
Petr Skoda's avatar
Petr Skoda committed
93
94
95
96
97
98
99
100
    error('Error reading book chapters.');
}

//check all variables
unset($id);
unset($chapterid);

/// chapter is hidden for students
101
if (!$viewhidden and $chapter->hidden) {
Petr Skoda's avatar
Petr Skoda committed
102
103
104
105
106
107
108
    error('Error reading book chapters.');
}

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

109
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
110
111
112
113
114
115
116
117


///read standard strings
$strbooks = get_string('modulenameplural', 'book');
$strbook  = get_string('modulename', 'book');
$strTOC = get_string('TOC', 'book');

/// prepare header
118
$navigation = build_navigation('', $cm);
Petr Skoda's avatar
Petr Skoda committed
119

120
$buttons = $allowedit ? '<table cellspacing="0" cellpadding="0"><tr><td>'.update_module_button($cm->id, $course->id, $strbook).'</td>'.
Petr Skoda's avatar
Petr Skoda committed
121
           '<td>&nbsp;</td><td>'.book_edit_button($book, $cm, $chapter).'</td></tr></table>'
Petr Skoda's avatar
Petr Skoda committed
122
123
124
125
           : '&nbsp;';

print_header( "$course->shortname: $book->name ($chapter->title)",
              $course->fullname,
126
              $navigation,
Petr Skoda's avatar
Petr Skoda committed
127
              '',
128
              '',
Petr Skoda's avatar
Petr Skoda committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
              true,
              $buttons,
              navmenu($course, $cm)
            );


/// prepare chapter navigation icons
$previd = null;
$nextid = null;
$found = 0;
foreach ($chapters as $ch) {
    if ($found) {
        $nextid= $ch->id;
        break;
    }
    if ($ch->id == $chapter->id) {
        $found = 1;
    }
    if (!$found) {
        $previd = $ch->id;
    }
}
if ($ch == current($chapters)) {
    $nextid = $ch->id;
}
$chnavigation = '';
if ($previd) {
156
    $chnavigation .= '<a title="'.get_string('navprev', 'book').'" href="view.php?id='.$cm->id.'&amp;chapterid='.$previd.'"><img src="pix/nav_prev.gif" class="bigicon" alt="'.get_string('navprev', 'book').'"/></a>';
Petr Skoda's avatar
Petr Skoda committed
157
} else {
158
    $chnavigation .= '<img src="pix/nav_prev_dis.gif" class="bigicon" alt="" />';
Petr Skoda's avatar
Petr Skoda committed
159
160
}
if ($nextid) {
161
    $chnavigation .= '<a title="'.get_string('navnext', 'book').'" href="view.php?id='.$cm->id.'&amp;chapterid='.$nextid.'"><img src="pix/nav_next.gif" class="bigicon" alt="'.get_string('navnext', 'book').'" /></a>';
Petr Skoda's avatar
Petr Skoda committed
162
163
} else {
    $sec = '';
Petr Skoda's avatar
Petr Skoda committed
164
    if ($section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
Petr Skoda's avatar
Petr Skoda committed
165
166
        $sec = $section->section;
    }
167
    $chnavigation .= '<a title="'.get_string('navexit', 'book').'" href="../../course/view.php?id='.$course->id.'#section-'.$sec.'"><img src="pix/nav_exit.gif" class="bigicon" alt="'.get_string('navexit', 'book').'" /></a>';
Petr Skoda's avatar
Petr Skoda committed
168
169
170
}

/// prepare print icons
171
if (!$allowprint) {
Petr Skoda's avatar
Petr Skoda committed
172
173
174
    $printbook = '';
    $printchapter = '';
} else {
175
176
    $printbook = '<a title="'.get_string('printbook', 'book').'" href="print.php?id='.$cm->id.'" onclick="this.target=\'_blank\'"><img src="pix/print_book.gif" class="bigicon" alt="'.get_string('printbook', 'book').'"/></a>';
    $printchapter = '<a title="'.get_string('printchapter', 'book').'" href="print.php?id='.$cm->id.'&amp;chapterid='.$chapter->id.'" onclick="this.target=\'_blank\'"><img src="pix/print_chapter.gif" class="bigicon" alt="'.get_string('printchapter', 'book').'"/></a>';
Petr Skoda's avatar
Petr Skoda committed
177
178
179
}

// prepare $toc and $currtitle, $currsubtitle
180
$print = 0;
Petr Skoda's avatar
Petr Skoda committed
181
182
183
184
185
186
187
188
require('toc.php');

if ($edit) {
    $tocwidth = $CFG->book_tocwidth + 80;
} else {
    $tocwidth = $CFG->book_tocwidth;
}

189
$doimport = ($allowimport and $edit) ? '<div>(<a href="import.php?id='.$cm->id.'">'.get_string('doimport', 'book').'</a>)</div>' : '';
190
191

/// Enable the IMS CP button
192
$generateimscp = ($allowexport) ? '<a title="'.get_string('generateimscp', 'book').'" href="generateimscp.php?id='.$cm->id.'"><img class="bigicon" src="pix/generateimscp.gif" alt="'.get_string('generateimscp', 'book').'"></img></a>' : '';
Petr Skoda's avatar
Petr Skoda committed
193
194
195
196
197
198
199


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

?>
200
<table class="booktable" width="100%" cellspacing="0" cellpadding="2">
Petr Skoda's avatar
Petr Skoda committed
201
202
203

<!-- subchapter title and upper navigation row //-->
<tr>
204
    <td style="width:<?php echo $tocwidth ?>px" valign="bottom">
205
206
        <?php
        print_string('toc', 'book');
207
        echo $doimport;
208
        ?>
Petr Skoda's avatar
Petr Skoda committed
209
    </td>
210
211
212
    <td>
        <div class="bookexport"><?php echo $printbook.$printchapter.$generateimscp ?></div>
        <div class="booknav"><?php echo $chnavigation ?></div>
Petr Skoda's avatar
Petr Skoda committed
213
214
215
216
    </td>
</tr>

<!-- toc and chapter row //-->
217
218
<tr class="tocandchapter">
    <td style="width:<?php echo $tocwidth ?>px" align="left"><div class="clearer">&nbsp;</div>
Petr Skoda's avatar
Petr Skoda committed
219
        <?php
220
        print_box_start('generalbox');
Petr Skoda's avatar
Petr Skoda committed
221
        echo $toc;
222
223
        print_box_end();
        if ($allowedit and $edit) {
224
            echo '<div class="faq">';
Petr Skoda's avatar
Petr Skoda committed
225
            helpbutton('faq', get_string('faq','book'), 'book', true, true);
226
            echo '</div>';
Petr Skoda's avatar
Petr Skoda committed
227
228
229
        }
        ?>
    </td>
230
    <td align="right"><div class="clearer">&nbsp;</div>
Petr Skoda's avatar
Petr Skoda committed
231
        <?php
232
        print_box_start('generalbox');
Petr Skoda's avatar
Petr Skoda committed
233
234
235
        $content = '';
        if (!$book->customtitles) {
          if ($currsubtitle == '&nbsp;') {
236
              $content .= '<p class="book_chapter_title">'.$currtitle.'</p>';
Petr Skoda's avatar
Petr Skoda committed
237
238
239
240
241
242
243
244
245
246
247
          } else {
              $content .= '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
          }
        }
        $content .= $chapter->content;

        $nocleanoption = new object();
        $nocleanoption->noclean = true;
        echo '<div class="book_content">';
        echo format_text($content, FORMAT_HTML, $nocleanoption, $course->id);
        echo '</div>';
248
        print_box_end();
Petr Skoda's avatar
Petr Skoda committed
249
        /// lower navigation
250
        echo '<div class="booknav">'.$chnavigation.'</div>';
Petr Skoda's avatar
Petr Skoda committed
251
252
253
254
255
256
257
        ?>
    </td>
</tr>
</table>

<?php
print_footer($course);
258