view.php 8 KB
Newer Older
1
<?PHP // $Id: view.php,v 1.6 2008/08/13 23:21:14 skodak Exp $
Petr Skoda's avatar
Petr Skoda committed
2

3
require('../../config.php');
Petr Skoda's avatar
Petr Skoda committed
4
5
6
7
require_once('lib.php');

$id        = required_param('id', PARAM_INT);           // Course Module ID
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
8
$edit      = optional_param('edit', -1, PARAM_BOOL);    // Edit mode
Petr Skoda's avatar
Petr Skoda committed
9
10
11
12

// =========================================================================
// security checks START - teachers edit; students view
// =========================================================================
13
14
if (!$cm = get_coursemodule_from_id('book', $id)) {
    error('Course Module ID was incorrect');
Petr Skoda's avatar
Petr Skoda committed
15
16
17
18
19
20
21
22
23
24
}

if (!$course = get_record('course', 'id', $cm->course)) {
    error('Course is misconfigured');
}

if (!$book = get_record('book', 'id', $cm->instance)) {
    error('Course module is incorrect');
}

25
26
27
require_course_login($course, true, $cm);

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
28
29
30
31
32
33
34
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);
35
36
37

if ($allowedit) {
    if ($edit != -1) {
Petr Skoda's avatar
Petr Skoda committed
38
39
40
41
42
43
44
45
46
47
48
49
50
        $USER->editing = $edit;
    } else {
        if (isset($USER->editing)) {
            $edit = $USER->editing;
        } else {
            $edit = 0;
        }
    }
} else {
    $edit = 0;
}

/// read chapters
51
$select = $viewhidden ? "bookid = $book->id" : "bookid = $book->id AND hidden = 0";
Petr Skoda's avatar
Petr Skoda committed
52
53
54
$chapters = get_records_select('book_chapters', $select, 'pagenum', 'id, pagenum, subchapter, title, hidden');

if (!$chapters) {
55
    if ($allowedit) {
56
        redirect('edit.php?cmid='.$cm->id); //no chapters - add new one
Petr Skoda's avatar
Petr Skoda committed
57
58
59
60
61
62
63
64
        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) {
65
        if ($allowedit) {
Petr Skoda's avatar
Petr Skoda committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
            $chapterid = $ch->id;
            break;
        }
        if (!$ch->hidden) {
            $chapterid = $ch->id;
            break;
        }
    }
}


if (!$chapter = get_record('book_chapters', 'id', $chapterid)) {
    error('Error reading book chapters.');
}

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

/// chapter is hidden for students
86
if (!$viewhidden and $chapter->hidden) {
Petr Skoda's avatar
Petr Skoda committed
87
88
89
90
91
92
93
94
95
96
97
    error('Error reading book chapters.');
}

/// chapter not part of this book!
if ($chapter->bookid != $book->id) {
    error('Chapter not part of this book!');
}
// =========================================================================
// security checks  END
// =========================================================================

98
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
99
100
101
102
103
104
105
106


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

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

109
$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
110
111
112
113
114
           '<td>&nbsp;</td><td>'.book_edit_button($cm->id, $course->id, $chapter->id).'</td></tr></table>'
           : '&nbsp;';

print_header( "$course->shortname: $book->name ($chapter->title)",
              $course->fullname,
115
              $navigation,
Petr Skoda's avatar
Petr Skoda committed
116
              '',
117
              '',
Petr Skoda's avatar
Petr Skoda committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
              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) {
145
    $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
146
} else {
147
    $chnavigation .= '<img src="pix/nav_prev_dis.gif" class="bigicon" alt="" />';
Petr Skoda's avatar
Petr Skoda committed
148
149
}
if ($nextid) {
150
    $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
151
152
153
154
155
} else {
    $sec = '';
    if ($section = get_record('course_sections', 'id', $cm->section)) {
        $sec = $section->section;
    }
156
    $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
157
158
159
}

/// prepare print icons
160
if (!$allowprint) {
Petr Skoda's avatar
Petr Skoda committed
161
162
163
    $printbook = '';
    $printchapter = '';
} else {
164
165
    $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
166
167
168
}

// prepare $toc and $currtitle, $currsubtitle
169
$print = 0;
Petr Skoda's avatar
Petr Skoda committed
170
171
172
173
174
175
176
177
require('toc.php');

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

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

/// Enable the IMS CP button
181
$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
182
183
184
185
186
187
188


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

?>
189
<table class="booktable" width="100%" cellspacing="0" cellpadding="2">
Petr Skoda's avatar
Petr Skoda committed
190
191
192

<!-- subchapter title and upper navigation row //-->
<tr>
193
    <td style="width:<?php echo $tocwidth ?>px" valign="bottom">
194
195
        <?php
        print_string('toc', 'book');
196
        echo $doimport;
197
        ?>
Petr Skoda's avatar
Petr Skoda committed
198
    </td>
199
200
201
    <td>
        <div class="bookexport"><?php echo $printbook.$printchapter.$generateimscp ?></div>
        <div class="booknav"><?php echo $chnavigation ?></div>
Petr Skoda's avatar
Petr Skoda committed
202
203
204
205
    </td>
</tr>

<!-- toc and chapter row //-->
206
207
<tr class="tocandchapter">
    <td style="width:<?php echo $tocwidth ?>px" align="left"><div class="clearer">&nbsp;</div>
Petr Skoda's avatar
Petr Skoda committed
208
        <?php
209
        print_box_start('generalbox');
Petr Skoda's avatar
Petr Skoda committed
210
        echo $toc;
211
212
        print_box_end();
        if ($allowedit and $edit) {
213
            echo '<div class="faq">';
Petr Skoda's avatar
Petr Skoda committed
214
            helpbutton('faq', get_string('faq','book'), 'book', true, true);
215
            echo '</div>';
Petr Skoda's avatar
Petr Skoda committed
216
217
218
        }
        ?>
    </td>
219
    <td align="right"><div class="clearer">&nbsp;</div>
Petr Skoda's avatar
Petr Skoda committed
220
        <?php
221
        print_box_start('generalbox');
Petr Skoda's avatar
Petr Skoda committed
222
223
224
        $content = '';
        if (!$book->customtitles) {
          if ($currsubtitle == '&nbsp;') {
225
              $content .= '<p class="book_chapter_title">'.$currtitle.'</p>';
Petr Skoda's avatar
Petr Skoda committed
226
227
228
229
230
231
232
233
234
235
236
          } 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>';
237
        print_box_end();
Petr Skoda's avatar
Petr Skoda committed
238
        /// lower navigation
239
        echo '<div class="booknav">'.$chnavigation.'</div>';
Petr Skoda's avatar
Petr Skoda committed
240
241
242
243
244
245
246
247
248
        ?>
    </td>
</tr>
</table>

<?php
print_footer($course);

?>