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