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
24require_once("./Services/COPage/classes/class.ilPCSkills.php");
25require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
26
27/**
28 * GUI class for learning history page content
29 *
30 * Handles user commands on skills data
31 *
32 * @author killin@leifos.com
33 *
34 * @ilCtrl_isCalledBy ilPCLearningHistoryGUI: ilPageEditorGUI
35 * @ingroup ServicesLearningHistory
36 */
37class ilPCLearningHistoryGUI extends ilPageContentGUI
38{
39    /**
40     * @var ilObjUser
41     */
42    protected $user;
43
44    /**
45     * @var \ILIAS\DI\UIServices
46     */
47    protected $ui;
48
49    /**
50     * @var ilLearningHistoryService
51     */
52    protected $service;
53
54    /**
55     * Constructor
56     */
57    public function __construct(ilPageObject $a_pg_obj, ilPCLearningHistory $a_content_obj = null, $a_hier_id = "", $a_pc_id = "")
58    {
59        global $DIC;
60
61        $this->tpl = $DIC["tpl"];
62        $this->ctrl = $DIC->ctrl();
63        $this->user = $DIC->user();
64        $this->lng = $DIC->language();
65        $this->lng->loadLanguageModule("lhist");
66        parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
67        $this->service = $DIC->learningHistory();
68        $this->ui = $DIC->ui();
69    }
70
71    /**
72     * execute command
73     */
74    public function executeCommand()
75    {
76        // get next class that processes or forwards current command
77        $next_class = $this->ctrl->getNextClass($this);
78
79        // get current command
80        $cmd = $this->ctrl->getCmd();
81
82        switch ($next_class) {
83            default:
84                $ret = $this->$cmd();
85                break;
86        }
87
88        return $ret;
89    }
90
91    /**
92     * Insert learning history form
93     *
94     * @param ilPropertyFormGUI $a_form
95     */
96    public function insert(ilPropertyFormGUI $a_form = null)
97    {
98        $tpl = $this->tpl;
99
100        $this->displayValidationError();
101
102        if (!$a_form) {
103            $a_form = $this->initForm(true);
104        }
105        $tpl->setContent($a_form->getHTML());
106    }
107
108    /**
109     * Edit skills form
110     *
111     * @param ilPropertyFormGUI $a_form
112     */
113    public function edit(ilPropertyFormGUI $a_form = null)
114    {
115        $tpl = $this->tpl;
116
117        $this->displayValidationError();
118
119        if (!$a_form) {
120            $a_form = $this->initForm();
121        }
122        $tpl->setContent($a_form->getHTML());
123    }
124
125    /**
126     * Init learning history edit form
127     *
128     * @param bool $a_insert
129     * @return ilPropertyFormGUI
130     */
131    protected function initForm($a_insert = false)
132    {
133        $ilCtrl = $this->ctrl;
134        $ilUser = $this->user;
135        $lng = $this->lng;
136
137        include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
138        $form = new ilPropertyFormGUI();
139        $form->setFormAction($ilCtrl->getFormAction($this));
140        if ($a_insert) {
141            $form->setTitle($this->lng->txt("cont_create_lhist"));
142        } else {
143            $form->setTitle($this->lng->txt("cont_update_lhist"));
144        }
145
146        // duration
147        $du = new ilDateDurationInputGUI($lng->txt("lhist_period"), "period");
148        if (!$a_insert) {
149            if ($this->content_obj->getFrom() != "") {
150                $du->setStart(new ilDate($this->content_obj->getFrom(), IL_CAL_DATE));
151            }
152            if ($this->content_obj->getTo() != "") {
153                $du->setEnd(new ilDate($this->content_obj->getTo(), IL_CAL_DATE));
154            }
155        }
156        $du->setAllowOpenIntervals(true);
157        $form->addItem($du);
158
159        //
160        $radg = new ilRadioGroupInputGUI($lng->txt("lhist_type_of_achievement"), "mode");
161        //$radg->setValue();
162        $op1 = new ilRadioOption($lng->txt("lhist_all"), 0);
163        $radg->addOption($op1);
164        $op2 = new ilRadioOption($lng->txt("lhist_selected"), 1);
165        $radg->addOption($op2);
166        $form->addItem($radg);
167
168
169        // select type
170        $options = [];
171        foreach ($this->service->provider()->getAllProviders(true) as $p) {
172            $options[get_class($p)] = $p->getName();
173        }
174        $si = new ilMultiSelectInputGUI($lng->txt(""), "class");
175        $si->setHeight(130);
176        if (!$a_insert) {
177            $si->setValue($this->content_obj->getClasses());
178            if (count($this->content_obj->getClasses()) > 0) {
179                $radg->setValue(1);
180            }
181        }
182        $si->setOptions($options);
183        $op2->addSubItem($si);
184
185
186
187        if ($a_insert) {
188            $form->addCommandButton("create_lhist", $this->lng->txt("insert"));
189            $form->addCommandButton("cancelCreate", $this->lng->txt("cancel"));
190        } else {
191            $form->addCommandButton("update", $this->lng->txt("save"));
192            $form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
193        }
194
195        return $form;
196    }
197
198
199    /**
200     * Create new learning history component
201     */
202    public function create()
203    {
204        $valid = false;
205
206        $form = $this->initForm(true);
207        if ($form->checkInput()) {
208            //$data = $form->getInput("skill_id");
209            $valid = true;
210        }
211
212        if ($valid) {
213            $this->content_obj = new ilPCLearningHistory($this->getPage());
214            $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
215            $this->setAttributesFromInput($form);
216            $this->updated = $this->pg_obj->update();
217            if ($this->updated === true) {
218                $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
219            }
220        }
221
222        $form->setValuesByPost();
223        return $this->insert($form);
224    }
225
226    /**
227     * Update learning history component
228     */
229    public function update()
230    {
231        $form = $this->initForm();
232        if ($form->checkInput()) {
233            $this->setAttributesFromInput($form);
234            $this->updated = $this->pg_obj->update();
235            if ($this->updated === true) {
236                ilUtil::sendInfo($this->lng->txt("msg_obj_modified"), true);
237                $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
238            }
239        }
240
241        $this->pg_obj->addHierIDs();
242        $form->setValuesByPost();
243        return $this->edit($form);
244    }
245
246    /**
247     *
248     *
249     * @param
250     * @return
251     */
252    protected function setAttributesFromInput($form)
253    {
254        /** @var ilDurationInputGUI $item */
255        $item = $form->getItemByPostVar("period");
256        $from = (is_null($item->getStart()))
257            ? ""
258            : $item->getStart()->get(IL_CAL_DATE);
259        $to = (is_null($item->getEnd()))
260            ? ""
261            : $item->getEnd()->get(IL_CAL_DATE);
262
263        $this->content_obj->setFrom($from);
264        $this->content_obj->setTo($to);
265        $classes = ($form->getInput("mode") == "1" && is_array($form->getInput("class")))
266            ? $form->getInput("class")
267            : array();
268        $this->content_obj->setClasses($classes);
269    }
270
271    /**
272     * Get placeholder presentation
273     *
274     * @param
275     * @return
276     */
277    public static function getPlaceholderPresentation()
278    {
279        global $DIC;
280
281        $lng = $DIC->language();
282        $lng->loadLanguageModule("lhist");
283
284        // @todo we need a ks element for this
285        $content = '<div style="margin:5px" class="ilBox"><h3>' . $lng->txt("lhist_lhist") . '</h3><div class="il_Description_no_margin">' .
286            $lng->txt("lhist_cont_placeholder_text") . '</div></div>';
287
288        return $content;
289    }
290}
291