ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
StakeholderDBRepository.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
4 
7 
14 {
15  const TABLE_NAME = 'il_resource_stkh_u';
16  const TABLE_NAME_REL = 'il_resource_stkh';
17  const IDENTIFICATION = 'rid';
21  protected $db;
22 
23  protected $cache = [];
24 
28  public function __construct(\ilDBInterface $db)
29  {
30  $this->db = $db;
31  }
32 
33  public function getNamesForLocking() : array
34  {
35  return [self::TABLE_NAME, self::TABLE_NAME_REL];
36  }
37 
38  public function register(ResourceIdentification $i, ResourceStakeholder $s) : bool
39  {
40  $identification = $i->serialize();
41  $stakeholder_id = $s->getId();
42  $stakeholder_class_name = $s->getFullyQualifiedClassName();
43 
44  if (strlen($stakeholder_id) > 64) {
45  throw new \InvalidArgumentException('stakeholder ids MUST be shorter or equal to than 64 characters');
46  }
47  if (strlen($stakeholder_class_name) > 250) {
48  throw new \InvalidArgumentException('stakeholder classnames MUST be shorter or equal to than 250 characters');
49  }
50 
51  $r = $this->db->queryF(
52  "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
53  ['text', 'text'],
54  [$identification, $stakeholder_id]
55  );
56 
57  if ($r->numRows() === 0) {
58  // CREATE
59  $this->db->insert(
60  self::TABLE_NAME,
61  [
62  self::IDENTIFICATION => ['text', $identification],
63  'stakeholder_id' => ['text', $stakeholder_id],
64  ]
65  );
66  }
67 
68  $r = $this->db->queryF(
69  "SELECT id FROM " . self::TABLE_NAME_REL . " WHERE id = %s",
70  ['text',],
71  [$stakeholder_id]
72  );
73  if ($r->numRows() === 0) {
74 
75  $this->db->insert(
76  self::TABLE_NAME_REL,
77  [
78  'id' => ['text', $stakeholder_id],
79  'class_name' => ['text', $stakeholder_class_name],
80  ]
81  );
82  }
83 
84  $this->cache[$identification][$stakeholder_id] = $s;
85 
86  return true;
87  }
88 
89  public function deregister(ResourceIdentification $i, ResourceStakeholder $s) : bool
90  {
91  $r = $this->db->manipulateF(
92  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
93  ['text', 'text'],
94  [$i->serialize(), $s->getId()]
95  );
96  unset($this->cache[$i->serialize()][$s->getId()]);
97 
98  return true;
99  }
100 
104  public function getStakeholders(ResourceIdentification $i) : array
105  {
106  $rid = $i->serialize();
107  if (isset($this->cache[$rid]) && is_array($this->cache[$rid])) {
108  return $this->cache[$rid];
109  }
110 
111  $r = $this->db->queryF(
112  "SELECT class_name, stakeholder_id FROM " . self::TABLE_NAME . "
113  JOIN ".self::TABLE_NAME_REL." ON stakeholder_id = id
114  WHERE " . self::IDENTIFICATION . " = %s",
115  ['text'],
116  [$rid]
117  );
118  while ($d = $this->db->fetchAssoc($r)) {
119  $d['rid'] = $rid;
120  $this->populateFromArray($d);
121  }
122  return $this->cache[$rid] ?? [];
123  }
124 
125  public function preload(array $identification_strings) : void
126  {
127  $r = $this->db->query(
128  "SELECT rid, class_name, stakeholder_id FROM " . self::TABLE_NAME
129  . " JOIN ".self::TABLE_NAME_REL." ON stakeholder_id = id
130  WHERE " . $this->db->in(self::IDENTIFICATION,
131  $identification_strings, false, 'text')
132  );
133  while ($d = $this->db->fetchAssoc($r)) {
134  $this->populateFromArray($d);
135  }
136  }
137 
138  public function populateFromArray(array $data) : void
139  {
140  $class_name = $data['class_name'];
141  $stakeholder = new $class_name();
142  $stakeholders[] = $stakeholder;
143  $this->cache[$data['rid']][$data['stakeholder_id']] = $stakeholder;
144  }
145 }
$data
Definition: storeScorm.php:23
deregister(ResourceIdentification $i, ResourceStakeholder $s)
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$i
Definition: metadata.php:24