ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCourseDefinedFieldDefinition.php
Go to the documentation of this file.
1<?php
24{
25 public const IL_CDF_SORT_ID = 'field_id';
26 public const IL_CDF_SORT_NAME = 'field_name';
27 public const IL_CDF_TYPE_TEXT = 1;
28 public const IL_CDF_TYPE_SELECT = 2;
29
30 protected ilDBInterface $db;
31 protected ilLanguage $lng;
32
33 private int $obj_id;
34
35 private int $id = 0;
36 private string $name = '';
37 private int $type = 0;
38 private array $values = [];
39 private array $value_options = [];
40 private bool $required = false;
41
42 public function __construct(int $a_obj_id, int $a_field_id = 0)
43 {
44 global $DIC;
45
46 $this->db = $DIC->database();
47 $this->lng = $DIC->language();
48 $this->obj_id = $a_obj_id;
49 $this->id = $a_field_id;
50
51 if ($this->id) {
52 $this->read();
53 }
54 }
55
56 public static function _clone(int $a_source_id, int $a_target_id): void
57 {
58 foreach (ilCourseDefinedFieldDefinition::_getFields($a_source_id) as $field_obj) {
59 $cdf = new ilCourseDefinedFieldDefinition($a_target_id);
60 $cdf->setName($field_obj->getName());
61 $cdf->setType($field_obj->getType());
62 $cdf->setValues($field_obj->getValues());
63 $cdf->setValueOptions($field_obj->getValueOptions());
64 $cdf->enableRequired($field_obj->isRequired());
65 $cdf->save();
66 }
67 }
68
69 public static function _deleteByContainer(int $a_container_id): void
70 {
71 global $DIC;
72
73 $ilDB = $DIC->database();
74 // Delete user entries
75 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id) as $field_id) {
77 }
78 $query = "DELETE FROM crs_f_definitions " .
79 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " ";
80 $res = $ilDB->manipulate($query);
81 }
82
86 public static function _hasFields(int $a_container_id): int
87 {
88 return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
89 }
90
96 public static function _getFields(int $a_container_id, $a_sort = self::IL_CDF_SORT_NAME): array
97 {
98 $fields = [];
99 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id, self::IL_CDF_SORT_ID) as $field_id) {
100 $fields[] = new ilCourseDefinedFieldDefinition($a_container_id, $field_id);
101 }
102 return $fields;
103 }
104
109 public static function _getRequiredFieldIds(int $a_obj_id): array
110 {
111 global $DIC;
112
113 $ilDB = $DIC->database();
114
115 $query = "SELECT * FROM crs_f_definitions " .
116 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
117 "AND field_required = 1";
118 $res = $ilDB->query($query);
119 $req_fields = [];
120 while ($row = $ilDB->fetchObject($res)) {
121 $req_fields[] = (int) $row->field_id;
122 }
123 return $req_fields;
124 }
125
126 public static function _fieldsToInfoString(int $a_obj_id): string
127 {
128 global $DIC;
129
130 $ilDB = $DIC->database();
131 $query = "SELECT field_name FROM crs_f_definitions " .
132 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
133 $res = $ilDB->query($query);
134 $fields = [];
135 while ($row = $ilDB->fetchObject($res)) {
136 $fields[] = $row->field_name;
137 }
138 return implode('<br />', $fields);
139 }
140
144 public static function _getFieldIds(int $a_container_id, string $a_sort = self::IL_CDF_SORT_ID): array
145 {
146 global $DIC;
147
148 $ilDB = $DIC->database();
149 $query = "SELECT field_id FROM crs_f_definitions " .
150 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " " .
151 "ORDER BY " . self::IL_CDF_SORT_ID;
152 $res = $ilDB->query($query);
153 $field_ids = [];
154 while ($row = $ilDB->fetchObject($res)) {
155 $field_ids[] = (int) $row->field_id;
156 }
157 return $field_ids;
158 }
159
160 public static function _lookupName(int $a_field_id): string
161 {
162 global $DIC;
163
164 $ilDB = $DIC->database();
165 $query = "SELECT * FROM crs_f_definitions " .
166 "WHERE field_id = " . $ilDB->quote($a_field_id, 'integer');
167
168 $res = $ilDB->query($query);
169 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
170 return $row->field_name ?: '';
171 }
172
173 public function getObjId(): int
174 {
175 return $this->obj_id;
176 }
177
178 public function getId(): int
179 {
180 return $this->id;
181 }
182
183 public function getType(): int
184 {
185 return $this->type;
186 }
187
188 public function setType(int $a_type): void
189 {
190 $this->type = $a_type;
191 }
192
193 public function getName(): string
194 {
195 return $this->name;
196 }
197
198 public function setName(string $a_name): void
199 {
200 $this->name = $a_name;
201 }
202
203 public function getValues(): array
204 {
205 return $this->values;
206 }
207
208 public function setValues(array $a_values): void
209 {
210 $this->values = $a_values;
211 }
212
213 public function getValueById(int $a_id): string
214 {
215 if (
216 is_array($this->values) &&
217 array_key_exists($a_id, $this->values)
218 ) {
219 return $this->values[$a_id];
220 }
221 return '';
222 }
223
224 public function getIdByValue(string $a_value): int
225 {
226 return (($pos = array_search($a_value, $this->values)) === false) ? -1 : $pos;
227 }
228
229 public function isRequired(): bool
230 {
231 return $this->required;
232 }
233
234 public function enableRequired(bool $a_status): void
235 {
236 $this->required = $a_status;
237 }
238
239 public function setValueOptions(array $a_options): void
240 {
241 $this->value_options = $a_options;
242 }
243
244 public function getValueOptions(): array
245 {
247 }
248
249 public function prepareSelectBox(): array
250 {
251 $options = array();
252 $options[''] = $this->lng->txt('select_one');
253
254 foreach ($this->values as $key => $value) {
255 $options[$this->getId() . '_' . $key] = $value;
256 }
257 return $options;
258 }
259
260 public function prepareValues(array $a_values): array
261 {
262 $tmp_values = [];
263 $tmp_values = array_filter($a_values, 'strlen');
264 return $tmp_values;
265 }
266
267 public function appendValues(array $a_values): bool
268 {
269 $this->values = array_unique(array_merge($this->values, $a_values));
270 #sort($this->values);
271 return true;
272 }
273
274 public function save(): void
275 {
276 $next_id = $this->db->nextId('crs_f_definitions');
277 $query = "INSERT INTO crs_f_definitions (field_id,obj_id,field_name,field_type,field_values,field_required,field_values_opt) " .
278 "VALUES ( " .
279 $this->db->quote($next_id, 'integer') . ", " .
280 $this->db->quote($this->getObjId(), 'integer') . ", " .
281 $this->db->quote($this->getName(), "text") . ", " .
282 $this->db->quote($this->getType(), 'integer') . ", " .
283 $this->db->quote(serialize($this->getValues()), 'text') . ", " .
284 $this->db->quote($this->isRequired(), 'integer') . ", " .
285 $this->db->quote(serialize($this->getValueOptions()), 'text') . ' ' .
286 ") ";
287 $res = $this->db->manipulate($query);
288 $this->id = $next_id;
289 }
290
291 public function update(): void
292 {
293 $query = "UPDATE crs_f_definitions " .
294 "SET field_name = " . $this->db->quote($this->getName(), 'text') . ", " .
295 "field_type = " . $this->db->quote($this->getType(), 'integer') . ", " .
296 "field_values = " . $this->db->quote(serialize($this->getValues()), 'text') . ", " .
297 "field_required = " . $this->db->quote($this->isRequired(), 'integer') . ", " .
298 'field_values_opt = ' . $this->db->quote(serialize($this->getValueOptions()), 'text') . ' ' .
299 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " .
300 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer');
301 $res = $this->db->manipulate($query);
302 }
303
304 public function delete(): void
305 {
307 $query = "DELETE FROM crs_f_definitions " .
308 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " ";
309 $res = $this->db->manipulate($query);
310 }
311
312 private function read(): void
313 {
314 $query = "SELECT * FROM crs_f_definitions " .
315 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " .
316 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " ";
317
318 $res = $this->db->query($query);
319 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
320
321 $this->setName((string) $row->field_name);
322 $this->setType((int) $row->field_type);
323 $this->setValues(unserialize($row->field_values) ?: []);
324 $this->setValueOptions(unserialize($row->field_values_opt) ?: []);
325 $this->enableRequired((bool) $row->field_required);
326 }
327}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
static _clone(int $a_source_id, int $a_target_id)
static _getFieldIds(int $a_container_id, string $a_sort=self::IL_CDF_SORT_ID)
__construct(int $a_obj_id, int $a_field_id=0)
static _hasFields(int $a_container_id)
Check if there are any define fields.
static _getRequiredFieldIds(int $a_obj_id)
Get required filed id's.
static _deleteByField(int $a_field_id)
language handling
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26