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