ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilLikeData Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ Collaboration diagram for ilLikeData:

Public Member Functions

 __construct (array $a_obj_ids=array(), ?ilDBInterface $db=null, ?ilLanguage $lng=null)
 
 getExpressionTypes ()
 Get types. More...
 
 addExpression (int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
 Add expression for a user and object. More...
 
 removeExpression (int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
 Remove expression for a user and object. More...
 
 getExpressionCounts (int $obj_id, string $obj_type, int $sub_obj_id, string $sub_obj_type, int $news_id)
 Get expression counts for obj/subobj/news. More...
 
 isExpressionSet (int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
 Is expression set for a user and object? More...
 
 getExpressionEntries (int $obj_id, string $obj_type, int $sub_obj_id, string $sub_obj_type, int $news_id)
 Get expression entries for obj/subobj/news. More...
 
 getExpressionEntriesForObject (int $obj_id, ?string $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 (array $a_obj_ids=array())
 Load data (for objects) More...
 

Protected Attributes

array $data = array()
 
ilDBInterface $db
 
ilLanguage $lng
 

Detailed Description

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning 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
Alexander Killing killi.nosp@m.ng@l.nosp@m.eifos.nosp@m..de

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

Constructor & Destructor Documentation

◆ __construct()

ilLikeData::__construct ( array  $a_obj_ids = array(),
?ilDBInterface  $db = null,
?ilLanguage  $lng = null 
)

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

References $db, $DIC, $lng, ILIAS\Repository\lng(), loadDataForObjects(), and null.

48  {
49  global $DIC;
50 
51  $this->db = ($db == null)
52  ? $DIC->database()
53  : $db;
54 
55  $this->lng = ($lng == null)
56  ? $DIC->language()
57  : $lng;
58  $this->loadDataForObjects($a_obj_ids);
59  $this->lng->loadLanguageModule("like");
60  }
loadDataForObjects(array $a_obj_ids=array())
Load data (for objects)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilDBInterface $db
global $DIC
Definition: shib_login.php:22
ilLanguage $lng
+ Here is the call graph for this function:

Member Function Documentation

◆ addExpression()

ilLikeData::addExpression ( int  $a_user_id,
int  $a_like_type,
int  $a_obj_id,
string  $a_obj_type,
int  $a_sub_obj_id = 0,
string  $a_sub_obj_type = "",
int  $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 90 of file class.ilLikeData.php.

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

98  : void {
99  $ilDB = $this->db;
100 
101  if ($a_user_id == ANONYMOUS_USER_ID) {
102  return;
103  }
104 
105  $this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id] = 1;
106 
107  if ($a_sub_obj_type == "") {
108  $a_sub_obj_type = "-";
109  }
110 
111  $ilDB->replace(
112  "like_data",
113  array(
114  "user_id" => array("integer", $a_user_id),
115  "obj_id" => array("integer", $a_obj_id),
116  "obj_type" => array("text", $a_obj_type),
117  "sub_obj_id" => array("integer", $a_sub_obj_id),
118  "sub_obj_type" => array("text", $a_sub_obj_type),
119  "news_id" => array("integer", $a_news_id),
120  "like_type" => array("integer", $a_like_type)
121  ),
122  array(
123  "exp_ts" => array("timestamp", ilUtil::now())
124  )
125  );
126  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
static now()
Return current timestamp in Y-m-d H:i:s format.
ilDBInterface $db
+ Here is the call graph for this function:

◆ getExpressionCounts()

ilLikeData::getExpressionCounts ( int  $obj_id,
string  $obj_type,
int  $sub_obj_id,
string  $sub_obj_type,
int  $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[] counts per expression type
Exceptions
ilLikeDataException

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

References $txt, and getExpressionTypes().

216  : array {
217  if (!is_array($this->data[$obj_id])) {
218  throw new ilLikeDataException("No data loaded for object $obj_id.");
219  }
220 
221  if ($sub_obj_type == "-") {
222  $sub_obj_type = "";
223  }
224 
225  $cnt = array();
226  foreach ($this->getExpressionTypes() as $k => $txt) {
227  $cnt[$k] = 0;
228  if (isset($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
229  $cnt[$k] = count($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k]);
230  }
231  }
232  return $cnt;
233  }
$txt
Definition: error.php:31
getExpressionTypes()
Get types.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
+ Here is the call graph for this function:

◆ getExpressionEntries()

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

Get expression entries for obj/subobj/news.

Exceptions
ilLikeDataException

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

References $txt, getExpressionTypes(), and ilArrayUtil\sortArray().

272  : array {
273  if (!is_array($this->data[$obj_id])) {
274  throw new ilLikeDataException("No data loaded for object $obj_id.");
275  }
276 
277  if ($sub_obj_type == "-") {
278  $sub_obj_type = "";
279  }
280 
281  $exp = array();
282  foreach ($this->getExpressionTypes() as $k => $txt) {
283  if (isset($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
284  foreach ($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k] as $user => $ts) {
285  $exp[] = array(
286  "expression" => $k,
287  "user_id" => $user,
288  "timestamp" => $ts
289  );
290  }
291  }
292  }
293 
294  $exp = ilArrayUtil::sortArray($exp, "timestamp", "desc");
295  return $exp;
296  }
$txt
Definition: error.php:31
getExpressionTypes()
Get types.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
+ Here is the call graph for this function:

◆ getExpressionEntriesForObject()

ilLikeData::getExpressionEntriesForObject ( int  $obj_id,
?string  $since_ts = null 
)

Get expression entries for obj/subobj/news.

Parameters
int$obj_id
?string$since_ts timestamp (show only data since...)
Returns
array
Exceptions
ilLikeDataException

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

References null, and ilArrayUtil\sortArray().

309  : array {
310  if (!is_array($this->data[$obj_id])) {
311  throw new ilLikeDataException("No data loaded for object $obj_id.");
312  }
313  $exp = array();
314  foreach ($this->data[$obj_id] as $sub_obj_id => $si) {
315  foreach ($si as $sub_obj_type => $so) {
316  foreach ($so as $news_id => $ni) {
317  foreach ($ni as $exp_type => $entry) {
318  foreach ($entry as $user => $ts) {
319  if ($since_ts == null || $ts > $since_ts) {
320  $exp[] = array(
321  "sub_obj_id" => $sub_obj_id,
322  "sub_obj_type" => $sub_obj_type,
323  "news_id" => $news_id,
324  "expression" => $exp_type,
325  "user_id" => $user,
326  "timestamp" => $ts
327  );
328  }
329  }
330  }
331  }
332  }
333  }
334 
335  $exp = ilArrayUtil::sortArray($exp, "timestamp", "desc");
336  return $exp;
337  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
+ Here is the call graph for this function:

◆ getExpressionTypes()

ilLikeData::getExpressionTypes ( )

Get types.

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

References ILIAS\Repository\lng().

Referenced by getExpressionCounts(), and getExpressionEntries().

65  : array
66  {
67  return array(
68  self::TYPE_LIKE => $this->lng->txt("like_like"),
69  self::TYPE_DISLIKE => $this->lng->txt("like_dislike"),
70  self::TYPE_LOVE => $this->lng->txt("like_love"),
71  self::TYPE_LAUGH => $this->lng->txt("like_laugh"),
72  self::TYPE_ASTOUNDED => $this->lng->txt("like_astounded"),
73  self::TYPE_SAD => $this->lng->txt("like_sad"),
74  self::TYPE_ANGRY => $this->lng->txt("like_angry")
75  );
76  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isExpressionSet()

ilLikeData::isExpressionSet ( int  $a_user_id,
int  $a_like_type,
int  $a_obj_id,
string  $a_obj_type,
int  $a_sub_obj_id = 0,
string  $a_sub_obj_type = "",
int  $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 247 of file class.ilLikeData.php.

255  : bool {
256  if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
257  return true;
258  }
259  return false;
260  }

◆ loadDataForObjects()

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

Load data (for objects)

Parameters
int[]$a_obj_ids load data for objects

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

References $db, $id, and $ilDB.

Referenced by __construct().

180  : void {
181  $ilDB = $this->db;
182 
183  foreach ($a_obj_ids as $id) {
184  $this->data[$id] = array();
185  }
186 
187  $set = $ilDB->query("SELECT * FROM like_data " .
188  " WHERE " . $ilDB->in("obj_id", $a_obj_ids, false, "integer") .
189  " ORDER by exp_ts DESC");
190  while ($rec = $ilDB->fetchAssoc($set)) {
191  $subtype = $rec["sub_obj_type"] == "-"
192  ? ""
193  : $rec["sub_obj_type"];
194  $this->data[$rec["obj_id"]][$rec["sub_obj_id"]][$subtype][$rec["news_id"]][$rec["like_type"]][$rec["user_id"]] =
195  $rec["exp_ts"];
196  }
197  }
ilDBInterface $db
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
+ Here is the caller graph for this function:

◆ removeExpression()

ilLikeData::removeExpression ( int  $a_user_id,
int  $a_like_type,
int  $a_obj_id,
string  $a_obj_type,
int  $a_sub_obj_id = 0,
string  $a_sub_obj_type = "",
int  $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 139 of file class.ilLikeData.php.

References $db, $ilDB, and ANONYMOUS_USER_ID.

147  : void {
148  $ilDB = $this->db;
149 
150  if ($a_user_id == ANONYMOUS_USER_ID) {
151  return;
152  }
153 
154  if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
155  unset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id]);
156  }
157 
158  if ($a_sub_obj_type == "") {
159  $a_sub_obj_type = "-";
160  }
161 
162  $ilDB->manipulate(
163  "DELETE FROM like_data WHERE " .
164  " user_id = " . $ilDB->quote($a_user_id, "integer") .
165  " AND obj_id = " . $ilDB->quote($a_obj_id, "integer") .
166  " AND obj_type = " . $ilDB->quote($a_obj_type, "text") .
167  " AND sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") .
168  " AND sub_obj_type = " . $ilDB->quote($a_sub_obj_type, "text") .
169  " AND news_id = " . $ilDB->quote($a_news_id, "integer") .
170  " AND like_type = " . $ilDB->quote($a_like_type, "integer")
171  );
172  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
ilDBInterface $db

Field Documentation

◆ $data

array ilLikeData::$data = array()
protected

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

◆ $db

ilDBInterface ilLikeData::$db
protected

◆ $lng

ilLanguage ilLikeData::$lng
protected

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

Referenced by __construct().

◆ TYPE_ANGRY

const ilLikeData::TYPE_ANGRY = 6

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

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

◆ TYPE_ASTOUNDED

const ilLikeData::TYPE_ASTOUNDED = 4

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

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

◆ TYPE_DISLIKE

const ilLikeData::TYPE_DISLIKE = 1

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

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

◆ TYPE_LAUGH

const ilLikeData::TYPE_LAUGH = 3

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

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

◆ TYPE_LIKE

const ilLikeData::TYPE_LIKE = 0

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

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

◆ TYPE_LOVE

const ilLikeData::TYPE_LOVE = 2

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

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

◆ TYPE_SAD

const ilLikeData::TYPE_SAD = 5

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

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


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