view.php 7.88 KB
Newer Older
1
<?PHP // $Id: view.php,v 1.5 2008/03/25 20:19:49 poltawski Exp $
Petr Skoda's avatar
Petr Skoda committed
2
3
4
5
6
7
8
9
10
11
12

require_once('../../config.php');
require_once('lib.php');

$id        = required_param('id', PARAM_INT);           // Course Module ID
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
$edit      = optional_param('edit', -1, PARAM_BOOL);     // Edit mode

// =========================================================================
// 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
28
29
30
31
require_course_login($course, true, $cm);

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$allowedit = has_capability('moodle/course:manageactivities', $context);

if ($allowedit) {
    if ($edit != -1) {
Petr Skoda's avatar
Petr Skoda committed
32
33
34
35
36
37
38
39
40
41
42
43
44
        $USER->editing = $edit;
    } else {
        if (isset($USER->editing)) {
            $edit = $USER->editing;
        } else {
            $edit = 0;
        }
    }
} else {
    $edit = 0;
}

/// read chapters
45
$select = $allowedit ? "bookid = $book->id" : "bookid = $book->id AND hidden = 0";
Petr Skoda's avatar
Petr Skoda committed
46
47
48
$chapters = get_records_select('book_chapters', $select, 'pagenum', 'id, pagenum, subchapter, title, hidden');

if (!$chapters) {
49
    if ($allowedit) {
Petr Skoda's avatar
Petr Skoda committed
50
51
52
53
54
55
56
57
58
        redirect('edit.php?id='.$cm->id); //no chapters - add new one
        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) {
59
        if ($allowedit) {
Petr Skoda's avatar
Petr Skoda committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
            $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
80
if (!$allowedit and $chapter->hidden) {
Petr Skoda's avatar
Petr Skoda committed
81
82
83
84
85
86
87
88
89
90
91
    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
// =========================================================================

92
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
93
94
95
96
97
98
99
100


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

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

103
$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
104
105
106
107
108
           '<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,
109
              $navigation,
Petr Skoda's avatar
Petr Skoda committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
              '',
              '<style type="text/css">@import url('.$CFG->wwwroot.'/mod/book/book_theme.css);</style>',
              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) {
139
    $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
140
} else {
141
    $chnavigation .= '<img src="pix/nav_prev_dis.gif" class="bigicon" alt="" />';
Petr Skoda's avatar
Petr Skoda committed
142
143
}
if ($nextid) {
144
    $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
145
146
147
148
149
} else {
    $sec = '';
    if ($section = get_record('course_sections', 'id', $cm->section)) {
        $sec = $section->section;
    }
150
    $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
151
152
153
154
155
156
157
}

/// prepare print icons
if ($book->disableprinting) {
    $printbook = '';
    $printchapter = '';
} else {
158
159
    $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
160
161
162
163
164
165
166
167
168
169
170
}

// prepare $toc and $currtitle, $currsubtitle
require('toc.php');

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

171
$doimport = ($allowedit and $edit) ? '<a href="import.php?id='.$cm->id.'">'.get_string('doimport', 'book').'</a>' : '';
172
173
174

/// Enable the IMS CP button
$generateimscp = ($allowedit) ? '<a title="'.get_string('generateimscp', 'book').'" href="generateimscp.php?id='.$cm->id.'"><img class="bigicon" src="pix/generateimscp.gif" height="24" width="24" border="0"></img></a>' : '';
Petr Skoda's avatar
Petr Skoda committed
175
176
177
178
179
180
181
182
183
184
185
186


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

?>
<table border="0" cellspacing="0" width="100%" valign="top" cellpadding="2">

<!-- subchapter title and upper navigation row //-->
<tr>
    <td width="<?php echo $tocwidth ?>" valign="bottom">
187
188
189
        <?php
        print_string('toc', 'book');
        if (!empty($doimport)) {
190
            echo "<br/>($doimport)";
191
192
        }
        ?>
Petr Skoda's avatar
Petr Skoda committed
193
194
195
196
    </td>
    <td valign="top">
        <table border="0" cellspacing="0" width="100%" valign="top" cellpadding="0">
        <tr>
197
198
            <td nowrap="nowrap" align="left"><?php echo $printbook.$printchapter.$generateimscp ?></td>
            <td nowrap ="nowrap" align="right"><?php echo $chnavigation ?></td>
Petr Skoda's avatar
Petr Skoda committed
199
200
201
202
203
204
205
206
207
        </tr>
        </table>
    </td>
</tr>

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

<?php
print_footer($course);

?>