ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilNote.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4define("IL_NOTE_PRIVATE", 1);
5define("IL_NOTE_PUBLIC", 2);
6
7define("IL_NOTE_UNLABELED", 0);
8define("IL_NOTE_IMPORTANT", 1);
9define("IL_NOTE_QUESTION", 2);
10define("IL_NOTE_PRO", 3);
11define("IL_NOTE_CONTRA", 4);
12
24class ilNote
25{
26
30 function ilNote($a_id = 0)
31 {
32 if ($a_id > 0)
33 {
34 $this->id = $a_id;
35 $this->read();
36 }
37 }
38
44 function setId($a_id)
45 {
46 $this->id = $a_id;
47 }
48
54 function getId()
55 {
56 return $this->id;
57 }
58
69 function setObject($a_obj_type, $a_rep_obj_id, $a_obj_id = 0)
70 {
71 $this->rep_obj_id = $a_rep_obj_id;
72 $this->obj_id = $a_obj_id;
73 $this->obj_type = $a_obj_type;
74 }
75
76 function getObject()
77 {
78 return array("rep_obj_id" => $this->rep_obj_id,
79 "obj_id" => $this->obj_id,
80 "obj_type" => $this->obj_type);
81 }
82
83
89 function setType($a_type)
90 {
91 $this->type = $a_type;
92 }
93
99 function getType()
100 {
101 return $this->type;
102 }
103
109 function setAuthor($a_user_id)
110 {
111 $this->author = $a_user_id;
112 }
113
119 function getAuthor()
120 {
121 return $this->author;
122 }
123
129 function setText($a_text)
130 {
131 $this->text = $a_text;
132 }
133
139 function getText()
140 {
141 return $this->text;
142 }
143
149 function setSubject($a_subject)
150 {
151 $this->subject = $a_subject;
152 }
153
159 function getSubject()
160 {
161 return $this->subject;
162 }
163
169 function setCreationDate($a_date)
170 {
171 $this->creation_date = $a_date;
172 }
173
180 {
181 return $this->creation_date;
182 }
183
189 function setUpdateDate($a_date)
190 {
191 $this->update_date = $a_date;
192 }
193
199 function getUpdateDate()
200 {
201 return $this->update_date;
202 }
203
210 function setLabel($a_label)
211 {
212 return $this->label = $a_label;
213 }
214
221 function getLabel()
222 {
223 return $this->label;
224 }
225
231 function setInRepository($a_value)
232 {
233 return $this->no_repository = !(bool)$a_value;
234 }
235
241 function isInRepository()
242 {
243 return !$this->no_repository;
244 }
245
246 function create($a_use_provided_creation_date = false)
247 {
248 global $ilDB;
249
250 $cd = ($a_use_provided_creation_date)
251 ? $this->getCreationDate()
252 : ilUtil::now();
253
254 $this->id = $ilDB->nextId("note");
255 /*$q = "INSERT INTO note (id, rep_obj_id, obj_id, obj_type, type,".
256 "author, note_text, subject, label, creation_date) VALUES (".
257 $ilDB->quote($this->id, "integer").",".
258 $ilDB->quote((int) $this->rep_obj_id, "integer").",".
259 $ilDB->quote((int) $this->obj_id, "integer").",".
260 $ilDB->quote((string) $this->obj_type, "text").",".
261 $ilDB->quote((int) $this->type, "integer").",".
262 $ilDB->quote((int) $this->author, "integer").",".
263 $ilDB->quote((string) $this->text, "clob").",".
264 $ilDB->quote((string) $this->subject, "text").",".
265 $ilDB->quote((int) $this->label, "integer").",".
266 $ilDB->now().")";
267 $ilDB->manipulate($q);*/
268
269 $ilDB->insert("note", array(
270 "id" => array("integer", $this->id),
271 "rep_obj_id" => array("integer", (int) $this->rep_obj_id),
272 "obj_id" => array("integer", (int) $this->obj_id),
273 "obj_type" => array("text", (string) $this->obj_type),
274 "type" => array("integer", (int) $this->type),
275 "author" => array("integer", (int) $this->author),
276 "note_text" => array("clob", (string) $this->text),
277 "subject" => array("text", (string) $this->subject),
278 "label" => array("integer", (int) $this->label),
279 "creation_date" => array("timestamp", $cd),
280 "no_repository" => array("integer", $this->no_repository)
281 ));
282
283 $this->creation_date = ilNote::_lookupCreationDate($this->getId());
284 }
285
286 function update()
287 {
288 global $ilDB;
289
290 /*$q = "UPDATE note SET ".
291 "rep_obj_id = ".$ilDB->quote((int) $this->rep_obj_id, "integer").",".
292 "obj_id = ".$ilDB->quote((int) $this->obj_id, "integer").",".
293 "obj_type = ".$ilDB->quote((string) $this->obj_type, "text").",".
294 "type = ".$ilDB->quote((int) $this->type, "integer").",".
295 "author = ".$ilDB->quote((int) $this->author,"integer").",".
296 "note_text = ".$ilDB->quote((string) $this->text, "clob").",".
297 "subject = ".$ilDB->quote((string) $this->subject, "text").",".
298 "update_date = ".$ilDB->now().",".
299 "label = ".$ilDB->quote((int) $this->label, "integer").
300 "WHERE id =".$ilDB->quote((int) $this->getId(), "integer");
301 $ilDB->manipulate($q);*/
302 $ilDB->update("note", array(
303 "rep_obj_id" => array("integer", (int) $this->rep_obj_id),
304 "obj_id" => array("integer", (int) $this->obj_id),
305 "obj_type" => array("text", (string) $this->obj_type),
306 "type" => array("integer", (int) $this->type),
307 "author" => array("integer", (int) $this->author),
308 "note_text" => array("clob", (string) $this->text),
309 "subject" => array("text", (string) $this->subject),
310 "label" => array("integer", (int) $this->label),
311 "update_date" => array("timestamp", ilUtil::now()),
312 "no_repository" => array("integer", $this->no_repository)
313 ), array(
314 "id" => array("integer", $this->getId())
315 ));
316
317 $this->update_date = ilNote::_lookupUpdateDate($this->getId());
318 }
319
320 function read()
321 {
322 global $ilDB;
323
324 $q = "SELECT * FROM note WHERE id = ".
325 $ilDB->quote((int) $this->getId(), "integer");
326 $set = $ilDB->query($q);
327 $note_rec = $ilDB->fetchAssoc($set);
328 $this->setAllData($note_rec);
329 }
330
334 function delete()
335 {
336 global $ilDB;
337
338 $q = "DELETE FROM note WHERE id = ".
339 $ilDB->quote((int) $this->getId(), "integer");
340 $ilDB->manipulate($q);
341 }
342
346 function setAllData($a_note_rec)
347 {
348 $this->setId($a_note_rec["id"]);
349 $this->setObject($a_note_rec["obj_type"], $a_note_rec["rep_obj_id"], $a_note_rec["obj_id"]);
350 $this->setType($a_note_rec["type"]);
351 $this->setAuthor($a_note_rec["author"]);
352 $this->setText($a_note_rec["note_text"]);
353 $this->setSubject($a_note_rec["subject"]);
354 $this->setLabel($a_note_rec["label"]);
355 $this->setCreationDate($a_note_rec["creation_date"]);
356 $this->setUpdateDate($a_note_rec["update_date"]);
357 $this->setInRepository(!(bool)$a_note_rec["no_repository"]);
358 }
359
363 function _lookupCreationDate($a_id)
364 {
365 global $ilDB;
366
367 $q = "SELECT * FROM note WHERE id = ".
368 $ilDB->quote((int) $this->getId(), "integer");
369 $set = $ilDB->query($q);
370 $note_rec = $ilDB->fetchAssoc($set);
371
372 return $note_rec["creation_date"];
373 }
374
378 function _lookupUpdateDate($a_id)
379 {
380 global $ilDB;
381
382 $q = "SELECT * FROM note WHERE id = ".
383 $ilDB->quote((int) $this->getId(), "integer");
384 $set = $ilDB->query($q);
385 $note_rec = $ilDB->fetchAssoc($set);
386
387 return $note_rec["update_date"];
388 }
389
393 function _getNotesOfObject($a_rep_obj_id, $a_obj_id, $a_obj_type,
394 $a_type = IL_NOTE_PRIVATE, $a_incl_sub = false, $a_filter = "",
395 $a_all_public = "y", $a_repository_mode = true, $a_sort_ascending = false)
396 {
397 global $ilDB, $ilUser;
398
399 $author_where = ($a_type == IL_NOTE_PRIVATE || $a_all_public == "n")
400 ? " AND author = ".$ilDB->quote((int) $ilUser->getId(), "integer")
401 : "";
402
403 $sub_where = (!$a_incl_sub)
404 ? " AND obj_id = ".$ilDB->quote((int) $a_obj_id, "integer").
405 " AND obj_type = ".$ilDB->quote((string) $a_obj_type, "text")
406 : "";
407
408 if(!$a_repository_mode)
409 {
410 $sub_where .= " AND no_repository = ".$ilDB->quote(1, "integer");
411 }
412
413 $q = "SELECT * FROM note WHERE ".
414 " rep_obj_id = ".$ilDB->quote((int) $a_rep_obj_id, "integer").
415 $sub_where.
416 " AND type = ".$ilDB->quote((int) $a_type, "integer").
417 $author_where.
418 " ORDER BY creation_date ";
419
420 $q .= ((bool)$a_sort_ascending) ? "ASC" : "DESC";
421
422 $set = $ilDB->query($q);
423 $notes = array();
424 while($note_rec = $ilDB->fetchAssoc($set))
425 {
426 if ($a_filter != "")
427 {
428 if (!is_array($a_filter))
429 {
430 $a_filter = array($a_filter);
431 }
432 if (!in_array($note_rec["id"], $a_filter))
433 {
434 continue;
435 }
436 }
437 $cnt = count($notes);
438 $notes[$cnt] = new ilNote();
439 $notes[$cnt]->setAllData($note_rec);
440 }
441
442 return $notes;
443 }
444
449 {
450 global $ilDB, $ilUser;
451
452 $q = "SELECT * FROM note WHERE ".
453 " type = ".$ilDB->quote((int) IL_NOTE_PRIVATE, "integer").
454 " AND author = ".$ilDB->quote((int) $ilUser->getId(), "integer").
455 " AND (no_repository IS NULL OR no_repository < ".$ilDB->quote(1, "integer").")".
456 " ORDER BY creation_date DESC";
457
458 $ilDB->quote($q);
459 $set = $ilDB->query($q);
460 $notes = array();
461 while($note_rec = $ilDB->fetchAssoc($set))
462 {
463 $cnt = count($notes);
464 $notes[$cnt] = new ilNote();
465 $notes[$cnt]->setAllData($note_rec);
466 }
467
468 return $notes;
469 }
470
474 function _getRelatedObjectsOfUser($a_mode)
475 {
476 global $ilDB, $ilUser, $tree;
477
478 if ($a_mode == ilPDNotesGUI::PRIVATE_NOTES)
479 {
480 $q = "SELECT DISTINCT rep_obj_id FROM note WHERE ".
481 " type = ".$ilDB->quote((int) IL_NOTE_PRIVATE, "integer").
482 " AND author = ".$ilDB->quote($ilUser->getId(), "integer").
483 " AND (no_repository IS NULL OR no_repository < ".$ilDB->quote(1, "integer").")".
484 " ORDER BY rep_obj_id";
485
486 $ilDB->quote($q);
487 $set = $ilDB->query($q);
488 $reps = array();
489 while($rep_rec = $ilDB->fetchAssoc($set))
490 {
491 // #9343: deleted objects
492 if(ilObject::_lookupType($rep_rec["rep_obj_id"]))
493 {
494 $reps[] = array("rep_obj_id" => $rep_rec["rep_obj_id"]);
495 }
496 }
497 }
498 else
499 {
500 // all objects where the user wrote at least one comment
501 $q = "SELECT DISTINCT rep_obj_id FROM note WHERE ".
502 " type = ".$ilDB->quote((int) IL_NOTE_PUBLIC, "integer").
503 " AND author = ".$ilDB->quote($ilUser->getId(), "integer").
504 " AND (no_repository IS NULL OR no_repository < ".$ilDB->quote(1, "integer").")".
505 " ORDER BY rep_obj_id";
506
507 $set = $ilDB->query($q);
508 $reps = array();
509 while($rep_rec = $ilDB->fetchAssoc($set))
510 {
511 // #9343: deleted objects
512 if ($type = ilObject::_lookupType($rep_rec["rep_obj_id"]))
513 {
514 if (ilNote::commentsActivated($rep_rec["rep_obj_id"], "", $type))
515 {
516 $reps[] = array("rep_obj_id" => $rep_rec["rep_obj_id"]);
517 }
518 }
519 }
520
521 // additionally all objects on the personal desktop of the user
522 // that have at least on comment
524 $obj_ids = array();
525 foreach($dis as $di)
526 {
527 $obj_ids[] = $di["obj_id"];
528 }
529 if (count($obj_ids) > 0)
530 {
531 $q = "SELECT DISTINCT rep_obj_id FROM note WHERE ".
532 $ilDB->in("rep_obj_id", $obj_ids, false, "integer").
533 " AND (no_repository IS NULL OR no_repository < ".$ilDB->quote(1, "integer").")";
534
535 $set = $ilDB->query($q);
536 while($rec = $ilDB->fetchAssoc($set))
537 {
538 $add = true;
539 reset($reps);
540 foreach ($reps as $r)
541 {
542 if ($r["rep_obj_id"] == $rec["rep_obj_id"])
543 {
544 $add = false;
545 }
546 }
547 if ($add)
548 {
549 $type = ilObject::_lookupType($rec["rep_obj_id"]);
550 if (ilNote::commentsActivated($rec["rep_obj_id"], "", $type))
551 {
552 $reps[] = array("rep_obj_id" => $rec["rep_obj_id"]);
553 }
554 }
555 }
556 }
557 }
558
559 if(sizeof($reps))
560 {
561 // check if notes/comments belong to objects in trash
562 // see ilNoteGUI::showTargets()
563 foreach($reps as $idx => $rep)
564 {
565 $has_active_ref = false;
566
567 // repository?
568 $ref_ids = ilObject::_getAllReferences($rep["rep_obj_id"]);
569 if($ref_ids)
570 {
571 $reps[$idx]["ref_ids"] = array_values($ref_ids);
572
573 foreach($ref_ids as $ref_id)
574 {
575 if(!$tree->isDeleted($ref_id))
576 {
577 $has_active_ref = true;
578 break;
579 }
580 }
581 }
582 else
583 {
584 // personal workspace?
585 include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
586 include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
587 $wsp_tree = new ilWorkspaceTree($ilUser->getId());
588 $node_id = $wsp_tree->lookupNodeId($rep["rep_obj_id"]);
589 if($node_id)
590 {
591 $reps[$idx]["wsp_id"] = $node_id;
592
593 $has_active_ref = true;
594 }
595 }
596
597 if(!$has_active_ref)
598 {
599 unset($reps[$idx]);
600 }
601 }
602 }
603
604 return $reps;
605 }
606
614 static function getUserCount($a_rep_obj_id, $a_obj_id, $a_type)
615 {
616 global $ilDB;
617
618 $set = $ilDB->queryF("SELECT count(DISTINCT author) cnt FROM note WHERE ".
619 "rep_obj_id = %s AND obj_id = %s AND obj_type = %s",
620 array("integer", "integer", "text"),
621 array((int) $a_rep_obj_id, (int) $a_obj_id, (string)$a_type));
622 $rec = $ilDB->fetchAssoc($set);
623 return (int) $rec["cnt"];
624 }
625
632 static function _countNotesAndCommentsMultiple($a_rep_obj_ids, $a_no_sub_objs = false)
633 {
634 global $ilDB, $ilUser;
635
636 $q = "SELECT count(id) c, rep_obj_id, type FROM note WHERE ".
637 " ((type = ".$ilDB->quote(IL_NOTE_PRIVATE, "integer")." AND ".
638 "author = ".$ilDB->quote((int) $ilUser->getId(), "integer").") OR ".
639 " type = ".$ilDB->quote(IL_NOTE_PUBLIC, "integer").") AND ".
640 $ilDB->in("rep_obj_id", $a_rep_obj_ids, false, "integer");
641
642 if ($a_no_sub_objs)
643 {
644 $q .= " AND obj_id = ".$ilDB->quote(0, "integer");
645 }
646
647 $q .= " GROUP BY rep_obj_id, type ";
648
649 $cnt = array();
650 $set = $ilDB->query($q);
651 while ($rec = $ilDB->fetchAssoc($set))
652 {
653 $cnt[$rec["rep_obj_id"]][$rec["type"]] = $rec["c"];
654 }
655
656 return $cnt;
657 }
658
665 static function _countNotesAndComments($a_rep_obj_id, $a_sub_obj_id = null)
666 {
667 global $ilDB, $ilUser;
668
669 $q = "SELECT count(id) c, rep_obj_id, type FROM note WHERE ".
670 " ((type = ".$ilDB->quote(IL_NOTE_PRIVATE, "integer")." AND ".
671 "author = ".$ilDB->quote((int) $ilUser->getId(), "integer").") OR ".
672 " type = ".$ilDB->quote(IL_NOTE_PUBLIC, "integer").") AND ".
673 " rep_obj_id = ".$ilDB->quote($a_rep_obj_id, "integer");
674
675 if ($a_sub_obj_id !== null)
676 {
677 $q .= " AND obj_id = ".$ilDB->quote($a_sub_obj_id, "integer");
678 }
679
680 $q .= " GROUP BY rep_obj_id, type ";
681
682 $cnt = array();
683 $set = $ilDB->query($q);
684 while ($rec = $ilDB->fetchAssoc($set))
685 {
686 $cnt[$rec["rep_obj_id"]][$rec["type"]] = $rec["c"];
687 }
688
689 return $cnt;
690 }
691
698 static function activateComments($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_activate = true)
699 {
700 global $ilDB;
701
702 if ($a_obj_type == "")
703 {
704 $a_obj_type = "-";
705 }
706 $set = $ilDB->query("SELECT * FROM note_settings ".
707 " WHERE rep_obj_id = ".$ilDB->quote((int) $a_rep_obj_id, "integer").
708 " AND obj_id = ".$ilDB->quote((int) $a_obj_id, "integer").
709 " AND obj_type = ".$ilDB->quote($a_obj_type, "text")
710 );
711 if ($rec = $ilDB->fetchAssoc($set))
712 {
713 if (($rec["activated"] == 0 && $a_activate) ||
714 ($rec["activated"] == 1 && !$a_activate))
715 {
716 $ilDB->manipulate("UPDATE note_settings SET ".
717 " activated = ".$ilDB->quote((int) $a_activate, "integer").
718 " WHERE rep_obj_id = ".$ilDB->quote((int) $a_rep_obj_id, "integer").
719 " AND obj_id = ".$ilDB->quote((int) $a_obj_id, "integer").
720 " AND obj_type = ".$ilDB->quote($a_obj_type, "text")
721 );
722 }
723 }
724 else
725 {
726 if ($a_activate)
727 {
728 $q = "INSERT INTO note_settings ".
729 "(rep_obj_id, obj_id, obj_type, activated) VALUES (".
730 $ilDB->quote((int) $a_rep_obj_id, "integer").",".
731 $ilDB->quote((int) $a_obj_id, "integer").",".
732 $ilDB->quote($a_obj_type, "text").",".
733 $ilDB->quote(1, "integer").
734 ")";
735 $ilDB->manipulate($q);
736 }
737 }
738 }
739
746 static function commentsActivated($a_rep_obj_id, $a_obj_id, $a_obj_type)
747 {
748 global $ilDB;
749
750 if ($a_obj_type == "")
751 {
752 $a_obj_type = "-";
753 }
754 $set = $ilDB->query("SELECT * FROM note_settings ".
755 " WHERE rep_obj_id = ".$ilDB->quote((int) $a_rep_obj_id, "integer").
756 " AND obj_id = ".$ilDB->quote((int) $a_obj_id, "integer").
757 " AND obj_type = ".$ilDB->quote($a_obj_type, "text")
758 );
759 $rec = $ilDB->fetchAssoc($set);
760 return $rec["activated"];
761 }
762
769 static function getRepObjActivation($a_rep_obj_ids)
770 {
771 global $ilDB;
772
773 $set = $ilDB->query("SELECT * FROM note_settings ".
774 " WHERE ".$ilDB->in("rep_obj_id", $a_rep_obj_ids, false, "integer").
775 " AND obj_id = 0 ");
776 $activations = array();
777 while ($rec = $ilDB->fetchAssoc($set))
778 {
779 if ($rec["activated"])
780 {
781 $activations[$rec["rep_obj_id"]][$rec["obj_type"]] = true;
782 }
783 }
784
785 return $activations;
786 }
787
788}
789?>
const IL_NOTE_PRIVATE
Definition: class.ilNote.php:4
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:5
Note class.
static commentsActivated($a_rep_obj_id, $a_obj_id, $a_obj_type)
Are comments activated for object?
create($a_use_provided_creation_date=false)
setObject($a_obj_type, $a_rep_obj_id, $a_obj_id=0)
set assigned object
getUpdateDate()
get update date
setType($a_type)
set type
static activateComments($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_activate=true)
Activate notes feature.
getSubject()
get subject
getLabel()
get label
_lookupCreationDate($a_id)
lookup creation date of note
_getLastNotesOfUser()
get last notes of current user
static _countNotesAndComments($a_rep_obj_id, $a_sub_obj_id=null)
Get all notes related to a specific object.
setLabel($a_label)
set label
setUpdateDate($a_date)
set update date
setId($a_id)
set id
setAllData($a_note_rec)
set all note data by record array
setAuthor($a_user_id)
set author
_lookupUpdateDate($a_id)
lookup update date of note
getAuthor()
get author
_getNotesOfObject($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_type=IL_NOTE_PRIVATE, $a_incl_sub=false, $a_filter="", $a_all_public="y", $a_repository_mode=true, $a_sort_ascending=false)
get all notes related to a specific object
setText($a_text)
set text
getText()
get text
ilNote($a_id=0)
constructor
getCreationDate()
get creation date
setInRepository($a_value)
set repository object status
static _countNotesAndCommentsMultiple($a_rep_obj_ids, $a_no_sub_objs=false)
Get all notes related to multiple objcts.
getId()
get id
static getRepObjActivation($a_rep_obj_ids)
Get activation for repository objects.
setCreationDate($a_date)
set creation date
isInRepository()
belongs note to repository object?
getType()
get type
_getRelatedObjectsOfUser($a_mode)
get all related objects for user
static getUserCount($a_rep_obj_id, $a_obj_id, $a_type)
How many users have attached a note/comment to a given object?
setSubject($a_subject)
set subject
static _lookupDesktopItems($user_id, $a_types="")
get all desktop items of user and specified type
static _getAllReferences($a_id)
get all reference ids of object
static _lookupType($a_id, $a_reference=false)
lookup object type
static now()
Return current timestamp in Y-m-d H:i:s format.
Tree handler for personal workspace.
$ref_id
Definition: sahs_server.php:39
global $ilDB
global $ilUser
Definition: imgupload.php:15