1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2001 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/**
26* class ilTimingCache
27*
28* @author Stefan Meyer <meyer@leifos.com>
29* @version $Id$
30*
31*/
32class ilTimingCache
33{
34    /**
35     * @var null | ilTimingCache
36     */
37    private static $instances = array();
38
39    /**
40     * @var int
41     */
42    private $ref_id = 0;
43
44    /**
45     * @var int
46     */
47    private $obj_id = 0;
48
49    /**
50     * @var bool
51     */
52    private $timings_active = false;
53
54    /**
55     * @var array
56     */
57    private $timings = array();
58
59    /**
60     * @var array
61     */
62    private $timings_user = array();
63
64    /**
65     * @var array
66     */
67    private $collection_items = array();
68
69    /**
70     * @var array
71     */
72    private $completed_users = array();
73
74    /**
75     * ilTimingCache constructor.
76     */
77    public function __construct($ref_id)
78    {
79        $this->ref_id = $ref_id;
80        $this->obj_id = ilObject::_lookupObjId($this->ref_id);
81        $this->readObjectInformation();
82    }
83
84    /**
85     * @param $ref_id
86     * @return ilTimingCache
87     */
88    public static function getInstanceByRefId($ref_id)
89    {
90        if (!isset(self::$instances[$ref_id])) {
91            self::$instances[$ref_id] = new self($ref_id);
92        }
93        return self::$instances[$ref_id];
94    }
95
96    /**
97     * @param int $usr_id
98     * @return bool
99     */
100    public function isWarningRequired($usr_id)
101    {
102        if (in_array($usr_id, $this->completed_users)) {
103            return false;
104        }
105        foreach ($this->collection_items as $item) {
106            $item_instance = self::getInstanceByRefId($item);
107            if ($item_instance->isWarningRequired($usr_id)) {
108                return true;
109            }
110        }
111        if (!$this->timings_active) {
112            return false;
113        }
114
115        // check constraints
116        if ($this->timings['changeable'] && isset($this->timings_user[$usr_id]['end'])) {
117            $end = $this->timings_user[$usr_id]['end'];
118        } else {
119            $end = $this->timings['suggestion_end'];
120        }
121        return $end < time();
122    }
123
124    /**
125     * Read timing information for object
126     */
127    protected function readObjectInformation()
128    {
129        $this->timings = ilObjectActivation::getItem($this->ref_id);
130        $this->timings_active = false;
131        if ($this->timings['timing_type'] == ilObjectActivation::TIMINGS_PRESETTING) {
132            $this->timings_active = true;
133            $this->timings_user = ilTimingPlaned::_getPlanedTimingsByItem($this->ref_id);
134        }
135
136        $olp = ilObjectLP::getInstance($this->obj_id);
137        $collection = $olp->getCollectionInstance();
138        if ($collection instanceof ilLPCollectionOfRepositoryObjects) {
139            $this->collection_items = $collection->getItems();
140        }
141        $this->completed_users = ilLPStatus::_getCompleted($this->obj_id);
142    }
143
144
145    /**
146     * @deprecated 7
147     * @param $a_ref_id
148     * @return mixed
149     */
150    public static function &_getTimings($a_ref_id)
151    {
152        static $cache = array();
153
154        if (isset($cache[$a_ref_id])) {
155            return $cache[$a_ref_id];
156        }
157        $cache[$a_ref_id]['item'] = ilObjectActivation::getItem($a_ref_id);
158        $cache[$a_ref_id]['user'] = ilTimingPlaned::_getPlanedTimingsByItem($a_ref_id);
159
160        return $cache[$a_ref_id];
161    }
162
163    /**
164     * @deprecated 7
165     * @param $a_ref_id
166     * @param $a_usr_id
167     * @return bool
168     */
169    public static function _showWarning($a_ref_id, $a_usr_id)
170    {
171        global $DIC;
172
173        $ilObjDataCache = $DIC['ilObjDataCache'];
174
175        $obj_id = $ilObjDataCache->lookupObjId($a_ref_id);
176
177        // if completed no warning
178        include_once './Services/Tracking/classes/class.ilLPStatus.php';
179        if (ilLPStatus::_hasUserCompleted($obj_id, $a_usr_id)) {
180            return false;
181        }
182
183        // if editing time reached => show warning
184        $timings = &ilTimingCache::_getTimings($a_ref_id);
185        if ($timings['item']['timing_type'] == ilObjectActivation::TIMINGS_PRESETTING) {
186            if ($timings['item']['changeable'] and $timings['user'][$a_usr_id]['end']) {
187                $end = $timings['user'][$a_usr_id]['end'];
188            } else {
189                $end = $timings['item']['suggestion_end'];
190            }
191            if ($end < time()) {
192                return true;
193            }
194        }
195
196        include_once './Services/Object/classes/class.ilObjectLP.php';
197        $olp = ilObjectLP::getInstance($obj_id);
198        $collection = $olp->getCollectionInstance();
199        if ($collection instanceof ilLPCollectionOfRepositoryObjects) {
200            foreach ($collection->getItems() as $item) {
201                if (ilTimingCache::_showWarning($item, $a_usr_id)) {
202                    return true;
203                }
204            }
205        }
206
207        // Really ???
208        return false;
209    }
210}
211