//$PAGE->set_context($context);
// other things you may want to set - remove if not needed
$PAGE->set_cacheable(false);
//$PAGE->set_focuscontrol('tb1');
//$PAGE->add_body_class('mootyper-'.$somevar);
// Output starts here
echo $OUTPUT->header();
require_once dirname(__FILE__) . '/locallib.php';
$lessonPO = optional_param('lesson', 0, PARAM_INT);
$jlnk2 = $CFG->wwwroot . '/mod/mootyper/eins.php?id=' . $id;
echo '<a href="' . $jlnk2 . '">' . get_string('eaddnew', 'mootyper') . '</a><br><br>';
//$lessons = get_typerlessons();
/*if(has_capability('mod/mootyper:editall', context_module::instance($id)))
	$lessons = get_typerlessons();
else */
$lessons = get_mootyperlessons($USER->id, $id);
if ($lessonPO == 0 && count($lessons) > 0) {
    $lessonPO = $lessons[0]['id'];
}
echo '<form method="post">';
echo get_string('excategory', 'mootyper') . ': <select onchange="this.form.submit()" name="lesson">';
$selected_lesson_index = 0;
for ($ij = 0; $ij < count($lessons); $ij++) {
    if ($lessons[$ij]['id'] == $lessonPO) {
        echo '<option selected="true" value="' . $lessons[$ij]['id'] . '">' . $lessons[$ij]['lessonname'] . '</option>';
        $selected_lesson_index = $ij;
    } else {
        echo '<option value="' . $lessons[$ij]['id'] . '">' . $lessons[$ij]['lessonname'] . '</option>';
    }
}
echo '</select>';
$htmlout .= '<script type="text/javascript">
function removeAtts()
{
	document.getElementById("lesson").disabled = false;
	document.getElementById("mode").disabled = false;	
	document.getElementById("exercise").disabled = false;
}
</script>';
$htmlout .= '<form id="setupform" onsubmit="removeAtts();" name="setupform" method="POST">';
$disSelect = $ePO == 1 ? ' disabled="disabled"' : '';
$htmlout .= '<table><tr><td>' . get_string('fmode', 'mootyper') . '</td><td><select' . $disSelect . ' onchange="this.form.submit()" name="mode" id="mode">';
//$lessons = get_typerlessons();
if (has_capability('mod/mootyper:editall', context_module::instance($cm->id))) {
    $lessons = get_typerlessons();
} else {
    $lessons = get_mootyperlessons($USER->id, $course->id);
}
if ($modePO == 0 || is_null($modePO)) {
    $htmlout .= '<option selected="true" value="0">' . get_string('sflesson', 'mootyper') . '</option><option value="1">' . get_string('isexamtext', 'mootyper') . '</option>';
    $htmlout .= '</select></td></tr><tr><td>';
    $htmlout .= get_string('excategory', 'mootyper') . '</td><td><select' . $disSelect . ' onchange="this.form.submit()" id="lesson" name="lesson">';
    for ($ij = 0; $ij < count($lessons); $ij++) {
        if ($lessons[$ij]['id'] == $lessonPO) {
            $htmlout .= '<option selected="true" value="' . $lessons[$ij]['id'] . '">' . $lessons[$ij]['lessonname'] . '</option>';
        } else {
            $htmlout .= '<option value="' . $lessons[$ij]['id'] . '">' . $lessons[$ij]['lessonname'] . '</option>';
        }
    }
    $htmlout .= '</select></td></tr><tr><td>' . get_string('requiredgoal', 'mootyper') . '</td><td><input value="' . $goalPO . '" style="width: 20px;" type="text" name="requiredgoal"> % </td></tr>';
} else {
    if ($modePO == 1) {