} else {
    print_error('nopermissions', 'error', $workshopplus->view_url(), 'view or manage example submission');
}
if ($id and $delete and $confirm and $canmanage) {
    require_sesskey();
    $workshopplus->delete_submission($example);
    redirect($workshopplus->view_url());
}
if ($id and $assess and $canmanage) {
    // reference assessment of an example is the assessment with the weight = 1. There should be just one
    // such assessment
    require_sesskey();
    if (!$refasid) {
        $refasid = $workshopplus->add_allocation($example, $USER->id, 1);
    }
    redirect($workshopplus->exassess_url($refasid));
}
if ($id and $assess and $canassess) {
    // training assessment of an example is the assessment with the weight = 0
    require_sesskey();
    $asid = $DB->get_field('workshopplus_assessments', 'id', array('submissionid' => $example->id, 'weight' => 0, 'reviewerid' => $USER->id));
    if (!$asid) {
        $asid = $workshopplus->add_allocation($example, $USER->id, 0);
    }
    if ($asid == workshopplus::ALLOCATION_EXISTS) {
        // the training assessment of the example was not found but the allocation already
        // exists. this probably means that the user is the author of the reference assessment.
        echo $output->header();
        echo $output->box(get_string('assessmentreferenceconflict', 'workshopplus'));
        echo $output->continue_button($workshopplus->view_url());
        echo $output->footer();
 */
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once dirname(__FILE__) . '/locallib.php';
$asid = required_param('asid', PARAM_INT);
// assessment id
$assessment = $DB->get_record('workshopplus_assessments', array('id' => $asid), '*', MUST_EXIST);
$example = $DB->get_record('workshopplus_submissions', array('id' => $assessment->submissionid, 'example' => 1), '*', MUST_EXIST);
$workshopplus = $DB->get_record('workshopplus', array('id' => $example->workshopplusid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $workshopplus->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('workshopplus', $workshopplus->id, $course->id, false, MUST_EXIST);
require_login($course, false, $cm);
if (isguestuser()) {
    print_error('guestsarenotallowed');
}
$workshopplus = new workshopplus($workshopplus, $cm, $course);
$PAGE->set_url($workshopplus->exassess_url($assessment->id));
$PAGE->set_title($workshopplus->name);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add(get_string('assessingexample', 'workshopplus'));
$currenttab = 'assessment';
$canmanage = has_capability('mod/workshopplus:manageexamples', $workshopplus->context);
$isreviewer = $USER->id == $assessment->reviewerid;
if ($isreviewer or $canmanage) {
    // such a user can continue
} else {
    print_error('nopermissions', 'error', $workshopplus->view_url(), 'assess example submission');
}
// only the reviewer is allowed to modify the assessment
if ($canmanage and $assessment->weight == 1 or $isreviewer and $workshopplus->assessing_examples_allowed()) {
    $assessmenteditable = true;
} else {
$PAGE->set_title($workshopplus->name);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add(get_string('examplecomparing', 'workshopplus'));
// Output starts here
$output = $PAGE->get_renderer('mod_workshopplus');
echo $output->header();
echo $output->heading(format_string($workshopplus->name));
echo $output->heading(get_string('assessedexample', 'workshopplus'), 3);
echo $output->render($workshopplus->prepare_example_submission($example));
// if the reference assessment is available, display it
if (!empty($mformreference)) {
    $options = array('showreviewer' => false, 'showauthor' => false, 'showform' => true);
    $reference = $workshopplus->prepare_example_reference_assessment($reference, $mformreference, $options);
    $reference->title = get_string('assessmentreference', 'workshopplus');
    if ($canmanage) {
        $reference->url = $workshopplus->exassess_url($reference->id);
    }
    echo $output->render($reference);
}
if ($isreviewer) {
    $options = array('showreviewer' => true, 'showauthor' => false, 'showform' => true);
    $assessment = $workshopplus->prepare_example_assessment($assessment, $mformassessment, $options);
    $assessment->title = get_string('assessmentbyyourself', 'workshopplus');
    if ($workshopplus->assessing_examples_allowed()) {
        $assessment->add_action(new moodle_url($workshopplus->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey())), get_string('reassess', 'workshopplus'));
    }
    echo $output->render($assessment);
} elseif ($canmanage) {
    $options = array('showreviewer' => true, 'showauthor' => false, 'showform' => true);
    $assessment = $workshopplus->prepare_example_assessment($assessment, $mformassessment, $options);
    echo $output->render($assessment);