ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilLikeData Class Reference

Data class for like feature. More...

+ Collaboration diagram for ilLikeData:

Public Member Functions

 __construct (array $a_obj_ids=array(), ilDB $db=null, $lng=null)
 Constructor. More...
 
 getExpressionTypes ()
 Get types. More...
 
 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. More...
 
 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. More...
 
 getExpressionCounts ($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
 Get expression counts for obj/subobj/news. More...
 
 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? More...
 
 getExpressionEntries ($obj_id, $obj_type, $sub_obj_id, $sub_obj_type, $news_id)
 Get expression entries for obj/subobj/news. More...
 
 getExpressionEntriesForObject ($obj_id, $since_ts=null)
 Get expression entries for obj/subobj/news. More...
 

Data Fields

const TYPE_LIKE = 0
 
const TYPE_DISLIKE = 1
 
const TYPE_LOVE = 2
 
const TYPE_LAUGH = 3
 
const TYPE_ASTOUNDED = 4
 
const TYPE_SAD = 5
 
const TYPE_ANGRY = 6
 

Protected Member Functions

 loadDataForObjects ($a_obj_ids=array())
 Load data (for objects) More...
 

Protected Attributes

 $data = array()
 
 $db
 
 $lng
 

Detailed Description

Data class for like feature.

DB related operations.

The like table only holds a record if an expression has been added. After a "dislike" the record disappears. This reduces space and increases performance. But we do not know "when" something has been disliked.

Since the subobject_type column is pk it must be not null and does not allow "" due to the abstract DB handling. We internally save "" as "-" here.

Author
Alex Killing alex..nosp@m.kill.nosp@m.ing@g.nosp@m.mx.d.nosp@m.e
Version
$Id$

Definition at line 19 of file class.ilLikeData.php.

Constructor & Destructor Documentation

◆ __construct()

ilLikeData::__construct ( array  $a_obj_ids = array(),
ilDB  $db = null,
  $lng = null 
)

Constructor.

Parameters
ilDB$db

Definition at line 49 of file class.ilLikeData.php.

References $db, $DIC, $lng, and loadDataForObjects().

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  }
global $DIC
Definition: saml.php:7
loadDataForObjects($a_obj_ids=array())
Load data (for objects)
+ Here is the call graph for this function:

Member Function Documentation

◆ addExpression()

ilLikeData::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.

Parameters
int$a_user_iduser id (who is liking)
int$a_like_typeone of self::TYPE_LIKE to self::TYPE_ANGRY
int$a_obj_idobject id (must be an repository object id)
string$a_obj_typeobject type (redundant, for performance reasons)
int$a_sub_obj_idsubobject id (as defined by the module being responsible for main object type)
string$a_sub_obj_typesubobject type (as defined by the module being responsible for main object type)
int$a_news_idnews is (optional news id, if like action is dedicated to a news for the object/subobject)

Definition at line 95 of file class.ilLikeData.php.

References $db, $ilDB, data, and ilUtil\now().

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  }
static now()
Return current timestamp in Y-m-d H:i:s format.
$this data['403_header']
global $ilDB
+ Here is the call graph for this function:

◆ getExpressionCounts()

ilLikeData::getExpressionCounts (   $obj_id,
  $obj_type,
  $sub_obj_id,
  $sub_obj_type,
  $news_id 
)

Get expression counts for obj/subobj/news.

Parameters
int$obj_id
string$obj_type
int$sub_obj_id
string$sub_obj_type
int$news_id
Returns
int[] $news_id
Exceptions
ilLikeDataException

Definition at line 215 of file class.ilLikeData.php.

References $txt, data, and getExpressionTypes().

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  }
$txt
Definition: error.php:11
getExpressionTypes()
Get types.
$this data['403_header']
Like data exceptions.
+ Here is the call graph for this function:

◆ getExpressionEntries()

ilLikeData::getExpressionEntries (   $obj_id,
  $obj_type,
  $sub_obj_id,
  $sub_obj_type,
  $news_id 
)

Get expression entries for obj/subobj/news.

Parameters
int$obj_id
string$obj_type
int$sub_obj_id
string$sub_obj_type
int$news_id
Returns
int[] $news_id
Exceptions
ilLikeDataException

