1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2008 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* @author Stefan Meyer <meyer@leifos.com>
26* @version $Id$
27*
28*
29* @ilCtrl_Calls
30* @ingroup ServicesTracking
31*/
32
33include_once 'Services/Tracking/classes/class.ilLPStatus.php';
34
35class ilLPStatusManualByTutor extends ilLPStatus
36{
37    /**
38     * Constructor
39     *
40     * @access public
41     * @param int object id
42     *
43     */
44    public function __construct($a_obj_id)
45    {
46        global $DIC;
47
48        $ilDB = $DIC['ilDB'];
49
50        parent::__construct($a_obj_id);
51        $this->db = $ilDB;
52    }
53
54    /**
55     * get not attempted
56     *
57     * @access public
58     * @param int object id
59     * @return array int Array of user ids
60     *
61     */
62    public static function _getNotAttempted($a_obj_id)
63    {
64        $users = array();
65
66        $members = self::getMembers($a_obj_id);
67        if ($members) {
68            // diff in progress and completed (use stored result in LPStatusWrapper)
69            $users = array_diff($members, ilLPStatusWrapper::_getInProgress($a_obj_id));
70            $users = array_diff($users, ilLPStatusWrapper::_getCompleted($a_obj_id));
71        }
72
73        return $users;
74    }
75
76    /**
77     * get in progress
78     *
79     * @access public
80     * @param int object id
81     * @return array int Array of user ids
82     */
83    public static function _getInProgress($a_obj_id)
84    {
85        include_once './Services/Tracking/classes/class.ilChangeEvent.php';
86        $users = ilChangeEvent::lookupUsersInProgress($a_obj_id);
87
88        // Exclude all users with status completed.
89        $users = array_diff((array) $users, ilLPStatusWrapper::_getCompleted($a_obj_id));
90
91        if ($users) {
92            // Exclude all non members
93            $users = array_intersect(self::getMembers($a_obj_id), (array) $users);
94        }
95
96        return $users;
97    }
98
99    public static function _getCompleted($a_obj_id)
100    {
101        global $DIC;
102
103        $ilDB = $DIC['ilDB'];
104
105        $usr_ids = array();
106
107        $query = "SELECT DISTINCT(usr_id) user_id FROM ut_lp_marks " .
108            "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
109            "AND completed = '1' ";
110
111        $res = $ilDB->query($query);
112        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
113            $usr_ids[] = $row->user_id;
114        }
115
116        if ($usr_ids) {
117            // Exclude all non members
118            $usr_ids = array_intersect(self::getMembers($a_obj_id), (array) $usr_ids);
119        }
120
121        return $usr_ids;
122    }
123
124    /**
125     * Determine status
126     *
127     * @param	integer		object id
128     * @param	integer		user id
129     * @param	object		object (optional depends on object type)
130     * @return	integer		status
131     */
132    public function determineStatus($a_obj_id, $a_user_id, $a_obj = null)
133    {
134        global $DIC;
135
136        $ilObjDataCache = $DIC['ilObjDataCache'];
137        $ilDB = $DIC['ilDB'];
138
139        $status = self::LP_STATUS_NOT_ATTEMPTED_NUM;
140        switch ($ilObjDataCache->lookupType($a_obj_id)) {
141            case "crs":
142            case "grp":
143                // completed?
144                $set = $ilDB->query($q = "SELECT usr_id FROM ut_lp_marks " .
145                    "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
146                    "AND usr_id = " . $ilDB->quote($a_user_id, 'integer') . " " .
147                    "AND completed = '1' ");
148                if ($rec = $ilDB->fetchAssoc($set)) {
149                    $status = self::LP_STATUS_COMPLETED_NUM;
150                } else {
151                    include_once './Services/Tracking/classes/class.ilChangeEvent.php';
152                    if (ilChangeEvent::hasAccessed($a_obj_id, $a_user_id)) {
153                        $status = self::LP_STATUS_IN_PROGRESS_NUM;
154                    }
155                }
156                break;
157        }
158        return $status;
159    }
160
161    /**
162     * Get members for object
163     * @param int $a_obj_id
164     * @return array
165     */
166    protected static function getMembers($a_obj_id)
167    {
168        global $DIC;
169
170        $ilObjDataCache = $DIC['ilObjDataCache'];
171
172        switch ($ilObjDataCache->lookupType($a_obj_id)) {
173            case 'crs':
174            case 'grp':
175                include_once './Services/Membership/classes/class.ilParticipants.php';
176                return ilParticipants::getInstanceByObjId($a_obj_id)->getMembers();
177        }
178
179        return array();
180    }
181
182    /**
183     * Get completed users for object
184     *
185     * @param int $a_obj_id
186     * @param array $a_user_ids
187     * @return array
188     */
189    public static function _lookupCompletedForObject($a_obj_id, $a_user_ids = null)
190    {
191        if (!$a_user_ids) {
192            $a_user_ids = self::getMembers($a_obj_id);
193            if (!$a_user_ids) {
194                return array();
195            }
196        }
197        return self::_lookupStatusForObject($a_obj_id, self::LP_STATUS_COMPLETED_NUM, $a_user_ids);
198    }
199
200    /**
201     * Get failed users for object
202     *
203     * @param int $a_obj_id
204     * @param array $a_user_ids
205     * @return array
206     */
207    public static function _lookupFailedForObject($a_obj_id, $a_user_ids = null)
208    {
209        return array();
210    }
211
212    /**
213     * Get in progress users for object
214     *
215     * @param int $a_obj_id
216     * @param array $a_user_ids
217     * @return array
218     */
219    public static function _lookupInProgressForObject($a_obj_id, $a_user_ids = null)
220    {
221        if (!$a_user_ids) {
222            $a_user_ids = self::getMembers($a_obj_id);
223            if (!$a_user_ids) {
224                return array();
225            }
226        }
227        return self::_lookupStatusForObject($a_obj_id, self::LP_STATUS_IN_PROGRESS_NUM, $a_user_ids);
228    }
229}
230