ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.TermManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
30 {
31  protected \ilAppEventHandler $event_handler;
34  protected \ilObjGlossary $glossary;
35  protected int $user_id;
36 
37  public function __construct(
38  InternalDomainService $domain_service,
39  TermSessionRepository $session_repo,
40  \ilObjGlossary $glossary,
41  int $user_id
42  ) {
43  global $DIC;
44 
45  $this->session_repo = $session_repo;
46  $this->glossary = $glossary;
47  $this->user_id = $user_id;
48  $this->domain = $domain_service;
49  $this->event_handler = $DIC->event();
50  }
51 
52  public function setSessionLang(string $lang): void
53  {
54  $this->session_repo->setLang($this->glossary->getRefId(), $lang);
55  }
56 
57  public function getSessionLang(): string
58  {
59  return $this->session_repo->getLang($this->glossary->getRefId());
60  }
61 
62  public function copyTermFromOtherGlossary(
63  int $other_glossary_ref_id,
64  int $term_id
65  ): void {
66  $access = $this->domain->access();
67 
68  if (!$access->checkAccessOfUser(
69  $this->user_id,
70  "write",
71  "",
72  $this->glossary->getRefId()
73  )
74  ) {
75  return;
76  }
77 
78  if (!$access->checkAccessOfUser(
79  $this->user_id,
80  "read",
81  "",
82  $other_glossary_ref_id
83  )) {
84  return;
85  }
86 
87  if (\ilGlossaryTerm::_lookGlossaryID($term_id) !=
88  \ilObject::_lookupObjectId($other_glossary_ref_id)) {
89  return;
90  }
91 
92  \ilGlossaryTerm::_copyTerm($term_id, $this->glossary->getId());
93  }
94 
99  int $other_glossary_ref_id,
100  array $term_ids
101  ): void {
102  $access = $this->domain->access();
103 
104  if (!$access->checkAccessOfUser(
105  $this->user_id,
106  "write",
107  "",
108  $this->glossary->getRefId()
109  )) {
110  return;
111  }
112 
113  if (!$access->checkAccessOfUser(
114  $this->user_id,
115  "read",
116  "",
117  $other_glossary_ref_id
118  )) {
119  return;
120  }
121 
122  $other_glossary_obj_id = \ilObject::_lookupObjectId($other_glossary_ref_id);
123  $refs = new \ilGlossaryTermReferences($this->glossary->getId());
124  foreach ($term_ids as $term_id) {
125  if (\ilGlossaryTerm::_lookGlossaryID($term_id) != $other_glossary_obj_id) {
126  continue;
127  }
128 
129  if ($this->glossary->getId() == $other_glossary_obj_id) {
130  continue;
131  }
132  $refs->addTerm($term_id);
133  }
134  $refs->update();
135  }
136 
137  public function getDataArrayFromInputString(string $input): array
138  {
139  $rows = explode("\n", $input);
140  $data = [];
141  foreach ($rows as $row) {
142  $cells = explode(";", $row);
143  if (count($cells) === 1) {
144  $cells = explode("\t", $row);
145  }
146  $data[] = [
147  "term" => trim($cells[0] ?? ""),
148  "definition" => trim($cells[1] ?? "")
149  ];
150  }
151  return $data;
152  }
153 
154  public function createTermDefinitionPairsFromBulkInputString(string $input, string $language): void
155  {
156  foreach ($this->getDataArrayFromInputString($input) as $data) {
157  $term = new \ilGlossaryTerm();
158  $term->setGlossaryId($this->glossary->getId());
159  $term->setTerm($data["term"]);
160  $term->setLanguage($language);
161  $term->setShortText($data["definition"]);
162  $term->create(true);
163 
164  $page_object = new \ilGlossaryDefPage();
165  $page_object->setId($term->getId());
166  $page_object->setParentId($this->glossary->getId());
167  $page_object->create(false);
168  $paragraph = new \ilPCParagraph($page_object);
169  $paragraph->create($page_object, "pg");
170  $paragraph->setLanguage($language);
171  $paragraph->setText($data["definition"]);
172  $page_object->update();
173  }
174  }
175 
176  public function deleteTerm(int $term_id): void
177  {
178  global $DIC;
179 
180  // todo: move to repo class
181  $db = $DIC->database();
182 
183  // delete term references
185 
186  // delete glossary_term record
187  $db->manipulate("DELETE FROM glossary_term " .
188  " WHERE id = " . $db->quote($term_id, "integer"));
189 
190  try {
191  $page_object = new \ilGlossaryDefPage($term_id);
192  $page_object->delete();
193  } catch (\Exception $e) {
194  }
195 
196  // delete flashcard entries
197  $this->event_handler->raise("Modules/Glossary", "deleteTerm", ["term_id" => $term_id]);
198  }
199 }
static deleteReferencesOfTerm(int $a_term_id)
Delete all references of a term.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
TermSessionRepository $session_repo
static _copyTerm(int $a_term_id, int $a_glossary_id)
Copy a term to a glossary.
createTermDefinitionPairsFromBulkInputString(string $input, string $language)
__construct(InternalDomainService $domain_service, TermSessionRepository $session_repo, \ilObjGlossary $glossary, int $user_id)
global $DIC
Definition: shib_login.php:26
static _lookupObjectId(int $ref_id)
referenceTermsFromOtherGlossary(int $other_glossary_ref_id, array $term_ids)
Reference terms of another glossary in current glossary.
$lang
Definition: xapiexit.php:25
copyTermFromOtherGlossary(int $other_glossary_ref_id, int $term_id)
static _lookGlossaryID(int $term_id)
get glossary id form term id