1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once 'Services/Tracking/classes/class.ilLPStatus.php';
6
7
8/**
9 * @author Stefan Meyer <meyer@leifos.com>
10 *
11 * @version $Id$
12 *
13 * @package ilias-tracking
14 *
15 */
16class ilLPStatusObjectives extends ilLPStatus
17{
18    public function __construct($a_obj_id)
19    {
20        global $DIC;
21
22        $ilDB = $DIC['ilDB'];
23
24        parent::__construct($a_obj_id);
25        $this->db = $ilDB;
26    }
27
28    public static function _getNotAttempted($a_obj_id)
29    {
30        $users = array();
31
32        $members = self::getMembers($a_obj_id);
33        if ($members) {
34            // diff in progress, completed and failed (use stored result in LPStatusWrapper)
35            $users = array_diff((array) $members, ilLPStatusWrapper::_getInProgress($a_obj_id));
36            $users = array_diff((array) $members, ilLPStatusWrapper::_getCompleted($a_obj_id));
37            $users = array_diff((array) $members, ilLPStatusWrapper::_getFailed($a_obj_id));
38        }
39
40        return $users;
41    }
42
43    public static function _getInProgress($a_obj_id)
44    {
45        $objective_results = ilLPStatusWrapper::_getStatusInfo($a_obj_id);
46        $usr_ids = (array) $objective_results['user_status'][self::LP_STATUS_IN_PROGRESS_NUM];
47
48        /* change_event is of no use when no objective has been tried yet
49        include_once './Services/Tracking/classes/class.ilChangeEvent.php';
50        $users = ilChangeEvent::lookupUsersInProgress($a_obj_id);
51
52        // Exclude all users with status completed/failed.
53        $users = array_diff((array) $users,ilLPStatusWrapper::_getCompleted($a_obj_id));
54        $users = array_diff((array) $users,ilLPStatusWrapper::_getFailed($a_obj_id));
55        */
56
57        if ($usr_ids) {
58            // Exclude all non members
59            $usr_ids = array_intersect(self::getMembers($a_obj_id), (array) $usr_ids);
60        }
61
62        return $usr_ids ? $usr_ids : array();
63    }
64
65    public static function _getCompleted($a_obj_id)
66    {
67        $objective_results = ilLPStatusWrapper::_getStatusInfo($a_obj_id);
68        $usr_ids = (array) $objective_results['user_status'][self::LP_STATUS_COMPLETED_NUM];
69
70        if ($usr_ids) {
71            // Exclude all non members
72            $usr_ids = array_intersect(self::getMembers($a_obj_id), (array) $usr_ids);
73        }
74
75        return $usr_ids ? $usr_ids : array();
76    }
77
78    public static function _getFailed($a_obj_id)
79    {
80        $objective_results = ilLPStatusWrapper::_getStatusInfo($a_obj_id);
81        $usr_ids = (array) $objective_results['user_status'][self::LP_STATUS_FAILED_NUM];
82
83        if ($usr_ids) {
84            // Exclude all non members
85            $usr_ids = array_intersect(self::getMembers($a_obj_id), (array) $usr_ids);
86        }
87
88        return $usr_ids ? $usr_ids : array();
89    }
90
91    public static function _getStatusInfo($a_obj_id)
92    {
93        global $DIC;
94
95        $ilDB = $DIC['ilDB'];
96
97        include_once 'Modules/Course/classes/class.ilCourseObjective.php';
98
99        $status_info = array();
100        $status_info['user_status'] = array();
101        $status_info['objectives'] = ilCourseObjective::_getObjectiveIds($a_obj_id, true);
102        $status_info['num_objectives'] = count($status_info['objectives']);
103
104        if ($status_info['num_objectives']) {
105            $in = $ilDB->in('objective_id', $status_info['objectives'], false, 'integer');
106
107            include_once "Modules/Course/classes/Objectives/class.ilLOUserResults.php";
108            foreach (ilLOUserResults::getSummarizedObjectiveStatusForLP($a_obj_id, $status_info['objectives']) as $user_id => $user_status) {
109                $status_info['user_status'][$user_status][] = $user_id;
110            }
111
112            // change event should lead to "in progress" - see determineStatus()
113            include_once("./Services/Tracking/classes/class.ilChangeEvent.php");
114            foreach (ilChangeEvent::lookupUsersInProgress($a_obj_id) as $user_id) {
115                if (!is_array($status_info['user_status'][ilLPStatus::LP_STATUS_IN_PROGRESS_NUM]) ||
116                    !in_array($user_id, $status_info['user_status'][ilLPStatus::LP_STATUS_IN_PROGRESS_NUM])) {
117                    $status_info['user_status'][ilLPStatus::LP_STATUS_IN_PROGRESS_NUM][] = $user_id;
118                }
119            }
120
121            // Read title/description
122            $query = "SELECT * FROM crs_objectives WHERE " . $in;
123            $res = $ilDB->query($query);
124            while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
125                $status_info['objective_title'][$row->objective_id] = $row->title;
126                $status_info['objective_description'][$row->objective_id] = $row->description;
127            }
128        }
129
130        return $status_info;
131    }
132
133    /**
134     * Determine status
135     *
136     * @param	integer		object id
137     * @param	integer		user id
138     * @param	object		object (optional depends on object type)
139     * @return	integer		status
140     */
141    public function determineStatus($a_obj_id, $a_user_id, $a_obj = null)
142    {
143        global $DIC;
144
145        $ilObjDataCache = $DIC['ilObjDataCache'];
146        $ilDB = $DIC['ilDB'];
147
148        // the status completed depends on:
149        // $status_info['num_objectives'] (ilLPStatusWrapper::_getStatusInfo($a_obj_id);)
150        // - ilCourseObjective::_getObjectiveIds($a_obj_id);
151        // - table crs_objectives manipulated in
152        // - ilCourseObjective
153
154        // $status_info['objective_result']  (ilLPStatusWrapper::_getStatusInfo($a_obj_id);)
155        // table crs_objective_status (must not contain a dataset)
156        // ilCourseObjectiveResult -> added ilLPStatusWrapper::_updateStatus()
157
158        $status = self::LP_STATUS_NOT_ATTEMPTED_NUM;
159        switch ($ilObjDataCache->lookupType($a_obj_id)) {
160            case "crs":
161                include_once("./Services/Tracking/classes/class.ilChangeEvent.php");
162                if (ilChangeEvent::hasAccessed($a_obj_id, $a_user_id)) {
163                    // an initial test (only) should also lead to "in progress"
164                    $status = self::LP_STATUS_IN_PROGRESS_NUM;
165
166                    include_once 'Modules/Course/classes/class.ilCourseObjective.php';
167                    $objectives = ilCourseObjective::_getObjectiveIds($a_obj_id, true);
168                    if ($objectives) {
169                        // #14051 - getSummarizedObjectiveStatusForLP() might return null
170                        include_once "Modules/Course/classes/Objectives/class.ilLOUserResults.php";
171                        $objtv_status = ilLOUserResults::getSummarizedObjectiveStatusForLP($a_obj_id, $objectives, $a_user_id);
172                        if ($objtv_status !== null) {
173                            $status = $objtv_status;
174                        }
175                    }
176                }
177                break;
178        }
179        return $status;
180    }
181
182    /**
183     * Get members for object
184     * @param int $a_obj_id
185     * @return array
186     */
187    protected static function getMembers($a_obj_id)
188    {
189        include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
190        $member_obj = ilCourseParticipants::_getInstanceByObjId($a_obj_id);
191        return $member_obj->getMembers();
192    }
193
194    /**
195     * Get completed users for object
196     *
197     * @param int $a_obj_id
198     * @param array $a_user_ids
199     * @return array
200     */
201    public static function _lookupCompletedForObject($a_obj_id, $a_user_ids = null)
202    {
203        if (!$a_user_ids) {
204            $a_user_ids = self::getMembers($a_obj_id);
205            if (!$a_user_ids) {
206                return array();
207            }
208        }
209        return self::_lookupStatusForObject($a_obj_id, self::LP_STATUS_COMPLETED_NUM, $a_user_ids);
210    }
211
212    /**
213     * Get failed users for object
214     *
215     * @param int $a_obj_id
216     * @param array $a_user_ids
217     * @return array
218     */
219    public static function _lookupFailedForObject($a_obj_id, $a_user_ids = null)
220    {
221        return array();
222    }
223
224    /**
225     * Get in progress users for object
226     *
227     * @param int $a_obj_id
228     * @param array $a_user_ids
229     * @return array
230     */
231    public static function _lookupInProgressForObject($a_obj_id, $a_user_ids = null)
232    {
233        if (!$a_user_ids) {
234            $a_user_ids = self::getMembers($a_obj_id);
235            if (!$a_user_ids) {
236                return array();
237            }
238        }
239        return self::_lookupStatusForObject($a_obj_id, self::LP_STATUS_IN_PROGRESS_NUM, $a_user_ids);
240    }
241}
242