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