ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
42  public function __construct(protected \ilDBInterface $db)
43  {
44  }
45 
49  public function getNamesForLocking(): array
50  {
51  return [self::TABLE_NAME, self::TABLE_NAME_REL];
52  }
53 
54  public function register(ResourceIdentification $i, ResourceStakeholder $s): bool
55  {
56  $identification = $i->serialize();
57  $stakeholder_id = $s->getId();
58  $stakeholder_class_name = $s->getFullyQualifiedClassName();
59 
60  if (\strlen($stakeholder_id) > 64) {
61  throw new \InvalidArgumentException('stakeholder ids MUST be shorter or equal to than 64 characters');
62  }
63  if (\strlen($stakeholder_class_name) > 250) {
64  throw new \InvalidArgumentException(
65  'stakeholder classnames MUST be shorter or equal to than 250 characters'
66  );
67  }
68 
69  $this->db->replace(
70  self::TABLE_NAME,
71  [
72  self::IDENTIFICATION => ['text', $identification],
73  'stakeholder_id' => ['text', $stakeholder_id],
74  ],
75  []
76  );
77 
78  $this->db->replace(
79  self::TABLE_NAME_REL,
80  [
81  'id' => ['text', $stakeholder_id]
82  ],
83  [
84  'class_name' => ['text', $stakeholder_class_name]
85  ]
86  );
87 
88  $this->cache[$identification][$stakeholder_id] = $s;
89 
90  return true;
91  }
92 
94  {
95  $this->db->manipulateF(
96  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
97  ['text', 'text'],
98  [$i->serialize(), $s->getId()]
99  );
100  unset($this->cache[$i->serialize()][$s->getId()]);
101 
102  return true;
103  }
104 
108  public function getStakeholders(ResourceIdentification $i): array
109  {
110  $rid = $i->serialize();
111  if (isset($this->cache[$rid]) && \is_array($this->cache[$rid])) {
112  return $this->cache[$rid];
113  }
114 
115  $r = $this->db->queryF(
116  "SELECT class_name, stakeholder_id FROM " . self::TABLE_NAME . "
117  JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
118  WHERE " . self::IDENTIFICATION . " = %s",
119  ['text'],
120  [$rid]
121  );
122  while ($d = $this->db->fetchAssoc($r)) {
123  $d['rid'] = $rid;
124  $this->populateFromArray($d);
125  }
126  return $this->cache[$rid] ?? [];
127  }
128 
129  public function preload(array $identification_strings): void
130  {
131  $r = $this->db->query(
132  "SELECT rid, class_name, stakeholder_id FROM " . self::TABLE_NAME
133  . " JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
134  WHERE " . $this->db->in(
135  self::IDENTIFICATION,
136  $identification_strings,
137  false,
138  'text'
139  )
140  );
141  while ($d = $this->db->fetchAssoc($r)) {
142  $this->populateFromArray($d);
143  }
144  }
145 
146  public function populateFromArray(array $data): void
147  {
148  $class_name = $data['class_name'];
149 
150  if (class_exists($class_name)) {
151  $stakeholder = new $class_name();
152  $this->cache[$data['rid']][$data['stakeholder_id']] = $stakeholder;
153  }
154  }
155 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
deregister(ResourceIdentification $i, ResourceStakeholder $s)
$r