* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2 * @category Stud.IP */ class TimedAdmission extends AdmissionRule { // --- ATTRIBUTES --- /** * End of course admission. */ public $endTime = 0; /** * Start of course admission. */ public $startTime = 0; // --- OPERATIONS --- /** * Standard constructor * * @param String ruleId */ public function __construct($ruleId='', $courseSetId = '') { parent::__construct($ruleId, $courseSetId); $this->default_message = _('Sie befinden sich nicht innerhalb des Anmeldezeitraums.'); if ($ruleId) { $this->load(); } else { $this->id = $this->generateId('timedadmissions'); } } /** * Deletes the admission rule and all associated data. */ public function delete() { parent::delete(); // Delete rule data. $stmt = DBManager::get()->prepare("DELETE FROM `timedadmissions` WHERE `rule_id`=?"); $stmt->execute([$this->id]); } /** * Gets some text that describes what this AdmissionRule (or respective * subclass) does. */ public static function getDescription() { return _("Anmelderegeln dieses Typs legen ein Zeitfenster fest, in ". "dem die Anmeldung zu Veranstaltungen möglich ist. Es kann auch ". "nur ein Start- oder Endzeitpunkt angegeben werden."); } /** * Gets the end of course admission. * * @return int */ public function getEndTime() { return $this->endTime; } /** * Return this rule's name. */ public static function getName() { return _("Zeitgesteuerte Anmeldung"); } /** * Gets the start of course admission. * * @return int */ public function getStartTime() { return $this->startTime; } /** * Helper function for loading rule definition from database. */ public function load() { // Load data. $stmt = DBManager::get()->prepare("SELECT * FROM `timedadmissions` WHERE `rule_id`=? LIMIT 1"); $stmt->execute([$this->id]); $current = $stmt->fetchOne(); if ($current) { $this->message = $current['message']; $this->startTime = $current['start_time']; $this->endTime = $current['end_time']; } else { $this->id = $this->generateId('timedadmissions'); } } /** * Is admission allowed according to the defined time frame? * * @param String userId * @param String courseId * @return Array */ public function ruleApplies($userId, $courseId) { $errors = []; if (!$this->checkTimeFrame()) { $errors[] = $this->getMessage(); } return $errors; } /** * Uses the given data to fill the object values. This can be used * as a generic function for storing data if the concrete rule type * isn't known in advance. * * @param Array $data * @return AdmissionRule This object. */ public function setAllData($data) { parent::setAllData($data); if ($data['starttime']) { $this->setStartTime($data['starttime']); } if ($data['endtime']) { $this->setEndTime($data['endtime']); } return $this; } /** * Sets a new end timestamp for course admission. * * @param int newEndTime * @return TimedAdmission */ public function setEndTime($newEndTime) { $this->endTime = $newEndTime; return $this; } /** * Sets a new start timestamp for course admission. * * @param int newStartTime * @return TimedAdmission */ public function setStartTime($newStartTime) { $this->startTime = $newStartTime; return $this; } /** * Store rule definition to database. */ public function store() { // Store data. $stmt = DBManager::get()->prepare("INSERT INTO `timedadmissions` (`rule_id`, `message`, `start_time`, `end_time`, `mkdate`, `chdate`) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `start_time`=VALUES(`start_time`), `end_time`=VALUES(`end_time`),message=VALUES(message), `chdate`=VALUES(`chdate`)"); $stmt->execute([$this->id, $this->message, (int)$this->startTime, (int)$this->endTime, time(), time()]); } /** * A textual description of the current rule. * * @return String */ public function toString() { $factory = new Flexi\Factory(dirname(__FILE__).'/templates/'); $tpl = $factory->open('info'); $tpl->set_attribute('rule', $this); return $tpl->render(); } /** * Validates if the given request data is sufficient to configure this rule * (e.g. if required values are present). * * @param Array Request data * @return Array Error messages. */ public function validate($data) { $errors = parent::validate($data); if (!$data['starttime'] && !$data['endtime']) { $errors[] = _('Bitte geben Sie entweder ein Start- oder Enddatum an.'); } if ($data['starttime'] && $data['endtime'] && $data['endtime'] < $data['starttime']) { $errors[] = _('Das Enddatum darf nicht vor dem Startdatum liegen.'); } return $errors; } /** * Get fields and settings defining this admission rule as array. */ public function getPayload(): array { return array_merge( parent::getPayload(), [ 'starttime' => $this->getStartTime(), 'endtime' => $this->getEndTime() ] ); } } /* end of class TimedAdmission */ ?>