ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.FlashcardManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Glossary\Flashcard;
22 
23 use ILIAS\Glossary;
27 use DateTime;
28 
33 {
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  InternalDomainService $domain_service,
51  Glossary\InternalRepoServiceInterface $repo,
52  int $glo_ref_id,
53  int $user_id
54  ) {
55  $data_factory = new DataFactory();
56 
57  $this->domain = $domain_service;
58  $this->shuffle_manager = $this->domain->flashcardShuffle();
59  $this->repo = $repo;
60  $this->term_db_repo = $this->repo->flashcardTerm();
61  $this->box_db_repo = $this->repo->flashcardBox();
62  $this->session_repo = $this->repo->flashcardSession();
63  $this->glo_id = \ilObject::_lookupObjectId($glo_ref_id);
64  $this->user_id = $user_id;
65  $this->glossary = new \ilObjGlossary($glo_ref_id);
66  $this->clock = $data_factory->clock()->system();
67 
68  $all_glossary_terms = $this->glossary->getTermList(
69  "",
70  "",
71  "",
72  0,
73  false,
74  false,
75  null,
76  false,
77  true
78  );
79  foreach ($all_glossary_terms as $term) {
80  $term_id = (int) $term["id"];
81  $this->all_glossary_term_ids[] = $term_id;
82  }
83  }
84 
85  public function setSessionInitialTerms(
86  int $box_nr,
87  array $initial_terms
88  ): void {
89  $this->session_repo->setInitialTerms($this->glo_id, $this->user_id, $box_nr, $initial_terms);
90  }
91 
95  public function getSessionInitialTerms(
96  int $box_nr
97  ): array {
98  return $this->session_repo->getInitialTerms($this->glo_id, $this->user_id, $box_nr);
99  }
100 
101  public function setSessionTerms(
102  int $box_nr,
103  array $terms
104  ): void {
105  $this->session_repo->setTerms($this->glo_id, $this->user_id, $box_nr, $terms);
106  }
107 
111  public function getSessionTerms(
112  int $box_nr
113  ): array {
114  return $this->session_repo->getTerms($this->glo_id, $this->user_id, $box_nr);
115  }
116 
120  public function getAllTermsWithoutEntry(): array
121  {
122  $terms_with_entry = $this->getAllUserTermIds();
123 
124  $terms_without_entry = [];
125  foreach ($this->all_glossary_term_ids as $term_id) {
126  if (!in_array($term_id, $terms_with_entry)) {
127  $terms_without_entry[] = $term_id;
128  }
129  }
130  $terms_without_entry = $this->shuffle_manager->shuffleEntries($terms_without_entry);
131 
132  return $terms_without_entry;
133  }
134 
138  public function getAllUserTermIds(): array
139  {
140  $entries = $this->term_db_repo->getAllUserEntries($this->user_id, $this->glo_id);
141  $term_ids = [];
142  foreach ($entries as $entry) {
143  $term_ids[] = (int) $entry["term_id"];
144  }
145 
146  return $term_ids;
147  }
148 
156  protected function filterTermsNotInGlossary(
157  array $term_ids
158  ): array {
159  $term_ids_filtered = [];
160  foreach ($term_ids as $id) {
161  if (in_array($id, $this->all_glossary_term_ids)) {
162  $term_ids_filtered[] = $id;
163  }
164  }
165 
166  return $term_ids_filtered;
167  }
168 
172  public function getUserTermIdsForBox(
173  int $box_nr
174  ): array {
175  $entries = $this->term_db_repo->getUserEntriesForBox($box_nr, $this->user_id, $this->glo_id);
176  $entries = $this->shuffle_manager->shuffleEntriesWithEqualDay($entries);
177  $term_ids = [];
178  foreach ($entries as $entry) {
179  $term_ids[] = (int) $entry["term_id"];
180  }
181  $term_ids = $this->filterTermsNotInGlossary($term_ids);
182 
183  return $term_ids;
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_term_ids = [];
195  foreach ($entries as $entry) {
196  $entry_day = substr($entry["last_access"], 0, 10);
197  $today = $this->clock->now()->format("Y-m-d");
198  if ($entry_day !== $today) {
199  $non_recent_term_ids[] = (int) $entry["term_id"];
200  }
201  }
202  $non_recent_term_ids = $this->filterTermsNotInGlossary($non_recent_term_ids);
203 
204  return $non_recent_term_ids;
205  }
206 
210  public function getTodayUserTermIdsForBox(
211  int $box_nr
212  ): array {
213  $entries = $this->term_db_repo->getUserEntriesForBox($box_nr, $this->user_id, $this->glo_id);
214  $recent_term_ids = [];
215  foreach ($entries as $entry) {
216  $entry_day = substr($entry["last_access"], 0, 10);
217  $today = $this->clock->now()->format("Y-m-d");
218  if ($entry_day === $today) {
219  $recent_term_ids[] = (int) $entry["term_id"];
220  }
221  }
222  $recent_term_ids = $this->shuffle_manager->shuffleEntries($recent_term_ids);
223  $recent_term_ids = $this->filterTermsNotInGlossary($recent_term_ids);
224 
225  return $recent_term_ids;
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->getUserTermIdsForBox($box_nr));
234  $item_cnt = $items_without_box + $items_in_box;
235  } else {
236  $item_cnt = count($this->getUserTermIdsForBox($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  $date = $entry["last_access"] ?? null;
247 
248  return $date;
249  }
250 
252  int $box_nr
253  ): string {
254  $lng = $this->domain->lng();
255  $date_str = $this->getLastAccessForBox($box_nr);
256  if (!$date_str) {
257  return $lng->txt("never");
258  }
259  $date_tmp = new \ilDateTime($date_str, IL_CAL_DATETIME);
260  $date = new DateTime($date_tmp->get(IL_CAL_DATE));
261  $now = new DateTime($this->clock->now()->format("Y-m-d"));
262  $diff = $date->diff($now)->days;
263  if ($diff === 0) {
264  return $lng->txt("today");
265  } elseif ($diff === 1) {
266  return $lng->txt("yesterday");
267  } else {
268  return sprintf($lng->txt("glo_days_ago"), $diff);
269  }
270  }
271 
272  public function getBoxNr(
273  int $term_id
274  ): int {
275  return $this->term_db_repo->getBoxNr($term_id, $this->user_id, $this->glo_id);
276  }
277 
278  public function getBoxProgress(
279  array $current_terms,
280  array $all_terms
281  ): int {
282  $shown_terms_cnt = count($all_terms) - count($current_terms);
283  $progress = (int) round((($shown_terms_cnt + 1) / count($all_terms)) * 100);
284 
285  return $progress;
286  }
287 
289  int $box_nr
290  ): void {
291  $now = $this->clock->now()->format("Y-m-d H:i:s");
292  $this->box_db_repo->createOrUpdateEntry($box_nr, $this->user_id, $this->glo_id, $now);
293  }
294 
296  int $term_id,
297  bool $correct
298  ): void {
299  $box_nr = $this->getBoxNr($term_id);
300  $now = $this->clock->now()->format("Y-m-d H:i:s");
301 
302  if ($box_nr !== 0) {
303  $box_nr = $correct ? ($box_nr + 1) : 1;
304  $this->term_db_repo->updateEntry($term_id, $this->user_id, $this->glo_id, $box_nr, $now);
305  } else {
306  $box_nr = $correct ? 2 : 1;
307  $this->term_db_repo->createEntry($term_id, $this->user_id, $this->glo_id, $box_nr, $now);
308  }
309  }
310 
311  public function resetEntries(): void
312  {
313  $this->term_db_repo->deleteEntries($this->glo_id, $this->user_id);
314  $this->box_db_repo->deleteEntries($this->glo_id, $this->user_id);
315  }
316 
317  public function deleteAllUserEntries(): void
318  {
319  $this->term_db_repo->deleteAllUserEntries($this->user_id);
320  $this->box_db_repo->deleteAllUserEntries($this->user_id);
321  }
322 
323  public function deleteAllGlossaryEntries(): void
324  {
325  if ($this->glo_id === 0) {
326  throw new \ilGlossaryException("No glossary id given in FlashcardManager.");
327  }
328  $this->term_db_repo->deleteAllGlossaryEntries($this->glo_id);
329  $this->box_db_repo->deleteAllGlossaryEntries($this->glo_id);
330  }
331 
332  public function deleteAllTermEntries(
333  int $term_id
334  ): void {
335  $this->term_db_repo->deleteAllTermEntries($term_id);
336  }
337 }
filterTermsNotInGlossary(array $term_ids)
Filter out the terms, for which already exist entries, but are not part of the glossary currently/any...
const IL_CAL_DATETIME
setSessionInitialTerms(int $box_nr, array $initial_terms)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lng
static _lookupObjectId(int $ref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_DATE
__construct(InternalDomainService $domain_service, Glossary\InternalRepoServiceInterface $repo, int $glo_ref_id, int $user_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
createOrUpdateUserTermEntry(int $term_id, bool $correct)
getBoxProgress(array $current_terms, array $all_terms)
Glossary InternalRepoServiceInterface $repo