// Unset all page parameters.
unset($id);
unset($bid);
unset($chapterid);
// Security checks  END.
add_to_log($course->id, 'giportfolio', 'view', 'viewgiportfolio.php?id=' . $cm->id . '&chapterid=' . $chapter->id, $giportfolio->id, $cm->id);
// Read standard strings.
$strgiportfolios = get_string('modulenameplural', 'mod_giportfolio');
$strgiportfolio = get_string('modulename', 'mod_giportfolio');
$strtoc = get_string('toc', 'mod_giportfolio');
// Prepare header.
$PAGE->set_title(format_string($giportfolio->name));
$PAGE->add_body_class('mod_giportfolio');
$PAGE->set_heading(format_string($course->fullname));
// Synergy add $userdit.
giportfolio_add_fake_block($chapters, $chapter, $giportfolio, $cm, $edit, $useredit);
// Prepare chapter navigation icons.
$previd = null;
$nextid = null;
$last = null;
foreach ($chapters as $ch) {
    if (!$edit and $ch->hidden) {
        continue;
    }
    if ($last == $chapter->id) {
        $nextid = $ch->id;
        break;
    }
    if ($ch->id != $chapter->id) {
        $previd = $ch->id;
    }
// Read chapters.
$chapters = giportfolio_preload_chapters($giportfolio);
// Add fake user chapters.
$additionalchapters = giportfolio_preload_userchapters($giportfolio, $userid = null);
if ($additionalchapters) {
    $chapters = $chapters + $additionalchapters;
}
$chapter = $DB->get_record('giportfolio_chapters', array('id' => $chapterid, 'giportfolioid' => $giportfolio->id));
if (!$chapter) {
    $chapter = $DB->get_record('giportfolio_userchapters', array('id' => $chapterid, 'giportfolioid' => $giportfolio->id, 'iduser' => $USER->id), '*', MUST_EXIST);
}
// Chapter is hidden for students.
if ($chapter->hidden) {
    require_capability('mod/giportfolio:viewhiddenchapters', $context);
}
giportfolio_add_fake_block($chapters, $chapter, $giportfolio, $cm, 0, 0);
// Add TOC.
$editoroptions = array('noclean' => true, 'subdirs' => true, 'maxfiles' => -1, 'maxbytes' => 0, 'context' => $context);
$attachmentoptions = array('subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
$contribution = null;
if ($contributionid) {
    $contribution = $DB->get_record('giportfolio_contributions', array('id' => $contributionid, 'chapterid' => $chapterid, 'userid' => $USER->id), '*', MUST_EXIST);
    $formdata = clone $contribution;
    $formdata = file_prepare_standard_editor($formdata, 'content', $editoroptions, $context, 'mod_giportfolio', 'contribution', $formdata->id);
    $formdata = file_prepare_standard_filemanager($formdata, 'attachment', $attachmentoptions, $context, 'mod_giportfolio', 'attachment', $formdata->id);
    $formdata->contributionid = $formdata->id;
} else {
    $formdata = new stdClass();
}
$formdata->id = $cm->id;
$formdata->chapterid = $chapter->id;