ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLinkResourceItems.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once "./classes/class.ilObjectGUI.php";
25 
35 {
40  function ilLinkResourceItems($webr_id)
41  {
42  global $ilDB;
43 
44  $this->webr_ref_id = 0;
45  $this->webr_id = $webr_id;
46 
47  $this->db =& $ilDB;
48  }
49 
50  // BEGIN PATCH Lucene search
51  public static function lookupItem($a_webr_id,$a_link_id)
52  {
53  global $ilDB;
54 
55  $query = "SELECT * FROM webr_items ".
56  "WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer')." ".
57  "AND link_id = ".$ilDB->quote($a_link_id ,'integer');
58 
59  $res = $ilDB->query($query);
60  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
61  {
62  $item['title'] = $row->title;
63  $item['description'] = $row->description;
64  $item['target'] = $row->target;
65  $item['active'] = (bool) $row->active;
66  $item['disable_check'] = $row->disable_check;
67  $item['create_date'] = $row->create_date;
68  $item['last_update'] = $row->last_update;
69  $item['last_check'] = $row->last_check;
70  $item['valid'] = $row->valid;
71  $item['link_id'] = $row->link_id;
72  }
73  return $item ? $item : array();
74 
75  }
76  // END PATCH Lucene Search
77 
78  // SET GET
79  function setLinkResourceRefId($a_ref_id)
80  {
81  $this->webr_ref_id = $a_ref_id;
82  }
84  {
85  return $this->webr_ref_id;
86  }
87  function setLinkResourceId($a_id)
88  {
89  $this->webr_id = $a_id;
90  }
91  function getLinkResourceId()
92  {
93  return $this->webr_id;
94  }
95  function setLinkId($a_id)
96  {
97  $this->id = $a_id;
98  }
99  function getLinkId()
100  {
101  return $this->id;
102  }
103  function setTitle($a_title)
104  {
105  $this->title = $a_title;
106  }
107  function getTitle()
108  {
109  return $this->title;
110  }
111  function setDescription($a_description)
112  {
113  $this->description = $a_description;
114  }
115  function getDescription()
116  {
117  return $this->description;
118  }
119  function setTarget($a_target)
120  {
121  $this->target = $a_target;
122  }
123  function getTarget()
124  {
125  return $this->target;
126  }
127  function setActiveStatus($a_status)
128  {
129  $this->status = (int) $a_status;
130  }
131  function getActiveStatus()
132  {
133  return (bool) $this->status;
134  }
135  function setDisableCheckStatus($a_status)
136  {
137  $this->check = (int) $a_status;
138  }
140  {
141  return (bool) $this->check;
142  }
143  // PRIVATE
144  function __setCreateDate($a_date)
145  {
146  $this->c_date = $a_date;
147  }
148  function getCreateDate()
149  {
150  return $this->c_date;
151  }
152  // PRIVATE
153  function __setLastUpdateDate($a_date)
154  {
155  $this->m_date = $a_date;
156  }
157  function getLastUpdateDate()
158  {
159  return $this->m_date;
160  }
161  function setLastCheckDate($a_date)
162  {
163  $this->last_check = $a_date;
164  }
165  function getLastCheckDate()
166  {
167  return $this->last_check;
168  }
169  function setValidStatus($a_status)
170  {
171  $this->valid = (int) $a_status;
172  }
173  function getValidStatus()
174  {
175  return (bool) $this->valid;
176  }
177 
185  public function cloneItems($a_new_id)
186  {
187  include_once 'Modules/WebResource/classes/class.ilParameterAppender.php';
188  $appender = new ilParameterAppender($this->getLinkResourceId());
189 
190  foreach($this->getAllItems() as $item)
191  {
192  $new_item = new ilLinkResourceItems($a_new_id);
193  $new_item->setTitle($item['title']);
194  $new_item->setDescription($item['description']);
195  $new_item->setTarget($item['target']);
196  $new_item->setActiveStatus($item['active']);
197  $new_item->setDisableCheckStatus($item['disable_check']);
198  $new_item->setLastCheckDate($item['last_check']);
199  $new_item->setValidStatus($item['valid']);
200  $new_item->add(true);
201 
202  // Add parameters
203  foreach(ilParameterAppender::_getParams($item['link_id']) as $param_id => $data)
204  {
205  $appender->setName($data['name']);
206  $appender->setValue($data['value']);
207  $appender->add($new_item->getLinkId());
208  }
209 
210  unset($new_item);
211  }
212  return true;
213  }
214 
215  function delete($a_item_id,$a_update_history = true)
216  {
217  global $ilDB;
218 
219  $item = $this->getItem($a_item_id);
220 
221  $query = "DELETE FROM webr_items ".
222  "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
223  "AND link_id = ".$ilDB->quote($a_item_id ,'integer');
224  $res = $ilDB->manipulate($query);
225 
226  if($a_update_history)
227  {
228  include_once("classes/class.ilHistory.php");
229  ilHistory::_createEntry($this->getLinkResourceId(), "delete",
230  $item['title']);
231  }
232 
233  return true;
234  }
235 
236  function update($a_update_history = true)
237  {
238  global $ilDB;
239 
240  if(!$this->getLinkId())
241  {
242  return false;
243  }
244 
245  $this->__setLastUpdateDate(time());
246  $query = "UPDATE webr_items ".
247  "SET title = ".$ilDB->quote($this->getTitle() ,'text').", ".
248  "description = ".$ilDB->quote($this->getDescription() ,'text').", ".
249  "target = ".$ilDB->quote($this->getTarget() ,'text').", ".
250  "active = ".$ilDB->quote($this->getActiveStatus() ,'integer').", ".
251  "valid = ".$ilDB->quote($this->getValidStatus() ,'integer').", ".
252  "disable_check = ".$ilDB->quote($this->getDisableCheckStatus() ,'integer').", ".
253  "last_update = ".$ilDB->quote($this->getLastUpdateDate() ,'integer').", ".
254  "last_check = ".$ilDB->quote($this->getLastCheckDate() ,'integer')." ".
255  "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer')." ".
256  "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
257  $res = $ilDB->manipulate($query);
258 
259  if($a_update_history)
260  {
261  include_once("classes/class.ilHistory.php");
262  ilHistory::_createEntry($this->getLinkResourceId(), "update",
263  $this->getTitle());
264  }
265 
266  return true;
267  }
268 
269  function updateValid($a_status)
270  {
271  global $ilDB;
272 
273  $query = "UPDATE webr_items ".
274  "SET valid = ".$ilDB->quote($a_status ,'integer')." ".
275  "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
276  $res = $ilDB->manipulate($query);
277 
278  return true;
279  }
280 
281  function updateActive($a_status)
282  {
283  global $ilDB;
284 
285  $query = "UPDATE webr_items ".
286  "SET active = ".$ilDB->quote($a_status ,'integer')." ".
287  "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
288 
289  $this->db->query($query);
290 
291  return true;
292  }
293  function updateDisableCheck($a_status)
294  {
295  global $ilDB;
296 
297  $query = "UPDATE webr_items ".
298  "SET disable_check = ".$ilDB->quote($a_status ,'integer')." ".
299  "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
300  $res = $ilDB->manipulate($query);
301 
302  return true;
303  }
304 
305  function updateLastCheck($a_offset = 0)
306  {
307  global $ilDB;
308 
309  if($a_offset)
310  {
311  $period = $a_offset ? $a_offset : 0;
312  $time = time() - $period;
313 
314 
315  $query = "UPDATE webr_items ".
316  "SET last_check = ".$ilDB->quote(time() ,'integer')." ".
317  "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
318  "AND disable_check = '0' ".
319  "AND last_check < ".$ilDB->quote($time ,'integer');
320  $res = $ilDB->manipulate($query);
321  }
322  else
323  {
324  $query = "UPDATE webr_items ".
325  "SET last_check = ".$ilDB->quote(time() ,'integer')." ".
326  "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
327  "AND disable_check = '0' ";
328  $res = $ilDB->manipulate($query);
329  }
330  return true;
331  }
332 
333  function updateValidByCheck($a_offset = 0)
334  {
335  global $ilDB;
336 
337  if($a_offset)
338  {
339  $period = $a_offset ? $a_offset : 0;
340  $time = time() - $period;
341 
342 
343  $query = "UPDATE webr_items ".
344  "SET valid = '1' ".
345  "WHERE disable_check = '0' ".
346  "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
347  "AND last_check < ".$ilDB->quote($time ,'integer');
348  $res = $ilDB->manipulate($query);
349  }
350  else
351  {
352  $query = "UPDATE webr_items ".
353  "SET valid = '1' ".
354  "WHERE disable_check = '0' ".
355  "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
356  $res = $ilDB->manipulate($query);
357  }
358  return true;
359  }
360 
361 
362  function add($a_update_history = true)
363  {
364  global $ilDB;
365 
366  $this->__setLastUpdateDate(time());
367  $this->__setCreateDate(time());
368 
369  $next_id = $ilDB->nextId('webr_items');
370  $query = "INSERT INTO webr_items (link_id,title,description,target,active,disable_check,".
371  "last_update,create_date,webr_id) ".
372  "VALUES( ".
373  $ilDB->quote($next_id ,'integer').", ".
374  $ilDB->quote($this->getTitle() ,'text').", ".
375  $ilDB->quote($this->getDescription() ,'text').", ".
376  $ilDB->quote($this->getTarget() ,'text').", ".
377  $ilDB->quote($this->getActiveStatus() ,'integer').", ".
378  $ilDB->quote($this->getDisableCheckStatus() ,'integer').", ".
379  $ilDB->quote($this->getLastUpdateDate() ,'integer').", ".
380  $ilDB->quote($this->getCreateDate() ,'integer').", ".
381  $ilDB->quote($this->getLinkResourceId() ,'integer')." ".
382  ")";
383  $res = $ilDB->manipulate($query);
384 
385  $link_id = $next_id;
386  $this->setLinkId($link_id);
387 
388  if($a_update_history)
389  {
390  include_once("classes/class.ilHistory.php");
392  $this->getTitle());
393  }
394 
395  return $link_id;
396  }
397  function readItem($a_link_id)
398  {
399  global $ilDB;
400 
401  $query = "SELECT * FROM webr_items ".
402  "WHERE link_id = ".$ilDB->quote($a_link_id ,'integer');
403 
404  $res = $this->db->query($query);
405  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
406  {
407  $this->setTitle($row->title);
408  $this->setDescription($row->description);
409  $this->setTarget($row->target);
410  $this->setActiveStatus($row->active);
411  $this->setDisableCheckStatus($row->disable_check);
412  $this->__setCreateDate($row->create_date);
413  $this->__setLastUpdateDate($row->last_update);
414  $this->setLastCheckDate($row->last_check);
415  $this->setValidStatus($row->valid);
416  $this->setLinkId($row->link_id);
417  }
418  return true;
419  }
420 
421 
422  function getItem($a_link_id)
423  {
424  global $ilDB;
425 
426  $query = "SELECT * FROM webr_items ".
427  "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
428  "AND link_id = ".$ilDB->quote($a_link_id ,'integer');
429 
430  $res = $this->db->query($query);
431  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
432  {
433  $item['title'] = $row->title;
434  $item['description'] = $row->description;
435  $item['target'] = $row->target;
436  $item['active'] = (bool) $row->active;
437  $item['disable_check'] = $row->disable_check;
438  $item['create_date'] = $row->create_date;
439  $item['last_update'] = $row->last_update;
440  $item['last_check'] = $row->last_check;
441  $item['valid'] = $row->valid;
442  $item['link_id'] = $row->link_id;
443  }
444  return $item ? $item : array();
445  }
446 
452  public static function getAllItemIds($a_webr_id)
453  {
454  global $ilDB;
455 
456  $query = "SELECT link_id FROM webr_items ".
457  "WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer');
458  $res = $ilDB->query($query);
459  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
460  {
461  $link_ids[] = $row['link_id'];
462  }
463  return (array) $link_ids;
464  }
465 
466  function getAllItems()
467  {
468  global $ilDB;
469 
470  $query = "SELECT * FROM webr_items ".
471  "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
472 
473  $res = $this->db->query($query);
474  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
475  {
476  $items[$row->link_id]['title'] = $row->title;
477  $items[$row->link_id]['description'] = $row->description;
478  $items[$row->link_id]['target'] = $row->target;
479  $items[$row->link_id]['active'] = (bool) $row->active;
480  $items[$row->link_id]['disable_check'] = $row->disable_check;
481  $items[$row->link_id]['create_date'] = $row->create_date;
482  $items[$row->link_id]['last_update'] = $row->last_update;
483  $items[$row->link_id]['last_check'] = $row->last_check;
484  $items[$row->link_id]['valid'] = $row->valid;
485  $items[$row->link_id]['link_id'] = $row->link_id;
486  }
487  return $items ? $items : array();
488  }
489 
495  public function sortItems($a_items)
496  {
497  include_once './Services/Container/classes/class.ilContainer.php';
498  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
500 
501  if($mode == ilContainer::SORT_TITLE)
502  {
503  $a_items = ilUtil::sortArray($a_items, 'title','asc',false,true);
504  return $a_items;
505  }
506 
507 
508  if($mode == ilContainer::SORT_MANUAL)
509  {
510  include_once './Services/Container/classes/class.ilContainerSorting.php';
512  foreach($a_items as $link_id => $item)
513  {
514  if(isset($pos[$link_id]))
515  {
516  $sorted[$link_id] = $item;
517  $sorted[$link_id]['position'] = $pos[$link_id];
518  }
519  else
520  {
521  $unsorted[$link_id] = $item;
522  }
523  }
524  $sorted = ilUtil::sortArray((array) $sorted, 'position','asc',true,true);
525  $unsorted = ilUtil::sortArray((array) $unsorted, 'title','asc',false,true);
526  $a_items = (array) $sorted + (array) $unsorted;
527  return $a_items;
528  }
529  return $a_items;
530  }
531 
532 
533 
534  function getActivatedItems()
535  {
536  foreach($this->getAllItems() as $id => $item_data)
537  {
538  if($item_data['active'])
539  {
540  $active_items[$id] = $item_data;
541  }
542  }
543  return $active_items ? $active_items : array();
544  }
545 
546  function getCheckItems($a_offset = 0)
547  {
548  $period = $a_offset ? $a_offset : 0;
549  $time = time() - $period;
550 
551  foreach($this->getAllItems() as $id => $item_data)
552  {
553  if(!$item_data['disable_check'])
554  {
555  if(!$item_data['last_check'] or $item_data['last_check'] < $time)
556  {
557  $check_items[$id] = $item_data;
558  }
559  }
560  }
561  return $check_items ? $check_items : array();
562  }
563 
564 
565 
566  // STATIC
567  function _deleteAll($webr_id)
568  {
569  global $ilDB;
570 
571  $ilDB->manipulate("DELETE FROM webr_items WHERE webr_id = ".$ilDB->quote($webr_id ,'integer'));
572 
573  return true;
574  }
575 
584  public static function _isSingular($a_webr_id)
585  {
586  global $ilDB;
587 
588  $res = $ilDB->query("SELECT * FROM webr_items WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer')." AND active = '1'");
589 
590  return $res->numRows() == 1 ? true : false;
591  }
592 
598  public static function lookupNumberOfLinks($a_webr_id)
599  {
600  global $ilDB;
601 
602  $query = "SELECT COUNT(*) num FROM webr_items ".
603  "WHERE webr_id = ".$ilDB->quote($a_webr_id,'integer');
604  $res = $ilDB->query($query);
605  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
606  return $row->num;
607  }
608 
617  public static function _getFirstLink($a_webr_id)
618  {
619  global $ilDB;
620 
621  $res = $ilDB->query("SELECT * FROM webr_items WHERE webr_id = ".
622  $ilDB->quote($a_webr_id ,'integer')." AND active = '1'");
623  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
624  {
625  $item['title'] = $row->title;
626  $item['description'] = $row->description;
627  $item['target'] = $row->target;
628  $item['active'] = (bool) $row->active;
629  $item['disable_check'] = $row->disable_check;
630  $item['create_date'] = $row->create_date;
631  $item['last_update'] = $row->last_update;
632  $item['last_check'] = $row->last_check;
633  $item['valid'] = $row->valid;
634  $item['link_id'] = $row->link_id;
635  }
636  return $item ? $item : array();
637  }
638 
643  public function validate()
644  {
645  return $this->getTarget() and $this->getTitle();
646  }
647 
648 
654  public function toXML(ilXmlWriter $writer)
655  {
656  foreach(self::getAllItemIds($this->getLinkResourceId()) as $link_id)
657  {
658  $link = self::lookupItem($this->getLinkResourceId(), $link_id);
659 
660  $writer->xmlStartTag(
661  'WebLink',
662  array(
663  'id' => $link['link_id'],
664  'active' => $link['active'] ? 1 : 0,
665  'valid' => $link['valid'] ? 1 : 0,
666  'disableValidation' => $link['disable_check'] ? 1 : 0,
667 # 'action' => 'Delete'
668  )
669  );
670  $writer->xmlElement('Title',array(),$link['title']);
671  $writer->xmlElement('Description',array(),$link['description']);
672  $writer->xmlElement('Target',array(),$link['target']);
673 
674  // Dynamic parameters
675  include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
676  foreach(ilParameterAppender::_getParams($link_id) as $param_id => $param)
677  {
678  $value = '';
679  switch($param['value'])
680  {
681  case LINKS_USER_ID:
682  $value = 'userId';
683  break;
684 
685  case LINKS_LOGIN:
686  $value = 'userName';
687  break;
688 
689  case LINKS_MATRICULATION:
690  $value = 'matriculation';
691  break;
692  }
693 
694  if(!$value)
695  {
696  // Fix for deprecated LINKS_SESSION
697  continue;
698  }
699 
700  $writer->xmlElement(
701  'DynamicParameter',
702  array(
703  'id' => $param_id,
704  'name' => $param['name'],
705  'type' => $value
706  )
707  );
708  }
709 
710  $writer->xmlEndTag('WebLink');
711  }
712  return true;
713  }
714 }
715 
716 ?>