1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Class ilExcCriteriaGUI
7 *
8 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
9 * @ilCtrl_Calls ilExcCriteriaGUI:
10 * @ingroup ModulesExercise
11 */
12class ilExcCriteriaGUI
13{
14    /**
15     * @var ilCtrl
16     */
17    protected $ctrl;
18
19    /**
20     * @var ilToolbarGUI
21     */
22    protected $toolbar;
23
24    /**
25     * @var ilLanguage
26     */
27    protected $lng;
28
29    /**
30     * @var ilTemplate
31     */
32    protected $tpl;
33
34    protected $cat_id; // [int]
35
36    public function __construct($a_cat_id)
37    {
38        global $DIC;
39
40        $this->ctrl = $DIC->ctrl();
41        $this->toolbar = $DIC->toolbar();
42        $this->lng = $DIC->language();
43        $this->tpl = $DIC["tpl"];
44        $this->cat_id = $a_cat_id;
45    }
46
47    public function executeCommand()
48    {
49        $ilCtrl = $this->ctrl;
50
51        $next_class = $ilCtrl->getNextClass($this);
52        $cmd = $ilCtrl->getCmd("view");
53
54        switch ($next_class) {
55            default:
56                $this->$cmd();
57                break;
58        }
59    }
60
61
62    //
63    // LIST/TABLE
64    //
65
66    protected function view()
67    {
68        $ilToolbar = $this->toolbar;
69        $ilCtrl = $this->ctrl;
70        $lng = $this->lng;
71        $tpl = $this->tpl;
72
73        $ilToolbar->setFormAction($ilCtrl->getFormAction($this, "add"));
74
75        $types = new ilSelectInputGUI($lng->txt("type"), "type");
76        $types->setOptions(ilExcCriteria::getTypesMap());
77        $ilToolbar->addStickyItem($types);
78
79        $button = ilSubmitButton::getInstance();
80        $button->setCaption("exc_add_criteria");
81        $button->setCommand("add");
82        $ilToolbar->addStickyItem($button);
83
84        $tbl = new ilExcCriteriaTableGUI($this, "view", $this->cat_id);
85        $tpl->setContent($tbl->getHTML());
86    }
87
88    protected function saveOrder()
89    {
90        $ilCtrl = $this->ctrl;
91        $lng = $this->lng;
92
93        $all_cat = ilExcCriteria::getInstancesByParentId($this->cat_id);
94
95        $pos = 0;
96        asort($_POST["pos"]);
97        foreach (array_keys($_POST["pos"]) as $id) {
98            if (array_key_exists($id, $all_cat)) {
99                $pos += 10;
100                $all_cat[$id]->setPosition($pos);
101                $all_cat[$id]->update();
102            }
103        }
104
105        ilUtil::sendSuccess($lng->txt("settings_saved"), true);
106        $ilCtrl->redirect($this, "view");
107    }
108
109    protected function confirmDeletion()
110    {
111        $ilCtrl = $this->ctrl;
112        $lng = $this->lng;
113        $tpl = $this->tpl;
114
115        $ids = $_POST["id"];
116        if (!sizeof($ids)) {
117            ilUtil::sendInfo($lng->txt("select_one"), true);
118            $ilCtrl->redirect($this, "view");
119        }
120
121        $confirmation_gui = new ilConfirmationGUI();
122        $confirmation_gui->setFormAction($ilCtrl->getFormAction($this, "delete"));
123        $confirmation_gui->setHeaderText($lng->txt("exc_criteria_deletion_confirmation"));
124        $confirmation_gui->setCancel($lng->txt("cancel"), "view");
125        $confirmation_gui->setConfirm($lng->txt("delete"), "delete");
126
127        foreach (ilExcCriteria::getInstancesByParentId($this->cat_id) as $item) {
128            if (in_array($item->getId(), $ids)) {
129                $confirmation_gui->addItem("id[]", $item->getId(), $item->getTitle());
130            }
131        }
132
133        $tpl->setContent($confirmation_gui->getHTML());
134    }
135
136    protected function delete()
137    {
138        $ilCtrl = $this->ctrl;
139        $lng = $this->lng;
140
141        $ids = $_POST["id"];
142        if (!sizeof($ids)) {
143            $ilCtrl->redirect($this, "view");
144        }
145
146        foreach (ilExcCriteria::getInstancesByParentId($this->cat_id) as $item) {
147            if (in_array($item->getId(), $ids)) {
148                $item->delete();
149            }
150        }
151
152        ilUtil::sendSuccess($lng->txt("settings_saved"), true);
153        $ilCtrl->redirect($this, "view");
154    }
155
156
157    //
158    // EDIT
159    //
160
161    protected function initForm(ilExcCriteria $a_crit_obj)
162    {
163        $ilCtrl = $this->ctrl;
164        $lng = $this->lng;
165
166        $form = new ilPropertyFormGUI();
167
168        $is_edit = (bool) $a_crit_obj->getId();
169        if (!$is_edit) {
170            $form->setFormAction($ilCtrl->getFormAction($this, "create"));
171            $form->setTitle($lng->txt("exc_criteria_create_form"));
172            $form->addCommandButton("create", $lng->txt("create"));
173        } else {
174            $form->setFormAction($ilCtrl->getFormAction($this, "update"));
175            $form->setTitle($lng->txt("exc_criteria_update_form"));
176            $form->addCommandButton("update", $lng->txt("save"));
177        }
178
179        $form->addCommandButton("view", $lng->txt("cancel"));
180
181        $type = new ilNonEditableValueGUI($lng->txt("type"));
182        $type->setValue($a_crit_obj->getTranslatedType());
183        $form->addItem($type);
184
185        $title = new ilTextInputGUI($lng->txt("title"), "title");
186        $title->setRequired(true);
187        $form->addItem($title);
188
189        $desc = new ilTextAreaInputGUI($lng->txt("description"), "desc");
190        $form->addItem($desc);
191
192        $req = new ilCheckboxInputGUI($lng->txt("required_field"), "req");
193        $form->addItem($req);
194
195        $a_crit_obj->initCustomForm($form);
196
197        return $form;
198    }
199
200    protected function add(ilPropertyFormGUI $a_form = null)
201    {
202        $tpl = $this->tpl;
203        $ilCtrl = $this->ctrl;
204
205        $new_type = trim($_REQUEST["type"]);
206        if (!$new_type) {
207            $ilCtrl->redirect($this, "view");
208        }
209
210        $ilCtrl->setParameter($this, "type", $new_type);
211
212        if (!$a_form) {
213            $crit_obj = ilExcCriteria::getInstanceByType($new_type);
214            $a_form = $this->initForm($crit_obj);
215        }
216
217        $tpl->setContent($a_form->getHTML());
218    }
219
220    protected function exportForm(ilExcCriteria $a_crit_obj, ilPropertyFormGUI $a_form)
221    {
222        $a_form->getItemByPostVar("title")->setValue($a_crit_obj->getTitle());
223        $a_form->getItemByPostVar("desc")->setValue($a_crit_obj->getDescription());
224        $a_form->getItemByPostVar("req")->setChecked($a_crit_obj->isRequired());
225
226        $a_crit_obj->exportCustomForm($a_form);
227    }
228
229    protected function importForm(ilExcCriteria $a_crit_obj)
230    {
231        $ilCtrl = $this->ctrl;
232        $lng = $this->lng;
233
234        $is_edit = (bool) $a_crit_obj->getId();
235
236        $form = $this->initForm($a_crit_obj);
237        if ($form->checkInput()) {
238            $a_crit_obj->setTitle($form->getInput("title"));
239            $a_crit_obj->setDescription($form->getInput("desc"));
240            $a_crit_obj->setRequired($form->getInput("req"));
241
242            $a_crit_obj->importCustomForm($form);
243
244            if (!$is_edit) {
245                $a_crit_obj->setParent($this->cat_id);
246                $a_crit_obj->save();
247            } else {
248                $a_crit_obj->update();
249            }
250
251            ilUtil::sendSuccess($lng->txt("settings_saved"), true);
252            $ilCtrl->redirect($this, "view");
253        }
254
255        $form->setValuesByPost();
256        $this->{$is_edit ? "edit" : "add"}($form);
257    }
258
259    protected function create()
260    {
261        $ilCtrl = $this->ctrl;
262
263        $new_type = trim($_REQUEST["type"]);
264        if (!$new_type) {
265            $ilCtrl->redirect($this, "view");
266        }
267
268        $crit_obj = ilExcCriteria::getInstanceByType($new_type);
269        $this->importForm($crit_obj);
270    }
271
272    protected function getCurrentCritera()
273    {
274        $ilCtrl = $this->ctrl;
275
276        $id = (int) $_REQUEST["crit_id"];
277        if ($id) {
278            $crit_obj = ilExcCriteria::getInstanceById($id);
279            if ($crit_obj->getParent() == $this->cat_id) {
280                $ilCtrl->setParameter($this, "crit_id", $id);
281                return $crit_obj;
282            }
283        }
284
285        $ilCtrl->redirect($this, "view");
286    }
287
288    protected function edit(ilPropertyFormGUI $a_form = null)
289    {
290        $tpl = $this->tpl;
291
292        $crit_obj = $this->getCurrentCritera();
293
294        if (!$a_form) {
295            $a_form = $this->initForm($crit_obj);
296            $this->exportForm($crit_obj, $a_form);
297        }
298
299        $tpl->setContent($a_form->getHTML());
300    }
301
302    protected function update()
303    {
304        $crit_obj = $this->getCurrentCritera();
305        $this->importForm($crit_obj);
306    }
307}
308