ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
PersonalSettingsDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
28{
29 public function __construct(
30 private readonly \ilDBInterface $db,
31 private readonly \ilObjUser $user,
32 private readonly SettingsFactory $factory,
33 ) {
34 }
35
39 public function getForUser(?Range $range = null, ?Order $order = null): array
40 {
41 $query = "SELECT * FROM tst_test_defaults WHERE user_fi = %s ";
42
43 if ($order === null) {
44 $order = new Order('name', Order::ASC);
45 }
46 $query .= $order->join('ORDER BY', fn(...$o) => implode(' ', $o));
47
48 if ($range !== null) {
49 $query .= " LIMIT {$range->getLength()} OFFSET {$range->getStart()}";
50 }
51
52 $stmt = $this->db->queryF($query, [\ilDBConstants::T_INTEGER], [$this->user->getId()]);
53
54 $templates = [];
55 while ($row = $this->db->fetchAssoc($stmt)) {
56 $templates[$row['test_defaults_id']] = $this->factory->createTemplateFromDBRow($row);
57 }
58 return $templates;
59 }
60
61 public function countForUser(): int
62 {
63 $stmt = $this->db->queryF(
64 "SELECT COUNT(*) as cnt FROM tst_test_defaults WHERE user_fi = %s",
66 [$this->user->getId()]
67 );
68 return (int) $this->db->fetchAssoc($stmt)['cnt'];
69 }
70
75 public function getByIds(array $ids): array
76 {
77 $stmt = $this->db->query(
78 "SELECT * FROM tst_test_defaults WHERE {$this->db->in('test_defaults_id', $ids, false, \ilDBConstants::T_INTEGER)}",
79 );
80
81 $templates = [];
82 while ($row = $this->db->fetchAssoc($stmt)) {
83 $templates[$row['test_defaults_id']] = $this->factory->createTemplateFromDBRow($row);
84 }
85 return $templates;
86 }
87
88 public function getById(int $id): ?PersonalSettingsTemplate
89 {
90 $stmt = $this->db->queryF(
91 "SELECT * FROM tst_test_defaults WHERE test_defaults_id = %s",
93 [$id]
94 );
95
96 if ($row = $this->db->fetchAssoc($stmt)) {
97 return $this->factory->createTemplateFromDBRow($row);
98 }
99 return null;
100 }
101
102 public function create(
103 string $name,
104 string $description,
105 string $author,
106 ?\DateTimeImmutable $timestamp = null
108 // 1. Create new blank settings (required as a foreign key)
109 $new_settings_id = $this->db->nextId('tst_test_settings');
110 $this->db->insert(
111 'tst_test_settings',
112 [
113 'id' => [\ilDBConstants::T_INTEGER, $new_settings_id],
114 ]
115 );
116
117 // 2. Instantiate template
118 $template = new PersonalSettingsTemplate(
119 $this->db->nextId('tst_test_defaults'),
120 $this->user->getId(),
121 $name,
122 $description,
123 $author,
124 $timestamp ?? \DateTimeImmutable::createFromFormat('U', (string) time()), // this will be UTC
125 $new_settings_id
126 );
127
128 // 3. Store template in the database
129 $this->db->insert(
130 'tst_test_defaults',
131 [
132 'test_defaults_id' => [\ilDBConstants::T_INTEGER, $template->getId()],
133 'user_fi' => [\ilDBConstants::T_INTEGER, $template->getUserId()],
134 'name' => [\ilDBConstants::T_TEXT, $template->getName()],
135 'description' => [\ilDBConstants::T_TEXT, $template->getDescription()],
136 'author' => [\ilDBConstants::T_TEXT, $template->getAuthor()],
137 'tstamp' => [\ilDBConstants::T_INTEGER, $template->getCreatedAt()->getTimestamp()],
138 'settings_id' => [\ilDBConstants::T_INTEGER, $template->getSettingsId()],
139 ]
140 );
141
142 return $template;
143 }
144
145 public function delete(PersonalSettingsTemplate $template): void
146 {
147 $this->db->manipulateF(
148 "DELETE FROM tst_test_defaults WHERE test_defaults_id = %s",
150 [$template->getId()]
151 );
152
153 $this->db->manipulateF(
154 "DELETE FROM tst_test_settings WHERE id = %s",
156 [$template->getSettingsId()]
157 );
158 }
159
163 public function lookupMarkSteps(int $template_id): array
164 {
165 $stmt = $this->db->queryF(
166 "SELECT mark_id FROM tst_defaults_marks WHERE tst_defaults_marks.defaults_id = %s",
168 [$template_id]
169 );
170 return array_column($this->db->fetchAll($stmt), 'mark_id');
171 }
172
176 public function associateMarkSteps(int $template_id, array $mark_ids): void
177 {
178 foreach ($mark_ids as $mark_id) {
179 $this->db->insert(
180 'tst_defaults_marks',
181 [
182 'defaults_id' => [\ilDBConstants::T_INTEGER, $template_id],
183 'mark_id' => [\ilDBConstants::T_INTEGER, $mark_id],
184 ]
185 );
186 }
187 }
188
192 public function detachMarkSteps(int $template_id, array $mark_ids): void
193 {
194 $in_marks = $this->db->in('mark_id', $mark_ids, false, \ilDBConstants::T_INTEGER);
195 $this->db->manipulate("DELETE FROM tst_defaults_marks WHERE {$in_marks} AND defaults_id = {$template_id}");
196 }
197}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
factory()
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
__construct(private readonly \ilDBInterface $db, private readonly \ilObjUser $user, private readonly SettingsFactory $factory,)
create(string $name, string $description, string $author, ?\DateTimeImmutable $timestamp=null)
User class.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...