1<?php
2/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Rating/classes/class.ilRatingCategory.php");
5
6/**
7 * Class ilRatingCategoryGUI. User interface class for rating categories.
8 *
9 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
10 * @version $Id$
11 *
12 * @ingroup ServicesRating
13 */
14class ilRatingCategoryGUI
15{
16    /**
17     * @var ilLanguage
18     */
19    protected $lng;
20
21    /**
22     * @var ilCtrl
23     */
24    protected $ctrl;
25
26    /**
27     * @var ilTemplate
28     */
29    protected $tpl;
30
31    /**
32     * @var ilToolbarGUI
33     */
34    protected $toolbar;
35
36    protected $parent_id; // [int]
37    protected $export_callback; // [string|array]
38    protected $export_subobj_title; // [string]
39
40    public function __construct($a_parent_id, $a_export_callback = null, $a_export_subobj_title = null)
41    {
42        global $DIC;
43
44        $this->lng = $DIC->language();
45        $this->ctrl = $DIC->ctrl();
46        $this->tpl = $DIC["tpl"];
47        $this->toolbar = $DIC->toolbar();
48        $lng = $DIC->language();
49
50        $this->parent_id = (int) $a_parent_id;
51        $this->export_callback = $a_export_callback;
52        $this->export_subobj_title = $a_export_subobj_title;
53
54        $lng->loadLanguageModule("rating");
55
56        if ($_REQUEST["cat_id"]) {
57            $cat = new ilRatingCategory($_REQUEST["cat_id"]);
58            if ($cat->getParentId() == $this->parent_id) {
59                $this->cat_id = $cat->getId();
60            }
61        }
62    }
63
64    /**
65     * execute command
66     */
67    public function executeCommand()
68    {
69        $ilCtrl = $this->ctrl;
70
71        $next_class = $ilCtrl->getNextClass($this);
72        $cmd = $ilCtrl->getCmd("listCategories");
73
74        switch ($next_class) {
75            default:
76                return $this->$cmd();
77                break;
78        }
79    }
80
81    protected function listCategories()
82    {
83        $tpl = $this->tpl;
84        $ilToolbar = $this->toolbar;
85        $lng = $this->lng;
86        $ilCtrl = $this->ctrl;
87
88        $ilToolbar->addButton(
89            $lng->txt("rating_add_category"),
90            $ilCtrl->getLinkTarget($this, "add")
91        );
92
93        $ilToolbar->addSeparator();
94
95        $ilToolbar->addButton(
96            $lng->txt("export"),
97            $ilCtrl->getLinkTarget($this, "export")
98        );
99
100        include_once "Services/Rating/classes/class.ilRatingCategoryTableGUI.php";
101        $table = new ilRatingCategoryTableGUI($this, "listCategories", $this->parent_id);
102        $tpl->setContent($table->getHTML());
103    }
104
105
106    protected function initCategoryForm($a_id = null)
107    {
108        $lng = $this->lng;
109        $ilCtrl = $this->ctrl;
110
111        include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
112        $form = new ilPropertyFormGUI();
113        $form->setTarget("_top");
114        $form->setFormAction($ilCtrl->getFormAction($this, "save"));
115        $form->setTitle($lng->txt("rating_category_" . ($a_id ? "edit" : "create")));
116
117        // title
118        $ti = new ilTextInputGUI($lng->txt("title"), "title");
119        $ti->setMaxLength(128);
120        $ti->setSize(40);
121        $ti->setRequired(true);
122        $form->addItem($ti);
123
124        // description
125        $ta = new ilTextAreaInputGUI($lng->txt("description"), "desc");
126        $ta->setCols(40);
127        $ta->setRows(2);
128        $form->addItem($ta);
129
130        if (!$a_id) {
131            $form->addCommandButton("save", $lng->txt("rating_category_add"));
132        } else {
133            $cat = new ilRatingCategory($a_id);
134            $ti->setValue($cat->getTitle());
135            $ta->setValue($cat->getDescription());
136
137            $form->addCommandButton("update", $lng->txt("rating_category_update"));
138        }
139        $form->addCommandButton("listCategories", $lng->txt("cancel"));
140
141        return $form;
142    }
143
144    protected function add($a_form = null)
145    {
146        $tpl = $this->tpl;
147
148        if (!$a_form) {
149            $a_form = $this->initCategoryForm();
150        }
151
152        $tpl->setContent($a_form->getHTML());
153    }
154
155    protected function save()
156    {
157        $ilCtrl = $this->ctrl;
158        $lng = $this->lng;
159
160        $form = $this->initCategoryForm("create");
161        if ($form->checkInput()) {
162            include_once "Services/Rating/classes/class.ilRatingCategory.php";
163            $cat = new ilRatingCategory();
164            $cat->setParentId($this->parent_id);
165            $cat->setTitle($form->getInput("title"));
166            $cat->setDescription($form->getInput("desc"));
167            $cat->save();
168
169            ilUtil::sendSuccess($lng->txt("rating_category_created"));
170            $ilCtrl->redirect($this, "listCategories");
171        }
172
173        $form->setValuesByPost();
174        $this->add($form);
175    }
176
177    protected function edit($a_form = null)
178    {
179        $tpl = $this->tpl;
180        $ilCtrl = $this->ctrl;
181
182        $ilCtrl->setParameter($this, "cat_id", $this->cat_id);
183
184        if (!$a_form) {
185            $a_form = $this->initCategoryForm($this->cat_id);
186        }
187
188        $tpl->setContent($a_form->getHTML());
189    }
190
191    protected function update()
192    {
193        $ilCtrl = $this->ctrl;
194        $lng = $this->lng;
195
196        $form = $this->initCategoryForm($this->cat_id);
197        if ($form->checkInput()) {
198            include_once "Services/Rating/classes/class.ilRatingCategory.php";
199            $cat = new ilRatingCategory($this->cat_id);
200            $cat->setTitle($form->getInput("title"));
201            $cat->setDescription($form->getInput("desc"));
202            $cat->update();
203
204            ilUtil::sendSuccess($lng->txt("rating_category_updated"));
205            $ilCtrl->redirect($this, "listCategories");
206        }
207
208        $form->setValuesByPost();
209        $this->add($form);
210    }
211
212    protected function updateOrder()
213    {
214        $ilCtrl = $this->ctrl;
215        $lng = $this->lng;
216
217        $order = $_POST["pos"];
218        asort($order);
219
220        $cnt = 0;
221        foreach ($order as $id => $pos) {
222            $cat = new ilRatingCategory($id);
223            if ($cat->getParentId() == $this->parent_id) {
224                $cnt += 10;
225                $cat->setPosition($cnt);
226                $cat->update();
227            }
228        }
229
230        ilUtil::sendSuccess($lng->txt("settings_saved"), true);
231        $ilCtrl->redirect($this, "listCategories");
232    }
233
234    protected function confirmDelete()
235    {
236        $tpl = $this->tpl;
237        $ilCtrl = $this->ctrl;
238        $lng = $this->lng;
239
240        if (!$this->cat_id) {
241            return $this->listCategories();
242        }
243
244        include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
245        $cgui = new ilConfirmationGUI();
246        $cgui->setHeaderText($lng->txt("rating_category_delete_sure") . "<br/>" .
247            $lng->txt("info_delete_warning_no_trash"));
248
249        $cgui->setFormAction($ilCtrl->getFormAction($this));
250        $cgui->setCancel($lng->txt("cancel"), "listCategories");
251        $cgui->setConfirm($lng->txt("confirm"), "delete");
252
253        $cat = new ilRatingCategory($this->cat_id);
254        $cgui->addItem("cat_id", $this->cat_id, $cat->getTitle());
255
256        $tpl->setContent($cgui->getHTML());
257    }
258
259    protected function delete()
260    {
261        $ilCtrl = $this->ctrl;
262        $lng = $this->lng;
263
264        if ($this->cat_id) {
265            ilRatingCategory::delete($this->cat_id);
266            ilUtil::sendSuccess($lng->txt("rating_category_deleted"), true);
267        }
268
269        // fix order
270        $cnt = 0;
271        foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
272            $cnt += 10;
273
274            $cat = new ilRatingCategory($item["id"]);
275            $cat->setPosition($cnt);
276            $cat->update();
277        }
278
279        $ilCtrl->redirect($this, "listCategories");
280    }
281
282    protected function export()
283    {
284        $lng = $this->lng;
285
286        include_once "./Services/Excel/classes/class.ilExcel.php";
287        $excel = new ilExcel();
288        $excel->addSheet($lng->txt("rating_categories"));
289
290        // restrict to currently active (probably not needed - see delete())
291        $active = array();
292        foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
293            $active[$item["id"]] = $item["title"];
294        }
295
296        // title row
297        $row = 1;
298        $excel->setCell($row, 0, $this->export_subobj_title . " (" . $lng->txt("id") . ")");
299        $excel->setCell($row, 1, $this->export_subobj_title);
300        $excel->setCell($row, 2, $lng->txt("rating_export_category") . " (" . $lng->txt("id") . ")");
301        $excel->setCell($row, 3, $lng->txt("rating_export_category"));
302        $excel->setCell($row, 4, $lng->txt("rating_export_date"));
303        $excel->setCell($row, 5, $lng->txt("rating_export_rating"));
304        $excel->setBold("A1:F1");
305
306        // content rows
307        foreach (ilRating::getExportData($this->parent_id, ilObject::_lookupType($this->parent_id), array_keys($active)) as $item) {
308            // overall rating?
309            if (!$item["sub_obj_id"]) {
310                continue;
311            }
312
313            $row++;
314
315            $sub_obj_title = $item["sub_obj_type"];
316            if ($this->export_callback) {
317                $sub_obj_title = call_user_func($this->export_callback, $item["sub_obj_id"], $item["sub_obj_type"]);
318            }
319
320            $excel->setCell($row, 0, (int) $item["sub_obj_id"]);
321            $excel->setCell($row, 1, $sub_obj_title);
322            $excel->setCell($row, 2, (int) $item["category_id"]);
323            $excel->setCell($row, 3, $active[$item["category_id"]]);
324            $excel->setCell($row, 4, new ilDateTime($item["tstamp"], IL_CAL_UNIX));
325            $excel->setCell($row, 5, $item["rating"]);
326        }
327
328        $excel->sendToClient(ilObject::_lookupTitle($this->parent_id));
329    }
330}
331