ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLikeData.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
20 {
21  const TYPE_LIKE = 0;
22  const TYPE_DISLIKE = 1;
23  const TYPE_LOVE = 2;
24  const TYPE_LAUGH = 3;
25  const TYPE_ASTOUNDED = 4;
26  const TYPE_SAD = 5;
27  const TYPE_ANGRY = 6;
28 
33  protected $data = array();
34 
38  protected $db;
39 
43  protected $lng;
44 
49  public function __construct(array $a_obj_ids = array(), ilDB $db = null, $lng = null)
50  {
51  global $DIC;
52 
53  $this->db = ($db == null)
54  ? $DIC->database()
55  : $db;
56 
57  $this->lng = ($lng == null)
58  ? $DIC->language()
59  : $lng;
60  $this->loadDataForObjects($a_obj_ids);
61  $this->lng->loadLanguageModule("like");
62  }
63 
70  public function getExpressionTypes()
71  {
72  return array(
73  self::TYPE_LIKE => $this->lng->txt("like_like"),
74  self::TYPE_DISLIKE => $this->lng->txt("like_dislike"),
75  self::TYPE_LOVE => $this->lng->txt("like_love"),
76  self::TYPE_LAUGH => $this->lng->txt("like_laugh"),
77  self::TYPE_ASTOUNDED => $this->lng->txt("like_astounded"),
78  self::TYPE_SAD => $this->lng->txt("like_sad"),
79  self::TYPE_ANGRY => $this->lng->txt("like_angry")
80  );
81  }
82 
83 
95  public function addExpression(
96  $a_user_id,
97  $a_like_type,
98  $a_obj_id,
99  $a_obj_type,
100  $a_sub_obj_id = 0,
101  $a_sub_obj_type = "",
102  $a_news_id = 0
103  ) {
104  $ilDB = $this->db;
105 
106  if ($a_user_id == ANONYMOUS_USER_ID) {
107  return;
108  }
109 
110  $this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id] = 1;
111 
112  if ($a_sub_obj_type == "") {
113  $a_sub_obj_type = "-";
114  }
115 
116  $ilDB->replace(
117  "like_data",
118  array(
119  "user_id" => array("integer", (int) $a_user_id),
120  "obj_id" => array("integer", (int) $a_obj_id),
121  "obj_type" => array("text", $a_obj_type),
122  "sub_obj_id" => array("integer", (int) $a_sub_obj_id),
123  "sub_obj_type" => array("text", $a_sub_obj_type),
124  "news_id" => array("integer", (int) $a_news_id),
125  "like_type" => array("integer", (int) $a_like_type)
126  ),
127  array(
128  "exp_ts" => array("timestamp", ilUtil::now())
129  )
130  );
131  }
132 
144  public function removeExpression(
145  $a_user_id,
146  $a_like_type,
147  $a_obj_id,
148  $a_obj_type,
149  $a_sub_obj_id = 0,
150  $a_sub_obj_type = "",
151  $a_news_id = 0
152  ) {
153  $ilDB = $this->db;
154 
155  if ($a_user_id == ANONYMOUS_USER_ID) {
156  return;
157  }
158 
159  if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
160  unset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id]);
161  }
162 
163  if ($a_sub_obj_type == "") {
164  $a_sub_obj_type = "-";
165  }
166 
167  $ilDB->manipulate(
168  "DELETE FROM like_data WHERE " .
169  " user_id = " . $ilDB->quote($a_user_id, "integer") .
170  " AND obj_id = " . $ilDB->quote($a_obj_id, "integer") .
171  " AND obj_type = " . $ilDB->quote($a_obj_type, "text") .
172  " AND sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") .
173  " AND sub_obj_type = " . $ilDB->quote($a_sub_obj_type, "text") .
174  " AND news_id = " . $ilDB->quote($a_news_id, "integer") .
175  " AND like_type = " . $ilDB->quote($a_like_type, "integer")
176  );
177  }
178 
184  protected function loadDataForObjects($a_obj_ids = array())
185  {
186  $ilDB = $this->db;
187 
188  foreach ($a_obj_ids as $id) {
189  $this->data[$id] = array();
190  }
191 
192  $set = $ilDB->query("SELECT * FROM like_data " .
193  " WHERE " . $ilDB->in("obj_id", $a_obj_ids, false, "integer") .
194  " ORDER by exp_ts DESC");
195  while ($rec = $ilDB->fetchAssoc($set)) {
196  $subtype = $rec["sub_obj_type"] == "-"
197  ? ""
198  : $rec["sub_obj_type"];
199  $this->data[$rec["obj_id"]][$rec["sub_obj_id"]][$subtype][$rec["news_id"]][$rec["like_type"]][$rec["user_id"]] =
200  $rec["exp_ts"];
201  }
202  }
203 
215  public function getExpressionCounts($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
216  {
217  if (!is_array($this->data[$obj_id])) {
218  include_once("./Services/Like/exceptions/class.ilLikeDataException.php");
219  throw new ilLikeDataException("No data loaded for object $obj_id.");
220  }
221 
222  if ($sub_obj_type == "-") {
223  $sub_obj_type = "";
224  }
225 
226  $cnt = array();
227  foreach ($this->getExpressionTypes() as $k => $txt) {
228  $cnt[$k] = 0;
229  if (is_array($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
230  $cnt[$k] = count($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k]);
231  }
232  }
233  return $cnt;
234  }
235 
248  public function isExpressionSet(
249  $a_user_id,
250  $a_like_type,
251  $a_obj_id,
252  $a_obj_type,
253  $a_sub_obj_id = 0,
254  $a_sub_obj_type = "",
255  $a_news_id = 0
256  ) {
257  if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
258  return true;
259  }
260  return false;
261  }
262 
274  public function getExpressionEntries($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
275  {
276  if (!is_array($this->data[$obj_id])) {
277  include_once("./Services/Like/exceptions/class.ilLikeDataException.php");
278  throw new ilLikeDataException("No data loaded for object $obj_id.");
279  }
280 
281  if ($sub_obj_type == "-") {
282  $sub_obj_type = "";
283  }
284 
285  $exp = array();
286  foreach ($this->getExpressionTypes() as $k => $txt) {
287  if (is_array($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
288  foreach ($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k] as $user => $ts) {
289  $exp[] = array(
290  "expression" => $k,
291  "user_id" => $user,
292  "timestamp" => $ts
293  );
294  }
295  }
296  }
297 
298  $exp = ilUtil::sortArray($exp, "timestamp", "desc");
299  return $exp;
300  }
301 
310  public function getExpressionEntriesForObject($obj_id, $since_ts = null)
311  {
312  if (!is_array($this->data[$obj_id])) {
313  include_once("./Services/Like/exceptions/class.ilLikeDataException.php");
314  throw new ilLikeDataException("No data loaded for object $obj_id.");
315  }
316  $exp = array();
317  foreach ($this->data[$obj_id] as $sub_obj_id => $si) {
318  foreach ($si as $sub_obj_type => $so) {
319  foreach ($so as $news_id => $ni) {
320  foreach ($ni as $exp_type => $entry) {
321  foreach ($entry as $user => $ts) {
322  if ($since_ts == null || $ts > $since_ts) {
323  $exp[] = array(
324  "sub_obj_id" => $sub_obj_id,
325  "sub_obj_type" => $sub_obj_type,
326  "news_id" => $news_id,
327  "expression" => $exp_type,
328  "user_id" => $user,
329  "timestamp" => $ts
330  );
331  }
332  }
333  }
334  }
335  }
336  }
337 
338  $exp = ilUtil::sortArray($exp, "timestamp", "desc");
339  return $exp;
340  }
341 }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
getExpressionEntries($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
Get expression entries for obj/subobj/news.
isExpressionSet( $a_user_id, $a_like_type, $a_obj_id, $a_obj_type, $a_sub_obj_id=0, $a_sub_obj_type="", $a_news_id=0)
Is expression set for a user and object?
getExpressionCounts($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
Get expression counts for obj/subobj/news.
__construct(array $a_obj_ids=array(), ilDB $db=null, $lng=null)
Constructor.
global $DIC
Definition: saml.php:7
if(!array_key_exists('StateId', $_REQUEST)) $id
const TYPE_ASTOUNDED
Data class for like feature.
static now()
Return current timestamp in Y-m-d H:i:s format.
removeExpression( $a_user_id, $a_like_type, $a_obj_id, $a_obj_type, $a_sub_obj_id=0, $a_sub_obj_type="", $a_news_id=0)
Remove expression for a user and object.
getExpressionEntriesForObject($obj_id, $since_ts=null)
Get expression entries for obj/subobj/news.
$user
Definition: migrateto20.php:57
$txt
Definition: error.php:11
getExpressionTypes()
Get types.
$this data['403_header']
global $ilDB
addExpression( $a_user_id, $a_like_type, $a_obj_id, $a_obj_type, $a_sub_obj_id=0, $a_sub_obj_type="", $a_news_id=0)
Add expression for a user and object.
Like data exceptions.
loadDataForObjects($a_obj_ids=array())
Load data (for objects)