ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLinkResourceItems.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "./Services/Object/classes/class.ilObjectGUI.php";
6 
16 {
21  public function __construct($webr_id)
22  {
23  global $DIC;
24 
25  $ilDB = $DIC['ilDB'];
26 
27  $this->webr_ref_id = 0;
28  $this->webr_id = $webr_id;
29 
30  $this->db = $ilDB;
31  }
32 
33  // BEGIN PATCH Lucene search
34  public static function lookupItem($a_webr_id, $a_link_id)
35  {
36  global $DIC;
37 
38  $ilDB = $DIC['ilDB'];
39 
40  $query = "SELECT * FROM webr_items " .
41  "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer') . " " .
42  "AND link_id = " . $ilDB->quote($a_link_id, 'integer');
43 
44  $res = $ilDB->query($query);
45  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
46  $item['title'] = $row->title;
47  $item['description'] = $row->description;
48  $item['target'] = $row->target;
49  $item['active'] = (bool) $row->active;
50  $item['disable_check'] = $row->disable_check;
51  $item['create_date'] = $row->create_date;
52  $item['last_update'] = $row->last_update;
53  $item['last_check'] = $row->last_check;
54  $item['valid'] = $row->valid;
55  $item['link_id'] = $row->link_id;
56  $item['internal'] = $row->internal;
57  }
58  return $item ? $item : array();
59  }
60  // END PATCH Lucene Search
61 
67  public static function updateTitle($a_link_id, $a_title)
68  {
69  global $DIC;
70 
71  $ilDB = $DIC['ilDB'];
72 
73  $query = 'UPDATE webr_items SET ' .
74  'title = ' . $ilDB->quote($a_title, 'text') . ' ' .
75  'WHERE link_id = ' . $ilDB->quote($a_link_id, 'integer');
76  $ilDB->manipulate($query);
77  return true;
78  }
79 
80 
81 
82  // SET GET
83  public function setLinkResourceRefId($a_ref_id)
84  {
85  $this->webr_ref_id = $a_ref_id;
86  }
87  public function getLinkResourceRefId()
88  {
89  return $this->webr_ref_id;
90  }
91  public function setLinkResourceId($a_id)
92  {
93  $this->webr_id = $a_id;
94  }
95  public function getLinkResourceId()
96  {
97  return $this->webr_id;
98  }
99  public function setLinkId($a_id)
100  {
101  $this->id = $a_id;
102  }
103  public function getLinkId()
104  {
105  return $this->id;
106  }
107  public function setTitle($a_title)
108  {
109  $this->title = $a_title;
110  }
111  public function getTitle()
112  {
113  return $this->title;
114  }
115  public function setDescription($a_description)
116  {
117  $this->description = $a_description;
118  }
119  public function getDescription()
120  {
121  return $this->description;
122  }
123  public function setTarget($a_target)
124  {
125  $this->target = $a_target;
126  }
127  public function getTarget()
128  {
129  return $this->target;
130  }
131  public function setActiveStatus($a_status)
132  {
133  $this->status = (int) $a_status;
134  }
135  public function getActiveStatus()
136  {
137  return (bool) $this->status;
138  }
139  public function setDisableCheckStatus($a_status)
140  {
141  $this->check = (int) $a_status;
142  }
143  public function getDisableCheckStatus()
144  {
145  return (bool) $this->check;
146  }
147  // PRIVATE
148  public function __setCreateDate($a_date)
149  {
150  $this->c_date = $a_date;
151  }
152  public function getCreateDate()
153  {
154  return $this->c_date;
155  }
156  // PRIVATE
157  public function __setLastUpdateDate($a_date)
158  {
159  $this->m_date = $a_date;
160  }
161  public function getLastUpdateDate()
162  {
163  return $this->m_date;
164  }
165  public function setLastCheckDate($a_date)
166  {
167  $this->last_check = $a_date;
168  }
169  public function getLastCheckDate()
170  {
171  return $this->last_check;
172  }
173  public function setValidStatus($a_status)
174  {
175  $this->valid = (int) $a_status;
176  }
177  public function getValidStatus()
178  {
179  return (bool) $this->valid;
180  }
181  public function setInternal($a_status)
182  {
183  $this->internal = (bool) $a_status;
184  }
185  public function getInternal()
186  {
187  return (bool) $this->internal;
188  }
189 
197  public function cloneItems($a_new_id)
198  {
199  include_once 'Modules/WebResource/classes/class.ilParameterAppender.php';
200  $appender = new ilParameterAppender($this->getLinkResourceId());
201 
202  foreach ($this->getAllItems() as $item) {
203  $new_item = new ilLinkResourceItems($a_new_id);
204  $new_item->setTitle($item['title']);
205  $new_item->setDescription($item['description']);
206  $new_item->setTarget($item['target']);
207  $new_item->setActiveStatus($item['active']);
208  $new_item->setDisableCheckStatus($item['disable_check']);
209  $new_item->setLastCheckDate($item['last_check']);
210  $new_item->setValidStatus($item['valid']);
211  $new_item->setInternal($item['internal']);
212  $new_item->add(true);
213 
214  // Add parameters
215  foreach (ilParameterAppender::_getParams($item['link_id']) as $param_id => $data) {
216  $appender->setName($data['name']);
217  $appender->setValue($data['value']);
218  $appender->add($new_item->getLinkId());
219  }
220 
221  unset($new_item);
222  }
223  return true;
224  }
225 
226  public function delete($a_item_id, $a_update_history = true)
227  {
228  global $DIC;
229 
230  $ilDB = $DIC['ilDB'];
231 
232  $item = $this->getItem($a_item_id);
233 
234  $query = "DELETE FROM webr_items " .
235  "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
236  "AND link_id = " . $ilDB->quote($a_item_id, 'integer');
237  $res = $ilDB->manipulate($query);
238 
239  if ($a_update_history) {
240  include_once("./Services/History/classes/class.ilHistory.php");
242  $this->getLinkResourceId(),
243  "delete",
244  $item['title']
245  );
246  }
247 
248  return true;
249  }
250 
251  public function update($a_update_history = true)
252  {
253  global $DIC;
254 
255  $ilDB = $DIC['ilDB'];
256 
257  if (!$this->getLinkId()) {
258  return false;
259  }
260 
261  $this->__setLastUpdateDate(time());
262  $query = "UPDATE webr_items " .
263  "SET title = " . $ilDB->quote($this->getTitle(), 'text') . ", " .
264  "description = " . $ilDB->quote($this->getDescription(), 'text') . ", " .
265  "target = " . $ilDB->quote($this->getTarget(), 'text') . ", " .
266  "active = " . $ilDB->quote($this->getActiveStatus(), 'integer') . ", " .
267  "valid = " . $ilDB->quote($this->getValidStatus(), 'integer') . ", " .
268  "disable_check = " . $ilDB->quote($this->getDisableCheckStatus(), 'integer') . ", " .
269  "internal = " . $ilDB->quote($this->getInternal(), 'integer') . ", " .
270  "last_update = " . $ilDB->quote($this->getLastUpdateDate(), 'integer') . ", " .
271  "last_check = " . $ilDB->quote($this->getLastCheckDate(), 'integer') . " " .
272  "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer') . " " .
273  "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
274  $res = $ilDB->manipulate($query);
275 
276  if ($a_update_history) {
277  include_once("./Services/History/classes/class.ilHistory.php");
279  $this->getLinkResourceId(),
280  "update",
281  $this->getTitle()
282  );
283  }
284 
285  return true;
286  }
287 
288  public function updateValid($a_status)
289  {
290  global $DIC;
291 
292  $ilDB = $DIC['ilDB'];
293 
294  $query = "UPDATE webr_items " .
295  "SET valid = " . $ilDB->quote($a_status, 'integer') . " " .
296  "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
297  $res = $ilDB->manipulate($query);
298 
299  return true;
300  }
301 
302  public function updateActive($a_status)
303  {
304  global $DIC;
305 
306  $ilDB = $DIC['ilDB'];
307 
308  $query = "UPDATE webr_items " .
309  "SET active = " . $ilDB->quote($a_status, 'integer') . " " .
310  "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
311 
312  $this->db->query($query);
313 
314  return true;
315  }
316  public function updateDisableCheck($a_status)
317  {
318  global $DIC;
319 
320  $ilDB = $DIC['ilDB'];
321 
322  $query = "UPDATE webr_items " .
323  "SET disable_check = " . $ilDB->quote($a_status, 'integer') . " " .
324  "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
325  $res = $ilDB->manipulate($query);
326 
327  return true;
328  }
329 
330  public function updateLastCheck($a_offset = 0)
331  {
332  global $DIC;
333 
334  $ilDB = $DIC['ilDB'];
335 
336  if ($a_offset) {
337  $period = $a_offset ? $a_offset : 0;
338  $time = time() - $period;
339 
340 
341  $query = "UPDATE webr_items " .
342  "SET last_check = " . $ilDB->quote(time(), 'integer') . " " .
343  "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
344  "AND disable_check = '0' " .
345  "AND last_check < " . $ilDB->quote($time, 'integer');
346  $res = $ilDB->manipulate($query);
347  } else {
348  $query = "UPDATE webr_items " .
349  "SET last_check = " . $ilDB->quote(time(), 'integer') . " " .
350  "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
351  "AND disable_check = '0' ";
352  $res = $ilDB->manipulate($query);
353  }
354  return true;
355  }
356 
357  public function updateValidByCheck($a_offset = 0)
358  {
359  global $DIC;
360 
361  $ilDB = $DIC['ilDB'];
362 
363  if ($a_offset) {
364  $period = $a_offset ? $a_offset : 0;
365  $time = time() - $period;
366 
367 
368  $query = "UPDATE webr_items " .
369  "SET valid = '1' " .
370  "WHERE disable_check = '0' " .
371  "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
372  "AND last_check < " . $ilDB->quote($time, 'integer');
373  $res = $ilDB->manipulate($query);
374  } else {
375  $query = "UPDATE webr_items " .
376  "SET valid = '1' " .
377  "WHERE disable_check = '0' " .
378  "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
379  $res = $ilDB->manipulate($query);
380  }
381  return true;
382  }
383 
384 
385  public function add($a_update_history = true)
386  {
387  global $DIC;
388 
389  $ilDB = $DIC['ilDB'];
390 
391  $this->__setLastUpdateDate(time());
392  $this->__setCreateDate(time());
393 
394  $next_id = $ilDB->nextId('webr_items');
395  $query = "INSERT INTO webr_items (link_id,title,description,target,active,disable_check," .
396  "last_update,create_date,webr_id,valid,internal) " .
397  "VALUES( " .
398  $ilDB->quote($next_id, 'integer') . ", " .
399  $ilDB->quote($this->getTitle(), 'text') . ", " .
400  $ilDB->quote($this->getDescription(), 'text') . ", " .
401  $ilDB->quote($this->getTarget(), 'text') . ", " .
402  $ilDB->quote($this->getActiveStatus(), 'integer') . ", " .
403  $ilDB->quote($this->getDisableCheckStatus(), 'integer') . ", " .
404  $ilDB->quote($this->getLastUpdateDate(), 'integer') . ", " .
405  $ilDB->quote($this->getCreateDate(), 'integer') . ", " .
406  $ilDB->quote($this->getLinkResourceId(), 'integer') . ", " .
407  $ilDB->quote($this->getValidStatus(), 'integer') . ', ' .
408  $ilDB->quote($this->getInternal(), 'integer') . ' ' .
409  ")";
410  $res = $ilDB->manipulate($query);
411 
412  $link_id = $next_id;
413  $this->setLinkId($link_id);
414 
415  if ($a_update_history) {
416  include_once("./Services/History/classes/class.ilHistory.php");
418  $this->getLinkResourceId(),
419  "add",
420  $this->getTitle()
421  );
422  }
423 
424  return $link_id;
425  }
426  public function readItem($a_link_id)
427  {
428  global $DIC;
429 
430  $ilDB = $DIC['ilDB'];
431 
432  $query = "SELECT * FROM webr_items " .
433  "WHERE link_id = " . $ilDB->quote($a_link_id, 'integer');
434 
435  $res = $this->db->query($query);
436  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
437  $this->setTitle($row->title);
438  $this->setDescription($row->description);
439  $this->setTarget($row->target);
440  $this->setActiveStatus($row->active);
441  $this->setDisableCheckStatus($row->disable_check);
442  $this->__setCreateDate($row->create_date);
443  $this->__setLastUpdateDate($row->last_update);
444  $this->setLastCheckDate($row->last_check);
445  $this->setValidStatus($row->valid);
446  $this->setLinkId($row->link_id);
447  $this->setInternal($row->internal);
448  }
449  return true;
450  }
451 
452 
453  public function getItem($a_link_id)
454  {
455  global $DIC;
456 
457  $ilDB = $DIC['ilDB'];
458 
459  $query = "SELECT * FROM webr_items " .
460  "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
461  "AND link_id = " . $ilDB->quote($a_link_id, 'integer');
462 
463  $res = $this->db->query($query);
464  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
465  $item['title'] = $row->title;
466  $item['description'] = $row->description;
467  $item['target'] = $row->target;
468  $item['active'] = (bool) $row->active;
469  $item['disable_check'] = $row->disable_check;
470  $item['create_date'] = $row->create_date;
471  $item['last_update'] = $row->last_update;
472  $item['last_check'] = $row->last_check;
473  $item['valid'] = $row->valid;
474  $item['link_id'] = $row->link_id;
475  $item['internal'] = $row->internal;
476  }
477  return $item ? $item : array();
478  }
479 
485  public static function getAllItemIds($a_webr_id)
486  {
487  global $DIC;
488 
489  $ilDB = $DIC['ilDB'];
490 
491  $query = "SELECT link_id FROM webr_items " .
492  "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer');
493  $res = $ilDB->query($query);
494  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
495  $link_ids[] = $row['link_id'];
496  }
497  return (array) $link_ids;
498  }
499 
500  public function getAllItems()
501  {
502  global $DIC;
503 
504  $ilDB = $DIC['ilDB'];
505 
506  $query = "SELECT * FROM webr_items " .
507  "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
508 
509  $res = $this->db->query($query);
510  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
511  $items[$row->link_id]['title'] = $row->title;
512  $items[$row->link_id]['description'] = $row->description;
513  $items[$row->link_id]['target'] = $row->target;
514  $items[$row->link_id]['active'] = (bool) $row->active;
515  $items[$row->link_id]['disable_check'] = $row->disable_check;
516  $items[$row->link_id]['create_date'] = $row->create_date;
517  $items[$row->link_id]['last_update'] = $row->last_update;
518  $items[$row->link_id]['last_check'] = $row->last_check;
519  $items[$row->link_id]['valid'] = $row->valid;
520  $items[$row->link_id]['link_id'] = $row->link_id;
521  $items[$row->link_id]['internal'] = $row->internal;
522  }
523  return $items ? $items : array();
524  }
525 
531  public function sortItems($a_items)
532  {
533  include_once './Services/Container/classes/class.ilContainer.php';
534  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
536 
537  if ($mode == ilContainer::SORT_TITLE) {
538  $a_items = ilUtil::sortArray($a_items, 'title', 'asc', false, true);
539  return $a_items;
540  }
541 
542 
543  if ($mode == ilContainer::SORT_MANUAL) {
544  include_once './Services/Container/classes/class.ilContainerSorting.php';
546  foreach ($a_items as $link_id => $item) {
547  if (isset($pos[$link_id])) {
548  $sorted[$link_id] = $item;
549  $sorted[$link_id]['position'] = $pos[$link_id];
550  } else {
551  $unsorted[$link_id] = $item;
552  }
553  }
554  $sorted = ilUtil::sortArray((array) $sorted, 'position', 'asc', true, true);
555  $unsorted = ilUtil::sortArray((array) $unsorted, 'title', 'asc', false, true);
556  $a_items = (array) $sorted + (array) $unsorted;
557  return $a_items;
558  }
559  return $a_items;
560  }
561 
562 
563 
564  public function getActivatedItems()
565  {
566  foreach ($this->getAllItems() as $id => $item_data) {
567  if ($item_data['active']) {
568  $active_items[$id] = $item_data;
569  }
570  }
571  return $active_items ? $active_items : array();
572  }
573 
574  public function getCheckItems($a_offset = 0)
575  {
576  $period = $a_offset ? $a_offset : 0;
577  $time = time() - $period;
578 
579  foreach ($this->getAllItems() as $id => $item_data) {
580  if (!$item_data['disable_check']) {
581  if (!$item_data['last_check'] or $item_data['last_check'] < $time) {
582  $check_items[$id] = $item_data;
583  }
584  }
585  }
586  return $check_items ? $check_items : array();
587  }
588 
589 
590 
591  // STATIC
592  public static function _deleteAll($webr_id)
593  {
594  global $DIC;
595 
596  $ilDB = $DIC['ilDB'];
597 
598  $ilDB->manipulate("DELETE FROM webr_items WHERE webr_id = " . $ilDB->quote($webr_id, 'integer'));
599 
600  return true;
601  }
602 
611  public static function _isSingular($a_webr_id)
612  {
613  global $DIC;
614 
615  $ilDB = $DIC['ilDB'];
616 
617  $query = "SELECT * FROM webr_items " .
618  "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer') . ' ' .
619  "AND active = " . $ilDB->quote(1, 'integer') . ' ';
620  $res = $ilDB->query($query);
621  return $res->numRows() == 1 ? true : false;
622  }
623 
629  public static function lookupNumberOfLinks($a_webr_id)
630  {
631  global $DIC;
632 
633  $ilDB = $DIC['ilDB'];
634 
635  $query = "SELECT COUNT(*) num FROM webr_items " .
636  "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer');
637  $res = $ilDB->query($query);
639  return $row->num;
640  }
641 
650  public static function _getFirstLink($a_webr_id)
651  {
652  global $DIC;
653 
654  $ilDB = $DIC['ilDB'];
655 
656  include_once("./Modules/WebResource/classes/class.ilObjLinkResourceAccess.php");
657  return ilObjLinkResourceAccess::_getFirstLink($a_webr_id);
658  }
659 
664  public function validate()
665  {
666  return $this->getTarget() and $this->getTitle();
667  }
668 
669 
675  public function toXML(ilXmlWriter $writer)
676  {
677  $items = $this->sortItems($this->getAllItems());
678 
679  $position = 0;
680  foreach ((array) $items as $item_id => $item) {
681  ++$position;
682  $link = self::lookupItem($this->getLinkResourceId(), $item_id);
683 
684  $writer->xmlStartTag(
685  'WebLink',
686  array(
687  'id' => $link['link_id'],
688  'active' => $link['active'] ? 1 : 0,
689  'valid' => $link['valid'] ? 1 : 0,
690  'disableValidation' => $link['disable_check'] ? 1 : 0,
691  'position' => $position,
692  'internal' => $link['internal']
693  )
694  );
695  $writer->xmlElement('Title', array(), $link['title']);
696  $writer->xmlElement('Description', array(), $link['description']);
697  $writer->xmlElement('Target', array(), $link['target']);
698 
699  // Dynamic parameters
700  include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
701  foreach (ilParameterAppender::_getParams($link_id) as $param_id => $param) {
702  $value = '';
703  switch ($param['value']) {
704  case LINKS_USER_ID:
705  $value = 'userId';
706  break;
707 
708  case LINKS_LOGIN:
709  $value = 'userName';
710  break;
711 
712  case LINKS_MATRICULATION:
713  $value = 'matriculation';
714  break;
715  }
716 
717  if (!$value) {
718  // Fix for deprecated LINKS_SESSION
719  continue;
720  }
721 
722  $writer->xmlElement(
723  'DynamicParameter',
724  array(
725  'id' => $param_id,
726  'name' => $param['name'],
727  'type' => $value
728  )
729  );
730  }
731 
732  $writer->xmlEndTag('WebLink');
733  }
734  return true;
735  }
736 }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
validate()
Validate required settings.
static _isSingular($a_webr_id)
Check whether there is only one active link in the web resource.
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
toXML(ilXmlWriter $writer)
Write link XML.
static updateTitle($a_link_id, $a_title)
Update title.
global $DIC
Definition: saml.php:7
$valid
static lookupNumberOfLinks($a_webr_id)
Get number of assigned links.
const LINKS_MATRICULATION
if(!array_key_exists('StateId', $_REQUEST)) $id
cloneItems($a_new_id)
Copy web resource items.
XML writer class.
const LINKS_LOGIN
static _createEntry( $a_obj_id, $a_action, $a_info_params="", $a_obj_type="", $a_user_comment="", $a_update_last=false)
Creates a new history entry for an object.
xmlEndTag($tag)
Writes an endtag.
static getAllItemIds($a_webr_id)
Get all link ids.
$time
Definition: cron.php:21
foreach($_POST as $key=> $value) $res
static lookupItem($a_webr_id, $a_link_id)
$query
$row
static _getFirstLink($a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one...
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
static _getFirstLink($a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one...
__construct($webr_id)
Constructor public.
global $ilDB
Class ilObjLinkResourceGUI.
Class ilParameterAppender.
static _lookupSortMode($a_obj_id)
lookup sort mode
add($a_update_history=true)
static lookupPositions($a_obj_id)
Get positions of subitems.
const LINKS_USER_ID
static _getParams($a_link_id)
Get dynamic parameter definitions.
$target
Definition: test.php:19
sortItems($a_items)
Sort items (sorting mode depends on sorting setting)
$data
Definition: bench.php:6
update($a_update_history=true)