ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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, glossary not supported yet
122 $valid = array("crs", "cat");
123
124 if(!$a_is_substitution)
125 {
126 $valid[] = "orgu";
127 }
128
129 return in_array($a_obj_type, $valid);
130 }
131
138 protected static function saveRecordObjTypes($a_record_id, array $a_obj_types)
139 {
140 global $ilDB;
141
142 foreach($a_obj_types as $type)
143 {
144 if(!is_array($type))
145 {
146 $type = strtolower(trim($type));
147 $subtype = "-";
148 }
149 else
150 {
151 $type = strtolower(trim($type[0]));
152 $subtype = strtolower(trim($type[1]));
153 }
154
155 if(self::isValidObjType($type))
156 {
157 $fields = array(
158 "record_id" => array("integer", $a_record_id),
159 "obj_type" => array("text", $type),
160 "sub_type" => array("text", $subtype)
161 );
162 $ilDB->insert("adv_md_record_objs", $fields);
163 }
164 }
165 }
166
177 public static function updateDBRecord($a_record_id, $a_title, $a_description, $a_active, array $a_obj_types)
178 {
179 global $ilDB;
180
181 if(self::hasDBRecord($a_record_id))
182 {
183 $fields = array(
184 "title" => array("text", trim($a_title)),
185 "description" => array("text", trim($a_description)),
186 "active" => array("integer", (int)$a_active)
187 );
188 $ilDB->update("adv_md_record", $fields,
189 array("record_id" => array("integer", $a_record_id)));
190
191 $ilDB->manipulate("DELETE FROM adv_md_record_objs".
192 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer"));
193
194 self::saveRecordObjTypes($a_record_id, $a_obj_types);
195
196 return true;
197 }
198
199 return false;
200 }
201
208 public static function deleteDBRecord($a_record_id)
209 {
210 global $ilDB;
211
212 if(self::hasDBRecord($a_record_id))
213 {
214 $ilDB->manipulate("DELETE FROM adv_md_record".
215 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer"));
216 return true;
217 }
218
219 return false;
220 }
221
228 public static function hasDBField($a_field_id)
229 {
230 global $ilDB;
231
232 $set = $ilDB->query("SELECT field_id FROM adv_mdf_definition".
233 " WHERE field_id = ".$ilDB->quote($a_field_id, "integer"));
234 return (bool)$ilDB->numRows($set);
235 }
236
237
245 protected static function getDBFieldLastPosition($a_record_id)
246 {
247 global $ilDB;
248
249 $sql = "SELECT max(position) pos".
250 " FROM adv_mdf_definition".
251 " WHERE record_id = ".$ilDB->quote($a_record_id, "integer");
252 $set = $ilDB->query($sql);
253 if($ilDB->numRows($set))
254 {
255 $pos = $ilDB->fetchAssoc($set);
256 return (int)$pos["pos"];
257 }
258
259 return 0;
260 }
261
273 public static function createDBField($a_record_id, $a_type, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
274 {
275 global $ilDB;
276
277 if(!self::hasDBRecord($a_record_id))
278 {
279 return;
280 }
281
282 $field_id = $ilDB->nextId("adv_mdf_definition");
283
284 // validating type
285 $a_type = (int)$a_type;
286 if($a_type < 1 || $a_type > 7)
287 {
288 return;
289 }
290
291 $pos = self::getDBFieldLastPosition($a_record_id)+1;
292
293 $fields = array(
294 "record_id" => array("integer", $a_record_id),
295 "field_id" => array("integer", $field_id),
296 "import_id" => array("text", "il_".IL_INST_ID."_adv_md_field_".$field_id),
297 "field_type" => array("integer", $a_type),
298 "position" => array("integer", $pos),
299 "title" => array("text", trim($a_title)),
300 "description" => array("text", trim($a_description)),
301 "searchable" => array("integer", (int)$a_searchable)
302 );
303 if($a_definition)
304 {
305 $fields["field_values"] = array("text", serialize($a_definition));
306 }
307 $ilDB->insert("adv_mdf_definition", $fields);
308
309 return $field_id;
310 }
311
322 public static function updateDBField($a_field_id, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
323 {
324 global $ilDB;
325
326 if(self::hasDBField($a_field_id))
327 {
328 $fields = array(
329 "field_id" => array("integer", $a_field_id),
330 "title" => array("text", trim($a_title)),
331 "description" => array("text", trim($a_description)),
332 "searchable" => array("integer", (int)$a_searchable)
333 );
334 if($a_definition)
335 {
336 $fields["field_values"] = array("text", serialize($a_definition));
337 }
338 $ilDB->update("adv_mdf_definition", $fields,
339 array("field_id" => array("integer", $a_field_id)));
340 return true;
341 }
342
343 return false;
344 }
345
352 public static function deleteDBField($a_field_id)
353 {
354 global $ilDB;
355
356 if(self::hasDBField($a_field_id))
357 {
358 $ilDB->manipulate("DELETE FROM adv_mdf_definition".
359 " WHERE field_id = ".$ilDB->quote($a_field_id, "integer"));
360 return true;
361 }
362
363 return false;
364 }
365
373 protected static function getDBSubstitution($a_obj_type, $a_include_field_data = false)
374 {
375 global $ilDB;
376
377 $set = $ilDB->query("SELECT * FROM adv_md_substitutions".
378 " WHERE obj_type = ".$ilDB->quote($a_obj_type, "text"));
379 if($ilDB->numRows($set))
380 {
381 $res = $ilDB->fetchAssoc($set);
382 $res["hide_description"] = array("integer", (bool)$res["hide_description"]);
383 $res["hide_field_names"] = array("integer", (bool)$res["hide_field_names"]);
384
385 if($a_include_field_data)
386 {
387 $res["substitution"] = array("text", (array)unserialize($res["substitution"]));
388 }
389 else
390 {
391 unset($res["substitution"]);
392 }
393 unset($res["obj_type"]);
394
395 return $res;
396 }
397 }
398
407 public static function setDBSubstitution($a_obj_type, $a_show_description, $a_show_field_names)
408 {
409 global $ilDB;
410
411 if(self::isValidObjType($a_obj_type, true))
412 {
413 $fields = self::getDBSubstitution($a_obj_type);
414
415 $create = false;
416 if(!$fields)
417 {
418 $create = true;
419 $fields = array("obj_type" => array("text", $a_obj_type));
420 }
421
422 $fields["hide_description"] = array("integer", !(bool)$a_show_description);
423 $fields["hide_field_names"] = array("integer", !(bool)$a_show_field_names);
424
425 if($create)
426 {
427 $ilDB->insert("adv_md_substitutions", $fields);
428 }
429 else
430 {
431 $ilDB->update("adv_md_substitutions", $fields,
432 array("obj_type" => array("text", $a_obj_type)));
433 }
434
435 return true;
436 }
437 return false;
438 }
439
447 public static function hasDBFieldSubstitution($a_obj_type, $a_field_id)
448 {
449 if(self::isValidObjType($a_obj_type, true))
450 {
451 $fields = self::getDBSubstitution($a_obj_type, true);
452 $fields = $fields["substitution"][1];
453 foreach($fields as $field)
454 {
455 if($field["field_id"] == $a_field_id)
456 {
457 return true;
458 }
459 }
460 return false;
461 }
462 }
463
473 public static function setDBFieldSubstitution($a_obj_type, $a_field_id, $a_bold = false, $a_newline = false)
474 {
475 global $ilDB;
476
477 if(self::isValidObjType($a_obj_type, true))
478 {
479 $fields = self::getDBSubstitution($a_obj_type, true);
480 if(!$fields)
481 {
482 self::setDBSubstitution($a_obj_type, true, true);
483 $fields = array();
484 }
485 else
486 {
487 $fields = $fields["substitution"][1];
488 }
489
490 $found = false;
491 foreach($fields as $idx => $field)
492 {
493 if($field["field_id"] == $a_field_id)
494 {
495 $fields[$idx]["bold"] = (bool)$a_bold;
496 $fields[$idx]["newline"] = (bool)$a_newline;
497 $found = true;
498 break;
499 }
500 }
501 if(!$found)
502 {
503 $fields[] = array(
504 "field_id" => $a_field_id
505 ,"bold" => (bool)$a_bold
506 ,"newline" => (bool)$a_newline
507 );
508 }
509
510 $fields = array("substitution"=>array("text", serialize($fields)));
511 $ilDB->update("adv_md_substitutions", $fields,
512 array("obj_type" => array("text", $a_obj_type)));
513 }
514 return false;
515 }
516
524 public static function removeDBFieldSubstitution($a_obj_type, $a_field_id)
525 {
526 global $ilDB;
527
528 if(self::isValidObjType($a_obj_type, true))
529 {
530 $fields = self::getDBSubstitution($a_obj_type, true);
531 if(!$fields)
532 {
533 return true;
534 }
535 else
536 {
537 $fields = $fields["substitution"][1];
538 }
539
540 $found = false;
541 foreach($fields as $idx => $field)
542 {
543 if($field["field_id"] == $a_field_id)
544 {
545 unset($fields[$idx]);
546 $found = true;
547 break;
548 }
549 }
550 if($found)
551 {
552 $fields = array("substitution"=>array("text", serialize($fields)));
553 $ilDB->update("adv_md_substitutions", $fields,
554 array("obj_type" => array("text", $a_obj_type)));
555 }
556 return true;
557 }
558 return false;
559 }
560}
561
562?>
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