1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Self evaluation application class
6 *
7 * @author Alex Killing <alex.killing@gmx.de>
8 * @version $Id$
9 *
10 * @ingroup ServicesSkill
11 */
12class ilSkillSelfEvaluation
13{
14    /**
15     * @var ilDB
16     */
17    protected $db;
18
19    /**
20     * Constructor
21     *
22     * @param
23     */
24    public function __construct($a_id = 0)
25    {
26        global $DIC;
27
28        $this->db = $DIC->database();
29        if ($a_id > 0) {
30            $this->setId($a_id);
31            $this->read();
32        }
33    }
34
35    /**
36     * Set id
37     *
38     * @param	integer	id
39     */
40    public function setId($a_val)
41    {
42        $this->id = $a_val;
43    }
44
45    /**
46     * Get id
47     *
48     * @return	integer	id
49     */
50    public function getId()
51    {
52        return $this->id;
53    }
54
55    /**
56     * Set user id
57     *
58     * @param	integer	user id
59     */
60    public function setUserId($a_val)
61    {
62        $this->user_id = $a_val;
63    }
64
65    /**
66     * Get user id
67     *
68     * @return	integer	user id
69     */
70    public function getUserId()
71    {
72        return $this->user_id;
73    }
74
75    /**
76     * Set top skill id
77     *
78     * @param	integer	top skill id
79     */
80    public function setTopSkillId($a_val)
81    {
82        $this->top_skill_id = $a_val;
83    }
84
85    /**
86     * Get top skill id
87     *
88     * @return	integer	top skill id
89     */
90    public function getTopSkillId()
91    {
92        return $this->top_skill_id;
93    }
94
95    /**
96     * Set created at
97     *
98     * @param	timestamp	created at
99     */
100    public function setCreated($a_val)
101    {
102        $this->created = $a_val;
103    }
104
105    /**
106     * Get created at
107     *
108     * @return	timestamp	created at
109     */
110    public function getCreated()
111    {
112        return $this->created;
113    }
114
115    /**
116     * Set last update
117     *
118     * @param	timestamp	last update
119     */
120    public function setLastUpdate($a_val)
121    {
122        $this->last_update = $a_val;
123    }
124
125    /**
126     * Get last update
127     *
128     * @return	timestamp	last update
129     */
130    public function getLastUpdate()
131    {
132        return $this->last_update;
133    }
134
135    /**
136     * Set level
137     *
138     * @param	array	level; index: skill id, value: level id (or 0 for no skills)
139     */
140    public function setLevels($a_val, $a_keep_existing = false)
141    {
142        if (!$a_keep_existing) {
143            $this->levels = $a_val;
144        } else {
145            if (is_array($a_val)) {
146                foreach ($a_val as $k => $v) {
147                    $this->levels[$k] = $v;
148                }
149            }
150        }
151    }
152
153    /**
154     * Get level
155     *
156     * @return	array	level
157     */
158    public function getLevels()
159    {
160        return $this->levels;
161    }
162
163    /**
164     * Read
165     *
166     * @param
167     * @return
168     */
169    public function read()
170    {
171        $ilDB = $this->db;
172
173        $set = $ilDB->query(
174            "SELECT * FROM skl_self_eval WHERE " .
175            " id = " . $ilDB->quote($this->getId(), "integer")
176        );
177        if ($rec = $ilDB->fetchAssoc($set)) {
178            $this->setUserId($rec["user_id"]);
179            $this->setTopSkillId($rec["top_skill_id"]);
180            $this->setCreated($rec["created"]);
181            $this->setLastUpdate($rec["last_update"]);
182        }
183
184        // levels
185        $set = $ilDB->query(
186            "SELECT * FROM skl_self_eval_level WHERE " .
187            " self_eval_id = " . $ilDB->quote($this->getId(), "integer")
188        );
189        $levels = array();
190        while ($rec = $ilDB->fetchAssoc($set)) {
191            $levels[$rec["skill_id"]] = $rec["level_id"];
192        }
193        $this->setLevels($levels);
194    }
195
196    /**
197     * Create self evaluation
198     */
199    public function create()
200    {
201        $ilDB = $this->db;
202
203        $this->setId($ilDB->nextId("skl_self_eval"));
204
205        $ilDB->manipulate("INSERT INTO skl_self_eval " .
206            "(id, user_id, top_skill_id, created, last_update) VALUES (" .
207            $ilDB->quote($this->getId(), "integer") . "," .
208            $ilDB->quote($this->getUserId(), "integer") . "," .
209            $ilDB->quote($this->getTopSkillId(), "integer") . "," .
210            $ilDB->now() . "," .
211            $ilDB->now() .
212            ")");
213
214        $levels = $this->getLevels();
215        if (is_array($levels)) {
216            foreach ($levels as $skill_id => $level_id) {
217                $ilDB->manipulate("INSERT INTO skl_self_eval_level " .
218                    "(self_eval_id, skill_id, level_id) VALUES (" .
219                    $ilDB->quote($this->getId(), "integer") . "," .
220                    $ilDB->quote($skill_id, "integer") . "," .
221                    $ilDB->quote($level_id, "integer") .
222                    ")");
223            }
224        }
225    }
226
227    /**
228     * Update self evaluation
229     */
230    public function update()
231    {
232        $ilDB = $this->db;
233
234        $ilDB->manipulate(
235            "UPDATE skl_self_eval SET " .
236            " user_id = " . $ilDB->quote($this->getUserId(), "integer") .
237            ", top_skill_id = " . $ilDB->quote($this->getTopSkillId(), "integer") .
238            ", last_update = " . $ilDB->now() .
239            " WHERE id = " . $ilDB->quote($this->getId(), "integer")
240        );
241
242        $ilDB->manipulate(
243            "DELETE FROM skl_self_eval_level WHERE "
244            . " self_eval_id = " . $ilDB->quote($this->getId(), "integer")
245        );
246
247        $levels = $this->getLevels();
248        if (is_array($levels)) {
249            foreach ($levels as $skill_id => $level_id) {
250                $ilDB->manipulate("INSERT INTO skl_self_eval_level " .
251                    "(self_eval_id, skill_id, level_id) VALUES (" .
252                    $ilDB->quote($this->getId(), "integer") . "," .
253                    $ilDB->quote($skill_id, "integer") . "," .
254                    $ilDB->quote($level_id, "integer") .
255                    ")");
256            }
257        }
258    }
259
260    /**
261     * Delete self evaluation
262     */
263    public function delete()
264    {
265        $ilDB = $this->db;
266
267        $ilDB->manipulate(
268            "DELETE FROM skl_self_eval WHERE "
269            . " id = " . $ilDB->quote($this->getId(), "integer")
270        );
271
272        $ilDB->manipulate(
273            "DELETE FROM skl_self_eval_level WHERE "
274            . " self_eval_id = " . $ilDB->quote($this->getId(), "integer")
275        );
276    }
277
278    /**
279     * Get all self evaluations
280     */
281    public static function getAllSelfEvaluationsOfUser($a_user, $a_one_per_top_skill = false)
282    {
283        global $DIC;
284
285        $ilDB = $DIC->database();
286
287        $set = $ilDB->query(
288            "SELECT * FROM skl_self_eval WHERE user_id = " .
289            $ilDB->quote($a_user, "integer") . " " .
290            "ORDER BY last_update DESC"
291        );
292
293        $self_evaluation = array();
294
295        $top_skills = array();
296        while ($rec = $ilDB->fetchAssoc($set)) {
297            if (!$a_one_per_top_skill || !in_array($rec["top_skill_id"], $top_skills)) {
298                $self_evaluation[] = $rec;
299                $top_skills[] = $rec["top_skill_id"];
300            }
301        }
302
303        return $self_evaluation;
304    }
305
306    /**
307     * Lookup property
308     *
309     * @param	id		level id
310     * @return	mixed	property value
311     */
312    protected static function lookupProperty($a_id, $a_prop)
313    {
314        global $DIC;
315
316        $ilDB = $DIC->database();
317
318        $set = $ilDB->query(
319            "SELECT $a_prop FROM skl_self_eval WHERE " .
320            " id = " . $ilDB->quote($a_id, "integer")
321        );
322        $rec = $ilDB->fetchAssoc($set);
323        return $rec[$a_prop];
324    }
325
326    /**
327     * Get average level of user self evaluation
328     *
329     * Note: this method does not make much sense in general, since it
330     * assumes that all basic skills have the same level
331     */
332    public static function getAverageLevel($a_se_id, $a_user_id, $a_top_skill_id)
333    {
334        global $DIC;
335
336        $lng = $DIC->language();
337
338        $lng->loadLanguageModule("skmg");
339
340        include_once("./Services/Skill/classes/class.ilSkillTree.php");
341        include_once("./Services/Skill/classes/class.ilBasicSkill.php");
342        $stree = new ilSkillTree();
343        $cnt = 0;
344        $sum = 0;
345        if ($stree->isInTree($a_top_skill_id)) {
346            $se = new ilSkillSelfEvaluation($a_se_id);
347            $levels = $se->getLevels();
348
349            $cnode = $stree->getNodeData($a_top_skill_id);
350            $childs = $stree->getSubTree($cnode);
351
352            foreach ($childs as $child) {
353                if ($child["type"] == "skll") {
354                    $sk = new ilBasicSkill($child["child"]);
355                    $ls = $sk->getLevelData();
356                    $ord = array();
357                    foreach ($ls as $k => $l) {
358                        $ord[$l["id"]] = $k + 1;
359                    }
360                    reset($ls);
361                    foreach ($ls as $ld) {
362                        if ($ld["id"] == $levels[$child["child"]]) {
363                            $sum += $ord[$ld["id"]];
364                        }
365                    }
366                    $cnt += 1;
367                }
368            }
369        }
370        if ($cnt > 0) {
371            $avg = round($sum / $cnt);
372            if ($avg > 0) {
373                return (array("skill_title" => $cnode["title"],
374                    "ord" => $avg, "avg_title" => $ls[$avg - 1]["title"]));
375            } else {
376                return (array("skill_title" => $cnode["title"],
377                    "ord" => $avg, "avg_title" => $lng->txt("skmg_no_skills")));
378            }
379        }
380        return null;
381    }
382
383    /**
384     * Determine steps
385     *
386     * @param
387     * @return
388     */
389    public static function determineSteps($a_sn_id)
390    {
391        $steps = array();
392        if ($a_sn_id > 0) {
393            include_once("./Services/Skill/classes/class.ilSkillTree.php");
394            include_once("./Services/Skill/classes/class.ilSkillSelfEvalSkillTableGUI.php");
395            $stree = new ilSkillTree();
396
397            if ($stree->isInTree($a_sn_id)) {
398                $cnode = $stree->getNodeData($a_sn_id);
399                $childs = $stree->getSubTree($cnode);
400                foreach ($childs as $child) {
401                    if ($child["type"] == "skll") {
402                        $steps[] = $child["child"];
403                    }
404                }
405            }
406        }
407        return $steps;
408    }
409}
410