ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilGlossaryFlashcardBoxGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 use ILIAS\UI;
24 
30 {
31  protected ilCtrl $ctrl;
32  protected ilLanguage $lng;
34  protected UI\Factory $ui_fac;
35  protected UI\Renderer $ui_ren;
38  protected int $box_nr = 0;
39  protected array $initial_terms_in_box = [];
40  protected array $terms_in_box = [];
41  protected int $current_term_id = 0;
43 
44  public function __construct()
45  {
46  global $DIC;
47 
48  $this->ctrl = $DIC->ctrl();
49  $this->lng = $DIC->language();
50  $this->tpl = $DIC->ui()->mainTemplate();
51  $this->ui_fac = $DIC->ui()->factory();
52  $this->ui_ren = $DIC->ui()->renderer();
53  $this->request = $DIC->glossary()
54  ->internal()
55  ->gui()
56  ->presentation()
57  ->request();
58  $gs = $DIC->glossary()->internal();
59  $this->manager = $gs->domain()->flashcard($this->request->getRefId());
60 
61  $this->ctrl->saveParameter($this, ["box_id"]);
62  $this->box_nr = $this->request->getBoxId();
63  $this->initial_terms_in_box = $this->manager->getSessionInitialTerms($this->box_nr);
64  $this->terms_in_box = $this->manager->getSessionTerms($this->box_nr);
65  $this->current_term_id = $this->terms_in_box[0] ?? 0;
66  $this->glossary = new ilObjGlossary($this->request->getRefId());
67  }
68 
69  public function executeCommand(): void
70  {
71  $next_class = $this->ctrl->getNextClass($this);
72  $cmd = $this->ctrl->getCmd();
73 
74  switch ($next_class) {
75  default:
76  $cmd = $this->ctrl->getCmd("show");
77  $ret = $this->$cmd();
78  break;
79  }
80  }
81 
82  public function show(): void
83  {
84  if ($this->box_nr === Flashcard\FlashcardBox::FIRST_BOX) {
85  $cnt_all = count($this->manager->getUserTermIdsForBox($this->box_nr))
86  + count($this->manager->getAllTermsWithoutEntry());
87  $cnt_remaining = count($this->manager->getNonTodayUserTermIdsForBox($this->box_nr))
88  + count($this->manager->getAllTermsWithoutEntry());
89  } else {
90  $cnt_all = count($this->manager->getUserTermIdsForBox($this->box_nr));
91  $cnt_remaining = count($this->manager->getNonTodayUserTermIdsForBox($this->box_nr));
92  }
93  $cnt_today = count($this->manager->getTodayUserTermIdsForBox($this->box_nr));
94 
95  if (($this->box_nr === Flashcard\FlashcardBox::FIRST_BOX
96  && !$this->manager->getAllTermsWithoutEntry()
97  && !$this->manager->getNonTodayUserTermIdsForBox($this->box_nr)
98  && $this->manager->getTodayUserTermIdsForBox($this->box_nr))
99  || ($this->box_nr !== Flashcard\FlashcardBox::FIRST_BOX
100  && !$this->manager->getNonTodayUserTermIdsForBox($this->box_nr)
101  && $this->manager->getTodayUserTermIdsForBox($this->box_nr))) {
102  $all_button = $this->ui_fac->button()->standard(
103  sprintf($this->lng->txt("glo_use_all_flashcards"), $cnt_all),
104  $this->ctrl->getLinkTarget($this, "showAllItems")
105  );
106  $cbox = $this->ui_fac->messageBox()->confirmation(
107  sprintf($this->lng->txt("glo_flashcards_from_today_only_info"), $cnt_all)
108  )->withButtons([$all_button]);
109  $this->tpl->setContent($this->ui_ren->render($cbox));
110  } elseif ($this->manager->getTodayUserTermIdsForBox($this->box_nr)) {
111  $remaining_button = $this->ui_fac->button()->standard(
112  sprintf($this->lng->txt("glo_use_remaining_flashcards"), $cnt_remaining),
113  $this->ctrl->getLinkTarget($this, "showRemainingItems")
114  );
115  $all_button = $this->ui_fac->button()->standard(
116  sprintf($this->lng->txt("glo_use_all_flashcards"), $cnt_all),
117  $this->ctrl->getLinkTarget($this, "showAllItems")
118  );
119  $cbox = $this->ui_fac->messageBox()->confirmation(
120  sprintf($this->lng->txt("glo_flashcards_from_today_confirmation"), $cnt_today, $cnt_remaining, $cnt_all)
121  )->withButtons([$remaining_button, $all_button]);
122  $this->tpl->setContent($this->ui_ren->render($cbox));
123  } else {
124  $this->showAllItems();
125  }
126  }
127 
128  public function showAllItems(): void
129  {
130  $this->showItems(true);
131  }
132 
133  public function showRemainingItems(): void
134  {
135  $this->showItems(false);
136  }
137 
138  public function showItems(bool $all): void
139  {
140  if ($all) {
141  $terms = $this->manager->getUserTermIdsForBox($this->box_nr);
142  } else {
143  $terms = $this->manager->getNonTodayUserTermIdsForBox($this->box_nr);
144  }
145  if ($this->box_nr === Flashcard\FlashcardBox::FIRST_BOX) {
146  $terms_without_entry = $this->manager->getAllTermsWithoutEntry();
147  $terms = array_merge($terms_without_entry, $terms);
148  }
149  $this->manager->setSessionInitialTerms($this->box_nr, $terms);
150  $this->manager->setSessionTerms($this->box_nr, $terms);
151  $this->manager->createOrUpdateBoxAccessEntry($this->box_nr);
152  $this->ctrl->redirect($this, "showHidden");
153  }
154 
155  public function showHidden(): void
156  {
157  $this->tpl->setDescription($this->lng->txt("glo_box") . " " . $this->box_nr);
158 
159  $progress = $this->manager->getBoxProgress($this->terms_in_box, $this->initial_terms_in_box);
160  $progress_bar = ilProgressBar::getInstance();
161  $progress_bar->setCurrent($progress);
162 
163  if ($this->glossary->getFlashcardsMode() === "term") {
164  $flashcard = $this->ui_fac->panel()->standard(
165  $this->lng->txt("term") . ": " . $this->getTermText(),
166  $this->ui_fac->legacy("???")
167  );
168  } else {
169  $flashcard = $this->ui_fac->panel()->standard(
170  $this->lng->txt("term") . ": ???",
171  $this->ui_fac->legacy($this->getDefinitionPage())
172  );
173  }
174 
175  if ($this->glossary->getFlashcardsMode() === "term") {
176  $btn_show = $this->ui_fac->button()->standard(
177  $this->lng->txt("glo_show_definition"),
178  $this->ctrl->getLinkTarget($this, "showRevealed")
179  );
180  } else {
181  $btn_show = $this->ui_fac->button()->standard(
182  $this->lng->txt("glo_show_term"),
183  $this->ctrl->getLinkTarget($this, "showRevealed")
184  );
185  }
186 
187  $btn_quit = $this->ui_fac->button()->standard(
188  $this->lng->txt("glo_quit_box"),
189  $this->ctrl->getLinkTargetByClass("ilglossarypresentationgui", "showFlashcards")
190  );
191 
192  $html = $progress_bar->render()
193  . $this->ui_ren->render($flashcard)
194  . $this->ui_ren->render($btn_show)
195  . $this->ui_ren->render($btn_quit);
196  $this->tpl->setContent($html);
197  }
198 
199  public function showRevealed(): void
200  {
201  $this->tpl->setDescription($this->lng->txt("glo_box") . " " . $this->box_nr);
202 
203 
204  $progress = $this->manager->getBoxProgress($this->terms_in_box, $this->initial_terms_in_box);
205  $progress_bar = ilProgressBar::getInstance();
206  $progress_bar->setCurrent($progress);
207 
208  $flashcard = $this->ui_fac->panel()->standard(
209  $this->lng->txt("term") . ": " . $this->getTermText(),
210  $this->ui_fac->legacy($this->getDefinitionPage())
211  );
212 
213  $btn_correct = $this->ui_fac->button()->standard(
214  $this->lng->txt("glo_answered_correctly"),
215  $this->ctrl->getLinkTarget($this, "answerCorrectly")
216  );
217 
218  $btn_not_correct = $this->ui_fac->button()->standard(
219  $this->lng->txt("glo_answered_not_correctly"),
220  $this->ctrl->getLinkTarget($this, "answerInCorrectly")
221  );
222 
223  $html = $progress_bar->render()
224  . $this->ui_ren->render($flashcard)
225  . $this->ui_ren->render($btn_correct)
226  . $this->ui_ren->render($btn_not_correct);
227  $this->tpl->setContent($html);
228  }
229 
230  public function answerCorrectly(): void
231  {
232  $this->answer(true);
233  }
234 
235  public function answerInCorrectly(): void
236  {
237  $this->answer(false);
238  }
239 
240  public function answer(bool $correct): void
241  {
242  $this->manager->createOrUpdateUserTermEntry($this->current_term_id, $correct);
243  array_shift($this->terms_in_box);
244  $this->manager->setSessionTerms($this->box_nr, $this->terms_in_box);
245  if ($this->terms_in_box) {
246  $this->ctrl->redirect($this, "showHidden");
247  }
248  $this->tpl->setOnScreenMessage("success", $this->lng->txt("glo_box_completed"), true);
249  $this->ctrl->redirectByClass("ilglossarypresentationgui", "showFlashcards");
250  }
251 
252  protected function getTermText(): string
253  {
254  $text = ilGlossaryTerm::_lookGlossaryTerm($this->current_term_id);
255  return $text;
256  }
257 
258  protected function getDefinitionPage(): string
259  {
260  $page_gui = new ilGlossaryDefPageGUI($this->current_term_id);
261  return $page_gui->showPage();
262  }
263 }
Presentation PresentationGUIRequest $request
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
GUI class for glossary flashcard boxes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
static _lookGlossaryTerm(int $term_id)
get glossary term
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...