ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
25 protected \ilDBInterface $db;
26
27 public function __construct(\ilDBInterface $db)
28 {
29 $this->db = $db;
30 }
31
32 public function isHarvestingBlocked(int $obj_id): bool
33 {
34 $res = $this->query(
35 'SELECT blocked FROM il_meta_oer_stat WHERE obj_id = ' . $this->quoteInteger($obj_id)
36 );
37
38 foreach ($res as $row) {
39 return (bool) $row['blocked'];
40 }
41 return false;
42 }
43
44 public function setHarvestingBlocked(int $obj_id, bool $blocked): void
45 {
46 $this->manipulate(
47 'INSERT INTO il_meta_oer_stat (obj_id, href_id, blocked) VALUES (' .
48 $this->quoteInteger($obj_id) . ', ' .
49 $this->quoteInteger(0) . ', ' .
50 $this->quoteInteger((int) $blocked) . ') ' .
51 'ON DUPLICATE KEY UPDATE blocked = ' . $this->quoteInteger((int) $blocked)
52 );
53 }
54
55 public function isAlreadyHarvested(int $obj_id): bool
56 {
57 $res = $this->query(
58 'SELECT href_id FROM il_meta_oer_stat WHERE obj_id = ' . $this->quoteInteger($obj_id)
59 );
60
61 foreach ($res as $row) {
62 return (bool) $row['href_id'];
63 }
64 return false;
65 }
66
70 public function getAllHarvestedObjIDs(): \Generator
71 {
72 $res = $this->query(
73 'SELECT obj_id FROM il_meta_oer_stat WHERE href_id > 0'
74 );
75
76 foreach ($res as $row) {
77 yield (int) $row['obj_id'];
78 }
79 }
80
81 public function getHarvestRefID(int $obj_id): int
82 {
83 $res = $this->query(
84 'SELECT href_id FROM il_meta_oer_stat WHERE obj_id = ' . $this->quoteInteger($obj_id)
85 );
86
87 foreach ($res as $row) {
88 return (int) $row['href_id'];
89 }
90 return 0;
91 }
92
93 public function setHarvestRefID(int $obj_id, int $harvested_ref_id): void
94 {
95 $this->manipulate(
96 'INSERT INTO il_meta_oer_stat (obj_id, href_id, blocked) VALUES (' .
97 $this->quoteInteger($obj_id) . ', ' .
98 $this->quoteInteger($harvested_ref_id) . ', ' .
99 $this->quoteInteger(0) . ') ' .
100 'ON DUPLICATE KEY UPDATE href_id = ' . $this->quoteInteger($harvested_ref_id)
101 );
102 }
103
104 public function deleteHarvestRefID(int $obj_id): void
105 {
106 $this->manipulate(
107 'UPDATE il_meta_oer_stat SET href_id = 0 WHERE obj_id = ' . $this->quoteInteger($obj_id)
108 );
109 }
110
114 public function filterOutBlockedObjects(int ...$obj_ids): \Generator
115 {
116 $res = $this->query(
117 'SELECT obj_id FROM il_meta_oer_stat WHERE blocked = 1 AND ' .
118 $this->inWithIntegers('obj_id', ...$obj_ids)
119 );
120
121 $blocked_ids = [];
122 foreach ($res as $row) {
123 $blocked_ids[] = (int) $row['obj_id'];
124 }
125
126 foreach ($obj_ids as $obj_id) {
127 if (!in_array($obj_id, $blocked_ids)) {
128 yield $obj_id;
129 }
130 }
131 }
132
133 public function deleteStatus(int $obj_id): void
134 {
135 $this->manipulate(
136 'DELETE FROM il_meta_oer_stat WHERE obj_id = ' . $this->quoteInteger($obj_id)
137 );
138 }
139
140 protected function query(string $query): \Generator
141 {
142 $res = $this->db->query($query);
143 while ($row = $res->fetchAssoc()) {
144 yield $row;
145 }
146 }
147
148 protected function manipulate(string $query): void
149 {
150 $this->db->manipulate($query);
151 }
152
153 protected function quoteInteger(int $integer): string
154 {
155 return $this->db->quote($integer, \ilDBConstants::T_INTEGER);
156 }
157
158 protected function inWithIntegers(string $field, int ...$integers): string
159 {
160 return $this->db->in($field, $integers, false, \ilDBConstants::T_INTEGER);
161 }
162}
Interface ilDBInterface.
$res
Definition: ltiservices.php:69