ILIAS  release_8 Revision v8.23
class.ilSurveyPhrases.php
Go to the documentation of this file.
1 <?php
2 
29 {
30  protected ilDBInterface $db;
31  protected ilObjUser $user;
32 
33  protected array $arrData;
34 
35  public function __construct()
36  {
37  global $DIC;
38 
39  $this->db = $DIC->database();
40  $this->user = $DIC->user();
41  $this->arrData = array();
42  }
43 
50  public static function _getAvailablePhrases(
51  bool $useronly = false
52  ): array {
53  global $DIC;
54 
55  $ilUser = $DIC->user();
56  $ilDB = $DIC->database();
57  $lng = $DIC->language();
58 
59  $phrases = array();
60  $result = $ilDB->queryF(
61  "SELECT * FROM svy_phrase WHERE defaultvalue = %s OR owner_fi = %s ORDER BY title",
62  array('text', 'integer'),
63  array('1', $ilUser->getId())
64  );
65  while ($row = $ilDB->fetchObject($result)) {
66  if ((int) $row->defaultvalue === 1 && (int) $row->owner_fi === 0) {
67  if (!$useronly) {
68  $phrases[$row->phrase_id] = array(
69  "title" => $lng->txt($row->title),
70  "owner" => $row->owner_fi,
71  "org_title" => $row->title
72  );
73  }
74  } elseif ($ilUser->getId() === (int) $row->owner_fi) {
75  $phrases[$row->phrase_id] = array(
76  "title" => $row->title,
77  "owner" => $row->owner_fi
78  );
79  }
80  }
81  return $phrases;
82  }
83 
89  public static function _getCategoriesForPhrase(
90  int $phrase_id
91  ): array {
92  global $DIC;
93 
94  $ilDB = $DIC->database();
95  $lng = $DIC->language();
96 
97  $categories = array();
98  $result = $ilDB->queryF(
99  "SELECT svy_category.* FROM svy_category, svy_phrase_cat WHERE svy_phrase_cat.category_fi = svy_category.category_id AND svy_phrase_cat.phrase_fi = %s ORDER BY svy_phrase_cat.sequence",
100  array('integer'),
101  array($phrase_id)
102  );
103  while ($row = $ilDB->fetchObject($result)) {
104  if ((int) $row->defaultvalue === 1 && (int) $row->owner_fi === 0) {
105  $categories[$row->category_id] = $lng->txt($row->title);
106  } else {
107  $categories[$row->category_id] = $row->title;
108  }
109  }
110  return $categories;
111  }
112 
117  public function deletePhrases(
118  array $phrase_array
119  ): void {
120  $ilDB = $this->db;
121 
122  if (count($phrase_array) > 0) {
123  $ilDB->manipulate("DELETE FROM svy_phrase WHERE " . $ilDB->in('phrase_id', $phrase_array, false, 'integer'));
124  $ilDB->manipulate("DELETE FROM svy_phrase_cat WHERE " . $ilDB->in('phrase_fi', $phrase_array, false, 'integer'));
125  }
126  }
127 
128  public function updatePhrase(
129  int $phrase_id
130  ): void {
132  $ilDB = $this->db;
133 
134  $ilDB->manipulateF(
135  "UPDATE svy_phrase SET title = %s, tstamp = %s WHERE phrase_id = %s",
136  array('text','integer','integer'),
137  array($this->title, time(), $phrase_id)
138  );
139 
140  $ilDB->manipulateF(
141  "DELETE FROM svy_phrase_cat WHERE phrase_fi = %s",
142  array('integer'),
143  array($phrase_id)
144  );
145 
146  $counter = 1;
147  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++) {
148  $cat = $this->categories->getCategory($i);
149  $next_id = $ilDB->nextId('svy_category');
150  $affectedRows = $ilDB->manipulateF(
151  "INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, tstamp, neutral) VALUES (%s, %s, %s, %s, %s, %s)",
152  array('integer','text','text','integer','integer','text'),
153  array($next_id, $cat->title, 1, $ilUser->getId(), time(), $cat->neutral)
154  );
155  $category_id = $next_id;
156  $next_id = $ilDB->nextId('svy_phrase_cat');
157  $affectedRows = $ilDB->manipulateF(
158  "INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence) VALUES (%s, %s, %s, %s)",
159  array('integer', 'integer', 'integer','integer'),
160  array($next_id, $phrase_id, $category_id, $counter)
161  );
162  $counter++;
163  }
164  }
165 
169  public function savePhrase(): void
170  {
172  $ilDB = $this->db;
173 
174  $next_id = $ilDB->nextId('svy_phrase');
175  $ilDB->manipulateF(
176  "INSERT INTO svy_phrase (phrase_id, title, defaultvalue, owner_fi, tstamp) VALUES (%s, %s, %s, %s, %s)",
177  array('integer','text','text','integer','integer'),
178  array($next_id, $this->title, 1, $ilUser->getId(), time())
179  );
180  $phrase_id = $next_id;
181 
182  $counter = 1;
183  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++) {
184  $cat = $this->categories->getCategory($i);
185  $next_id = $ilDB->nextId('svy_category');
186  $affectedRows = $ilDB->manipulateF(
187  "INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, tstamp, neutral) VALUES (%s, %s, %s, %s, %s, %s)",
188  array('integer','text','text','integer','integer','text'),
189  array($next_id, $cat->title, 1, $ilUser->getId(), time(), $cat->neutral)
190  );
191  $category_id = $next_id;
192  $next_id = $ilDB->nextId('svy_phrase_cat');
193  $affectedRows = $ilDB->manipulateF(
194  "INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence) VALUES (%s, %s, %s, %s)",
195  array('integer', 'integer', 'integer','integer'),
196  array($next_id, $phrase_id, $category_id, $counter)
197  );
198  $counter++;
199  }
200  }
201 
205  public function __get(string $value)
206  {
207  switch ($value) {
208  default:
209  return $this->arrData[$value] ?? null;
210  }
211  }
212 
216  public function __set(string $key, $value): void
217  {
218  switch ($key) {
219  default:
220  $this->arrData[$key] = $value;
221  break;
222  }
223  }
224 }
$lng
__set(string $key, $value)
deletePhrases(array $phrase_array)
Delete phrases from the database.
global $DIC
Definition: feed.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193
static _getCategoriesForPhrase(int $phrase_id)
Gets the available categories for a given phrase.
savePhrase()
Saves a set of categories to a default phrase.
$ilUser
Definition: imgupload.php:34
static _getAvailablePhrases(bool $useronly=false)
Gets the available phrases from the database.
updatePhrase(int $phrase_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$i
Definition: metadata.php:41