* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2 * @category Stud.IP */ class TermsAdmission extends AdmissionRule { // Terms of admission public $terms; /** * Standard constructor. * * @param String ruleId */ public function __construct($ruleId = '', $courseSetId = '') { parent::__construct($ruleId, $courseSetId); if ($ruleId) { $this->load(); } else { $this->id = $this->generateId('termsadmissions'); } } /** * Deletes the admission rule and all associated data. */ public function delete() { parent::delete(); $stmt = DBManager::get()->prepare('DELETE FROM termsadmissions WHERE rule_id = ?'); $stmt->execute([$this->getId()]); } /** * Gets some text that describes what this AdmissionRule (or respective * subclass) does. */ public static function getDescription() { return _('Mit dieser Anmelderegel können Sie einen Kurs mit spezifischen Teilnahmebedingungen realisieren. ' . 'Die Anmeldung ist erst möglich, nachdem diese akzeptiert wurden.'); } /** * Shows an input form * * @return string A template-based input form. * @throws Flexi\TemplateNotFoundException */ public function getInput() { $factory = new Flexi\Factory(__DIR__ . '/templates'); $template = $factory->open('input'); $template->rule = $this; return (string) MessageBox::info($template->render())->hideClose(); } /** * Return this rule's name. */ public static function getName() { return _('Kurs mit Teilnahmebedingungen'); } /** * Does the current rule allow the given user to register as participant * in the given course? * * @param String userId * @param String courseId * @return Array */ public function ruleApplies($userId, $courseId) { $errors = []; // check if the user has accepted the terms if (Request::int('terms_accepted')) { $_SESSION['terms_accepted_' . $this->getId()] = true; } if (empty($_SESSION['terms_accepted_' . $this->getId()])) { $errors[] = _('Um sich anzumelden, müssen Sie die Teilnahmebedingungen akzeptieren.'); } 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); $this->terms = trim($data['terms']); return $this; } /** * Internal helper function for loading rule definition from database. */ public function load() { $rule = DBManager::get()->fetchOne('SELECT * FROM termsadmissions WHERE rule_id = ?', [$this->getId()]); if ($rule) { $this->terms = $rule['terms']; } else { $this->id = $this->generateId('termsadmissions'); } return $this; } /** * Store rule definition to database. */ public function store() { // Store data. $stmt = DBManager::get()->prepare('INSERT INTO termsadmissions (rule_id, terms, mkdate, chdate) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE terms = VALUES(terms), chdate = VALUES(chdate)'); $stmt->execute([$this->id, $this->terms, time(), time()]); } /** * A textual description of the current rule. * * @return String * @throws Flexi\TemplateNotFoundException */ public function toString() { $factory = new Flexi\Factory(__DIR__ . '/templates/'); $template = $factory->open('info'); $template->rule = $this; return $template->render(); } /** * Get fields and settings defining this admission rule as array. */ public function getPayload(): array { return array_merge( parent::getPayload(), [ 'terms' => $this->terms ] ); } }