ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  $this->db->replace(
72  self::TABLE_NAME,
73  [
74  self::IDENTIFICATION => ['text', $identification],
75  'stakeholder_id' => ['text', $stakeholder_id],
76  ],
77  []
78  );
79 
80  $this->db->replace(
81  self::TABLE_NAME_REL,
82  [
83  'id' => ['text', $stakeholder_id]
84  ],
85  [
86  'class_name' => ['text', $stakeholder_class_name]
87  ]
88  );
89 
90  $this->cache[$identification][$stakeholder_id] = $s;
91 
92  return true;
93  }
94 
96  {
97  $this->db->manipulateF(
98  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND stakeholder_id = %s",
99  ['text', 'text'],
100  [$i->serialize(), $s->getId()]
101  );
102  unset($this->cache[$i->serialize()][$s->getId()]);
103 
104  return true;
105  }
106 
110  public function getStakeholders(ResourceIdentification $i): array
111  {
112  $rid = $i->serialize();
113  if (isset($this->cache[$rid]) && \is_array($this->cache[$rid])) {
114  return $this->cache[$rid];
115  }
116 
117  $r = $this->db->queryF(
118  "SELECT class_name, stakeholder_id FROM " . self::TABLE_NAME . "
119  JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
120  WHERE " . self::IDENTIFICATION . " = %s",
121  ['text'],
122  [$rid]
123  );
124  while ($d = $this->db->fetchAssoc($r)) {
125  $d['rid'] = $rid;
126  $this->populateFromArray($d);
127  }
128  return $this->cache[$rid] ?? [];
129  }
130 
131  public function preload(array $identification_strings): void
132  {
133  $r = $this->db->query(
134  "SELECT rid, class_name, stakeholder_id FROM " . self::TABLE_NAME
135  . " JOIN " . self::TABLE_NAME_REL . " ON stakeholder_id = id
136  WHERE " . $this->db->in(
137  self::IDENTIFICATION,
138  $identification_strings,
139  false,
140  'text'
141  )
142  );
143  while ($d = $this->db->fetchAssoc($r)) {
144  $this->populateFromArray($d);
145  }
146  }
147 
148  public function populateFromArray(array $data): void
149  {
150  $class_name = $data['class_name'];
151 
152  if (class_exists($class_name)) {
153  $stakeholder = new $class_name();
154  $this->cache[$data['rid']][$data['stakeholder_id']] = $stakeholder;
155  }
156  }
157 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
deregister(ResourceIdentification $i, ResourceStakeholder $s)
$r