1<?php
2
3/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("./Services/Object/classes/class.ilObjectAccess.php");
6
7/**
8* Class ilObjGroupAccess
9*
10*
11* @author Alex Killing <alex.killing@gmx.de>
12* @version $Id$
13*
14*/
15class ilObjGroupAccess extends ilObjectAccess
16{
17    protected static $using_code = false;
18    /**
19    * checks wether a user may invoke a command or not
20    * (this method is called by ilAccessHandler::checkAccess)
21    *
22    * @param	string		$a_cmd		command (not permission!)
23    * @param	string		$a_permission	permission
24    * @param	int			$a_ref_id	reference id
25    * @param	int			$a_obj_id	object id
26    * @param	int			$a_user_id	user id (if not provided, current user is taken)
27    *
28    * @return	boolean		true, if everything is ok
29    */
30    public function _checkAccess($a_cmd, $a_permission, $a_ref_id, $a_obj_id, $a_user_id = "")
31    {
32        global $DIC;
33
34        $ilUser = $DIC['ilUser'];
35        $lng = $DIC['lng'];
36        $rbacsystem = $DIC['rbacsystem'];
37        $ilAccess = $DIC['ilAccess'];
38
39        if ($a_user_id == "") {
40            $a_user_id = $ilUser->getId();
41        }
42
43        switch ($a_cmd) {
44            case "info":
45
46                include_once './Modules/Group/classes/class.ilGroupParticipants.php';
47                if (ilGroupParticipants::_isParticipant($a_ref_id, $a_user_id)) {
48                    $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_INFO, $lng->txt("info_is_member"));
49                } else {
50                    $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_INFO, $lng->txt("info_is_not_member"));
51                }
52                break;
53
54            case "join":
55
56                if (!self::_registrationEnabled($a_obj_id)) {
57                    return false;
58                }
59
60                include_once './Modules/Group/classes/class.ilGroupWaitingList.php';
61                if (ilGroupWaitingList::_isOnList($ilUser->getId(), $a_obj_id)) {
62                    return false;
63                }
64
65                include_once './Modules/Group/classes/class.ilGroupParticipants.php';
66                if (ilGroupParticipants::_isParticipant($a_ref_id, $a_user_id)) {
67                    return false;
68                }
69                break;
70
71            case 'leave':
72
73                // Regular member
74                if ($a_permission == 'leave') {
75                    include_once './Modules/Group/classes/class.ilObjGroup.php';
76                    $limit = null;
77                    if (!ilObjGroup::mayLeave($a_obj_id, $a_user_id, $limit)) {
78                        $ilAccess->addInfoItem(
79                            ilAccessInfo::IL_STATUS_INFO,
80                            sprintf($lng->txt("grp_cancellation_end_rbac_info"), ilDatePresentation::formatDate($limit))
81                        );
82                        return false;
83                    }
84
85                    include_once './Modules/Group/classes/class.ilGroupParticipants.php';
86                    if (!ilGroupParticipants::_isParticipant($a_ref_id, $a_user_id)) {
87                        return false;
88                    }
89                }
90                // Waiting list
91                if ($a_permission == 'join') {
92                    include_once './Modules/Group/classes/class.ilGroupWaitingList.php';
93                    if (!ilGroupWaitingList::_isOnList($ilUser->getId(), $a_obj_id)) {
94                        return false;
95                    }
96                }
97                break;
98
99        }
100
101        switch ($a_permission) {
102            case 'leave':
103                include_once './Modules/Group/classes/class.ilObjGroup.php';
104                return ilObjGroup::mayLeave($a_obj_id, $a_user_id);
105        }
106        return true;
107    }
108
109    /**
110     * get commands
111     *
112     * this method returns an array of all possible commands/permission combinations
113     *
114     * example:
115     * $commands = array
116     *	(
117     *		array("permission" => "read", "cmd" => "view", "lang_var" => "show"),
118     *		array("permission" => "write", "cmd" => "edit", "lang_var" => "edit"),
119     *	);
120     */
121    public static function _getCommands()
122    {
123        $commands = array();
124        $commands[] = array("permission" => "grp_linked", "cmd" => "", "lang_var" => "show", "default" => true);
125
126        include_once './Services/WebServices/FileManager/classes/class.ilFMSettings.php';
127        if (ilFMSettings::getInstance()->isEnabled()) {
128            $commands[] = array(
129                'permission' => 'read',
130                'cmd' => 'fileManagerLaunch',
131                'lang_var' => 'fm_start',
132                'enable_anonymous' => false
133            );
134        }
135
136        $commands[] = array("permission" => "join", "cmd" => "join", "lang_var" => "join");
137
138        // on waiting list
139        $commands[] = array('permission' => "join", "cmd" => "leave", "lang_var" => "leave_waiting_list");
140
141        // regualar users
142        $commands[] = array('permission' => "leave", "cmd" => "leave", "lang_var" => "grp_btn_unsubscribe");
143
144        include_once('Services/WebDAV/classes/class.ilDAVActivationChecker.php');
145        if (ilDAVActivationChecker::_isActive()) {
146            include_once './Services/WebDAV/classes/class.ilWebDAVUtil.php';
147            if (ilWebDAVUtil::getInstance()->isLocalPasswordInstructionRequired()) {
148                $commands[] = array('permission' => 'read', 'cmd' => 'showPasswordInstruction', 'lang_var' => 'mount_webfolder', 'enable_anonymous' => 'false');
149            } else {
150                $commands[] = array("permission" => "read", "cmd" => "mount_webfolder", "lang_var" => "mount_webfolder", "enable_anonymous" => "false");
151            }
152        }
153
154        $commands[] = array("permission" => "write", "cmd" => "enableAdministrationPanel", "lang_var" => "edit_content");
155        $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "settings");
156
157        return $commands;
158    }
159
160    /**
161    * check whether goto script will succeed
162    */
163    public static function _checkGoto($a_target)
164    {
165        global $DIC;
166
167        $ilAccess = $DIC['ilAccess'];
168        $ilUser = $DIC['ilUser'];
169
170        $t_arr = explode("_", $a_target);
171        // registration codes
172        if (substr($t_arr[2], 0, 5) == 'rcode' and $ilUser->getId() != ANONYMOUS_USER_ID) {
173            self::$using_code = true;
174            return true;
175        }
176
177        if ($t_arr[0] != "grp" || ((int) $t_arr[1]) <= 0) {
178            return false;
179        }
180
181        if ($ilAccess->checkAccess("read", "", $t_arr[1]) ||
182            $ilAccess->checkAccess("visible", "", $t_arr[1])) {
183            return true;
184        }
185        return false;
186    }
187
188    /**
189     *
190     * @return
191     * @param object $a_obj_id
192     */
193    public static function _registrationEnabled($a_obj_id)
194    {
195        global $DIC;
196
197        $ilDB = $DIC['ilDB'];
198
199        $query = "SELECT * FROM grp_settings " .
200            "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
201
202        $res = $ilDB->query($query);
203
204        $enabled = $unlimited = false;
205        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
206            $enabled = $row->registration_enabled;
207            $unlimited = $row->registration_unlimited;
208            $start = $row->registration_start;
209            $end = $row->registration_end;
210        }
211
212        if (!$enabled) {
213            return false;
214        }
215        if ($unlimited) {
216            return true;
217        }
218
219        if (!$unlimited) {
220            $start = new ilDateTime($start, IL_CAL_DATETIME);
221            $end = new ilDateTime($end, IL_CAL_DATETIME);
222            $time = new ilDateTime(time(), IL_CAL_UNIX);
223
224            return ilDateTime::_after($time, $start) and ilDateTime::_before($time, $end);
225        }
226        return false;
227    }
228
229
230    /**
231     * Preload data
232     *
233     * @param array $a_obj_ids array of object ids
234     */
235    public static function _preloadData($a_obj_ids, $a_ref_ids)
236    {
237        global $DIC;
238
239        $ilDB = $DIC['ilDB'];
240        $ilUser = $DIC['ilUser'];
241
242        include_once("./Modules/Group/classes/class.ilGroupWaitingList.php");
243        ilGroupWaitingList::_preloadOnListInfo($ilUser->getId(), $a_obj_ids);
244    }
245
246    /**
247     * Lookup registration info
248     * @global ilDB $ilDB
249     * @global ilObjUser $ilUser
250     * @global ilLanguage $lng
251     * @param int $a_obj_id
252     * @return array
253     */
254    public static function lookupRegistrationInfo($a_obj_id)
255    {
256        global $DIC;
257
258        $ilDB = $DIC['ilDB'];
259        $ilUser = $DIC['ilUser'];
260        $lng = $DIC['lng'];
261
262        $query = 'SELECT registration_type, registration_enabled, registration_unlimited,  registration_start, ' .
263            'registration_end, registration_mem_limit, registration_max_members FROM grp_settings ' .
264            'WHERE obj_id = ' . $ilDB->quote($a_obj_id);
265        $res = $ilDB->query($query);
266
267        $info = array();
268        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
269            $info['reg_info_start'] = new ilDateTime($row->registration_start, IL_CAL_DATETIME);
270            $info['reg_info_end'] = new ilDateTime($row->registration_end, IL_CAL_DATETIME);
271            $info['reg_info_type'] = $row->registration_type;
272            $info['reg_info_max_members'] = $row->registration_max_members;
273            $info['reg_info_mem_limit'] = $row->registration_mem_limit;
274            $info['reg_info_unlimited'] = $row->registration_unlimited;
275
276            $info['reg_info_max_members'] = 0;
277            if ($info['reg_info_mem_limit']) {
278                $info['reg_info_max_members'] = $row->registration_max_members;
279            }
280
281            $info['reg_info_enabled'] = $row->registration_enabled;
282        }
283
284        $registration_possible = $info['reg_info_enabled'];
285
286        // Limited registration (added $registration_possible, see bug 0010157)
287        if (!$info['reg_info_unlimited'] && $registration_possible) {
288            $dt = new ilDateTime(time(), IL_CAL_UNIX);
289            if (ilDateTime::_before($dt, $info['reg_info_start'])) {
290                $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_start');
291                $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_start']);
292            } elseif (ilDateTime::_before($dt, $info['reg_info_end'])) {
293                $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_end');
294                $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_end']);
295            } else {
296                $registration_possible = false;
297                $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_period');
298                $info['reg_info_list_prop']['value'] = $lng->txt('grp_list_reg_noreg');
299            }
300        } else {
301            // added !$registration_possible, see bug 0010157
302            if (!$registration_possible) {
303                $registration_possible = false;
304                $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg');
305                $info['reg_info_list_prop']['value'] = $lng->txt('grp_list_reg_noreg');
306            }
307        }
308
309        if ($info['reg_info_mem_limit'] && $info['reg_info_max_members'] && $registration_possible) {
310            // Check for free places
311            include_once './Modules/Group/classes/class.ilGroupParticipants.php';
312            $part = ilGroupParticipants::_getInstanceByObjId($a_obj_id);
313
314            include_once './Modules/Course/classes/class.ilCourseWaitingList.php';
315            $info['reg_info_list_size'] = ilCourseWaitingList::lookupListSize($a_obj_id);
316            if ($info['reg_info_list_size']) {
317                $info['reg_info_free_places'] = 0;
318            } else {
319                $info['reg_info_free_places'] = max(0, $info['reg_info_max_members'] - $part->getCountMembers());
320            }
321
322            if ($info['reg_info_free_places']) {
323                $info['reg_info_list_prop_limit']['property'] = $lng->txt('grp_list_reg_limit_places');
324                $info['reg_info_list_prop_limit']['value'] = $info['reg_info_free_places'];
325            } else {
326                $info['reg_info_list_prop_limit']['property'] = '';
327                $info['reg_info_list_prop_limit']['value'] = $lng->txt('grp_list_reg_limit_full');
328            }
329        }
330
331        return $info;
332    }
333
334    /**
335     * Lookup course period info
336     *
337     * @param int $a_obj_id
338     * @return array
339     */
340    public static function lookupPeriodInfo($a_obj_id)
341    {
342        global $DIC;
343
344        $ilDB = $DIC['ilDB'];
345        $lng = $DIC['lng'];
346
347        $start = $end = null;
348        $query = 'SELECT period_start, period_end, period_time_indication FROM grp_settings ' .
349            'WHERE obj_id = ' . $ilDB->quote($a_obj_id);
350
351        $res = $ilDB->query($query);
352        while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
353            if (!$row->period_time_indication) {
354                $start = ($row->period_start
355                    ? new \ilDate($row->period_start, IL_CAL_DATETIME)
356                    : null);
357                $end = ($row->period_end
358                    ? new \ilDate($row->period_end, IL_CAL_DATETIME)
359                    : null);
360            } else {
361                $start = ($row->period_start
362                    ? new \ilDateTime($row->period_start, IL_CAL_DATETIME, \ilTimeZone::UTC)
363                    : null);
364                $end = ($row->period_end
365                    ? new \ilDateTime($row->period_end, IL_CAL_DATETIME, \ilTimeZone::UTC)
366                    : null);
367            }
368        }
369        if ($start && $end) {
370            $lng->loadLanguageModule('grp');
371
372            return
373                [
374                    'property' => $lng->txt('grp_period'),
375                    'value' => ilDatePresentation::formatPeriod($start, $end)
376                ];
377        }
378    }
379    /**
380     * Using Registration code
381     *
382     * @return bool
383     */
384    public static function _usingRegistrationCode()
385    {
386        return self::$using_code;
387    }
388}
389