. /** * Edit an outage. * * @package auth_outage * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; use auth_outage\local\outage; use auth_outage\local\outagelib; use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); admin_externalpage_setup('auth_outage_manage'); $PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new edit(); if ($mform->is_cancelled()) { redirect(new moodle_url('/auth/outage/manage.php')); } else if ($outage = $mform->get_data()) { $id = outagedb::save($outage); redirect(new moodle_url('/auth/outage/manage.php')); } $clone = optional_param('clone', 0, PARAM_INT); $edit = optional_param('edit', 0, PARAM_INT); if ($clone && $edit) { throw new invalid_parameter_exception('Cannot provide both clone and edit ids.'); } if ($clone) { // Remove outage id to force creating a new one. $outage = outagedb::get_by_id($clone); $outage->id = null; $action = 'outageclone'; } else if ($edit) { $outage = outagedb::get_by_id($edit); $action = 'outageedit'; } else { $config = outagelib::get_config(); $time = time(); $default = $config->default_time; if ($default) { // First try natural language parsing. $time = strtotime($default); // Lean on the Task API to convert the cron syntax to // the next valid outage date and time. $parts = explode(' ', $default); if (count($parts) == 5) { $task = new \core\task\calendar_cron_task(); $task->set_minute($parts[0]); $task->set_hour($parts[1]); $task->set_day($parts[2]); $task->set_month($parts[3]); $task->set_day_of_week($parts[4]); $time = $task->get_next_scheduled_time(); } } $outage = new outage([ 'autostart' => $config->default_autostart, 'starttime' => $time, 'stoptime' => $time + $config->default_duration, 'warntime' => $time - $config->default_warning_duration, 'title' => $config->default_title, 'description' => $config->default_description, ]); $action = 'outagecreate'; } if ($outage == null) { throw new invalid_parameter_exception('Outage not found.'); } $mform->set_data($outage); $PAGE->navbar->add(get_string($action.'crumb', 'auth_outage')); echo $OUTPUT->header(); echo renderer::get()->rendersubtitle($action); $mform->display(); echo $OUTPUT->footer();