ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilAdvancedMDClaimingPlugin.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("./Services/Component/classes/class.ilPlugin.php");
6
16{
17 //
18 // plugin slot
19 //
20
21 final function getComponentType()
22 {
23 return IL_COMP_SERVICE;
24 }
25
26 final function getComponentName()
27 {
28 return "AdvancedMetaData";
29 }
30
31 final function getSlot()
32 {
33 return "AdvancedMDClaiming";
34 }
35
36 final function getSlotId()
37 {
38 return "amdc";
39 }
40
41 protected final function slotInit()
42 {
43 require_once "Services/AdvancedMetaData/classes/class.ilAdvancedMDPermissionHelper.php";
44 }
45
46
47 //
48 // permission
49 //
50
61 abstract public function checkPermission($a_user_id, $a_context_type, $a_context_id, $a_action_id, $a_action_sub_id);
62
63
64 //
65 // db update helper
66 //
67
74 public static function hasDBRecord($a_record_id)
75 {
76 global $ilDB;
77
78 $set = $ilDB->query("SELECT record_id FROM adv_md_record".
79 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer"));
80 return (bool)$ilDB->numRows($set);
81 }
82
92 public static function createDBRecord($a_title, $a_description, $a_active, array $a_obj_types)
93 {
94 global $ilDB;
95
96 $record_id = $ilDB->nextId("adv_md_record");
97
98 $fields = array(
99 "record_id" => array("integer", $record_id),
100 "import_id" => array("text", 'il_'.IL_INST_ID.'_adv_md_record_'.$record_id),
101 "title" => array("text", trim($a_title)),
102 "description" => array("text", trim($a_description)),
103 "active" => array("integer", (int)$a_active)
104 );
105 $ilDB->insert("adv_md_record", $fields);
106
107 self::saveRecordObjTypes($record_id, $a_obj_types);
108
109 return $record_id;
110 }
111
119 protected static function isValidObjType($a_obj_type, $a_is_substitution = false)
120 {
121 // ecs not supported yet
122 $valid = array("crs", "cat", "book", "wiki", "glo", "orgu", "prg");
123
124 if(!$a_is_substitution)
125 {
126 $valid[] = "orgu";
127 $valid[] = "prg";
128 }
129
130 return in_array($a_obj_type, $valid);
131 }
132
139 protected static function saveRecordObjTypes($a_record_id, array $a_obj_types)
140 {
141 global $ilDB;
142
143 foreach($a_obj_types as $type)
144 {
145 if(!is_array($type))
146 {
147 $type = strtolower(trim($type));
148 $subtype = "-";
149 }
150 else
151 {
152 $subtype = strtolower(trim($type[1]));
153 $type = strtolower(trim($type[0]));
154 }
155
156 if(self::isValidObjType($type))
157 {
158 $fields = array(
159 "record_id" => array("integer", $a_record_id),
160 "obj_type" => array("text", $type),
161 "sub_type" => array("text", $subtype)
162 );
163 $ilDB->insert("adv_md_record_objs", $fields);
164 }
165 }
166 }
167
178 public static function updateDBRecord($a_record_id, $a_title, $a_description, $a_active, array $a_obj_types)
179 {
180 global $ilDB;
181
182 if(self::hasDBRecord($a_record_id))
183 {
184 $fields = array(
185 "title" => array("text", trim($a_title)),
186 "description" => array("text", trim($a_description)),
187 "active" => array("integer", (int)$a_active)
188 );
189 $ilDB->update("adv_md_record", $fields,
190 array("record_id" => array("integer", $a_record_id)));
191
192 $ilDB->manipulate("DELETE FROM adv_md_record_objs".
193 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer"));
194
195 self::saveRecordObjTypes($a_record_id, $a_obj_types);
196
197 return true;
198 }
199
200 return false;
201 }
202
209 public static function deleteDBRecord($a_record_id)
210 {
211 global $ilDB;
212
213 if(self::hasDBRecord($a_record_id))
214 {
215 $ilDB->manipulate("DELETE FROM adv_md_record".
216 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer"));
217 return true;
218 }
219
220 return false;
221 }
222
229 public static function hasDBField($a_field_id)
230 {
231 global $ilDB;
232
233 $set = $ilDB->query("SELECT field_id FROM adv_mdf_definition".
234 " WHERE field_id = ".$ilDB->quote($a_field_id, "integer"));
235 return (bool)$ilDB->numRows($set);
236 }
237
238
246 protected static function getDBFieldLastPosition($a_record_id)
247 {
248 global $ilDB;
249
250 $sql = "SELECT max(position) pos".
251 " FROM adv_mdf_definition".
252 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer");
253 $set = $ilDB->query($sql);
254 if($ilDB->numRows($set))
255 {
256 $pos = $ilDB->fetchAssoc($set);
257 return (int)$pos["pos"];
258 }
259
260 return 0;
261 }
262
274 public static function createDBField($a_record_id, $a_type, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
275 {
276 global $ilDB;
277
278 if(!self::hasDBRecord($a_record_id))
279 {
280 return;
281 }
282
283 $field_id = $ilDB->nextId("adv_mdf_definition");
284
285 // validating type
286 $a_type = (int)$a_type;
287 if($a_type < 1 || $a_type > 8)
288 {
289 return;
290 }
291
292 $pos = self::getDBFieldLastPosition($a_record_id)+1;
293
294 $fields = array(
295 "record_id" => array("integer", $a_record_id),
296 "field_id" => array("integer", $field_id),
297 "import_id" => array("text", "il_".IL_INST_ID."_adv_md_field_".$field_id),
298 "field_type" => array("integer", $a_type),
299 "position" => array("integer", $pos),
300 "title" => array("text", trim($a_title)),
301 "description" => array("text", trim($a_description)),
302 "searchable" => array("integer", (int)$a_searchable)
303 );
304 if($a_definition)
305 {
306 $fields["field_values"] = array("text", serialize($a_definition));
307 }
308 $ilDB->insert("adv_mdf_definition", $fields);
309
310 return $field_id;
311 }
312
323 public static function updateDBField($a_field_id, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
324 {
325 global $ilDB;
326
327 if(self::hasDBField($a_field_id))
328 {
329 $fields = array(
330 "field_id" => array("integer", $a_field_id),
331 "title" => array("text", trim($a_title)),
332 "description" => array("text", trim($a_description)),
333 "searchable" => array("integer", (int)$a_searchable)
334 );
335 if($a_definition)
336 {
337 $fields["field_values"] = array("text", serialize($a_definition));
338 }
339 $ilDB->update("adv_mdf_definition", $fields,
340 array("field_id" => array("integer", $a_field_id)));
341 return true;
342 }
343
344 return false;
345 }
346
353 public static function deleteDBField($a_field_id)
354 {
355 global $ilDB;
356
357 if(self::hasDBField($a_field_id))
358 {
359 $ilDB->manipulate("DELETE FROM adv_mdf_definition".
360 " WHERE field_id = ".$ilDB->quote($a_field_id, "integer"));
361 return true;
362 }
363
364 return false;
365 }
366
374 protected static function getDBSubstitution($a_obj_type, $a_include_field_data = false)
375 {
376 global $ilDB;
377
378 $set = $ilDB->query("SELECT * FROM adv_md_substitutions".
379 " WHERE obj_type = ".$ilDB->quote($a_obj_type, "text"));
380 if($ilDB->numRows($set))
381 {
382 $res = $ilDB->fetchAssoc($set);
383 $res["hide_description"] = array("integer", (bool)$res["hide_description"]);
384 $res["hide_field_names"] = array("integer", (bool)$res["hide_field_names"]);
385
386 if($a_include_field_data)
387 {
388 $res["substitution"] = array("text", (array)unserialize($res["substitution"]));
389 }
390 else
391 {
392 unset($res["substitution"]);
393 }
394 unset($res["obj_type"]);
395
396 return $res;
397 }
398 }
399
408 public static function setDBSubstitution($a_obj_type, $a_show_description, $a_show_field_names)
409 {
410 global $ilDB;
411
412 if(self::isValidObjType($a_obj_type, true))
413 {
414 $fields = self::getDBSubstitution($a_obj_type);
415
416 $create = false;
417 if(!$fields)
418 {
419 $create = true;
420 $fields = array("obj_type" => array("text", $a_obj_type));
421 }
422
423 $fields["hide_description"] = array("integer", !(bool)$a_show_description);
424 $fields["hide_field_names"] = array("integer", !(bool)$a_show_field_names);
425
426 if($create)
427 {
428 $ilDB->insert("adv_md_substitutions", $fields);
429 }
430 else
431 {
432 $ilDB->update("adv_md_substitutions", $fields,
433 array("obj_type" => array("text", $a_obj_type)));
434 }
435
436 return true;
437 }
438 return false;
439 }
440
448 public static function hasDBFieldSubstitution($a_obj_type, $a_field_id)
449 {
450 if(self::isValidObjType($a_obj_type, true))
451 {
452 $fields = self::getDBSubstitution($a_obj_type, true);
453 $fields = $fields["substitution"][1];
454 foreach($fields as $field)
455 {
456 if($field["field_id"] == $a_field_id)
457 {
458 return true;
459 }
460 }
461 return false;
462 }
463 }
464
474 public static function setDBFieldSubstitution($a_obj_type, $a_field_id, $a_bold = false, $a_newline = false)
475 {
476 global $ilDB;
477
478 if(self::isValidObjType($a_obj_type, true))
479 {
480 $fields = self::getDBSubstitution($a_obj_type, true);
481 if(!$fields)
482 {
483 self::setDBSubstitution($a_obj_type, true, true);
484 $fields = array();
485 }
486 else
487 {
488 $fields = $fields["substitution"][1];
489 }
490
491 $found = false;
492 foreach($fields as $idx => $field)
493 {
494 if($field["field_id"] == $a_field_id)
495 {
496 $fields[$idx]["bold"] = (bool)$a_bold;
497 $fields[$idx]["newline"] = (bool)$a_newline;
498 $found = true;
499 break;
500 }
501 }
502 if(!$found)
503 {
504 $fields[] = array(
505 "field_id" => $a_field_id
506 ,"bold" => (bool)$a_bold
507 ,"newline" => (bool)$a_newline
508 );
509 }
510
511 $fields = array("substitution"=>array("text", serialize($fields)));
512 $ilDB->update("adv_md_substitutions", $fields,
513 array("obj_type" => array("text", $a_obj_type)));
514 }
515 return false;
516 }
517
525 public static function removeDBFieldSubstitution($a_obj_type, $a_field_id)
526 {
527 global $ilDB;
528
529 if(self::isValidObjType($a_obj_type, true))
530 {
531 $fields = self::getDBSubstitution($a_obj_type, true);
532 if(!$fields)
533 {
534 return true;
535 }
536 else
537 {
538 $fields = $fields["substitution"][1];
539 }
540
541 $found = false;
542 foreach($fields as $idx => $field)
543 {
544 if($field["field_id"] == $a_field_id)
545 {
546 unset($fields[$idx]);
547 $found = true;
548 break;
549 }
550 }
551 if($found)
552 {
553 $fields = array("substitution"=>array("text", serialize($fields)));
554 $ilDB->update("adv_md_substitutions", $fields,
555 array("obj_type" => array("text", $a_obj_type)));
556 }
557 return true;
558 }
559 return false;
560 }
561}
562
563?>
const IL_COMP_SERVICE
Abstract parent class for all advanced md claiming plugin classes.
static getDBSubstitution($a_obj_type, $a_include_field_data=false)
Get substitution DB data for object type.
static deleteDBField($a_field_id)
Delete field db entry.
static getDBFieldLastPosition($a_record_id)
Get last position of record.
static removeDBFieldSubstitution($a_obj_type, $a_field_id)
Remove field substitution entry in DB.
static hasDBFieldSubstitution($a_obj_type, $a_field_id)
Is substitution active for field in object type.
slotInit()
Object initialization done by slot.
static updateDBField($a_field_id, $a_title, $a_description=null, $a_searchable=false, array $a_definition=null)
Update field db entry.
static deleteDBRecord($a_record_id)
Delete record db entry.
static updateDBRecord($a_record_id, $a_title, $a_description, $a_active, array $a_obj_types)
Update record db entry.
static createDBRecord($a_title, $a_description, $a_active, array $a_obj_types)
Create record db entry.
static hasDBField($a_field_id)
Check if field has db entry.
static isValidObjType($a_obj_type, $a_is_substitution=false)
Validate object type.
static hasDBRecord($a_record_id)
Check if record has db entry.
static createDBField($a_record_id, $a_type, $a_title, $a_description=null, $a_searchable=false, array $a_definition=null)
Create field db entry.
static saveRecordObjTypes($a_record_id, array $a_obj_types)
Save object type assignments for record.
checkPermission($a_user_id, $a_context_type, $a_context_id, $a_action_id, $a_action_sub_id)
Check permission.
static setDBFieldSubstitution($a_obj_type, $a_field_id, $a_bold=false, $a_newline=false)
Update field substitution entry in DB.
static setDBSubstitution($a_obj_type, $a_show_description, $a_show_field_names)
Set substitution DB entry (for object type)
$valid
global $ilDB