/**
 * Generate user toc structure
 *
 * @param array $chapters
 * @param stdClass $chapter
 * @param stdClass $giportfolio
 * @param stdClass $cm
 * @param bool $edit
 * @param $userid
 * @param $useredit
 * @return string
 */
function giportfolio_get_usertoc($chapters, $chapter, $giportfolio, $cm, $edit, $userid, $useredit)
{
    global $USER, $OUTPUT;
    $toc = '';
    // Representation of toc (HTML).
    $nch = 0;
    // Chapter number.
    $ns = 0;
    // Subchapter number.
    $first = 1;
    $context = context_module::instance($cm->id);
    // SYNERGY - add 'giportfolio-toc' ID.
    $tocid = ' id="giportfolio-toc" ';
    switch ($giportfolio->numbering) {
        case PORTFOLIO_NUM_NONE:
            $toc .= '<div class="giportfolio_toc_none" ' . $tocid . '>';
            break;
        case PORTFOLIO_NUM_NUMBERS:
            $toc .= '<div class="giportfolio_toc_numbered" ' . $tocid . '>';
            break;
        case PORTFOLIO_NUM_BULLETS:
            $toc .= '<div class="giportfolio_toc_bullets" ' . $tocid . '>';
            break;
        case PORTFOLIO_NUM_INDENTED:
            $toc .= '<div class="giportfolio_toc_indented" ' . $tocid . '>';
            break;
    }
    // SYNERGY - add 'giportfolio-toc' ID.
    $allowuser = giportfolio_get_collaborative_status($giportfolio);
    if ($allowuser && $useredit) {
        // Edit students view.
        $toc .= '<ul>';
        $i = 0;
        // SYNERGY - Find the open chapter.
        $currentch = 0;
        $opench = 0;
        foreach ($chapters as $ch) {
            if (!$currentch || !$ch->subchapter) {
                $currentch = $ch->id;
            }
            if ($ch->id == $chapter->id) {
                $opench = $currentch;
                break;
            }
        }
        // SYNERGY - Find the open chapter.
        echo '<br/>';
        foreach ($chapters as $ch) {
            $i++;
            $title = trim(format_string($ch->title, true, array('context' => $context)));
            if (!$ch->subchapter) {
                $toc .= $first ? '<li>' : '</ul></li><li>';
                if (!$ch->hidden) {
                    $nch++;
                    $ns = 0;
                    // SYNERGY - Make sure the right subchapters are expanded by default.
                    $li = '<li>';
                    if ($ch->id == $opench || !$giportfolio->collapsesubchapters) {
                        $li = '<li class="expanded">';
                    }
                    $toc .= $first ? $li : '</ul></li>' . $li;
                    // SYNERGY - Make sure the right subchapters are expanded by default.
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "{$nch} {$title}";
                    }
                } else {
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "x {$title}";
                    }
                    $title = '<span class="dimmed_text">' . $title . '</span>';
                }
            } else {
                $toc .= $first ? '<li><ul><li>' : '<li>';
                if (!$ch->hidden) {
                    $ns++;
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "{$nch}.{$ns} {$title}";
                    }
                } else {
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "x.x {$title}";
                    }
                    $title = '<span class="dimmed_text">' . $title . '</span>';
                }
            }
            if ($ch->id == $chapter->id) {
                $toc .= '<strong>' . $title . '</strong>';
            } else {
                $toc .= '<a title="' . s($title) . '" href="viewgiportfolio.php?id=' . $cm->id . '&amp;chapterid=' . $ch->id . '&amp;useredit=1' . '">' . $title . '</a>';
            }
            $toc .= '&nbsp;&nbsp;';
            if (giportfolio_check_user_chapter($giportfolio->id, $ch->id, $userid)) {
                if ($i != 1) {
                    if (!giportfolio_get_first_userchapter($giportfolio->id, $ch->id, $userid)) {
                        $toc .= ' <a title="' . get_string('up') . '" href="moveuserchapter.php?id=' . $cm->id . '&amp;chapterid=' . $ch->id . '&amp;up=1&amp;sesskey=' . $USER->sesskey . '">
                            <img src="' . $OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="' . get_string('up') . '" /></a>';
                    }
                }
                if ($i != count($chapters)) {
                    $toc .= ' <a title="' . get_string('down') . '" href="moveuserchapter.php?id=' . $cm->id . '&amp;chapterid=' . $ch->id . '&amp;up=0&amp;sesskey=' . $USER->sesskey . '">
                        <img src="' . $OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="' . get_string('down') . '" /></a>';
                }
            }
            if (giportfolio_check_user_chapter($giportfolio->id, $ch->id, $userid)) {
                $toc .= ' <a title="' . get_string('edit') . '" href="editstudent.php?cmid=' . $cm->id . '&amp;id=' . $ch->id . '">
                <img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' . get_string('edit') . '" /></a>';
            }
            if (giportfolio_check_user_chapter($giportfolio->id, $ch->id, $userid)) {
                $toc .= ' <a title="' . get_string('delete') . '" href="deleteuserchapter.php?id=' . $cm->id . '&amp;chapterid=' . $ch->id . '&amp;sesskey=' . $USER->sesskey . '">
                    <img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' . get_string('delete') . '" /></a>';
            }
            if (giportfolio_check_user_chapter($giportfolio->id, $ch->id, $userid) || giportfolio_get_last_chapter($giportfolio->id, $ch->id)) {
                $toc .= ' <a title="' . get_string('addafter', 'mod_giportfolio') . '" href="editstudent.php?cmid=' . $cm->id . '&amp;pagenum=' . $ch->pagenum . '&amp;subchapter=' . $ch->subchapter . '">
                    <img src="' . $OUTPUT->pix_url('add', 'mod_giportfolio') . '" class="iconsmall" alt="' . get_string('addafter', 'mod_giportfolio') . '" /></a>';
            }
            $toc .= !$ch->subchapter ? '<ul>' : '</li>';
            $first = 0;
        }
        $toc .= '</ul></li></ul>';
    } else {
        // Normal stdent nonediting view.
        $toc .= '<ul>';
        // SYNERGY - Find the open chapter.
        $currentch = 0;
        $opench = 0;
        foreach ($chapters as $ch) {
            if (!$currentch || !$ch->subchapter) {
                $currentch = $ch->id;
            }
            if ($ch->id == $chapter->id) {
                $opench = $currentch;
                break;
            }
        }
        // SYNERGY - Find the open chapter.
        foreach ($chapters as $ch) {
            $title = trim(format_string($ch->title, true, array('context' => $context)));
            if (!$ch->hidden) {
                if (!$ch->subchapter) {
                    $nch++;
                    $ns = 0;
                    // SYNERGY - Make sure the right subchapters are expanded by default.
                    $li = '<li>';
                    if ($ch->id == $opench || !$giportfolio->collapsesubchapters) {
                        $li = '<li class="expanded">';
                    }
                    $toc .= $first ? $li : '</ul></li>' . $li;
                    // SYNERGY - Make sure the right subchapters are expanded by default.
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "{$nch} {$title}";
                    }
                } else {
                    $ns++;
                    $toc .= $first ? '<li><ul><li>' : '<li>';
                    if ($giportfolio->numbering == PORTFOLIO_NUM_NUMBERS) {
                        $title = "{$nch}.{$ns} {$title}";
                    }
                }
                if ($ch->id == $chapter->id) {
                    $toc .= '<strong>' . $title . '</strong>';
                } else {
                    $toc .= '<a title="' . s($title) . '" href="viewgiportfolio.php?id=' . $cm->id . '&amp;chapterid=' . $ch->id . '">' . $title . '</a>';
                }
                $toc .= !$ch->subchapter ? '<ul>' : '</li>';
                $first = 0;
            }
        }
        $toc .= '</ul></li></ul>';
    }
    $toc .= '</div>';
    $toc = str_replace('<ul></ul>', '', $toc);
    // Cleanup of invalid structures.
    return $toc;
}
$chapterid = optional_param('chapterid', 0, PARAM_INT);
// Chapter ID.
// Security checks START - teachers and students view.
$cm = get_coursemodule_from_id('giportfolio', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$giportfolio = $DB->get_record('giportfolio', array('id' => $cm->instance), '*', MUST_EXIST);
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/giportfolio:view', $context);
require_capability('giportfoliotool/print:print', $context);
// Check all variables.
if ($chapterid && !giportfolio_check_user_chapter($giportfolio->id, $chapterid, $USER->id)) {
    // Single chapter printing - only visible!
    $chapter = $DB->get_record('giportfolio_chapters', array('id' => $chapterid, 'giportfolioid' => $giportfolio->id), '*', MUST_EXIST);
} else {
    if ($chapterid && giportfolio_check_user_chapter($giportfolio->id, $chapterid, $USER->id)) {
        $chapter = $DB->get_record('giportfolio_userchapters', array('id' => $chapterid, 'giportfolioid' => $giportfolio->id, 'iduser' => $USER->id), '*', MUST_EXIST);
    } else {
        // Complete giportfolio.
        $chapter = false;
    }
}
$PAGE->set_url('/mod/giportfolio/print.php', array('id' => $id, 'chapterid' => $chapterid));
unset($id);
unset($chapterid);
// Security checks END.
// Read chapters.
$chapters = giportfolio_preload_chapters($giportfolio);
$additionalchapters = giportfolio_preload_userchapters($giportfolio, $userid = null);
if ($additionalchapters) {
    $chapters = $chapters + $additionalchapters;