Definition at line 274 of file class.ilLikeData.php.

References $txt, $user, data, getExpressionTypes(), and ilUtil\sortArray().

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  }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$user
Definition: migrateto20.php:57
$txt
Definition: error.php:11
getExpressionTypes()
Get types.
$this data['403_header']
Like data exceptions.
+ Here is the call graph for this function:

◆ getExpressionEntriesForObject()

ilLikeData::getExpressionEntriesForObject (   $obj_id,
  $since_ts = null 
)

Get expression entries for obj/subobj/news.

Parameters
int$obj_id
int$since_tstimestamp (show only data since...)
Returns
array
Exceptions
ilLikeDataException

Definition at line 310 of file class.ilLikeData.php.

References $si, $user, data, and ilUtil\sortArray().

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  }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$user
Definition: migrateto20.php:57
$this data['403_header']
Like data exceptions.
+ Here is the call graph for this function:

◆ getExpressionTypes()

ilLikeData::getExpressionTypes ( )

Get types.

Parameters

Definition at line 70 of file class.ilLikeData.php.

Referenced by getExpressionCounts(), and getExpressionEntries().

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  }
+ Here is the caller graph for this function:

◆ isExpressionSet()

ilLikeData::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?

Parameters
int$a_user_iduser id (who is liking)
int$a_like_typeone of self::TYPE_LIKE to self::TYPE_ANGRY
int$a_obj_idobject id (must be an repository object id)
string$a_obj_typeobject type (redundant, for performance reasons)
int$a_sub_obj_idsubobject id (as defined by the module being responsible for main object type)
string$a_sub_obj_typesubobject type (as defined by the module being responsible for main object type)
int$a_news_idnews is (optional news id, if like action is dedicated to a news for the object/subobject)
Returns
bool

Definition at line 248 of file class.ilLikeData.php.

References data.

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  }
$this data['403_header']

◆ loadDataForObjects()

ilLikeData::loadDataForObjects (   $a_obj_ids = array())
protected

Load data (for objects)

Parameters
int[]load data for objects

Definition at line 184 of file class.ilLikeData.php.

References $db, $id, $ilDB, and data.

Referenced by __construct().

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  }
if(!array_key_exists('StateId', $_REQUEST)) $id
$this data['403_header']
global $ilDB
+ Here is the caller graph for this function:

◆ removeExpression()

ilLikeData::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.

Parameters
int$a_user_iduser id (who is liking)
int$a_like_typeone of self::TYPE_LIKE to self::TYPE_ANGRY
int$a_obj_idobject id (must be an repository object id)
string$a_obj_typeobject type (redundant, for performance reasons)
int$a_sub_obj_idsubobject id (as defined by the module being responsible for main object type)
string$a_sub_obj_typesubobject type (as defined by the module being responsible for main object type)
int$a_news_idnews is (optional news id, if like action is dedicated to a news for the object/subobject)

Definition at line 144 of file class.ilLikeData.php.

References $db, $ilDB, and data.

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  }
$this data['403_header']
global $ilDB

Field Documentation

◆ $data

ilLikeData::$data = array()
protected

Definition at line 33 of file class.ilLikeData.php.

◆ $db

ilLikeData::$db
protected

◆ $lng

ilLikeData::$lng
protected

Definition at line 43 of file class.ilLikeData.php.

Referenced by __construct().

◆ TYPE_ANGRY

const ilLikeData::TYPE_ANGRY = 6

Definition at line 27 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_ASTOUNDED

const ilLikeData::TYPE_ASTOUNDED = 4

Definition at line 25 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_DISLIKE

const ilLikeData::TYPE_DISLIKE = 1

Definition at line 22 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_LAUGH

const ilLikeData::TYPE_LAUGH = 3

Definition at line 24 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_LIKE

const ilLikeData::TYPE_LIKE = 0

Definition at line 21 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_LOVE

const ilLikeData::TYPE_LOVE = 2

Definition at line 23 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().

◆ TYPE_SAD

const ilLikeData::TYPE_SAD = 5

Definition at line 26 of file class.ilLikeData.php.

Referenced by ilLikeGUI\getExpressionText(), and ilLikeGUI\getGlyphForConst().


The documentation for this class was generated from the following file: