ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjGroup.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
25 //TODO: function getRoleId($groupRole) returns the object-id of grouprole
26 
27 require_once "./Services/Container/classes/class.ilContainer.php";
28 include_once('./Services/Calendar/classes/class.ilDateTime.php');
29 
30 
31 define('GRP_REGISTRATION_DEACTIVATED',-1);
32 define('GRP_REGISTRATION_DIRECT',0);
33 define('GRP_REGISTRATION_REQUEST',1);
34 define('GRP_REGISTRATION_PASSWORD',2);
35 
36 define('GRP_REGISTRATION_LIMITED',1);
37 define('GRP_REGISTRATION_UNLIMITED',2);
38 
39 define('GRP_TYPE_UNKNOWN',0);
40 define('GRP_TYPE_CLOSED',1);
41 define('GRP_TYPE_OPEN',2);
42 define('GRP_TYPE_PUBLIC',3);
43 
54 class ilObjGroup extends ilContainer
55 {
56  const CAL_REG_START = 1;
57  const CAL_REG_END = 2;
58 
59  const GRP_MEMBER = 1;
60  const GRP_ADMIN = 2;
61 
62  const ERR_MISSING_TITLE = 'msg_no_title';
63  const ERR_MISSING_GROUP_TYPE = 'grp_missing_grp_type';
64  const ERR_MISSING_PASSWORD = 'grp_missing_password';
65  const ERR_WRONG_MAX_MEMBERS = 'grp_wrong_max_members';
66  const ERR_WRONG_REG_TIME_LIMIT = 'grp_wrong_reg_time_limit';
67 
68  protected $information;
69  protected $group_type = null;
71  protected $reg_enabled = true;
72  protected $reg_unlimited = true;
73  protected $reg_start = null;
74  protected $reg_end = null;
75  protected $reg_password = '';
76  protected $reg_membership_limitation = false;
77  protected $reg_max_members = 0;
78  protected $waiting_list = false;
79 
80 
81  // Map
82  private $latitude = '';
83  private $longitude = '';
84  private $locationzoom = 0;
85  private $enablemap = 0;
86 
87 
88  public $members_obj;
89 
90 
94  var $file_obj = null;
95 
97 
99 
101 
108  public function __construct($a_id = 0,$a_call_by_reference = true)
109  {
110  global $tree;
111 
112  $this->tree =& $tree;
113 
114  $this->type = "grp";
115  $this->ilObject($a_id,$a_call_by_reference);
116  $this->setRegisterMode(true); // ???
117  }
118 
124  public static function lookupGroupTye($a_id)
125  {
126  global $ilDB;
127 
128  $query = "SELECT grp_type FROM grp_settings ".
129  "WHERE obj_id = ".$ilDB->quote($a_id,'integer');
130  $res = $ilDB->query($query);
131  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
132  {
133  return $row->grp_type;
134  }
135  return GRP_TYPE_UNKNOWN;
136  }
137 
138  // Setter/Getter
146  public function setInformation($a_information)
147  {
148  $this->information = $a_information;
149  }
150 
158  public function getInformation()
159  {
160  return $this->information;
161  }
162 
169  public function setGroupType($a_type)
170  {
171  $this->group_type = $a_type;
172  }
173 
180  public function getGroupType()
181  {
182  return $this->group_type;
183  }
184 
192  public function isGroupTypeModified($a_old_type)
193  {
194  if($a_old_type == GRP_TYPE_UNKNOWN)
195  {
196  $group_type = $this->readGroupStatus();
197  }
198  else
199  {
200  $group_type = $a_old_type;
201  }
202  return $group_type != $this->getGroupType();
203  }
204 
212  public function setRegistrationType($a_type)
213  {
214  $this->reg_type = $a_type;
215  }
216 
223  public function getRegistrationType()
224  {
225  return $this->reg_type;
226  }
227 
234  public function isRegistrationEnabled()
235  {
237  }
238 
246  public function enableUnlimitedRegistration($a_status)
247  {
248  $this->reg_unlimited = $a_status;
249  }
250 
257  public function isRegistrationUnlimited()
258  {
259  return $this->reg_unlimited;
260  }
261 
269  public function setRegistrationStart($a_start)
270  {
271  $this->reg_start = $a_start;
272  }
273 
280  public function getRegistrationStart()
281  {
282  return $this->reg_start ? $this->reg_start : $this->reg_start = new ilDateTime(date('Y-m-d').' 08:00:00',IL_CAL_DATETIME);
283  }
284 
285 
293  public function setRegistrationEnd($a_end)
294  {
295  $this->reg_end = $a_end;
296  }
297 
304  public function getRegistrationEnd()
305  {
306  return $this->reg_end ? $this->reg_end : $this->reg_end = new ilDateTime(date('Y-m-d').' 16:00:00',IL_CAL_DATETIME);
307  }
308 
315  public function setPassword($a_pass)
316  {
317  $this->reg_password = $a_pass;
318  }
319 
326  public function getPassword()
327  {
328  return $this->reg_password;
329  }
330 
338  public function enableMembershipLimitation($a_status)
339  {
340  $this->reg_membership_limitation = $a_status;
341  }
342 
349  public function isMembershipLimited()
350  {
351  return (bool) $this->reg_membership_limitation;
352  }
353 
360  public function setMaxMembers($a_max)
361  {
362  $this->reg_max_members = $a_max;
363  }
364 
371  public function getMaxMembers()
372  {
373  return $this->reg_max_members;
374  }
375 
383  public function enableWaitingList($a_status)
384  {
385  $this->waiting_list = $a_status;
386  }
387 
395  public function isWaitingListEnabled()
396  {
397  return $this->waiting_list;
398  }
399 
405  function setLatitude($a_latitude)
406  {
407  $this->latitude = $a_latitude;
408  }
409 
415  function getLatitude()
416  {
417  return $this->latitude;
418  }
419 
425  function setLongitude($a_longitude)
426  {
427  $this->longitude = $a_longitude;
428  }
429 
435  function getLongitude()
436  {
437  return $this->longitude;
438  }
439 
445  function setLocationZoom($a_locationzoom)
446  {
447  $this->locationzoom = $a_locationzoom;
448  }
449 
455  function getLocationZoom()
456  {
457  return $this->locationzoom;
458  }
459 
465  function setEnableGroupMap($a_enablemap)
466  {
467  $this->enablemap = $a_enablemap;
468  }
469 
475  function getEnableGroupMap()
476  {
477  return (bool) $this->enablemap;
478  }
479 
486  public function validate()
487  {
488  global $ilErr;
489 
490  if(!$this->getTitle())
491  {
492  $this->title = '';
493  $ilErr->appendMessage($this->lng->txt(self::ERR_MISSING_TITLE));
494  }
495  if(!$this->getGroupType())
496  {
497  $ilErr->appendMessage($this->lng->txt(self::ERR_MISSING_GROUP_TYPE));
498  }
499  if($this->getRegistrationType() == GRP_REGISTRATION_PASSWORD and !strlen($this->getPassword()))
500  {
501  $ilErr->appendMessage($this->lng->txt(self::ERR_MISSING_PASSWORD));
502  }
504  {
505  $ilErr->appendMessage($this->lng->txt(self::ERR_WRONG_REG_TIME_LIMIT));
506  }
507  if($this->isMembershipLimited() and (!is_numeric($this->getMaxMembers()) or $this->getMaxMembers() <= 0))
508  {
509  $ilErr->appendMessage($this->lng->txt(self::ERR_WRONG_MAX_MEMBERS));
510  }
511  return strlen($ilErr->getMessage()) == 0;
512  }
513 
514 
515 
516 
520  function create()
521  {
522  global $ilDB,$ilAppEventHandler;
523 
524  if(!parent::create())
525  {
526  return false;
527  }
528 
529  $query = "INSERT INTO grp_settings (obj_id,information,grp_type,registration_type,registration_enabled,".
530  "registration_unlimited,registration_start,registration_end,registration_password,registration_mem_limit,".
531  "registration_max_members,waiting_list,latitude,longitude,location_zoom,enablemap) ".
532  "VALUES(".
533  $ilDB->quote($this->getId() ,'integer').", ".
534  $ilDB->quote($this->getInformation() ,'text').", ".
535  $ilDB->quote((int) $this->getGroupType() ,'integer').", ".
536  $ilDB->quote($this->getRegistrationType() ,'integer').", ".
537  $ilDB->quote(($this->isRegistrationEnabled() ? 1 : 0) ,'integer').", ".
538  $ilDB->quote(($this->isRegistrationUnlimited() ? 1 : 0) ,'integer').", ".
539  $ilDB->quote($this->getRegistrationStart()->get(IL_CAL_DATETIME,'') ,'timestamp').", ".
540  $ilDB->quote($this->getRegistrationEnd()->get(IL_CAL_DATETIME,'') ,'timestamp').", ".
541  $ilDB->quote($this->getPassword() ,'text').", ".
542  $ilDB->quote((int) $this->isMembershipLimited() ,'integer').", ".
543  $ilDB->quote($this->getMaxMembers() ,'integer').", ".
544  $ilDB->quote($this->isWaitingListEnabled() ? 1 : 0 ,'integer').", ".
545  $ilDB->quote($this->getLatitude() ,'text').", ".
546  $ilDB->quote($this->getLongitude() ,'text').", ".
547  $ilDB->quote($this->getLocationZoom() ,'integer').", ".
548  $ilDB->quote((int) $this->getEnableGroupMap() ,'integer')." ".
549  ")";
550  $res = $ilDB->manipulate($query);
551 
552  $ilAppEventHandler->raise('Modules/Group',
553  'create',
554  array('object' => $this,
555  'obj_id' => $this->getId(),
556  'appointments' => $this->prepareAppointments('create')));
557 
558  return $this->getId();
559  }
560 
564  function update()
565  {
566  global $ilDB,$ilAppEventHandler;
567 
568  if (!parent::update())
569  {
570  return false;
571  }
572 
573  $query = "UPDATE grp_settings ".
574  "SET information = ".$ilDB->quote($this->getInformation() ,'text').", ".
575  "grp_type = ".$ilDB->quote((int) $this->getGroupType() ,'integer').", ".
576  "registration_type = ".$ilDB->quote($this->getRegistrationType() ,'integer').", ".
577  "registration_enabled = ".$ilDB->quote($this->isRegistrationEnabled() ? 1 : 0 ,'integer').", ".
578  "registration_unlimited = ".$ilDB->quote($this->isRegistrationUnlimited() ? 1 : 0 ,'integer').", ".
579  "registration_start = ".$ilDB->quote($this->getRegistrationStart()->get(IL_CAL_DATETIME,'') ,'timestamp').", ".
580  "registration_end = ".$ilDB->quote($this->getRegistrationEnd()->get(IL_CAL_DATETIME,'') ,'timestamp').", ".
581  "registration_password = ".$ilDB->quote($this->getPassword() ,'text').", ".
582 // "registration_membership_limited = ".$ilDB->quote((int) $this->isMembershipLimited() ,'integer').", ".
583  "registration_mem_limit = ".$ilDB->quote((int) $this->isMembershipLimited() ,'integer').", ".
584  "registration_max_members = ".$ilDB->quote($this->getMaxMembers() ,'integer').", ".
585  "waiting_list = ".$ilDB->quote($this->isWaitingListEnabled() ? 1 : 0 ,'integer').", ".
586  "latitude = ".$ilDB->quote($this->getLatitude() ,'text').", ".
587  "longitude = ".$ilDB->quote($this->getLongitude() ,'text').", ".
588  "location_zoom = ".$ilDB->quote($this->getLocationZoom() ,'integer').", ".
589  "enablemap = ".$ilDB->quote((int) $this->getEnableGroupMap() ,'integer')." ".
590  "WHERE obj_id = ".$ilDB->quote($this->getId() ,'integer')." ";
591  $res = $ilDB->manipulate($query);
592 
593  $ilAppEventHandler->raise('Modules/Group',
594  'update',
595  array('object' => $this,
596  'obj_id' => $this->getId(),
597  'appointments' => $this->prepareAppointments('update')));
598 
599 
600  return true;
601  }
602 
609  public function delete()
610  {
611  global $ilDB,$ilAppEventHandler;
612 
613  // always call parent delete function first!!
614  if (!parent::delete())
615  {
616  return false;
617  }
618 
619  $query = "DELETE FROM grp_settings ".
620  "WHERE obj_id = ".$ilDB->quote($this->getId() ,'integer');
621  $res = $ilDB->manipulate($query);
622 
623  include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
625 
626  $ilAppEventHandler->raise('Modules/Group',
627  'delete',
628  array('object' => $this,
629  'obj_id' => $this->getId(),
630  'appointments' => $this->prepareAppointments('delete')));
631 
632 
633  return true;
634  }
635 
636 
640  function read()
641  {
642  global $ilDB;
643 
644  parent::read();
645 
646  $query = "SELECT * FROM grp_settings ".
647  "WHERE obj_id = ".$ilDB->quote($this->getId() ,'integer');
648 
649  $res = $ilDB->query($query);
650  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
651  {
652  $this->setInformation($row->information);
653  $this->setGroupType($row->grp_type);
654  $this->setRegistrationType($row->registration_type);
655  $this->enableUnlimitedRegistration($row->registration_unlimited);
656  $this->setRegistrationStart(new ilDateTime($row->registration_start,IL_CAL_DATETIME));
657  $this->setRegistrationEnd(new ilDateTime($row->registration_end,IL_CAL_DATETIME));
658  $this->setPassword($row->registration_password);
659  $this->enableMembershipLimitation((bool) $row->registration_mem_limit);
660  $this->setMaxMembers($row->registration_max_members);
661  $this->enableWaitingList($row->waiting_list);
662  $this->setLatitude($row->latitude);
663  $this->setLongitude($row->longitude);
664  $this->setLocationZoom($row->location_zoom);
665  $this->setEnableGroupMap($row->enablemap);
666  }
667  $this->initParticipants();
668 
669  // Inherit order type from parent course (if exists)
670  include_once('./Services/Container/classes/class.ilContainerSortingSettings.php');
672  }
673 
682  public function cloneObject($a_target_id,$a_copy_id = 0)
683  {
684  global $ilDB,$ilUser;
685 
686  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
687  $new_obj->setGroupType($this->getGroupType());
688  $new_obj->initGroupStatus($this->getGroupType() ? $this->getGroupType() : $this->readGroupStatus());
689 
690  $this->cloneAutoGeneratedRoles($new_obj);
691 
692  $new_obj->setInformation($this->getInformation());
693  $new_obj->setRegistrationStart($this->getRegistrationStart());
694  $new_obj->setRegistrationEnd($this->getRegistrationEnd());
695  $new_obj->enableUnlimitedRegistration($this->isRegistrationUnlimited());
696  $new_obj->setPassword($this->getPassword());
697  $new_obj->enableMembershipLimitation($this->isMembershipLimited());
698  $new_obj->setMaxMembers($this->getMaxMembers());
699  $new_obj->enableWaitingList($this->isWaitingListEnabled());
700 
701  // map
702  $new_obj->setLatitude($this->getLatitude());
703  $new_obj->setLongitude($this->getLongitude());
704  $new_obj->setLocationZoom($this->getLocationZoom());
705  $new_obj->setEnableGroupMap($this->getEnableGroupMap());
706  $new_obj->update();
707 
708  global $ilLog;
709  $ilLog->write(__METHOD__.': Starting add user');
710 
711  // Assign user as admin
712  include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
713  $part = ilGroupParticipants::_getInstanceByObjId($new_obj->getId());
714  $part->add($ilUser->getId(),IL_GRP_ADMIN);
715  $part->updateNotification($ilUser->getId(),1);
716 
717  // Copy learning progress settings
718  include_once('Services/Tracking/classes/class.ilLPObjSettings.php');
719  $obj_settings = new ilLPObjSettings($this->getId());
720  $obj_settings->cloneSettings($new_obj->getId());
721  unset($obj_settings);
722 
723  // clone icons
724  $new_obj->saveIcons($this->getBigIconPath(),
725  $this->getSmallIconPath(),
726  $this->getTinyIconPath());
727 
728  return $new_obj;
729  }
730 
739  public function cloneDependencies($a_target_id,$a_copy_id)
740  {
741  global $tree;
742 
743  parent::cloneDependencies($a_target_id,$a_copy_id);
744 
745  if($course_ref_id = $tree->checkForParentType($this->getRefId(),'crs') and
746  $new_course_ref_id = $tree->checkForParentType($a_target_id,'crs'))
747  {
748  include_once('Modules/Course/classes/class.ilCourseItems.php');
749  $course_obj =& ilObjectFactory::getInstanceByRefId($course_ref_id,false);
750  $course_items = new ilCourseItems($course_obj,$this->getRefId());
751  $course_items->cloneDependencies($a_target_id,$a_copy_id);
752  }
753 
754  include_once('Services/Tracking/classes/class.ilLPCollections.php');
755  $lp_collection = new ilLPCollections($this->getId());
756  $lp_collection->cloneCollections($a_target_id,$a_copy_id);
757 
758  return true;
759  }
760 
768  public function cloneAutoGeneratedRoles($new_obj)
769  {
770  global $ilLog,$rbacadmin,$rbacreview;
771 
772  $admin = $this->getDefaultAdminRole();
773  $new_admin = $new_obj->getDefaultAdminRole();
774  $source_rolf = $rbacreview->getRoleFolderIdOfObject($this->getRefId());
775  $target_rolf = $rbacreview->getRoleFolderIdOfObject($new_obj->getRefId());
776 
777  if(!$admin || !$new_admin || !$source_rolf || !$target_rolf)
778  {
779  $ilLog->write(__METHOD__.' : Error cloning auto generated role: il_grp_admin');
780  }
781  $rbacadmin->copyRolePermissions($admin,$source_rolf,$target_rolf,$new_admin,true);
782  $ilLog->write(__METHOD__.' : Finished copying of role il_grp_admin.');
783 
784  $member = $this->getDefaultMemberRole();
785  $new_member = $new_obj->getDefaultMemberRole();
786  if(!$member || !$new_member)
787  {
788  $ilLog->write(__METHOD__.' : Error cloning auto generated role: il_grp_member');
789  }
790  $rbacadmin->copyRolePermissions($member,$source_rolf,$target_rolf,$new_member,true);
791  $ilLog->write(__METHOD__.' : Finished copying of role grp_member.');
792  }
793 
794 
800  function join($a_user_id, $a_mem_role="")
801  {
802  global $rbacadmin;
803 
804  if (is_array($a_mem_role))
805  {
806  foreach ($a_mem_role as $role)
807  {
808  $rbacadmin->assignUser($role,$a_user_id, false);
809  }
810  }
811  else
812  {
813  $rbacadmin->assignUser($a_mem_role,$a_user_id, false);
814  }
815 
816  return true;
817  }
818 
824  {
825  $local_group_Roles = $this->getLocalGroupRoles();
826 
827  return $local_group_Roles["il_grp_member_".$this->getRefId()];
828  }
829 
835  {
836  $local_group_Roles = $this->getLocalGroupRoles();
837 
838  return $local_group_Roles["il_grp_admin_".$this->getRefId()];
839  }
840 
847  function addMember($a_user_id, $a_mem_role)
848  {
849  global $rbacadmin;
850 
851  if (isset($a_user_id) && isset($a_mem_role) )
852  {
853  $this->join($a_user_id,$a_mem_role);
854  return true;
855  }
856  else
857  {
858  $this->ilias->raiseError(get_class($this)."::addMember(): Missing parameters !",$this->ilias->error_obj->WARNING);
859  return false;
860  }
861  }
862 
863 
870  function leaveGroup()
871  {
872  global $rbacadmin, $rbacreview;
873 
874  $member_ids = $this->getGroupMemberIds();
875 
876  if (count($member_ids) <= 1 || !in_array($this->ilias->account->getId(), $member_ids))
877  {
878  return 2;
879  }
880  else
881  {
882  if (!$this->isAdmin($this->ilias->account->getId()))
883  {
884  $this->leave($this->ilias->account->getId());
885  $member = new ilObjUser($this->ilias->account->getId());
886  $member->dropDesktopItem($this->getRefId(), "grp");
887 
888  return 0;
889  }
890  else if (count($this->getGroupAdminIds()) == 1)
891  {
892  return 1;
893  }
894  }
895  }
896 
901  function leave($a_user_id)
902  {
903  global $rbacadmin;
904 
905  $arr_groupRoles = $this->getMemberRoles($a_user_id);
906 
907  if (is_array($arr_groupRoles))
908  {
909  foreach ($arr_groupRoles as $groupRole)
910  {
911  $rbacadmin->deassignUser($groupRole, $a_user_id);
912  }
913  }
914  else
915  {
916  $rbacadmin->deassignUser($arr_groupRoles, $a_user_id);
917  }
918 
919  return true;
920  }
921 
928  function getGroupMemberIds()
929  {
930  global $rbacadmin, $rbacreview;
931 
932  $usr_arr= array();
933 
934  $rol = $this->getLocalGroupRoles();
935 
936  foreach ($rol as $value)
937  {
938  foreach ($rbacreview->assignedUsers($value) as $member_id)
939  {
940  array_push($usr_arr,$member_id);
941  }
942  }
943 
944  $mem_arr = array_unique($usr_arr);
945 
946  return $mem_arr ? $mem_arr : array();
947  }
948 
956  function getGroupMemberData($a_mem_ids, $active = 1)
957  {
958  global $rbacadmin, $rbacreview, $ilBench, $ilDB;
959 
960  $usr_arr= array();
961 
962  $q = "SELECT login,firstname,lastname,title,usr_id,last_login ".
963  "FROM usr_data ".
964  "WHERE usr_id IN (".implode(',',ilUtil::quoteArray($a_mem_ids)).") ";
965 
966  if (is_numeric($active) && $active > -1)
967  $q .= "AND active = '$active'";
968 
969  $q .= 'ORDER BY lastname,firstname';
970 
971  $r = $ilDB->query($q);
972 
973  while($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
974  {
975  $mem_arr[] = array("id" => $row->usr_id,
976  "login" => $row->login,
977  "firstname" => $row->firstname,
978  "lastname" => $row->lastname,
979  "last_login" => $row->last_login
980  );
981  }
982 
983  return $mem_arr ? $mem_arr : array();
984  }
985 
986  function getCountMembers()
987  {
988  return count($this->getGroupMemberIds());
989  }
990 
997  function getGroupAdminIds($a_grpId = "")
998  {
999  global $rbacreview;
1000 
1001  if (!empty($a_grpId))
1002  {
1003  $grp_id = $a_grpId;
1004  }
1005  else
1006  {
1007  $grp_id = $this->getRefId();
1008  }
1009 
1010  $usr_arr = array();
1011  $roles = $this->getDefaultGroupRoles($this->getRefId());
1012 
1013  foreach ($rbacreview->assignedUsers($this->getDefaultAdminRole()) as $member_id)
1014  {
1015  array_push($usr_arr,$member_id);
1016  }
1017 
1018  return $usr_arr;
1019  }
1020 
1026  function getDefaultGroupRoles($a_grp_id="")
1027  {
1028  global $rbacadmin, $rbacreview;
1029 
1030  if (strlen($a_grp_id) > 0)
1031  {
1032  $grp_id = $a_grp_id;
1033  }
1034  else
1035  {
1036  $grp_id = $this->getRefId();
1037  }
1038 
1039  $rolf = $rbacreview->getRoleFolderOfObject($grp_id);
1040  $role_arr = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"]);
1041 
1042  foreach ($role_arr as $role_id)
1043  {
1044  $role_Obj =& $this->ilias->obj_factory->getInstanceByObjId($role_id);
1045 
1046  $grp_Member ="il_grp_member_".$grp_id;
1047  $grp_Admin ="il_grp_admin_".$grp_id;
1048 
1049  if (strcmp($role_Obj->getTitle(), $grp_Member) == 0 )
1050  {
1051  $arr_grpDefaultRoles["grp_member_role"] = $role_Obj->getId();
1052  }
1053 
1054  if (strcmp($role_Obj->getTitle(), $grp_Admin) == 0)
1055  {
1056  $arr_grpDefaultRoles["grp_admin_role"] = $role_Obj->getId();
1057  }
1058  }
1059 
1060  return $arr_grpDefaultRoles;
1061  }
1062 
1069  function getLocalGroupRoles($a_translate = false)
1070  {
1071  global $rbacadmin,$rbacreview;
1072 
1073  if (empty($this->local_roles))
1074  {
1075  $this->local_roles = array();
1076  $rolf = $rbacreview->getRoleFolderOfObject($this->getRefId());
1077  $role_arr = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"]);
1078 
1079  foreach ($role_arr as $role_id)
1080  {
1081  if ($rbacreview->isAssignable($role_id,$rolf["ref_id"]) == true)
1082  {
1083  $role_Obj =& $this->ilias->obj_factory->getInstanceByObjId($role_id);
1084 
1085  if ($a_translate)
1086  {
1087  $role_name = ilObjRole::_getTranslation($role_Obj->getTitle());
1088  }
1089  else
1090  {
1091  $role_name = $role_Obj->getTitle();
1092  }
1093 
1094  $this->local_roles[$role_name] = $role_Obj->getId();
1095  }
1096  }
1097  }
1098 
1099  return $this->local_roles;
1100  }
1101 
1108  {
1109  $q = "SELECT obj_id FROM object_data WHERE type='rolt' AND title='il_grp_status_closed'";
1110  $res = $this->ilias->db->query($q);
1111  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
1112 
1113  return $row["obj_id"];
1114  }
1115 
1122  {
1123  $q = "SELECT obj_id FROM object_data WHERE type='rolt' AND title='il_grp_status_open'";
1124  $res = $this->ilias->db->query($q);
1125  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
1126 
1127  return $row["obj_id"];
1128  }
1129 
1130 
1131 
1141  public function updateGroupType()
1142  {
1143  global $tree,$rbacreview,$rbacadmin;
1144 
1145  $parent_roles = $rbacreview->getParentRoleIds($this->getRefId());
1146  $real_parent_roles = array_diff(array_keys($parent_roles),$this->getDefaultGroupRoles());
1147  $rolf_data = $rbacreview->getRoleFolderOfObject($this->getRefId());
1148 
1149  // Delete parent roles with stopped inheritance
1150  foreach($real_parent_roles as $role_id)
1151  {
1152  // Delete local role
1153  if(isset($rolf_data['child']) and $rolf_data['child'])
1154  {
1155  $rbacadmin->deleteLocalRole($role_id,$rolf_data['child']);
1156  }
1157  }
1158  $parent_roles = $rbacreview->getParentRoleIds($this->getRefId());
1159  $real_parent_roles = array_diff(array_keys($parent_roles),$this->getDefaultGroupRoles());
1160 
1161  switch($this->getGroupType())
1162  {
1163  case GRP_TYPE_PUBLIC:
1164  $template_id = $this->getGrpStatusOpenTemplateId();
1165  break;
1166 
1167  case GRP_TYPE_CLOSED:
1168  $template_id = $this->getGrpStatusClosedTemplateId();
1169  break;
1170  }
1171 
1172  $first = true;
1173  foreach($tree->getFilteredSubTree($this->getRefId(),array('rolf','grp')) as $subnode)
1174  {
1175  // Read template operations
1176  $template_ops = $rbacreview->getOperationsOfRole($template_id,$subnode['type'], ROLE_FOLDER_ID);
1177 
1178  $rolf_data = $rbacreview->getRoleFolderOfObject($subnode['child']);
1179 
1180 
1181  // for all parent roles
1182  foreach($real_parent_roles as $role_id)
1183  {
1184  if($rbacreview->isProtected($parent_roles[$role_id]['parent'],$role_id))
1185  {
1186  continue;
1187  }
1188 
1189  // Delete local role
1190  if(isset($rolf_data['child']) and $rolf_data['child'])
1191  {
1192  $rbacadmin->deleteLocalRole($role_id,$rolf_data['child']);
1193  }
1194 
1195  // Store current operations
1196  $current_ops = $rbacreview->getOperationsOfRole($role_id,$subnode['type'],$parent_roles[$role_id]['parent']);
1197 
1198  // Revoke permissions
1199  $rbacadmin->revokePermission($subnode['child'],$role_id);
1200 
1201  // Grant permissions
1202  $granted = array();
1203  foreach($template_ops as $operation)
1204  {
1205  if(in_array($operation,$current_ops))
1206  {
1207  $granted[] = $operation;
1208  }
1209  }
1210  if($granted)
1211  {
1212  $rbacadmin->grantPermission($role_id, $granted,$subnode['child']);
1213  }
1214 
1215  if($first)
1216  {
1217  // This is the group itself
1218  $rbacadmin->copyRolePermissionIntersection(
1219  $template_id, ROLE_FOLDER_ID,
1220  $role_id, $parent_roles[$role_id]['parent'],
1221  $rolf_data["child"],$role_id);
1222  $rbacadmin->assignRoleToFolder($role_id,$rolf_data["child"],"n");
1223 
1224  }
1225  }
1226  $first = false;
1227  }
1228  }
1229 
1247  function initGroupStatus($a_grpStatus = GRP_TYPE_PUBLIC)
1248  {
1249  global $rbacadmin, $rbacreview, $rbacsystem;
1250 
1251  //get Rolefolder of group
1252  $rolf_data = $rbacreview->getRoleFolderOfObject($this->getRefId());
1253 
1254  //define all relevant roles that rights are needed to be changed
1255  $arr_parentRoles = $rbacreview->getParentRoleIds($this->getRefId());
1256 
1257  $real_local_roles = $rbacreview->getRolesOfRoleFolder($rolf_data['ref_id'],false);
1258  $arr_relevantParentRoleIds = array_diff(array_keys($arr_parentRoles),$real_local_roles);
1259 
1260  //group status open (aka public) or group status closed
1261  if ($a_grpStatus == GRP_TYPE_PUBLIC || $a_grpStatus == GRP_TYPE_CLOSED)
1262  {
1263  if ($a_grpStatus == GRP_TYPE_PUBLIC)
1264  {
1265  $template_id = $this->getGrpStatusOpenTemplateId();
1266  }
1267  else
1268  {
1269  $template_id = $this->getGrpStatusClosedTemplateId();
1270  }
1271  //get defined operations from template
1272  $template_ops = $rbacreview->getOperationsOfRole($template_id, 'grp', ROLE_FOLDER_ID);
1273 
1274  foreach ($arr_relevantParentRoleIds as $parentRole)
1275  {
1276  if ($rbacreview->isProtected($arr_parentRoles[$parentRole]['parent'],$parentRole))
1277  {
1278  continue;
1279  }
1280 
1281  $granted_permissions = array();
1282 
1283  // Delete the linked role for the parent role
1284  // (just in case if it already exists).
1285 
1286  // Added additional check, since this operation is very dangerous.
1287  // If there is no role folder ALL parent roles are deleted.
1288  if(isset($rolf_data['child']) and $rolf_data['child'])
1289  {
1290  $rbacadmin->deleteLocalRole($parentRole,$rolf_data["child"]);
1291  }
1292 
1293  // Grant permissions on the group object for
1294  // the parent role. In the foreach loop we
1295  // compute the intersection of the role
1296  // template il_grp_status_open/_closed and the
1297  // permission template of the parent role.
1298  $current_ops = $rbacreview->getRoleOperationsOnObject($parentRole, $this->getRefId());
1299  $rbacadmin->revokePermission($this->getRefId(), $parentRole);
1300  foreach ($template_ops as $template_op)
1301  {
1302  if (in_array($template_op,$current_ops))
1303  {
1304  array_push($granted_permissions,$template_op);
1305  }
1306  }
1307  if (!empty($granted_permissions))
1308  {
1309  $rbacadmin->grantPermission($parentRole, $granted_permissions, $this->getRefId());
1310  }
1311 
1312  // Create a linked role for the parent role and
1313  // initialize it with the intersection of
1314  // il_grp_status_open/_closed and the permission
1315  // template of the parent role
1316  $rbacadmin->copyRolePermissionIntersection(
1317  $template_id, ROLE_FOLDER_ID,
1318  $parentRole, $arr_parentRoles[$parentRole]['parent'],
1319  $rolf_data["child"], $parentRole
1320  );
1321  $rbacadmin->assignRoleToFolder($parentRole,$rolf_data["child"],"false");
1322  }//END foreach
1323  }
1324  }
1325 
1333  public function setGroupStatus($a_status)
1334  {
1335  $this->group_status = $a_status;
1336  }
1337 
1345  public function getGroupStatus()
1346  {
1347  return $this->group_status;
1348  }
1349 
1355  function readGroupStatus()
1356  {
1357  global $rbacsystem,$rbacreview;
1358 
1359  $role_folder = $rbacreview->getRoleFolderOfObject($this->getRefId());
1360  $local_roles = $rbacreview->getRolesOfRoleFolder($role_folder["ref_id"]);
1361 
1362  //get Rolefolder of group
1363  $rolf_data = $rbacreview->getRoleFolderOfObject($this->getRefId());
1364  //get all relevant roles
1365  $arr_globalRoles = array_diff($local_roles, $this->getDefaultGroupRoles());
1366 
1367  //if one global role has no permission to join the group is officially closed !
1368  foreach ($arr_globalRoles as $globalRole)
1369  {
1370  $ops_of_role = $rbacreview->getOperationsOfRole($globalRole,"grp", ROLE_FOLDER_ID);
1371 
1372  if ($rbacsystem->checkPermission($this->getRefId(), $globalRole ,"join"))
1373  {
1374  return $this->group_status = GRP_TYPE_PUBLIC;
1375  }
1376  }
1377 
1378  return $this->group_status = GRP_TYPE_CLOSED;
1379  }
1380 
1387  function getMemberRoles($a_user_id)
1388  {
1389  global $rbacadmin, $rbacreview,$ilBench;
1390 
1391  $ilBench->start("Group", "getMemberRoles");
1392 
1393  $arr_assignedRoles = array();
1394 
1395  $arr_assignedRoles = array_intersect($rbacreview->assignedRoles($a_user_id),$this->getLocalGroupRoles());
1396 
1397  $ilBench->stop("Group", "getMemberRoles");
1398 
1399  return $arr_assignedRoles;
1400  }
1401 
1408  function isAdmin($a_userId)
1409  {
1410  global $rbacreview;
1411 
1412  $grp_Roles = $this->getDefaultGroupRoles();
1413 
1414  if (in_array($a_userId,$rbacreview->assignedUsers($grp_Roles["grp_admin_role"])))
1415  {
1416  return true;
1417  }
1418  else
1419  {
1420  return false;
1421  }
1422  }
1423 
1424 
1425 
1431  function initDefaultRoles()
1432  {
1433  global $rbacadmin, $rbacreview;
1434 
1435  // create a local role folder
1436  $rfoldObj =& $this->createRoleFolder();
1437 
1438  // ADMIN ROLE
1439  // create role and assign role to rolefolder...
1440  $roleObj = $rfoldObj->createRole("il_grp_admin_".$this->getRefId(),"Groupadmin of group obj_no.".$this->getId());
1441  $this->m_roleAdminId = $roleObj->getId();
1442 
1443  //set permission template of new local role
1444  $q = "SELECT obj_id FROM object_data WHERE type='rolt' AND title='il_grp_admin'";
1445  $r = $this->ilias->db->getRow($q, DB_FETCHMODE_OBJECT);
1446  $rbacadmin->copyRoleTemplatePermissions($r->obj_id,ROLE_FOLDER_ID,$rfoldObj->getRefId(),$roleObj->getId());
1447 
1448  // set object permissions of group object
1449  $ops = $rbacreview->getOperationsOfRole($roleObj->getId(),"grp",$rfoldObj->getRefId());
1450  $rbacadmin->grantPermission($roleObj->getId(),$ops,$this->getRefId());
1451 
1452  // set object permissions of role folder object
1453  //$ops = $rbacreview->getOperationsOfRole($roleObj->getId(),"rolf",$rfoldObj->getRefId());
1454  //$rbacadmin->grantPermission($roleObj->getId(),$ops,$rfoldObj->getRefId());
1455 
1456  // MEMBER ROLE
1457  // create role and assign role to rolefolder...
1458  $roleObj = $rfoldObj->createRole("il_grp_member_".$this->getRefId(),"Groupmember of group obj_no.".$this->getId());
1459  $this->m_roleMemberId = $roleObj->getId();
1460 
1461  //set permission template of new local role
1462  $q = "SELECT obj_id FROM object_data WHERE type='rolt' AND title='il_grp_member'";
1463  $r = $this->ilias->db->getRow($q, DB_FETCHMODE_OBJECT);
1464  $rbacadmin->copyRoleTemplatePermissions($r->obj_id,ROLE_FOLDER_ID,$rfoldObj->getRefId(),$roleObj->getId());
1465 
1466  // set object permissions of group object
1467  $ops = $rbacreview->getOperationsOfRole($roleObj->getId(),"grp",$rfoldObj->getRefId());
1468  $rbacadmin->grantPermission($roleObj->getId(),$ops,$this->getRefId());
1469 
1470  // set object permissions of role folder object
1471  //$ops = $rbacreview->getOperationsOfRole($roleObj->getId(),"rolf",$rfoldObj->getRefId());
1472  //$rbacadmin->grantPermission($roleObj->getId(),$ops,$rfoldObj->getRefId());
1473 
1474  unset($rfoldObj);
1475  unset($roleObj);
1476 
1477  $roles[] = $this->m_roleAdminId;
1478  $roles[] = $this->m_roleMemberId;
1479 
1480  return $roles ? $roles : array();
1481  }
1482 
1493  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
1494  {
1495  global $tree;
1496 
1497  $parent_id = (int) $tree->getParentId($a_node_id);
1498 
1499  if ($parent_id != 0)
1500  {
1501  $obj_data =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
1502  $obj_data->notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$parent_id,$a_params);
1503  }
1504 
1505  return true;
1506  }
1507 
1508 
1509  function exportXML()
1510  {
1511  include_once 'Modules/Group/classes/class.ilGroupXMLWriter.php';
1512 
1513  $xml_writer = new ilGroupXMLWriter($this);
1514  $xml_writer->start();
1515 
1516  $xml = $xml_writer->getXML();
1517 
1518  $name = time().'__'.$this->ilias->getSetting('inst_id').'__grp_'.$this->getId();
1519 
1520  $this->__initFileObject();
1521 
1522  $this->file_obj->addGroupDirectory();
1523  $this->file_obj->addDirectory($name);
1524  $this->file_obj->writeToFile($xml,$name.'/'.$name.'.xml');
1525  $this->file_obj->zipFile($name,$name.'.zip');
1526  $this->file_obj->deleteDirectory($name);
1527 
1528  return true;
1529  }
1530 
1531  function deleteExportFiles($a_files)
1532  {
1533  $this->__initFileObject();
1534 
1535  foreach($a_files as $file)
1536  {
1537  $this->file_obj->deleteFile($file);
1538  }
1539  return true;
1540  }
1541 
1543  {
1544  $this->__initFileObject();
1545 
1546  if($abs_name = $this->file_obj->getExportFile($file))
1547  {
1548  ilUtil::deliverFile($abs_name,$file);
1549  // Not reached
1550  }
1551  return false;
1552  }
1553 
1562  function _importFromXMLString($xml,$parent_id)
1563  {
1564  include_once 'Modules/Group/classes/class.ilGroupXMLParser.php';
1565 
1566  $import_parser = new ilGroupXMLParser($xml,$parent_id);
1567 
1568  return $import_parser->startParsing();
1569  }
1570 
1578  function _importFromFile($file,$parent_id)
1579  {
1580  global $lng;
1581 
1582  include_once 'classes/class.ilFileDataGroup.php';
1583 
1584  $file_obj = new ilFileDataGroup(null);
1585  $file_obj->addImportDirectory();
1586  $file_obj->createImportFile($_FILES["xmldoc"]["tmp_name"],$_FILES['xmldoc']['name']);
1587  $file_obj->unpackImportFile();
1588 
1589  if(!$file_obj->validateImportFile())
1590  {
1591  return false;
1592  }
1593  return ilObjGroup::_importFromXMLString(file_get_contents($file_obj->getImportFile()),$parent_id);
1594  }
1595 
1604  function _getLinkToObject($a_id)
1605  {
1606  return array("repository.php?ref_id=".$a_id."&set_mode=flat&cmdClass=ilobjgroupgui","");
1607  }
1608 
1609  function _lookupIdByTitle($a_title)
1610  {
1611  global $ilDB;
1612 
1613  $query = "SELECT * FROM object_data WHERE title = ".
1614  $ilDB->quote($a_title ,'text')." AND type = 'grp'";
1615  $res = $ilDB->query($query);
1616  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1617  {
1618  return $row->obj_id;
1619  }
1620  return 0;
1621  }
1622 
1623 
1624  function _isMember($a_user_id,$a_ref_id,$a_field = '')
1625  {
1626  global $rbacreview,$ilObjDataCache,$ilDB;
1627 
1628  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
1629  $local_roles = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"],false);
1630  $user_roles = $rbacreview->assignedRoles($a_user_id);
1631 
1632  // Used for membership limitations -> check membership by given field
1633  if($a_field)
1634  {
1635  include_once './Services/User/classes/class.ilObjUser.php';
1636 
1637  $tmp_user =& ilObjectFactory::getInstanceByObjId($a_user_id);
1638  switch($a_field)
1639  {
1640  case 'login':
1641  $and = "AND login = '".$tmp_user->getLogin()."' ";
1642  break;
1643  case 'email':
1644  $and = "AND email = '".$tmp_user->getEmail()."' ";
1645  break;
1646  case 'matriculation':
1647  $and = "AND matriculation = '".$tmp_user->getMatriculation()."' ";
1648  break;
1649 
1650  default:
1651  $and = "AND usr_id = '".$a_user_id."'";
1652  break;
1653  }
1654  if(!$members = ilObjGroup::_getMembers($ilObjDataCache->lookupObjId($a_ref_id)))
1655  {
1656  return false;
1657  }
1658  $query = "SELECT * FROM usr_data as ud ".
1659  "WHERE usr_id IN (".implode(",",ilUtil::quoteArray($members)).") ".
1660  $and;
1661  $res = $ilDB->query($query);
1662 
1663  return $res->numRows() ? true : false;
1664  }
1665 
1666  if (!array_intersect($local_roles,$user_roles))
1667  {
1668  return false;
1669  }
1670 
1671  return true;
1672  }
1673 
1674  function _getMembers($a_obj_id)
1675  {
1676  global $rbacreview;
1677 
1678  // get reference
1679  $ref_ids = ilObject::_getAllReferences($a_obj_id);
1680  $ref_id = current($ref_ids);
1681 
1682  $rolf = $rbacreview->getRoleFolderOfObject($ref_id);
1683  $local_roles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
1684 
1685  $users = array();
1686  foreach($local_roles as $role_id)
1687  {
1688  $users = array_merge($users,$rbacreview->assignedUsers($role_id));
1689  }
1690 
1691  return array_unique($users);
1692  }
1693 
1700  public function getViewMode()
1701  {
1702  global $tree;
1703 
1704  // default: by type
1706 
1707  if ($course_ref_id = $tree->checkForParentType($this->ref_id,'crs'))
1708  {
1709  include_once("./Modules/Course/classes/class.ilObjCourse.php");
1710  $view_mode = ilObjCourse::_lookupViewMode(
1711  ilObject::_lookupObjId($course_ref_id));
1712  if ($view_mode == ilContainer::VIEW_SESSIONS ||
1713  $view_mode == ilContainer::VIEW_BY_TYPE ||
1714  $view_mode == ilContainer::VIEW_SIMPLE)
1715  {
1716  $view = $view_mode;
1717  }
1718  }
1719  return $view;
1720  }
1721 
1726  function addAdditionalSubItemInformation(&$a_item_data)
1727  {
1728  global $tree;
1729 
1730  static $items = null;
1731 
1732  if(!is_object($items[$this->getRefId()]))
1733  {
1734  if ($course_ref_id = $tree->checkForParentType($this->getRefId(),'crs'))
1735  {
1736  include_once("./Modules/Course/classes/class.ilObjCourse.php");
1737  include_once("./Modules/Course/classes/class.ilCourseItems.php");
1738  $course_obj = new ilObjCourse($course_ref_id);
1739  $items[$this->getRefId()] = new ilCourseItems($course_obj, $this->getRefId());
1740  }
1741  }
1742  if(is_object($items[$this->getRefId()]))
1743  {
1744  $items[$this->getRefId()]->addAdditionalSubItemInformation($a_item_data);
1745  }
1746  }
1747 
1748 
1749  // Private / Protected
1750  function __initFileObject()
1751  {
1752  if($this->file_obj)
1753  {
1754  return $this->file_obj;
1755  }
1756  else
1757  {
1758  include_once 'classes/class.ilFileDataGroup.php';
1759 
1760  return $this->file_obj = new ilFileDataGroup($this);
1761  }
1762  }
1763 
1764  function getMessage()
1765  {
1766  return $this->message;
1767  }
1768  function setMessage($a_message)
1769  {
1770  $this->message = $a_message;
1771  }
1772  function appendMessage($a_message)
1773  {
1774  if($this->getMessage())
1775  {
1776  $this->message .= "<br /> ";
1777  }
1778  $this->message .= $a_message;
1779  }
1780 
1788  protected function prepareAppointments($a_mode = 'create')
1789  {
1790  include_once('./Services/Calendar/classes/class.ilCalendarAppointmentTemplate.php');
1791 
1792  switch($a_mode)
1793  {
1794  case 'create':
1795  case 'update':
1796  if($this->isRegistrationUnlimited())
1797  {
1798  return array();
1799  }
1800  $app = new ilCalendarAppointmentTemplate(self::CAL_REG_START);
1801  $app->setTitle($this->getTitle());
1802  $app->setSubtitle('grp_cal_reg_start');
1803  $app->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
1804  $app->setDescription($this->getLongDescription());
1805  $app->setStart($this->getRegistrationStart());
1806  $apps[] = $app;
1807 
1808  $app = new ilCalendarAppointmentTemplate(self::CAL_REG_END);
1809  $app->setTitle($this->getTitle());
1810  $app->setSubtitle('grp_cal_reg_end');
1811  $app->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
1812  $app->setDescription($this->getLongDescription());
1813  $app->setStart($this->getRegistrationEnd());
1814  $apps[] = $app;
1815 
1816  return $apps;
1817 
1818  case 'delete':
1819  // Nothing to do: The category and all assigned appointments will be deleted.
1820  return array();
1821  }
1822  }
1823 
1831  protected function initParticipants()
1832  {
1833  include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
1834  $this->members_obj = ilGroupParticipants::_getInstanceByObjId($this->getId());
1835  }
1836 
1837 } //END class.ilObjGroup
1838 ?>