ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 public 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 $item['title'] = $row->title;
43 $item['description'] = $row->description;
44 $item['target'] = $row->target;
45 $item['active'] = (bool) $row->active;
46 $item['disable_check'] = $row->disable_check;
47 $item['create_date'] = $row->create_date;
48 $item['last_update'] = $row->last_update;
49 $item['last_check'] = $row->last_check;
50 $item['valid'] = $row->valid;
51 $item['link_id'] = $row->link_id;
52 $item['internal'] = $row->internal;
53 }
54 return $item ? $item : array();
55 }
56 // END PATCH Lucene Search
57
63 public static function updateTitle($a_link_id, $a_title)
64 {
65 global $ilDB;
66
67 $query = 'UPDATE webr_items SET ' .
68 'title = ' . $ilDB->quote($a_title, 'text') . ' ' .
69 'WHERE link_id = ' . $ilDB->quote($a_link_id, 'integer');
70 $ilDB->manipulate($query);
71 return true;
72 }
73
74
75
76 // SET GET
77 public function setLinkResourceRefId($a_ref_id)
78 {
79 $this->webr_ref_id = $a_ref_id;
80 }
81 public function getLinkResourceRefId()
82 {
83 return $this->webr_ref_id;
84 }
85 public function setLinkResourceId($a_id)
86 {
87 $this->webr_id = $a_id;
88 }
89 public function getLinkResourceId()
90 {
91 return $this->webr_id;
92 }
93 public function setLinkId($a_id)
94 {
95 $this->id = $a_id;
96 }
97 public function getLinkId()
98 {
99 return $this->id;
100 }
101 public function setTitle($a_title)
102 {
103 $this->title = $a_title;
104 }
105 public function getTitle()
106 {
107 return $this->title;
108 }
109 public function setDescription($a_description)
110 {
111 $this->description = $a_description;
112 }
113 public function getDescription()
114 {
115 return $this->description;
116 }
117 public function setTarget($a_target)
118 {
119 $this->target = $a_target;
120 }
121 public function getTarget()
122 {
123 return $this->target;
124 }
125 public function setActiveStatus($a_status)
126 {
127 $this->status = (int) $a_status;
128 }
129 public function getActiveStatus()
130 {
131 return (bool) $this->status;
132 }
133 public function setDisableCheckStatus($a_status)
134 {
135 $this->check = (int) $a_status;
136 }
137 public function getDisableCheckStatus()
138 {
139 return (bool) $this->check;
140 }
141 // PRIVATE
142 public function __setCreateDate($a_date)
143 {
144 $this->c_date = $a_date;
145 }
146 public function getCreateDate()
147 {
148 return $this->c_date;
149 }
150 // PRIVATE
151 public function __setLastUpdateDate($a_date)
152 {
153 $this->m_date = $a_date;
154 }
155 public function getLastUpdateDate()
156 {
157 return $this->m_date;
158 }
159 public function setLastCheckDate($a_date)
160 {
161 $this->last_check = $a_date;
162 }
163 public function getLastCheckDate()
164 {
165 return $this->last_check;
166 }
167 public function setValidStatus($a_status)
168 {
169 $this->valid = (int) $a_status;
170 }
171 public function getValidStatus()
172 {
173 return (bool) $this->valid;
174 }
175 public function setInternal($a_status)
176 {
177 $this->internal = (bool) $a_status;
178 }
179 public function getInternal()
180 {
181 return (bool) $this->internal;
182 }
183
191 public function cloneItems($a_new_id)
192 {
193 include_once 'Modules/WebResource/classes/class.ilParameterAppender.php';
194 $appender = new ilParameterAppender($this->getLinkResourceId());
195
196 foreach ($this->getAllItems() as $item) {
197 $new_item = new ilLinkResourceItems($a_new_id);
198 $new_item->setTitle($item['title']);
199 $new_item->setDescription($item['description']);
200 $new_item->setTarget($item['target']);
201 $new_item->setActiveStatus($item['active']);
202 $new_item->setDisableCheckStatus($item['disable_check']);
203 $new_item->setLastCheckDate($item['last_check']);
204 $new_item->setValidStatus($item['valid']);
205 $new_item->setInternal($item['internal']);
206 $new_item->add(true);
207
208 // Add parameters
209 foreach (ilParameterAppender::_getParams($item['link_id']) as $param_id => $data) {
210 $appender->setName($data['name']);
211 $appender->setValue($data['value']);
212 $appender->add($new_item->getLinkId());
213 }
214
215 unset($new_item);
216 }
217 return true;
218 }
219
220 public function delete($a_item_id, $a_update_history = true)
221 {
222 global $ilDB;
223
224 $item = $this->getItem($a_item_id);
225
226 $query = "DELETE FROM webr_items " .
227 "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
228 "AND link_id = " . $ilDB->quote($a_item_id, 'integer');
229 $res = $ilDB->manipulate($query);
230
231 if ($a_update_history) {
232 include_once("./Services/History/classes/class.ilHistory.php");
234 $this->getLinkResourceId(),
235 "delete",
236 $item['title']
237 );
238 }
239
240 return true;
241 }
242
243 public function update($a_update_history = true)
244 {
245 global $ilDB;
246
247 if (!$this->getLinkId()) {
248 return false;
249 }
250
251 $this->__setLastUpdateDate(time());
252 $query = "UPDATE webr_items " .
253 "SET title = " . $ilDB->quote($this->getTitle(), 'text') . ", " .
254 "description = " . $ilDB->quote($this->getDescription(), 'text') . ", " .
255 "target = " . $ilDB->quote($this->getTarget(), 'text') . ", " .
256 "active = " . $ilDB->quote($this->getActiveStatus(), 'integer') . ", " .
257 "valid = " . $ilDB->quote($this->getValidStatus(), 'integer') . ", " .
258 "disable_check = " . $ilDB->quote($this->getDisableCheckStatus(), 'integer') . ", " .
259 "internal = " . $ilDB->quote($this->getInternal(), 'integer') . ", " .
260 "last_update = " . $ilDB->quote($this->getLastUpdateDate(), 'integer') . ", " .
261 "last_check = " . $ilDB->quote($this->getLastCheckDate(), 'integer') . " " .
262 "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer') . " " .
263 "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
264 $res = $ilDB->manipulate($query);
265
266 if ($a_update_history) {
267 include_once("./Services/History/classes/class.ilHistory.php");
269 $this->getLinkResourceId(),
270 "update",
271 $this->getTitle()
272 );
273 }
274
275 return true;
276 }
277
278 public function updateValid($a_status)
279 {
280 global $ilDB;
281
282 $query = "UPDATE webr_items " .
283 "SET valid = " . $ilDB->quote($a_status, 'integer') . " " .
284 "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
285 $res = $ilDB->manipulate($query);
286
287 return true;
288 }
289
290 public function updateActive($a_status)
291 {
292 global $ilDB;
293
294 $query = "UPDATE webr_items " .
295 "SET active = " . $ilDB->quote($a_status, 'integer') . " " .
296 "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
297
298 $this->db->query($query);
299
300 return true;
301 }
302 public function updateDisableCheck($a_status)
303 {
304 global $ilDB;
305
306 $query = "UPDATE webr_items " .
307 "SET disable_check = " . $ilDB->quote($a_status, 'integer') . " " .
308 "WHERE link_id = " . $ilDB->quote($this->getLinkId(), 'integer');
309 $res = $ilDB->manipulate($query);
310
311 return true;
312 }
313
314 public function updateLastCheck($a_offset = 0)
315 {
316 global $ilDB;
317
318 if ($a_offset) {
319 $period = $a_offset ? $a_offset : 0;
320 $time = time() - $period;
321
322
323 $query = "UPDATE webr_items " .
324 "SET last_check = " . $ilDB->quote(time(), 'integer') . " " .
325 "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
326 "AND disable_check = '0' " .
327 "AND last_check < " . $ilDB->quote($time, 'integer');
328 $res = $ilDB->manipulate($query);
329 } else {
330 $query = "UPDATE webr_items " .
331 "SET last_check = " . $ilDB->quote(time(), 'integer') . " " .
332 "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
333 "AND disable_check = '0' ";
334 $res = $ilDB->manipulate($query);
335 }
336 return true;
337 }
338
339 public function updateValidByCheck($a_offset = 0)
340 {
341 global $ilDB;
342
343 if ($a_offset) {
344 $period = $a_offset ? $a_offset : 0;
345 $time = time() - $period;
346
347
348 $query = "UPDATE webr_items " .
349 "SET valid = '1' " .
350 "WHERE disable_check = '0' " .
351 "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
352 "AND last_check < " . $ilDB->quote($time, 'integer');
353 $res = $ilDB->manipulate($query);
354 } else {
355 $query = "UPDATE webr_items " .
356 "SET valid = '1' " .
357 "WHERE disable_check = '0' " .
358 "AND webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
359 $res = $ilDB->manipulate($query);
360 }
361 return true;
362 }
363
364
365 public function add($a_update_history = true)
366 {
367 global $ilDB;
368
369 $this->__setLastUpdateDate(time());
370 $this->__setCreateDate(time());
371
372 $next_id = $ilDB->nextId('webr_items');
373 $query = "INSERT INTO webr_items (link_id,title,description,target,active,disable_check," .
374 "last_update,create_date,webr_id,valid,internal) " .
375 "VALUES( " .
376 $ilDB->quote($next_id, 'integer') . ", " .
377 $ilDB->quote($this->getTitle(), 'text') . ", " .
378 $ilDB->quote($this->getDescription(), 'text') . ", " .
379 $ilDB->quote($this->getTarget(), 'text') . ", " .
380 $ilDB->quote($this->getActiveStatus(), 'integer') . ", " .
381 $ilDB->quote($this->getDisableCheckStatus(), 'integer') . ", " .
382 $ilDB->quote($this->getLastUpdateDate(), 'integer') . ", " .
383 $ilDB->quote($this->getCreateDate(), 'integer') . ", " .
384 $ilDB->quote($this->getLinkResourceId(), 'integer') . ", " .
385 $ilDB->quote($this->getValidStatus(), 'integer') . ', ' .
386 $ilDB->quote($this->getInternal(), 'integer') . ' ' .
387 ")";
388 $res = $ilDB->manipulate($query);
389
390 $link_id = $next_id;
391 $this->setLinkId($link_id);
392
393 if ($a_update_history) {
394 include_once("./Services/History/classes/class.ilHistory.php");
396 $this->getLinkResourceId(),
397 "add",
398 $this->getTitle()
399 );
400 }
401
402 return $link_id;
403 }
404 public function readItem($a_link_id)
405 {
406 global $ilDB;
407
408 $query = "SELECT * FROM webr_items " .
409 "WHERE link_id = " . $ilDB->quote($a_link_id, 'integer');
410
411 $res = $this->db->query($query);
412 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
413 $this->setTitle($row->title);
414 $this->setDescription($row->description);
415 $this->setTarget($row->target);
416 $this->setActiveStatus($row->active);
417 $this->setDisableCheckStatus($row->disable_check);
418 $this->__setCreateDate($row->create_date);
419 $this->__setLastUpdateDate($row->last_update);
420 $this->setLastCheckDate($row->last_check);
421 $this->setValidStatus($row->valid);
422 $this->setLinkId($row->link_id);
423 $this->setInternal($row->internal);
424 }
425 return true;
426 }
427
428
429 public function getItem($a_link_id)
430 {
431 global $ilDB;
432
433 $query = "SELECT * FROM webr_items " .
434 "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer') . " " .
435 "AND link_id = " . $ilDB->quote($a_link_id, 'integer');
436
437 $res = $this->db->query($query);
438 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
439 $item['title'] = $row->title;
440 $item['description'] = $row->description;
441 $item['target'] = $row->target;
442 $item['active'] = (bool) $row->active;
443 $item['disable_check'] = $row->disable_check;
444 $item['create_date'] = $row->create_date;
445 $item['last_update'] = $row->last_update;
446 $item['last_check'] = $row->last_check;
447 $item['valid'] = $row->valid;
448 $item['link_id'] = $row->link_id;
449 $item['internal'] = $row->internal;
450 }
451 return $item ? $item : array();
452 }
453
459 public static function getAllItemIds($a_webr_id)
460 {
461 global $ilDB;
462
463 $query = "SELECT link_id FROM webr_items " .
464 "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer');
465 $res = $ilDB->query($query);
466 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
467 $link_ids[] = $row['link_id'];
468 }
469 return (array) $link_ids;
470 }
471
472 public function getAllItems()
473 {
474 global $ilDB;
475
476 $query = "SELECT * FROM webr_items " .
477 "WHERE webr_id = " . $ilDB->quote($this->getLinkResourceId(), 'integer');
478
479 $res = $this->db->query($query);
480 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
481 $items[$row->link_id]['title'] = $row->title;
482 $items[$row->link_id]['description'] = $row->description;
483 $items[$row->link_id]['target'] = $row->target;
484 $items[$row->link_id]['active'] = (bool) $row->active;
485 $items[$row->link_id]['disable_check'] = $row->disable_check;
486 $items[$row->link_id]['create_date'] = $row->create_date;
487 $items[$row->link_id]['last_update'] = $row->last_update;
488 $items[$row->link_id]['last_check'] = $row->last_check;
489 $items[$row->link_id]['valid'] = $row->valid;
490 $items[$row->link_id]['link_id'] = $row->link_id;
491 $items[$row->link_id]['internal'] = $row->internal;
492 }
493 return $items ? $items : array();
494 }
495
501 public function sortItems($a_items)
502 {
503 include_once './Services/Container/classes/class.ilContainer.php';
504 include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
506
507 if ($mode == ilContainer::SORT_TITLE) {
508 $a_items = ilUtil::sortArray($a_items, 'title', 'asc', false, true);
509 return $a_items;
510 }
511
512
513 if ($mode == ilContainer::SORT_MANUAL) {
514 include_once './Services/Container/classes/class.ilContainerSorting.php';
516 foreach ($a_items as $link_id => $item) {
517 if (isset($pos[$link_id])) {
518 $sorted[$link_id] = $item;
519 $sorted[$link_id]['position'] = $pos[$link_id];
520 } else {
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 public function getActivatedItems()
535 {
536 foreach ($this->getAllItems() as $id => $item_data) {
537 if ($item_data['active']) {
538 $active_items[$id] = $item_data;
539 }
540 }
541 return $active_items ? $active_items : array();
542 }
543
544 public function getCheckItems($a_offset = 0)
545 {
546 $period = $a_offset ? $a_offset : 0;
547 $time = time() - $period;
548
549 foreach ($this->getAllItems() as $id => $item_data) {
550 if (!$item_data['disable_check']) {
551 if (!$item_data['last_check'] or $item_data['last_check'] < $time) {
552 $check_items[$id] = $item_data;
553 }
554 }
555 }
556 return $check_items ? $check_items : array();
557 }
558
559
560
561 // STATIC
562 public static function _deleteAll($webr_id)
563 {
564 global $ilDB;
565
566 $ilDB->manipulate("DELETE FROM webr_items WHERE webr_id = " . $ilDB->quote($webr_id, 'integer'));
567
568 return true;
569 }
570
579 public static function _isSingular($a_webr_id)
580 {
581 global $ilDB;
582
583 $query = "SELECT * FROM webr_items " .
584 "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer') . ' ' .
585 "AND active = " . $ilDB->quote(1, 'integer') . ' ';
586 $res = $ilDB->query($query);
587 return $res->numRows() == 1 ? true : false;
588 }
589
595 public static function lookupNumberOfLinks($a_webr_id)
596 {
597 global $ilDB;
598
599 $query = "SELECT COUNT(*) num FROM webr_items " .
600 "WHERE webr_id = " . $ilDB->quote($a_webr_id, 'integer');
601 $res = $ilDB->query($query);
603 return $row->num;
604 }
605
614 public static function _getFirstLink($a_webr_id)
615 {
616 global $ilDB;
617
618 include_once("./Modules/WebResource/classes/class.ilObjLinkResourceAccess.php");
620 }
621
626 public function validate()
627 {
628 return $this->getTarget() and $this->getTitle();
629 }
630
631
637 public function toXML(ilXmlWriter $writer)
638 {
639 $items = $this->sortItems($this->getAllItems());
640
641 $position = 0;
642 foreach ((array) $items as $item_id => $item) {
643 ++$position;
644 $link = self::lookupItem($this->getLinkResourceId(), $item_id);
645
646 $writer->xmlStartTag(
647 'WebLink',
648 array(
649 'id' => $link['link_id'],
650 'active' => $link['active'] ? 1 : 0,
651 'valid' => $link['valid'] ? 1 : 0,
652 'disableValidation' => $link['disable_check'] ? 1 : 0,
653 'position' => $position,
654 'internal' => $link['internal']
655 )
656 );
657 $writer->xmlElement('Title', array(), $link['title']);
658 $writer->xmlElement('Description', array(), $link['description']);
659 $writer->xmlElement('Target', array(), $link['target']);
660
661 // Dynamic parameters
662 include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
663 foreach (ilParameterAppender::_getParams($link_id) as $param_id => $param) {
664 $value = '';
665 switch ($param['value']) {
666 case LINKS_USER_ID:
667 $value = 'userId';
668 break;
669
670 case LINKS_LOGIN:
671 $value = 'userName';
672 break;
673
675 $value = 'matriculation';
676 break;
677 }
678
679 if (!$value) {
680 // Fix for deprecated LINKS_SESSION
681 continue;
682 }
683
684 $writer->xmlElement(
685 'DynamicParameter',
686 array(
687 'id' => $param_id,
688 'name' => $param['name'],
689 'type' => $value
690 )
691 );
692 }
693
694 $writer->xmlEndTag('WebLink');
695 }
696 return true;
697 }
698}
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.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
$valid
if(!array_key_exists('StateId', $_REQUEST)) $id
$time
Definition: cron.php:21
$query
foreach($_POST as $key=> $value) $res
global $ilDB