ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
5 include_once("./Services/Component/classes/class.ilPlugin.php");
6 
15 abstract class ilAdvancedMDClaimingPlugin extends ilPlugin
16 {
17  //
18  // plugin slot
19  //
20 
21  final public function getComponentType()
22  {
23  return IL_COMP_SERVICE;
24  }
25 
26  final public function getComponentName()
27  {
28  return "AdvancedMetaData";
29  }
30 
31  final public function getSlot()
32  {
33  return "AdvancedMDClaiming";
34  }
35 
36  final public function getSlotId()
37  {
38  return "amdc";
39  }
40 
41  final protected 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 $DIC;
77 
78  $ilDB = $DIC['ilDB'];
79 
80  $set = $ilDB->query("SELECT record_id FROM adv_md_record" .
81  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
82  return (bool) $ilDB->numRows($set);
83  }
84 
94  public static function createDBRecord($a_title, $a_description, $a_active, array $a_obj_types)
95  {
96  global $DIC;
97 
98  $ilDB = $DIC['ilDB'];
99 
100  $record_id = $ilDB->nextId("adv_md_record");
101 
102  $fields = array(
103  "record_id" => array("integer", $record_id),
104  "import_id" => array("text", 'il_' . IL_INST_ID . '_adv_md_record_' . $record_id),
105  "title" => array("text", trim($a_title)),
106  "description" => array("text", trim($a_description)),
107  "active" => array("integer", (int) $a_active)
108  );
109  $ilDB->insert("adv_md_record", $fields);
110 
111  self::saveRecordObjTypes($record_id, $a_obj_types);
112 
113  return $record_id;
114  }
115 
123  protected static function isValidObjType($a_obj_type, $a_is_substitution = false)
124  {
125  // ecs not supported yet
126  $valid = array("crs", "cat", "book", "wiki", "glo", "orgu", "prg", 'grp', 'iass');
127 
128  if (!$a_is_substitution) {
129  $valid[] = "orgu";
130  $valid[] = "prg";
131  }
132 
133  return in_array($a_obj_type, $valid);
134  }
135 
142  protected static function saveRecordObjTypes($a_record_id, array $a_obj_types)
143  {
144  global $DIC;
145 
146  $ilDB = $DIC['ilDB'];
147 
148  foreach ($a_obj_types as $type) {
149  if (!is_array($type)) {
150  $type = strtolower(trim($type));
151  $subtype = "-";
152  } else {
153  $subtype = strtolower(trim($type[1]));
154  $type = strtolower(trim($type[0]));
155  }
156 
157  if (self::isValidObjType($type)) {
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 $DIC;
181 
182  $ilDB = $DIC['ilDB'];
183 
184  if (self::hasDBRecord($a_record_id)) {
185  $fields = array(
186  "title" => array("text", trim($a_title)),
187  "description" => array("text", trim($a_description)),
188  "active" => array("integer", (int) $a_active)
189  );
190  $ilDB->update(
191  "adv_md_record",
192  $fields,
193  array("record_id" => array("integer", $a_record_id))
194  );
195 
196  $ilDB->manipulate("DELETE FROM adv_md_record_objs" .
197  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
198 
199  self::saveRecordObjTypes($a_record_id, $a_obj_types);
200 
201  return true;
202  }
203 
204  return false;
205  }
206 
213  public static function deleteDBRecord($a_record_id)
214  {
215  global $DIC;
216 
217  $ilDB = $DIC['ilDB'];
218 
219  if (self::hasDBRecord($a_record_id)) {
220  $ilDB->manipulate("DELETE FROM adv_md_record" .
221  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
222  return true;
223  }
224 
225  return false;
226  }
227 
234  public static function hasDBField($a_field_id)
235  {
236  global $DIC;
237 
238  $ilDB = $DIC['ilDB'];
239 
240  $set = $ilDB->query("SELECT field_id FROM adv_mdf_definition" .
241  " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
242  return (bool) $ilDB->numRows($set);
243  }
244 
245 
253  protected static function getDBFieldLastPosition($a_record_id)
254  {
255  global $DIC;
256 
257  $ilDB = $DIC['ilDB'];
258 
259  $sql = "SELECT max(position) pos" .
260  " FROM adv_mdf_definition" .
261  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer");
262  $set = $ilDB->query($sql);
263  if ($ilDB->numRows($set)) {
264  $pos = $ilDB->fetchAssoc($set);
265  return (int) $pos["pos"];
266  }
267 
268  return 0;
269  }
270 
282  public static function createDBField($a_record_id, $a_type, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
283  {
284  global $DIC;
285 
286  $ilDB = $DIC['ilDB'];
287 
288  if (!self::hasDBRecord($a_record_id)) {
289  return;
290  }
291 
292  $field_id = $ilDB->nextId("adv_mdf_definition");
293 
294  // validating type
295  $a_type = (int) $a_type;
296  if ($a_type < 1 || $a_type > 8) {
297  return;
298  }
299 
300  $pos = self::getDBFieldLastPosition($a_record_id) + 1;
301 
302  $fields = array(
303  "record_id" => array("integer", $a_record_id),
304  "field_id" => array("integer", $field_id),
305  "import_id" => array("text", "il_" . IL_INST_ID . "_adv_md_field_" . $field_id),
306  "field_type" => array("integer", $a_type),
307  "position" => array("integer", $pos),
308  "title" => array("text", trim($a_title)),
309  "description" => array("text", trim($a_description)),
310  "searchable" => array("integer", (int) $a_searchable)
311  );
312  if ($a_definition) {
313  $fields["field_values"] = array("text", serialize($a_definition));
314  }
315  $ilDB->insert("adv_mdf_definition", $fields);
316 
317  return $field_id;
318  }
319 
330  public static function updateDBField($a_field_id, $a_title, $a_description = null, $a_searchable = false, array $a_definition = null)
331  {
332  global $DIC;
333 
334  $ilDB = $DIC['ilDB'];
335 
336  if (self::hasDBField($a_field_id)) {
337  $fields = array(
338  "field_id" => array("integer", $a_field_id),
339  "title" => array("text", trim($a_title)),
340  "description" => array("text", trim($a_description)),
341  "searchable" => array("integer", (int) $a_searchable)
342  );
343  if ($a_definition) {
344  $fields["field_values"] = array("text", serialize($a_definition));
345  }
346  $ilDB->update(
347  "adv_mdf_definition",
348  $fields,
349  array("field_id" => array("integer", $a_field_id))
350  );
351  return true;
352  }
353 
354  return false;
355  }
356 
363  public static function deleteDBField($a_field_id)
364  {
365  global $DIC;
366 
367  $ilDB = $DIC['ilDB'];
368 
369  if (self::hasDBField($a_field_id)) {
370  $ilDB->manipulate("DELETE FROM adv_mdf_definition" .
371  " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
372  return true;
373  }
374 
375  return false;
376  }
377 
385  protected static function getDBSubstitution($a_obj_type, $a_include_field_data = false)
386  {
387  global $DIC;
388 
389  $ilDB = $DIC['ilDB'];
390 
391  $set = $ilDB->query("SELECT * FROM adv_md_substitutions" .
392  " WHERE obj_type = " . $ilDB->quote($a_obj_type, "text"));
393  if ($ilDB->numRows($set)) {
394  $res = $ilDB->fetchAssoc($set);
395  $res["hide_description"] = array("integer", (bool) $res["hide_description"]);
396  $res["hide_field_names"] = array("integer", (bool) $res["hide_field_names"]);
397 
398  if ($a_include_field_data) {
399  $res["substitution"] = array("text", (array) unserialize($res["substitution"]));
400  } else {
401  unset($res["substitution"]);
402  }
403  unset($res["obj_type"]);
404 
405  return $res;
406  }
407  }
408 
417  public static function setDBSubstitution($a_obj_type, $a_show_description, $a_show_field_names)
418  {
419  global $DIC;
420 
421  $ilDB = $DIC['ilDB'];
422 
423  if (self::isValidObjType($a_obj_type, true)) {
424  $fields = self::getDBSubstitution($a_obj_type);
425 
426  $create = false;
427  if (!$fields) {
428  $create = true;
429  $fields = array("obj_type" => array("text", $a_obj_type));
430  }
431 
432  $fields["hide_description"] = array("integer", !(bool) $a_show_description);
433  $fields["hide_field_names"] = array("integer", !(bool) $a_show_field_names);
434 
435  if ($create) {
436  $ilDB->insert("adv_md_substitutions", $fields);
437  } else {
438  $ilDB->update(
439  "adv_md_substitutions",
440  $fields,
441  array("obj_type" => array("text", $a_obj_type))
442  );
443  }
444 
445  return true;
446  }
447  return false;
448  }
449 
457  public static function hasDBFieldSubstitution($a_obj_type, $a_field_id)
458  {
459  if (self::isValidObjType($a_obj_type, true)) {
460  $fields = self::getDBSubstitution($a_obj_type, true);
461  $fields = $fields["substitution"][1];
462  foreach ($fields as $field) {
463  if ($field["field_id"] == $a_field_id) {
464  return true;
465  }
466  }
467  return false;
468  }
469  }
470 
480  public static function setDBFieldSubstitution($a_obj_type, $a_field_id, $a_bold = false, $a_newline = false)
481  {
482  global $DIC;
483 
484  $ilDB = $DIC['ilDB'];
485 
486  if (self::isValidObjType($a_obj_type, true)) {
487  $fields = self::getDBSubstitution($a_obj_type, true);
488  if (!$fields) {
489  self::setDBSubstitution($a_obj_type, true, true);
490  $fields = array();
491  } else {
492  $fields = $fields["substitution"][1];
493  }
494 
495  $found = false;
496  foreach ($fields as $idx => $field) {
497  if ($field["field_id"] == $a_field_id) {
498  $fields[$idx]["bold"] = (bool) $a_bold;
499  $fields[$idx]["newline"] = (bool) $a_newline;
500  $found = true;
501  break;
502  }
503  }
504  if (!$found) {
505  $fields[] = array(
506  "field_id" => $a_field_id
507  ,"bold" => (bool) $a_bold
508  ,"newline" => (bool) $a_newline
509  );
510  }
511 
512  $fields = array("substitution" => array("text", serialize($fields)));
513  $ilDB->update(
514  "adv_md_substitutions",
515  $fields,
516  array("obj_type" => array("text", $a_obj_type))
517  );
518  }
519  return false;
520  }
521 
529  public static function removeDBFieldSubstitution($a_obj_type, $a_field_id)
530  {
531  global $DIC;
532 
533  $ilDB = $DIC['ilDB'];
534 
535  if (self::isValidObjType($a_obj_type, true)) {
536  $fields = self::getDBSubstitution($a_obj_type, true);
537  if (!$fields) {
538  return true;
539  } else {
540  $fields = $fields["substitution"][1];
541  }
542 
543  $found = false;
544  foreach ($fields as $idx => $field) {
545  if ($field["field_id"] == $a_field_id) {
546  unset($fields[$idx]);
547  $found = true;
548  break;
549  }
550  }
551  if ($found) {
552  $fields = array("substitution" => array("text", serialize($fields)));
553  $ilDB->update(
554  "adv_md_substitutions",
555  $fields,
556  array("obj_type" => array("text", $a_obj_type))
557  );
558  }
559  return true;
560  }
561  return false;
562  }
563 }
$type
global $DIC
Definition: saml.php:7
$valid
static hasDBField($a_field_id)
Check if field has db entry.
static saveRecordObjTypes($a_record_id, array $a_obj_types)
Save object type assignments for record.
$a_context_id
Definition: workflow.php:97
static updateDBRecord($a_record_id, $a_title, $a_description, $a_active, array $a_obj_types)
Update record db entry.
static deleteDBRecord($a_record_id)
Delete record db entry.
static isValidObjType($a_obj_type, $a_is_substitution=false)
Validate object type.
Abstract parent class for all advanced md claiming plugin classes.
static setDBSubstitution($a_obj_type, $a_show_description, $a_show_field_names)
Set substitution DB entry (for object type)
static getDBFieldLastPosition($a_record_id)
Get last position of record.
$a_type
Definition: workflow.php:92
static setDBFieldSubstitution($a_obj_type, $a_field_id, $a_bold=false, $a_newline=false)
Update field substitution entry in DB.
foreach($_POST as $key=> $value) $res
static getDBSubstitution($a_obj_type, $a_include_field_data=false)
Get substitution DB data for object type.
static createDBRecord($a_title, $a_description, $a_active, array $a_obj_types)
Create record db entry.
$a_context_type
Definition: workflow.php:96
static deleteDBField($a_field_id)
Delete field db entry.
static updateDBField($a_field_id, $a_title, $a_description=null, $a_searchable=false, array $a_definition=null)
Update field db entry.
static removeDBFieldSubstitution($a_obj_type, $a_field_id)
Remove field substitution entry in DB.
global $ilDB
checkPermission($a_user_id, $a_context_type, $a_context_id, $a_action_id, $a_action_sub_id)
Check permission.
static createDBField($a_record_id, $a_type, $a_title, $a_description=null, $a_searchable=false, array $a_definition=null)
Create field db entry.
static hasDBFieldSubstitution($a_obj_type, $a_field_id)
Is substitution active for field in object type.
const IL_COMP_SERVICE
static hasDBRecord($a_record_id)
Check if record has db entry.