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