$phase = required_param('phase', PARAM_INT);
// the code of the new phase
$confirm = optional_param('confirm', false, PARAM_BOOL);
// confirmation
$cm = get_coursemodule_from_id('workshopplus', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$workshopplus = $DB->get_record('workshopplus', array('id' => $cm->instance), '*', MUST_EXIST);
$workshopplus = new workshopplus($workshopplus, $cm, $course);
$PAGE->set_url($workshopplus->switchphase_url($phase), array('cmid' => $cmid, 'phase' => $phase));
require_login($course, false, $cm);
require_capability('mod/workshopplus:switchphase', $PAGE->context);
if ($confirm) {
    if (!confirm_sesskey()) {
        throw new moodle_exception('confirmsesskeybad');
    }
    if (!$workshopplus->switch_phase($phase)) {
        print_error('errorswitchingphase', 'workshopplus', $workshopplus->view_url());
    }
    $workshopplus->log('update switch phase', $workshopplus->view_url(), $workshopplus->phase);
    redirect($workshopplus->view_url());
}
$PAGE->set_title($workshopplus->name);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add(get_string('switchingphase', 'workshopplus'));
//
// Output starts here
//
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($workshopplus->name));
echo $OUTPUT->confirm(get_string('switchphase' . $phase . 'info', 'workshopplus'), new moodle_url($PAGE->url, array('confirm' => 1)), $workshopplus->view_url());
echo $OUTPUT->footer();
/**
 * Regular jobs to execute via cron
 *
 * @return boolean true on success, false otherwise
 */
function workshopplus_cron()
{
    global $CFG, $DB;
    $now = time();
    mtrace(' processing workshopplus subplugins ...');
    cron_execute_plugin_type('workshopplusallocation', 'workshopplus allocation methods');
    // now when the scheduled allocator had a chance to do its job, check if there
    // are some workshoppluss to switch into the assessment phase
    $workshoppluss = $DB->get_records_select("workshopplus", "phase = 20 AND phaseswitchassessment = 1 AND submissionend > 0 AND submissionend < ?", array($now));
    if (!empty($workshoppluss)) {
        mtrace('Processing automatic assessment phase switch in ' . count($workshoppluss) . ' workshopplus(s) ... ', '');
        require_once $CFG->dirroot . '/mod/workshopplus/locallib.php';
        foreach ($workshoppluss as $workshopplus) {
            $cm = get_coursemodule_from_instance('workshopplus', $workshopplus->id, $workshopplus->course, false, MUST_EXIST);
            $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
            $workshopplus = new workshopplus($workshopplus, $cm, $course);
            $workshopplus->switch_phase(workshopplus::PHASE_ASSESSMENT);
            $workshopplus->log('update switch phase', $workshopplus->view_url(), $workshopplus->phase);
            // disable the automatic switching now so that it is not executed again by accident
            // if the teacher changes the phase back to the submission one
            $DB->set_field('workshopplus', 'phaseswitchassessment', 0, array('id' => $workshopplus->id));
            // todo inform the teachers
        }
        mtrace('done');
    }
    return true;
}
$eventdata = array();
$eventdata['objectid'] = $workshopplus->id;
$eventdata['context'] = $workshopplus->context;
$eventdata['courseid'] = $course->id;
$eventdata['other']['content'] = $workshopplus->phase;
$PAGE->set_url($workshopplus->view_url());
$event = \mod_workshopplus\event\course_module_viewed::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('workshopplus', $workshopplusrecord);
$event->add_record_snapshot('course_modules', $cm);
$event->set_page_detail();
$event->trigger();
// If the phase is to be switched, do it asap. This just has to happen after triggering
// the event so that the scheduled allocator had a chance to allocate submissions.
if ($workshopplus->phase == workshopplus::PHASE_SUBMISSION and $workshopplus->phaseswitchassessment and $workshopplus->submissionend > 0 and $workshopplus->submissionend < time()) {
    $workshopplus->switch_phase(workshopplus::PHASE_ASSESSMENT);
    $workshopplus->log('update switch phase', $workshopplus->view_url(), $workshopplus->phase);
    // Disable the automatic switching now so that it is not executed again by accident
    // if the teacher changes the phase back to the submission one.
    $DB->set_field('workshopplus', 'phaseswitchassessment', 0, array('id' => $workshopplus->id));
    $workshopplus->phaseswitchassessment = 0;
}
if (!is_null($editmode) && $PAGE->user_allowed_editing()) {
    $USER->editing = $editmode;
}
$PAGE->set_title($workshopplus->name);
$PAGE->set_heading($course->fullname);
if ($perpage and $perpage > 0 and $perpage <= 1000) {
    require_sesskey();
    set_user_preference('workshopplus_perpage', $perpage);
    redirect($PAGE->url);