ILIAS  release_8 Revision v8.23
StakeholderDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
32 {
33  public const TABLE_NAME = 'il_resource_stkh_u';
34  public const TABLE_NAME_REL = 'il_resource_stkh';
35  public const IDENTIFICATION = 'rid';
36 
40  protected array $cache = [];
41  protected \ilDBInterface $db;
42 
43  public function __construct(\ilDBInterface $db)
44  {
45  $this->db = $db;
46  }
47 
51  public function getNamesForLocking(): array
52  {
53  return [self::TABLE_NAME, self::TABLE_NAME_REL];
54  }
55 
56  public function register(ResourceIdentification $i, ResourceStakeholder $s): bool
57  {
58  $identification = $i->serialize();
59  $stakeholder_id = $s->getId();
60  $stakeholder_class_name = $s->getFullyQualifiedClassName();
61 
62  if (strlen($stakeholder_id) > 64) {
63  throw new \InvalidArgumentException('stakeholder ids MUST be shorter or equal to than 64 characters');
64  }
65  if (strlen($stakeholder_class_name) > 250) {
66  throw new \InvalidArgumentException(
67  'stakeholder classnames MUST be shorter or equal to than 250 characters'
68  );
69  }
70 
71  $r = $this->db->queryF(
72  "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
73  ['text', 'text'],
74  [$identification, $stakeholder_id]
75  );
76 
77  if ($r->numRows() === 0) {
78  // CREATE
79  $this->db->insert(
80  self::TABLE_NAME,
81  [
82  self::IDENTIFICATION => ['text', $identification],
83  'stakeholder_id' => ['text', $stakeholder_id],
84  ]
85  );
86  }
87 
88  $r = $this->db->queryF(
89  "SELECT id FROM " . self::TABLE_NAME_REL . " WHERE id = %s",
90  ['text',],
91  [$stakeholder_id]
92  );
93  if ($r->numRows() === 0) {
94  $this->db->insert(
95  self::TABLE_NAME_REL,
96  [
97  'id' => ['text', $stakeholder_id],
98  'class_name' => ['text', $stakeholder_class_name],
99  ]
100  );
101  }
102 
103  $this->cache[$identification][$stakeholder_id] = $s;
104 
105  return true;
106  }
107 
109  {
110  $this->db->manipulateF(
111  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
112  ['text', 'text'],
113  [$i->serialize(), $s->getId()]
114  );
115  unset($this->cache[$i->serialize()][$s->getId()]);
116 
117  return true;
118  }
119 
123  public function getStakeholders(ResourceIdentification $i): array
124  {
125  $rid = $i->serialize();
126  if (isset($this->cache[$rid]) && is_array($this->cache[$rid])) {
127  return $this->cache[$rid];
128  }
129 
130  $r = $this->db->queryF(
131  "SELECT class_name, stakeholder_id FROM " . self::TABLE_NAME . "
132  JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
133  WHERE " . self::IDENTIFICATION . " = %s",
134  ['text'],
135  [$rid]
136  );
137  while ($d = $this->db->fetchAssoc($r)) {
138  $d['rid'] = $rid;
139  $this->populateFromArray($d);
140  }
141  return $this->cache[$rid] ?? [];
142  }
143 
144  public function preload(array $identification_strings): void
145  {
146  $r = $this->db->query(
147  "SELECT rid, class_name, stakeholder_id FROM " . self::TABLE_NAME
148  . " JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
149  WHERE " . $this->db->in(
150  self::IDENTIFICATION,
151  $identification_strings,
152  false,
153  'text'
154  )
155  );
156  while ($d = $this->db->fetchAssoc($r)) {
157  $this->populateFromArray($d);
158  }
159  }
160 
161  public function populateFromArray(array $data): void
162  {
163  $stakeholders = [];
164  $class_name = $data['class_name'];
165  $stakeholder = new $class_name();
166  $stakeholders[] = $stakeholder;
167  $this->cache[$data['rid']][$data['stakeholder_id']] = $stakeholder;
168  }
169 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$i
Definition: metadata.php:41