////////////end by Morteza
if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
    // ok you can go
} elseif ($submission->id and $ispublished) {
    // ok you can go
    $seenaspublished = true;
} elseif (is_null($submission->id) and $cansubmit) {
    // ok you can go
} elseif ($grouphassubmitted and $groupsubmission->id == $id) {
    // ok you can go
} else {
    print_error('nopermissions', 'error', $workshopplus->view_url(), 'view or create submission');
}
if ($assess and $submission->id and !$isreviewer and $canallocate and $workshopplus->assessing_allowed($USER->id)) {
    require_sesskey();
    $assessmentid = $workshopplus->add_allocation($submission, $USER->id);
    redirect($workshopplus->assess_url($assessmentid));
}
if ($edit) {
    require_once dirname(__FILE__) . '/submission_form.php';
    $maxfiles = $workshopplus->nattachments;
    $maxbytes = $workshopplus->maxbytes;
    $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes, 'context' => $workshopplus->context, 'return_types' => FILE_INTERNAL | FILE_EXTERNAL);
    $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes, 'return_types' => FILE_INTERNAL);
    $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshopplus->context, 'mod_workshopplus', 'submission_content', $submission->id);
    $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshopplus->context, 'mod_workshopplus', 'submission_attachment', $submission->id);
    $mform = new workshopplus_submission_form($PAGE->url, array('current' => $submission, 'workshopplus' => $workshopplus, 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
    //print_r("SUBMIT");
    //print_r($cansubmit);
    //print_r("SUBMIT");
    if ($mform->is_cancelled()) {
} elseif (is_null($example->id) and $canmanage) {
    // ok you can go
} 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'));