$geogebra = $DB->get_record('geogebra', array('id' => $cm->instance), '*', MUST_EXIST);
} elseif ($n) {
    $geogebra = $DB->get_record('geogebra', array('id' => $n), '*', MUST_EXIST);
    $course = $DB->get_record('course', array('id' => $geogebra->course), '*', MUST_EXIST);
    $cm = get_coursemodule_from_instance('geogebra', $geogebra->id, $course->id, false, MUST_EXIST);
} else {
    print_error('You must specify a course_module ID or an instance ID');
}
$context = context_module::instance($cm->id);
//Activity was sended before the applet was fully loaded
parse_str($vars, $parsedVars);
if (empty($vars)) {
    print_error('The applet has not sent correct data');
}
require_login($course, true, $cm);
$attempt = geogebra_get_unfinished_attempt($geogebra->id, $USER->id);
if ($geogebra->autograde == 0) {
    //If geogebra is not autograding, change grade from 0 to undefined or get the one specified by the teacher
    $grade = '-1';
    if ($attempt) {
        parse_str($attempt->vars, $attemptVars);
        $grade = $attemptVars['grade'];
    }
    $vars = http_build_query(array('grade' => $grade, 'duration' => $parsedVars['duration'], 'attempts' => $parsedVars['attempts'], 'state' => $parsedVars['state']), '', '&');
    parse_str($vars, $parsedVars);
} else {
    $vars = http_build_query(array('grade' => round($parsedVars['grade'], 2), 'duration' => $parsedVars['duration'], 'attempts' => $parsedVars['attempts'], 'state' => $parsedVars['state']), '', '&');
    parse_str($vars, $parsedVars);
}
if ($attempt) {
    //Exists an unfishined attempt
/**
 * Display the geogebra applet
 *
 * @param type $geogebra
 * @param type $cm
 * @param type $context
 * @param type $attempt
 * @param type $ispreview if is a preview or not
 * @param type $timenow
 */
function geogebra_view_applet($geogebra, $cm, $context, $attempt = null, $ispreview = false)
{
    global $OUTPUT, $PAGE, $CFG, $USER;
    $timenow = time();
    if ($attempt) {
        $viewmode = 'view';
        $userid = $attempt->userid;
    } else {
        $userid = $USER->id;
        if ($ispreview) {
            $viewmode = 'preview';
        } else {
            $viewmode = 'submit';
        }
    }
    $isopen = empty($geogebra->timeavailable) || $geogebra->timeavailable < $timenow;
    if (!$isopen) {
        $content .= $OUTPUT->notify(get_string('notopenyet', 'geogebra', userdate($geogebra->timeavailable)));
        if (!$ispreview) {
            return $content;
        }
    }
    $isclosed = !empty($geogebra->timedue) && $geogebra->timedue < $timenow;
    if ($isclosed) {
        $content .= $OUTPUT->notify(get_string('expired', 'geogebra', userdate($geogebra->timedue)));
        if (!$ispreview) {
            return $content;
        }
    }
    $attempts = geogebra_count_finished_attempts($geogebra->id, $userid);
    if ($ispreview || $geogebra->maxattempts < 0 || $attempts < $geogebra->maxattempts) {
        // Show results when viewmode is "view"
        if (!empty($attempt)) {
            // TODO: Change $USER by selected userid
            geogebra_view_userid_results($geogebra, $userid, $cm, $context, $viewmode, $attempt);
        } else {
            if (!$ispreview) {
                echo $OUTPUT->box_start('generalbox');
                if ($geogebra->maxattempts < 0) {
                    echo get_string('unlimitedattempts', 'geogebra') . '<br/>';
                } else {
                    if ($attempts == $geogebra->maxattempts - 1) {
                        echo get_string('lastattemptremaining', 'geogebra') . '<br/>';
                    } else {
                        echo get_string('attemptsremaining', 'geogebra') . ($geogebra->maxattempts - $attempts) . '<br/>';
                    }
                }
                // If there is some unfinished attempt, show it
                $attempt = geogebra_get_unfinished_attempt($geogebra->id, $userid);
                if (!empty($attempt)) {
                    echo '(' . get_string('resumeattempt', 'geogebra') . ')';
                }
                echo $OUTPUT->box_end();
            }
        }
        geogebra_print_content($geogebra, $context);
        // If not preview mode, load state
        $parsedvars = null;
        if ($attempt) {
            parse_str($attempt->vars, $parsedvars);
        }
        if (isset($parsedvars['state'])) {
            // Continue previuos attempt
            $eduxtecadapterparameters = http_build_query(array('state' => $parsedvars['state'], 'grade' => $parsedvars['grade'], 'duration' => $parsedvars['duration'], 'attempts' => $parsedvars['attempts']), '', '&');
        } else {
            // New attempt
            $attempts = geogebra_count_finished_attempts($geogebra->id, $userid) + 1;
            $eduxtecadapterparameters = http_build_query(array('attempts' => $attempts), '', '&');
        }
        $PAGE->requires->js('/mod/geogebra/geogebra_view.js');
        echo '<form id="geogebra_form" method="POST" action="attempt.php">';
        echo '<input type="hidden" name="appletInformation" />';
        echo '<input type="hidden" name="id" value="' . $context->instanceid . '"/>';
        echo '<input type="hidden" name="n" value="' . $geogebra->id . '"/>';
        echo '<input type="hidden" name="f" value="0"/>';
        // Only show submit buttons if not view mode
        if (empty($attempt) || !$attempt->finished) {
            echo '<input id="geogebra_form_save" type="button" value="' . get_string('savewithoutsubmitting', 'geogebra') . '" />';
            echo '<input id="geogebra_form_submit" type="button" value="' . get_string('submitandfinish', 'geogebra') . '" />';
        }
        echo '<input type="hidden" name="prevAppletInformation" value="' . $eduxtecadapterparameters . '" />';
        echo ' </form>';
    } else {
        echo $OUTPUT->box(get_string('msg_noattempts', 'geogebra'), 'generalbox boxaligncenter');
    }
    geogebra_view_dates($geogebra, $context, $timenow);
}