1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Self evaluation
6 *
7 * @author Alex Killing <alex.kiling@gmx.de>
8 * @version $Id$
9 * @ilCtrl_Calls ilSkillSelfEvaluationGUI:
10 * @ingroup ServicesSkill
11 */
12class ilSkillSelfEvaluationGUI
13{
14    /**
15     * @var ilCtrl
16     */
17    protected $ctrl;
18
19    /**
20     * @var ilLanguage
21     */
22    protected $lng;
23
24    /**
25     * @var ilTemplate
26     */
27    protected $tpl;
28
29    /**
30     * @var ilToolbarGUI
31     */
32    protected $toolbar;
33
34    /**
35     * @var ilObjUser
36     */
37    protected $user;
38
39    /**
40     * Constructor
41     *
42     * @param
43     * @return
44     */
45    public function __construct()
46    {
47        global $DIC;
48
49        $this->ctrl = $DIC->ctrl();
50        $this->lng = $DIC->language();
51        $this->tpl = $DIC["tpl"];
52        $this->toolbar = $DIC->toolbar();
53        $this->user = $DIC->user();
54        $ilCtrl = $DIC->ctrl();
55        $lng = $DIC->language();
56
57        $ilCtrl->saveParameter($this, array("se_id", "sn_id"));
58        $lng->loadLanguageModule("skmg");
59
60        $this->readSelfEvaluation();
61
62        $this->se_id = (int) $_GET["se_id"];
63        $this->sn_id = ((int) $_POST["sn_id"] > 0)
64            ? (int) $_POST["sn_id"]
65            : (int) $_GET["sn_id"];
66        $ilCtrl->setParameter($this, "sn_id", $this->sn_id);
67    }
68
69    /**
70     * Execute command
71     */
72    public function executeCommand()
73    {
74        $ilCtrl = $this->ctrl;
75
76        $cmd = $ilCtrl->getCmd("listSelfEvaluations");
77        $this->$cmd();
78    }
79
80    /**
81     * Read self_evaluation
82     *
83     * @param
84     * @return
85     */
86    public function readSelfEvaluation()
87    {
88        //		$this->self_evaluation = new ilSelfEvaluation((int) $_GET[save_param]);
89    }
90
91    /**
92     * List all self evaluations
93     */
94    public function listSelfEvaluations()
95    {
96        $tpl = $this->tpl;
97        $ilToolbar = $this->toolbar;
98        $ilCtrl = $this->ctrl;
99        $lng = $this->lng;
100
101        $ilToolbar->setFormAction($ilCtrl->getFormAction($this));
102
103        // desc
104        /*$ne = new ilNonEditableValueGUI($lng->txt("lang"), var);
105        $ne->setValue();
106        $ne->setInfo();
107        $this->form->addItem($ne);*/
108
109        // select skill for self evaluation
110        include_once("./Services/Skill/classes/class.ilSkillTreeNode.php");
111        $se_nodes = ilSkillTreeNode::getAllSelfEvaluationNodes();
112        foreach ($se_nodes as $n_id => $title) {
113            $options[$n_id] = $title;
114        }
115        include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
116        $si = new ilSelectInputGUI($lng->txt("skmg_please_select_self_skill"), "sn_id");
117        $si->setOptions($options);
118
119        //$si->setInfo($lng->txt(""));
120        $ilToolbar->addInputItem($si, true);
121
122        $ilToolbar->addFormButton($lng->txt("skmg_execute_self_evaluation"), "startSelfEvaluation");
123
124        include_once("./Services/Skill/classes/class.ilSelfEvaluationTableGUI.php");
125        $table = new ilSelfEvaluationTableGUI($this, "listSelfEvaluations");
126
127        $tpl->setContent($table->getHTML());
128    }
129
130
131    /**
132     * Confirm self_evaluation deletion
133     */
134    public function confirmSelfEvaluationDeletion()
135    {
136        $ilCtrl = $this->ctrl;
137        $tpl = $this->tpl;
138        $lng = $this->lng;
139
140        if (!is_array($_POST["id"]) || count($_POST["id"]) == 0) {
141            ilUtil::sendInfo($lng->txt("no_checkbox"), true);
142            $ilCtrl->redirect($this, "listSelfEvaluations");
143        } else {
144            include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
145            include_once("./Services/Skill/classes/class.ilSkillTreeNode.php");
146            include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
147            $cgui = new ilConfirmationGUI();
148            $cgui->setFormAction($ilCtrl->getFormAction($this));
149            $cgui->setHeaderText($lng->txt("skmg_sure_delete_self_evaluation"));
150            $cgui->setCancel($lng->txt("cancel"), "listSelfEvaluations");
151            $cgui->setConfirm($lng->txt("delete"), "deleteSelfEvaluation");
152
153            foreach ($_POST["id"] as $i) {
154                $se = new ilSkillSelfEvaluation((int) $i);
155                $se_title =
156                    ilSkillTreeNode::_lookupTitle($se->getTopSkillId());
157                $cgui->addItem("id[]", $i, $se_title . ", " . $lng->txt("created") . ": " .
158                    $se->getCreated() . ", " . $lng->txt("last_update") . ": " . $se->getLastUpdate());
159            }
160
161            $tpl->setContent($cgui->getHTML());
162        }
163    }
164
165    /**
166     * Delete self_evaluation
167     *
168     * @param
169     * @return
170     */
171    public function deleteSelfEvaluation()
172    {
173        $ilCtrl = $this->ctrl;
174        $lng = $this->lng;
175        $ilUser = $this->user;
176
177        include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
178        if (is_array($_POST["id"])) {
179            foreach ($_POST["id"] as $i) {
180                $se = new ilSkillSelfEvaluation((int) $i);
181                if ($se->getUserId() == $ilUser->getId()) {
182                    $se->delete();
183                }
184            }
185        }
186        ilUtil::sendSuccess("msg_obj_modified");
187        $ilCtrl->redirect($this, "listSelfEvaluations");
188    }
189
190
191    /**
192     * startSelfEvaluation
193     *
194     * @param
195     * @return
196     */
197    public function startSelfEvaluation($a_mode = "create")
198    {
199        $tpl = $this->tpl;
200        $ilCtrl = $this->ctrl;
201        $lng = $this->lng;
202
203        $se = null;
204        if ($a_mode == "edit") {
205            include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
206            $se = new ilSkillSelfEvaluation((int) $_GET["se_id"]);
207            $this->sn_id = $se->getTopSkillId();
208        }
209        ilUtil::sendInfo($lng->txt("skmg_please_select_your_skill_levels"));
210
211        $se_tpl = new ilTemplate("tpl.self_evaluation.html", true, true, "Services/Skill");
212        include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
213
214        $steps = ilSkillSelfEvaluation::determineSteps($this->sn_id);
215        $cstep = (int) $_GET["step"];
216        $ilCtrl->setParameter($this, "step", $cstep);
217        include_once("./Services/Skill/classes/class.ilSkillSelfEvalSkillTableGUI.php");
218        $table = new ilSkillSelfEvalSkillTableGUI(
219            $this,
220            "startSelfEvaluation",
221            $steps[$cstep],
222            $se
223        );
224
225        $se_tpl->setCurrentBlock("se_table");
226        $se_tpl->setVariable("SE_TABLE", $table->getHTML());
227        $se_tpl->parseCurrentBlock();
228
229        include_once("./Services/UIComponent/Toolbar/classes/class.ilToolbarGUI.php");
230        $tb = new ilToolbarGUI();
231        if ($a_mode == "edit") {
232            if ($cstep > 0) {
233                $tb->addFormButton("< " . $lng->txt("skmg_previous_step"), "updateBackSelfEvaluation");
234            }
235            if ($cstep < count($steps) - 1) {
236                $tb->addFormButton($lng->txt("skmg_next_step") . " >", "updateSelfEvaluation");
237            } elseif ($cstep == count($steps) - 1) {
238                $tb->addFormButton($lng->txt("skmg_save_self_evaluation"), "updateSelfEvaluation");
239            }
240        } else {
241            if ($cstep < count($steps) - 1) {
242                $tb->addFormButton($lng->txt("skmg_next_step") . " >", "saveSelfEvaluation");
243            } elseif ($cstep == count($steps) - 1) {
244                $tb->addFormButton($lng->txt("skmg_save_self_evaluation"), "saveSelfEvaluation");
245            }
246        }
247        $se_tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this));
248        $se_tpl->setVariable("TOOLBAR", $tb->getHTML());
249        $tpl->setContent($se_tpl->get());
250    }
251
252    /**
253     * Save self evaluation
254     *
255     * @param
256     * @return
257     */
258    public function saveSelfEvaluation()
259    {
260        $ilUser = $this->user;
261        $lng = $this->lng;
262        $ilCtrl = $this->ctrl;
263
264        include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
265
266        $se = new ilSkillSelfEvaluation();
267        $se->setUserId($ilUser->getId());
268        $se->setTopSkillId($_GET["sn_id"]);
269        if (is_array($_POST["se_sk"])) {
270            $se->setLevels($_POST["se_sk"]);
271        }
272        $se->create();
273
274        $steps = ilSkillSelfEvaluation::determineSteps($this->sn_id);
275        $cstep = (int) $_GET["step"];
276
277        if (count($steps)) {
278            $ilCtrl->setParameter($this, "step", 1);
279            $ilCtrl->setParameter($this, "se_id", $se->getId());
280            $ilCtrl->redirect($this, "editSelfEvaluation");
281        }
282
283        ilUtil::sendInfo($lng->txt("msg_obj_modified"), true);
284        $ilCtrl->redirect($this, "");
285    }
286
287    /**
288     * Edit self evaluation
289     *
290     * @param
291     * @return
292     */
293    public function editSelfEvaluation()
294    {
295        $this->startSelfEvaluation("edit");
296    }
297
298    /**
299     * Update self evaluation and go one step back
300     *
301     * @param
302     * @return
303     */
304    public function updateBackSelfEvaluation()
305    {
306        $this->updateSelfEvaluation(true);
307    }
308
309
310
311    /**
312     * Update self evaluation
313     *
314     * @param
315     * @return
316     */
317    public function updateSelfEvaluation($a_back = false)
318    {
319        $ilUser = $this->user;
320        $lng = $this->lng;
321        $ilCtrl = $this->ctrl;
322
323        include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
324        $se = new ilSkillSelfEvaluation((int) $_GET["se_id"]);
325
326        if ($se->getUserId() == $ilUser->getId()) {
327            $steps = ilSkillSelfEvaluation::determineSteps($this->sn_id);
328            $cstep = (int) $_GET["step"];
329
330            if (is_array($_POST["se_sk"])) {
331                $se->setLevels($_POST["se_sk"], true);
332            }
333            $se->update();
334
335            if ($a_back) {
336                $ilCtrl->setParameter($this, "step", (int) $_GET["step"] - 1);
337                $ilCtrl->setParameter($this, "se_id", $se->getId());
338                $ilCtrl->redirect($this, "editSelfEvaluation");
339            } elseif (count($steps) - 1 > $cstep) {
340                $ilCtrl->setParameter($this, "step", (int) $_GET["step"] + 1);
341                $ilCtrl->setParameter($this, "se_id", $se->getId());
342                $ilCtrl->redirect($this, "editSelfEvaluation");
343            }
344
345            ilUtil::sendInfo($lng->txt("msg_obj_modified"), true);
346        }
347
348        $ilCtrl->redirect($this, "");
349    }
350
351    ////
352    //// Presentation view
353    ////
354
355    /**
356     * Get presentation view
357     *
358     * @param
359     * @return
360     */
361    public function getPresentationView($a_user_id)
362    {
363        include_once("./Services/Skill/classes/class.ilSkillSelfEvaluation.php");
364        $ses = ilSkillSelfEvaluation::getAllSelfEvaluationsOfUser($a_user_id);
365
366        $html = "";
367        foreach ($ses as $se) {
368            $this->setSelfEvaluationPresentationForm($se);
369            $html .= $this->form->getHTML() . "<br /><br />";
370        }
371
372        return $html;
373    }
374
375    /**
376     * Set self evaluation presentation form
377     */
378    public function setSelfEvaluationPresentationForm($se)
379    {
380        $lng = $this->lng;
381        $ilCtrl = $this->ctrl;
382
383        include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
384        $this->form = new ilPropertyFormGUI();
385
386        ilDatePresentation::setUseRelativeDates(false);
387        $dates = ", " .
388            $lng->txt("created") . ": " .
389            ilDatePresentation::formatDate(
390                new ilDateTime($se["created"], IL_CAL_DATETIME)
391            );
392        if ($se["created"] != $se["last_update"]) {
393            $dates .= ", " . $lng->txt("last_update") . ": " .
394            ilDatePresentation::formatDate(
395                new ilDateTime($se["last_update"], IL_CAL_DATETIME)
396            );
397        }
398        ilDatePresentation::setUseRelativeDates(true);
399
400        $se = new ilSkillSelfEvaluation($se["id"]);
401        $levels = $se->getLevels();
402
403        $this->form->setTitle($lng->txt("skmg_self_evaluation") . $dates);
404
405        include_once("./Services/Skill/classes/class.ilBasicSkill.php");
406        include_once("./Services/Skill/classes/class.ilSkillTree.php");
407        $stree = new ilSkillTree();
408
409        if ($stree->isInTree($se->getTopSkillId())) {
410            $cnode = $stree->getNodeData($se->getTopSkillId());
411            $childs = $stree->getSubTree($cnode);
412            foreach ($childs as $child) {
413                if ($child["type"] == "skll") {
414                    // build title
415                    $path = $stree->getPathFull($child["child"]);
416                    $title = $sep = "";
417                    foreach ($path as $p) {
418                        if ($p["type"] != "skrt") {
419                            $title .= $sep . $p["title"];
420                            $sep = " > ";
421                        }
422                    }
423
424                    $sk = new ilBasicSkill($child["child"]);
425                    $ls = $sk->getLevelData();
426
427                    $ne = new ilNonEditableValueGUI($title, "");
428                    foreach ($ls as $ld) {
429                        if ($ld["id"] == $levels[$child["child"]]) {
430                            $ne->setValue($ld["title"]);
431                        }
432                    }
433                    $this->form->addItem($ne);
434                }
435            }
436        }
437    }
438}
439