ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
5include_once "./Services/Object/classes/class.ilObjectGUI.php";
6
16{
21 function __construct($webr_id)
22 {
23 global $ilDB;
24
25 $this->webr_ref_id = 0;
26 $this->webr_id = $webr_id;
27
28 $this->db = $ilDB;
29 }
30
31 // BEGIN PATCH Lucene search
32 public static function lookupItem($a_webr_id,$a_link_id)
33 {
34 global $ilDB;
35
36 $query = "SELECT * FROM webr_items ".
37 "WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer')." ".
38 "AND link_id = ".$ilDB->quote($a_link_id ,'integer');
39
40 $res = $ilDB->query($query);
41 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
42 {
43 $item['title'] = $row->title;
44 $item['description'] = $row->description;
45 $item['target'] = $row->target;
46 $item['active'] = (bool) $row->active;
47 $item['disable_check'] = $row->disable_check;
48 $item['create_date'] = $row->create_date;
49 $item['last_update'] = $row->last_update;
50 $item['last_check'] = $row->last_check;
51 $item['valid'] = $row->valid;
52 $item['link_id'] = $row->link_id;
53 $item['internal'] = $row->internal;
54 }
55 return $item ? $item : array();
56
57 }
58 // END PATCH Lucene Search
59
65 public static function updateTitle($a_link_id, $a_title)
66 {
67 global $ilDB;
68
69 $query = 'UPDATE webr_items SET '.
70 'title = '.$ilDB->quote($a_title,'text').' '.
71 'WHERE link_id = '.$ilDB->quote($a_link_id,'integer');
72 $ilDB->manipulate($query);
73 return true;
74 }
75
76
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 }
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 }
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 }
158 {
159 return $this->m_date;
160 }
161 function setLastCheckDate($a_date)
162 {
163 $this->last_check = $a_date;
164 }
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 function setInternal($a_status)
178 {
179 $this->internal = (bool) $a_status;
180 }
181 function getInternal()
182 {
183 return (bool) $this->internal;
184 }
185
193 public function cloneItems($a_new_id)
194 {
195 include_once 'Modules/WebResource/classes/class.ilParameterAppender.php';
196 $appender = new ilParameterAppender($this->getLinkResourceId());
197
198 foreach($this->getAllItems() as $item)
199 {
200 $new_item = new ilLinkResourceItems($a_new_id);
201 $new_item->setTitle($item['title']);
202 $new_item->setDescription($item['description']);
203 $new_item->setTarget($item['target']);
204 $new_item->setActiveStatus($item['active']);
205 $new_item->setDisableCheckStatus($item['disable_check']);
206 $new_item->setLastCheckDate($item['last_check']);
207 $new_item->setValidStatus($item['valid']);
208 $new_item->setInternal($item['internal']);
209 $new_item->add(true);
210
211 // Add parameters
212 foreach(ilParameterAppender::_getParams($item['link_id']) as $param_id => $data)
213 {
214 $appender->setName($data['name']);
215 $appender->setValue($data['value']);
216 $appender->add($new_item->getLinkId());
217 }
218
219 unset($new_item);
220 }
221 return true;
222 }
223
224 function delete($a_item_id,$a_update_history = true)
225 {
226 global $ilDB;
227
228 $item = $this->getItem($a_item_id);
229
230 $query = "DELETE FROM webr_items ".
231 "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
232 "AND link_id = ".$ilDB->quote($a_item_id ,'integer');
233 $res = $ilDB->manipulate($query);
234
235 if($a_update_history)
236 {
237 include_once("./Services/History/classes/class.ilHistory.php");
239 $item['title']);
240 }
241
242 return true;
243 }
244
245 function update($a_update_history = true)
246 {
247 global $ilDB;
248
249 if(!$this->getLinkId())
250 {
251 return false;
252 }
253
254 $this->__setLastUpdateDate(time());
255 $query = "UPDATE webr_items ".
256 "SET title = ".$ilDB->quote($this->getTitle() ,'text').", ".
257 "description = ".$ilDB->quote($this->getDescription() ,'text').", ".
258 "target = ".$ilDB->quote($this->getTarget() ,'text').", ".
259 "active = ".$ilDB->quote($this->getActiveStatus() ,'integer').", ".
260 "valid = ".$ilDB->quote($this->getValidStatus() ,'integer').", ".
261 "disable_check = ".$ilDB->quote($this->getDisableCheckStatus() ,'integer').", ".
262 "internal = ".$ilDB->quote($this->getInternal() ,'integer').", ".
263 "last_update = ".$ilDB->quote($this->getLastUpdateDate() ,'integer').", ".
264 "last_check = ".$ilDB->quote($this->getLastCheckDate() ,'integer')." ".
265 "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer')." ".
266 "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
267 $res = $ilDB->manipulate($query);
268
269 if($a_update_history)
270 {
271 include_once("./Services/History/classes/class.ilHistory.php");
273 $this->getTitle());
274 }
275
276 return true;
277 }
278
279 function updateValid($a_status)
280 {
281 global $ilDB;
282
283 $query = "UPDATE webr_items ".
284 "SET valid = ".$ilDB->quote($a_status ,'integer')." ".
285 "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
286 $res = $ilDB->manipulate($query);
287
288 return true;
289 }
290
291 function updateActive($a_status)
292 {
293 global $ilDB;
294
295 $query = "UPDATE webr_items ".
296 "SET active = ".$ilDB->quote($a_status ,'integer')." ".
297 "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
298
299 $this->db->query($query);
300
301 return true;
302 }
303 function updateDisableCheck($a_status)
304 {
305 global $ilDB;
306
307 $query = "UPDATE webr_items ".
308 "SET disable_check = ".$ilDB->quote($a_status ,'integer')." ".
309 "WHERE link_id = ".$ilDB->quote($this->getLinkId() ,'integer');
310 $res = $ilDB->manipulate($query);
311
312 return true;
313 }
314
315 function updateLastCheck($a_offset = 0)
316 {
317 global $ilDB;
318
319 if($a_offset)
320 {
321 $period = $a_offset ? $a_offset : 0;
322 $time = time() - $period;
323
324
325 $query = "UPDATE webr_items ".
326 "SET last_check = ".$ilDB->quote(time() ,'integer')." ".
327 "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
328 "AND disable_check = '0' ".
329 "AND last_check < ".$ilDB->quote($time ,'integer');
330 $res = $ilDB->manipulate($query);
331 }
332 else
333 {
334 $query = "UPDATE webr_items ".
335 "SET last_check = ".$ilDB->quote(time() ,'integer')." ".
336 "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
337 "AND disable_check = '0' ";
338 $res = $ilDB->manipulate($query);
339 }
340 return true;
341 }
342
343 function updateValidByCheck($a_offset = 0)
344 {
345 global $ilDB;
346
347 if($a_offset)
348 {
349 $period = $a_offset ? $a_offset : 0;
350 $time = time() - $period;
351
352
353 $query = "UPDATE webr_items ".
354 "SET valid = '1' ".
355 "WHERE disable_check = '0' ".
356 "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
357 "AND last_check < ".$ilDB->quote($time ,'integer');
358 $res = $ilDB->manipulate($query);
359 }
360 else
361 {
362 $query = "UPDATE webr_items ".
363 "SET valid = '1' ".
364 "WHERE disable_check = '0' ".
365 "AND webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
366 $res = $ilDB->manipulate($query);
367 }
368 return true;
369 }
370
371
372 function add($a_update_history = true)
373 {
374 global $ilDB;
375
376 $this->__setLastUpdateDate(time());
377 $this->__setCreateDate(time());
378
379 $next_id = $ilDB->nextId('webr_items');
380 $query = "INSERT INTO webr_items (link_id,title,description,target,active,disable_check,".
381 "last_update,create_date,webr_id,valid,internal) ".
382 "VALUES( ".
383 $ilDB->quote($next_id ,'integer').", ".
384 $ilDB->quote($this->getTitle() ,'text').", ".
385 $ilDB->quote($this->getDescription() ,'text').", ".
386 $ilDB->quote($this->getTarget() ,'text').", ".
387 $ilDB->quote($this->getActiveStatus() ,'integer').", ".
388 $ilDB->quote($this->getDisableCheckStatus() ,'integer').", ".
389 $ilDB->quote($this->getLastUpdateDate() ,'integer').", ".
390 $ilDB->quote($this->getCreateDate() ,'integer').", ".
391 $ilDB->quote($this->getLinkResourceId() ,'integer').", ".
392 $ilDB->quote($this->getValidStatus(),'integer'). ', '.
393 $ilDB->quote($this->getInternal(),'integer'). ' '.
394 ")";
395 $res = $ilDB->manipulate($query);
396
397 $link_id = $next_id;
398 $this->setLinkId($link_id);
399
400 if($a_update_history)
401 {
402 include_once("./Services/History/classes/class.ilHistory.php");
404 $this->getTitle());
405 }
406
407 return $link_id;
408 }
409 function readItem($a_link_id)
410 {
411 global $ilDB;
412
413 $query = "SELECT * FROM webr_items ".
414 "WHERE link_id = ".$ilDB->quote($a_link_id ,'integer');
415
416 $res = $this->db->query($query);
417 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
418 {
419 $this->setTitle($row->title);
420 $this->setDescription($row->description);
421 $this->setTarget($row->target);
422 $this->setActiveStatus($row->active);
423 $this->setDisableCheckStatus($row->disable_check);
424 $this->__setCreateDate($row->create_date);
425 $this->__setLastUpdateDate($row->last_update);
426 $this->setLastCheckDate($row->last_check);
427 $this->setValidStatus($row->valid);
428 $this->setLinkId($row->link_id);
429 $this->setInternal($row->internal);
430 }
431 return true;
432 }
433
434
435 function getItem($a_link_id)
436 {
437 global $ilDB;
438
439 $query = "SELECT * FROM webr_items ".
440 "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer')." ".
441 "AND link_id = ".$ilDB->quote($a_link_id ,'integer');
442
443 $res = $this->db->query($query);
444 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
445 {
446 $item['title'] = $row->title;
447 $item['description'] = $row->description;
448 $item['target'] = $row->target;
449 $item['active'] = (bool) $row->active;
450 $item['disable_check'] = $row->disable_check;
451 $item['create_date'] = $row->create_date;
452 $item['last_update'] = $row->last_update;
453 $item['last_check'] = $row->last_check;
454 $item['valid'] = $row->valid;
455 $item['link_id'] = $row->link_id;
456 $item['internal'] = $row->internal;
457 }
458 return $item ? $item : array();
459 }
460
466 public static function getAllItemIds($a_webr_id)
467 {
468 global $ilDB;
469
470 $query = "SELECT link_id FROM webr_items ".
471 "WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer');
472 $res = $ilDB->query($query);
473 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
474 {
475 $link_ids[] = $row['link_id'];
476 }
477 return (array) $link_ids;
478 }
479
480 function getAllItems()
481 {
482 global $ilDB;
483
484 $query = "SELECT * FROM webr_items ".
485 "WHERE webr_id = ".$ilDB->quote($this->getLinkResourceId() ,'integer');
486
487 $res = $this->db->query($query);
488 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
489 {
490 $items[$row->link_id]['title'] = $row->title;
491 $items[$row->link_id]['description'] = $row->description;
492 $items[$row->link_id]['target'] = $row->target;
493 $items[$row->link_id]['active'] = (bool) $row->active;
494 $items[$row->link_id]['disable_check'] = $row->disable_check;
495 $items[$row->link_id]['create_date'] = $row->create_date;
496 $items[$row->link_id]['last_update'] = $row->last_update;
497 $items[$row->link_id]['last_check'] = $row->last_check;
498 $items[$row->link_id]['valid'] = $row->valid;
499 $items[$row->link_id]['link_id'] = $row->link_id;
500 $items[$row->link_id]['internal'] = $row->internal;
501 }
502 return $items ? $items : array();
503 }
504
510 public function sortItems($a_items)
511 {
512 include_once './Services/Container/classes/class.ilContainer.php';
513 include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
515
516 if($mode == ilContainer::SORT_TITLE)
517 {
518 $a_items = ilUtil::sortArray($a_items, 'title','asc',false,true);
519 return $a_items;
520 }
521
522
523 if($mode == ilContainer::SORT_MANUAL)
524 {
525 include_once './Services/Container/classes/class.ilContainerSorting.php';
527 foreach($a_items as $link_id => $item)
528 {
529 if(isset($pos[$link_id]))
530 {
531 $sorted[$link_id] = $item;
532 $sorted[$link_id]['position'] = $pos[$link_id];
533 }
534 else
535 {
536 $unsorted[$link_id] = $item;
537 }
538 }
539 $sorted = ilUtil::sortArray((array) $sorted, 'position','asc',true,true);
540 $unsorted = ilUtil::sortArray((array) $unsorted, 'title','asc',false,true);
541 $a_items = (array) $sorted + (array) $unsorted;
542 return $a_items;
543 }
544 return $a_items;
545 }
546
547
548
550 {
551 foreach($this->getAllItems() as $id => $item_data)
552 {
553 if($item_data['active'])
554 {
555 $active_items[$id] = $item_data;
556 }
557 }
558 return $active_items ? $active_items : array();
559 }
560
561 function getCheckItems($a_offset = 0)
562 {
563 $period = $a_offset ? $a_offset : 0;
564 $time = time() - $period;
565
566 foreach($this->getAllItems() as $id => $item_data)
567 {
568 if(!$item_data['disable_check'])
569 {
570 if(!$item_data['last_check'] or $item_data['last_check'] < $time)
571 {
572 $check_items[$id] = $item_data;
573 }
574 }
575 }
576 return $check_items ? $check_items : array();
577 }
578
579
580
581 // STATIC
582 static function _deleteAll($webr_id)
583 {
584 global $ilDB;
585
586 $ilDB->manipulate("DELETE FROM webr_items WHERE webr_id = ".$ilDB->quote($webr_id ,'integer'));
587
588 return true;
589 }
590
599 public static function _isSingular($a_webr_id)
600 {
601 global $ilDB;
602
603 $query = "SELECT * FROM webr_items ".
604 "WHERE webr_id = ".$ilDB->quote($a_webr_id ,'integer').' '.
605 "AND active = ".$ilDB->quote(1,'integer').' ';
606 $res = $ilDB->query($query);
607 return $res->numRows() == 1 ? true : false;
608 }
609
615 public static function lookupNumberOfLinks($a_webr_id)
616 {
617 global $ilDB;
618
619 $query = "SELECT COUNT(*) num FROM webr_items ".
620 "WHERE webr_id = ".$ilDB->quote($a_webr_id,'integer');
621 $res = $ilDB->query($query);
623 return $row->num;
624 }
625
634 public static function _getFirstLink($a_webr_id)
635 {
636 global $ilDB;
637
638 include_once("./Modules/WebResource/classes/class.ilObjLinkResourceAccess.php");
640 }
641
646 public function validate()
647 {
648 return $this->getTarget() and $this->getTitle();
649 }
650
651
657 public function toXML(ilXmlWriter $writer)
658 {
659 $items = $this->sortItems($this->getAllItems());
660
661 $position = 0;
662 foreach((array) $items as $item_id => $item)
663 {
664 ++$position;
665 $link = self::lookupItem($this->getLinkResourceId(), $item_id);
666
667 $writer->xmlStartTag(
668 'WebLink',
669 array(
670 'id' => $link['link_id'],
671 'active' => $link['active'] ? 1 : 0,
672 'valid' => $link['valid'] ? 1 : 0,
673 'disableValidation' => $link['disable_check'] ? 1 : 0,
674 'position' => $position,
675 'internal' => $link['internal']
676 )
677 );
678 $writer->xmlElement('Title',array(),$link['title']);
679 $writer->xmlElement('Description',array(),$link['description']);
680 $writer->xmlElement('Target',array(),$link['target']);
681
682 // Dynamic parameters
683 include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
684 foreach(ilParameterAppender::_getParams($link_id) as $param_id => $param)
685 {
686 $value = '';
687 switch($param['value'])
688 {
689 case LINKS_USER_ID:
690 $value = 'userId';
691 break;
692
693 case LINKS_LOGIN:
694 $value = 'userName';
695 break;
696
698 $value = 'matriculation';
699 break;
700 }
701
702 if(!$value)
703 {
704 // Fix for deprecated LINKS_SESSION
705 continue;
706 }
707
708 $writer->xmlElement(
709 'DynamicParameter',
710 array(
711 'id' => $param_id,
712 'name' => $param['name'],
713 'type' => $value
714 )
715 );
716 }
717
718 $writer->xmlEndTag('WebLink');
719 }
720 return true;
721 }
722}
723
724?>
An exception for terminatinating execution or to throw for unit testing.
const LINKS_USER_ID
const LINKS_LOGIN
const LINKS_MATRICULATION
static _lookupSortMode($a_obj_id)
lookup sort mode
static lookupPositions($a_obj_id)
Get positions of subitems.
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.
Class ilObjLinkResourceGUI.
validate()
Validate required settings.
static updateTitle($a_link_id, $a_title)
Update title.
update($a_update_history=true)
static getAllItemIds($a_webr_id)
Get all link ids.
static lookupItem($a_webr_id, $a_link_id)
toXML(ilXmlWriter $writer)
Write link XML.
__construct($webr_id)
Constructor @access public.
static _getFirstLink($a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one.
add($a_update_history=true)
cloneItems($a_new_id)
Copy web resource items.
sortItems($a_items)
Sort items (sorting mode depends on sorting setting)
static _isSingular($a_webr_id)
Check whether there is only one active link in the web resource.
static lookupNumberOfLinks($a_webr_id)
Get number of assigned links.
static _getFirstLink($a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one.
Class ilParameterAppender.
static _getParams($a_link_id)
Get dynamic parameter definitions.
static sortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
$valid
global $ilDB