1<?php
2
3/* Copyright (c) 2015 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4/* Copyright (c) 2019 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
5
6declare(strict_types = 1);
7
8/**
9 * Class ilStudyProgramme
10 *
11 * @author: Richard Klees <richard.klees@concepts-and-training.de>
12 * @author: Denis Klöpfer <richard.klees@concepts-and-training.de>
13 * @version: 0.1.0
14 */
15
16class ilStudyProgrammeSettings
17{
18
19    // There are two different modes the programs calculation of the learning
20    // progress can run in. It is also possible, that the mode is not defined
21    // yet.
22    const MODE_UNDEFINED = 0;
23
24    // User is successful if he collected enough points in the subnodes of
25    // this node.
26    const MODE_POINTS = 1;
27
28    // User is successful if he has the "completed" learning progress in any
29    // subobject.
30    const MODE_LP_COMPLETED = 2;
31
32    public static $MODES = array(
33        self::MODE_UNDEFINED,
34        self::MODE_POINTS,
35        self::MODE_LP_COMPLETED
36    );
37
38
39    // A program tree has a lifecycle during which it has three status.
40
41    // The program is a draft, that is users won't be assigned to the program
42    // already.
43    const STATUS_DRAFT = 10;
44
45    // The program is active, that is used can be assigned to it.
46    const STATUS_ACTIVE = 20;
47
48    // The program is outdated, that is user won't be assigned to it but can
49    // still complete the program.
50    const STATUS_OUTDATED = 30;
51
52    const NO_RESTART = -1;
53    const NO_VALIDITY_OF_QUALIFICATION_PERIOD = -1;
54
55    // Defaults
56    const DEFAULT_POINTS = 100;
57    const DEFAULT_SUBTYPE = 0; // TODO: What should that be?
58
59    const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
60    const DATE_FORMAT = 'Y-m-d';
61
62    /**
63     * Id of this study program and the corresponding ILIAS-object as well.
64     *
65     * @var int
66     */
67    protected $obj_id;
68
69    /**
70     * Timestamp of the moment the last change was made on this object or any
71     * object in the subtree of the program.
72     *
73     * @var string
74     */
75    protected $last_change;
76
77    /**
78     * Mode the calculation of the learning progress on this node is run in.
79     *
80     * @var int
81     */
82    protected $lp_mode;
83
84    /**
85     * @var \ilStudyProgrammeTypeSettings
86     */
87    protected $type_settings;
88
89    /**
90     * @var \ilStudyProgrammeAssessmentSettings
91     */
92    protected $assessment_settings;
93
94    /**
95     * @var \ilStudyProgrammeDeadlineSettings
96     */
97    protected $deadline_settings;
98
99    /**
100     * @var \ilStudyProgrammeValidityOfAchievedQualificationSettings
101     */
102    protected $validity_of_qualification_settings;
103
104    /**
105     * Is the access control governed by positions?
106     *
107     * @var bool
108     */
109    protected $access_ctrl_positions;
110
111    /**
112     * @var \ilStudyProgrammeAutoMailSettings
113     */
114    protected $automail_settings;
115
116    public function __construct(
117        int $a_id,
118        \ilStudyProgrammeTypeSettings $type_settings,
119        \ilStudyProgrammeAssessmentSettings $assessment_settings,
120        \ilStudyProgrammeDeadlineSettings $deadline_settings,
121        \ilStudyProgrammeValidityOfAchievedQualificationSettings $validity_of_qualification_settings,
122        \ilStudyProgrammeAutoMailSettings $automail_settings
123    ) {
124        $this->obj_id = $a_id;
125        $this->type_settings = $type_settings;
126        $this->assessment_settings = $assessment_settings;
127        $this->deadline_settings = $deadline_settings;
128        $this->validity_of_qualification_settings = $validity_of_qualification_settings;
129        $this->automail_settings = $automail_settings;
130    }
131
132    /**
133     * Get the id of the study program.
134     *
135     * @return integer
136     */
137    public function getObjId() : int
138    {
139        return (int) $this->obj_id;
140    }
141
142    /**
143     * Get the timestamp of the last change on this program or a sub program.
144     *
145     * @return DateTime
146     */
147    public function getLastChange() : DateTime
148    {
149        return DateTime::createFromFormat(self::DATE_TIME_FORMAT, $this->last_change);
150    }
151
152    /**
153     * Update the last change timestamp to the current time.
154     *
155     * @return $this
156     */
157    public function updateLastChange() : ilStudyProgrammeSettings
158    {
159        $this->setLastChange(new DateTime());
160        return $this;
161    }
162
163    /**
164     * Set the last change timestamp to the given time.
165     *
166     * Throws when given time is smaller then current timestamp
167     * since that is logically impossible.
168     *
169     * @return $this
170     */
171    public function setLastChange(DateTime $a_timestamp) : ilStudyProgrammeSettings
172    {
173        $this->last_change = $a_timestamp->format(self::DATE_TIME_FORMAT);
174        return $this;
175    }
176
177    /**
178     * Set the lp mode.
179     *
180     * Throws when program is not in draft status.
181     *
182     * @param integer $a_mode       - one of self::$MODES
183     * @return $this
184     */
185    public function setLPMode(int $a_mode) : ilStudyProgrammeSettings
186    {
187        $a_mode = (int) $a_mode;
188        if (!in_array($a_mode, self::$MODES)) {
189            throw new ilException("ilStudyProgramme::setLPMode: No lp mode: "
190                                 . "'$a_mode'");
191        }
192        $this->lp_mode = $a_mode;
193        $this->updateLastChange();
194        return $this;
195    }
196
197    /**
198     * Get the lp mode.
199     *
200     * @return integer  - one of self::$MODES
201     */
202    public function getLPMode() : int
203    {
204        return (int) $this->lp_mode;
205    }
206
207    public function getTypeSettings() : \ilStudyProgrammeTypeSettings
208    {
209        return $this->type_settings;
210    }
211
212    public function withTypeSettings(
213        \ilStudyProgrammeTypeSettings $type_settings
214    ) : ilStudyProgrammeSettings {
215        $clone = clone $this;
216        $clone->type_settings = $type_settings;
217        return $clone;
218    }
219
220    public function getAssessmentSettings() : \ilStudyProgrammeAssessmentSettings
221    {
222        return $this->assessment_settings;
223    }
224
225    public function withAssessmentSettings(
226        \ilStudyProgrammeAssessmentSettings $assessment_settings
227    ) : ilStudyProgrammeSettings {
228        $clone = clone $this;
229        $clone->assessment_settings = $assessment_settings;
230        $clone->updateLastChange();
231        return $clone;
232    }
233
234    public function getDeadlineSettings() : \ilStudyProgrammeDeadlineSettings
235    {
236        return $this->deadline_settings;
237    }
238
239    public function withDeadlineSettings(
240        \ilStudyProgrammeDeadlineSettings $deadline_settings
241    ) : ilStudyProgrammeSettings {
242        $clone = clone $this;
243        $clone->deadline_settings = $deadline_settings;
244        return $clone;
245    }
246
247    public function getValidityOfQualificationSettings() : \ilStudyProgrammeValidityOfAchievedQualificationSettings
248    {
249        return $this->validity_of_qualification_settings;
250    }
251
252    public function withValidityOfQualificationSettings(
253        \ilStudyProgrammeValidityOfAchievedQualificationSettings $validity_of_qualification_settings
254    ) : ilStudyProgrammeSettings {
255        $clone = clone $this;
256        $clone->validity_of_qualification_settings = $validity_of_qualification_settings;
257        return $clone;
258    }
259
260    public function validationExpires() : bool
261    {
262        return !is_null($this->getValidityOfQualificationSettings()->getQualificationDate()) ||
263                $this->getValidityOfQualificationSettings()->getQualificationPeriod() != -1;
264    }
265
266    public function getAutoMailSettings() : \ilStudyProgrammeAutoMailSettings
267    {
268        return $this->automail_settings;
269    }
270
271    public function withAutoMailSettings(
272        \ilStudyProgrammeAutoMailSettings $automail_settings
273    ) : ilStudyProgrammeSettings {
274        $clone = clone $this;
275        $clone->automail_settings = $automail_settings;
276        return $clone;
277    }
278}
279