ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTermsOfServiceDocument.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
10 const TABLE_NAME = 'tos_documents';
11
20 protected $id;
21
28 protected $creation_ts = 0;
29
36 protected $modification_ts = 0;
37
44 protected $owner_usr_id = 0;
45
52 protected $last_modified_usr_id = '';
53
60 protected $sorting = 0;
61
68 protected $title = '';
69
75 protected $text = '';
76
80 protected $criteria = [];
81
86
90 private $criteriaFetched = false;
91
95 public static function returnDbTableName()
96 {
97 return self::TABLE_NAME;
98 }
99
103 public function content() : string
104 {
105 return $this->text;
106 }
107
111 public function title() : string
112 {
113 return $this->title;
114 }
115
119 public function id() : int
120 {
121 return $this->id;
122 }
123
127 public function read()
128 {
129 parent::read();
130
131 $this->fetchAllCriterionAssignments();
132 }
133
137 public function buildFromArray(array $array)
138 {
139 $document = parent::buildFromArray($array);
140
141 $this->fetchAllCriterionAssignments();
142
143 return $document;
144 }
145
149 public function create()
150 {
151 $this->setCreationTs(time());
152
153 parent::create();
154
155 foreach ($this->criteria as $criterionAssignment) {
157 $criterionAssignment->setDocId($this->getId());
158 $criterionAssignment->store();
159 }
160
161 $this->initialPersistedCriteria = $this->criteria;
162 }
163
167 public function update()
168 {
169 $this->setModificationTs(time());
170
171 foreach ($this->criteria as $criterionAssignment) {
173 $criterionAssignment->setDocId($this->getId());
174 $criterionAssignment->store();
175 }
176
177 foreach ($this->initialPersistedCriteria as $criterionAssignment) {
179 $found = array_filter($this->criteria, function (\ilTermsOfServiceDocumentCriterionAssignment $criterionToMatch) use ($criterionAssignment) {
180 return $criterionToMatch->getId() == $criterionAssignment->getId();
181 });
182
183 if (0 === count($found)) {
184 $criterionAssignment->delete();
185 }
186 }
187
188 $this->initialPersistedCriteria = $this->criteria;
189
191 }
192
193
197 public function delete()
198 {
199 foreach ($this->initialPersistedCriteria as $criterionAssignment) {
201 $criterionAssignment->delete();
202 }
203
204 $this->initialPersistedCriteria = $this->criteria = [];
205
206 parent::delete();
207 }
208
212 public function criteria() : array
213 {
214 return $this->criteria;
215 }
216
221 public function attachCriterion(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
222 {
223 foreach ($this->criteria as $currentAssignment) {
225 if ($currentAssignment->equals($criterionAssignment)) {
226 throw new \ilTermsOfServiceDuplicateCriterionAssignmentException(sprintf(
227 "Cannot attach duplicate criterion with criterion typeIdent %s and value: %s",
228 $criterionAssignment->getCriterionId(),
229 var_export($criterionAssignment->getCriterionValue(), 1)
230 ));
231 }
232 }
233
234 $this->criteria[] = $criterionAssignment;
235 }
236
242 {
243 $numCriteriaBeforeRemoval = count($this->criteria);
244
245 $this->criteria = array_filter($this->criteria, function (\ilTermsOfServiceDocumentCriterionAssignment $currentAssignment) use ($criterionAssignment) {
246 return !$currentAssignment->equals($criterionAssignment);
247 });
248
249 $numCriteriaAfterRemoval = count($this->criteria);
250
251 if ($numCriteriaAfterRemoval === $numCriteriaBeforeRemoval) {
252 throw new \OutOfBoundsException(sprintf(
253 "Could not find any criterion with criterion typeIdent %s and value: %s",
254 $criterionAssignment->getCriterionId(),
255 var_export($criterionAssignment->getCriterionValue(), 1)
256 ));
257 }
258 }
259
263 public function fetchAllCriterionAssignments()
264 {
265 if (!$this->criteriaFetched) {
266 $this->criteriaFetched = true;
267
268 $this->initialPersistedCriteria = [];
269 $this->criteria = [];
270
271 $criteria = \ilTermsOfServiceDocumentCriterionAssignment::where(['doc_id' => $this->getId()])->get();
272 foreach ($criteria as $criterionAssignment) {
274 $this->criteria[] = $criterionAssignment;
275 }
276
277 $this->initialPersistedCriteria = $this->criteria;
278 }
279 }
280}
Class ActiveRecord.
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
Class ilTermsOfServiceDocument.
criteria()
\ilTermsOfServiceEvaluableCriterion[]
detachCriterion(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
Interface ilTermsOfServiceSignableDocument.
update($pash, $contents, Config $config)