ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.FlashcardManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\Factory as DataFactory;
26use DateTime;
27
32{
40 protected int $glo_id;
41 protected int $user_id;
42 protected \ilObjGlossary $glossary;
47 protected array $all_glossary_term_ids = [];
48
49 public function __construct(
50 Glossary\InternalDomainService $domain_service,
53 int $glo_ref_id,
54 int $user_id
55 ) {
56 $data_factory = new DataFactory();
57
58 $this->domain = $domain_service;
59 $this->shuffle_manager = $this->domain->flashcardShuffle();
60 $this->repo = $repo;
61 $this->data_service = $data_service;
62 $this->term_db_repo = $this->repo->flashcardTerm();
63 $this->box_db_repo = $this->repo->flashcardBox();
64 $this->session_repo = $this->repo->flashcardSession();
65 $this->glo_id = \ilObject::_lookupObjectId($glo_ref_id);
66 $this->user_id = $user_id;
67 $this->glossary = new \ilObjGlossary($glo_ref_id);
68 $this->clock = $data_factory->clock()->system();
69
70 $all_glossary_terms = $this->glossary->getTermList(
71 "",
72 "",
73 "",
74 0,
75 false,
76 false,
77 null,
78 false,
79 true
80 );
81 foreach ($all_glossary_terms as $term) {
82 $this->all_glossary_term_ids[] = (int) $term["id"];
83 }
84 }
85
86 protected function getNow(): \DateTimeImmutable
87 {
88 $now = $this->clock->now();
89
90 return $now;
91 }
92
93 public function setSessionInitialTerms(
94 int $box_nr,
95 array $initial_terms
96 ): void {
97 $this->session_repo->setInitialTerms($this->glo_id, $this->user_id, $box_nr, $initial_terms);
98 }
99
103 public function getSessionInitialTerms(
104 int $box_nr
105 ): array {
106 return $this->session_repo->getInitialTerms($this->glo_id, $this->user_id, $box_nr);
107 }
108
109 public function setSessionTerms(
110 int $box_nr,
111 array $terms
112 ): void {
113 $this->session_repo->setTerms($this->glo_id, $this->user_id, $box_nr, $terms);
114 }
115
119 public function getSessionTerms(
120 int $box_nr
121 ): array {
122 return $this->session_repo->getTerms($this->glo_id, $this->user_id, $box_nr);
123 }
124
128 public function getAllTermsWithoutEntry(): array
129 {
130 $terms_with_entry = array_map(fn($term) => $term->getTermId(), $this->getAllUserTerms());
131
132 $terms_without_entry = [];
133 foreach ($this->all_glossary_term_ids as $term_id) {
134 if (!in_array($term_id, $terms_with_entry)) {
135 $terms_without_entry[] = $this->data_service->flashcardTerm($term_id, $this->user_id, $this->glo_id, 1);
136 }
137 }
138 $terms_without_entry = $this->shuffle_manager->shuffleEntries($terms_without_entry);
139
140 return $terms_without_entry;
141 }
142
146 public function getAllUserTerms(): array
147 {
148 $entries = $this->term_db_repo->getAllUserEntries($this->user_id, $this->glo_id);
149
150 return $entries;
151 }
152
160 protected function filterTermsNotInGlossary(
161 array $terms
162 ): array {
163 $terms_filtered = [];
164 foreach ($terms as $term) {
165 if (in_array($term->getTermId(), $this->all_glossary_term_ids)) {
166 $terms_filtered[] = $term;
167 }
168 }
169
170 return $terms_filtered;
171 }
172
176 public function getUserTermsForBox(
177 int $box_nr
178 ): array {
179 $entries = $this->term_db_repo->getUserEntriesForBox($box_nr, $this->user_id, $this->glo_id);
180 $entries = $this->shuffle_manager->shuffleEntriesWithEqualDay($entries);
181 $entries = $this->filterTermsNotInGlossary($entries);
182
183 return $entries;
184 }
185
190 int $box_nr
191 ): array {
192 $entries = $this->term_db_repo->getUserEntriesForBox($box_nr, $this->user_id, $this->glo_id);
193 $entries = $this->shuffle_manager->shuffleEntriesWithEqualDay($entries);
194 $non_recent_terms = [];
195 foreach ($entries as $entry) {
196 $entry_day = substr($entry->getLastAccess(), 0, 10);
197 $today = $this->getNow()->format("Y-m-d");
198 if ($entry_day !== $today) {
199 $non_recent_terms[] = $entry;
200 }
201 }
202 $non_recent_terms = $this->filterTermsNotInGlossary($non_recent_terms);
203
204 return $non_recent_terms;
205 }
206
210 public function getTodayUserTermsForBox(
211 int $box_nr
212 ): array {
213 $entries = $this->term_db_repo->getUserEntriesForBox($box_nr, $this->user_id, $this->glo_id);
214 $recent_terms = [];
215 foreach ($entries as $entry) {
216 $entry_day = substr($entry->getLastAccess(), 0, 10);
217 $today = $this->getNow()->format("Y-m-d");
218 if ($entry_day === $today) {
219 $recent_terms[] = $entry;
220 }
221 }
222 $recent_terms = $this->shuffle_manager->shuffleEntries($recent_terms);
223 $recent_terms = $this->filterTermsNotInGlossary($recent_terms);
224
225 return $recent_terms;
226 }
227
228 public function getItemsForBoxCount(
229 int $box_nr
230 ): int {
231 if ($box_nr === FlashcardBox::FIRST_BOX) {
232 $items_without_box = count($this->getAllTermsWithoutEntry());
233 $items_in_box = count($this->getUserTermsForBox($box_nr));
234 $item_cnt = $items_without_box + $items_in_box;
235 } else {
236 $item_cnt = count($this->getUserTermsForBox($box_nr));
237 }
238
239 return $item_cnt;
240 }
241
242 public function getLastAccessForBox(
243 int $box_nr
244 ): ?string {
245 $entry = $this->box_db_repo->getEntry($box_nr, $this->user_id, $this->glo_id);
246
247 return $entry?->getLastAccess();
248 }
249
251 int $box_nr
252 ): string {
253 $lng = $this->domain->lng();
254 $date_str = $this->getLastAccessForBox($box_nr);
255 if (!$date_str) {
256 return $lng->txt("never");
257 }
258 $date_tmp = new \ilDateTime($date_str, IL_CAL_DATETIME);
259 $date = new DateTime($date_tmp->get(IL_CAL_DATE));
260 $now = new DateTime($this->getNow()->format("Y-m-d"));
261 $diff = $date->diff($now)->days;
262 if ($diff < 0) {
263 return "invalid";
264 } elseif ($diff === 0) {
265 return $lng->txt("today");
266 } elseif ($diff === 1) {
267 return $lng->txt("yesterday");
268 } else {
269 return sprintf($lng->txt("glo_days_ago"), $diff);
270 }
271 }
272
273 public function getBoxNr(
274 int $term_id
275 ): ?int {
276 $entry = $this->term_db_repo->getEntry($term_id, $this->user_id, $this->glo_id);
277
278 return $entry?->getBoxNr();
279 }
280
282 int $box_nr
283 ): void {
284 $now = $this->getNow()->format("Y-m-d H:i:s");
285 $box = $this->data_service->flashcardBox($box_nr, $this->user_id, $this->glo_id, $now);
286 $this->box_db_repo->createOrUpdateEntry($box);
287 }
288
290 int $term_id,
291 bool $correct
292 ): void {
293 $box_nr = $this->getBoxNr($term_id);
294 $now = $this->getNow()->format("Y-m-d H:i:s");
295
296 if ($box_nr) {
297 $box_nr = $correct ? ($box_nr + 1) : 1;
298 $term = $this->data_service->flashcardTerm($term_id, $this->user_id, $this->glo_id, $box_nr, $now);
299 $this->term_db_repo->updateEntry($term);
300 } else {
301 $box_nr = $correct ? 2 : 1;
302 $term = $this->data_service->flashcardTerm($term_id, $this->user_id, $this->glo_id, $box_nr, $now);
303 $this->term_db_repo->createEntry($term);
304 }
305 }
306
307 public function resetEntries(): void
308 {
309 $this->term_db_repo->deleteEntries($this->glo_id, $this->user_id);
310 $this->box_db_repo->deleteEntries($this->glo_id, $this->user_id);
311 }
312
313 public function deleteAllUserEntries(): void
314 {
315 $this->term_db_repo->deleteAllUserEntries($this->user_id);
316 $this->box_db_repo->deleteAllUserEntries($this->user_id);
317 }
318
319 public function deleteAllGlossaryEntries(): void
320 {
321 if ($this->glo_id === 0) {
322 throw new \ilGlossaryException("No glossary id given in FlashcardManager.");
323 }
324 $this->term_db_repo->deleteAllGlossaryEntries($this->glo_id);
325 $this->box_db_repo->deleteAllGlossaryEntries($this->glo_id);
326 }
327
328 public function deleteAllTermEntries(
329 int $term_id
330 ): void {
331 $this->term_db_repo->deleteAllTermEntries($term_id);
332 }
333}
Builds data types.
Definition: Factory.php:36
Class for glossary flashcard box.
FlashcardSessionRepositoryInterface $session_repo
createOrUpdateUserTermEntry(int $term_id, bool $correct)
setSessionInitialTerms(int $box_nr, array $initial_terms)
Glossary InternalRepoServiceInterface $repo
__construct(Glossary\InternalDomainService $domain_service, Glossary\InternalRepoServiceInterface $repo, Glossary\InternalDataService $data_service, int $glo_ref_id, int $user_id)
filterTermsNotInGlossary(array $terms)
Filter out the terms, for which already exist entries, but are not part of the glossary currently/any...
const IL_CAL_DATE
const IL_CAL_DATETIME
static _lookupObjectId(int $ref_id)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))