aboutsummaryrefslogtreecommitdiff
path: root/lib/admissionrules/termsadmission/TermsAdmission.php
blob: eb83dfcc2d07331a8e4abee4296c75991df26e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
 * TermsAdmission.php
 *
 * Represents a rule for course access with conditions of admission to be accepted.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Niklas Dettmer <ndettmer@uos.de>
 * @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');
    }

    /**
     * Gets the template that provides a configuration GUI for this rule.
     *
     * @return String
     * @throws Flexi\TemplateNotFoundException
     */
    public function getTemplate()
    {
        $factory = new Flexi\Factory(__DIR__ . '/templates');
        $template = $factory->open('configure');
        $template->rule = $this;

        return $template->render();
    }

    /**
     * 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 (!$_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()]);
        $this->terms = $rule['terms'];
        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();
    }
}