ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSurveyPhrasesGUI.php
Go to the documentation of this file.
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 
36 {
37  var $object;
39  var $lng;
40  var $tpl;
41  var $ctrl;
42  var $ilias;
43  var $tree;
44  var $ref_id;
45 
50  function __construct($a_object)
51  {
52  global $lng, $tpl, $ilCtrl, $ilias, $tree;
53 
54  include_once "./Modules/SurveyQuestionPool/classes/class.ilSurveyPhrases.php";
55  $this->lng =& $lng;
56  $this->tpl =& $tpl;
57  $this->ctrl =& $ilCtrl;
58  $this->ilias =& $ilias;
59  $this->gui_object =& $a_object;
60  $this->object = new ilSurveyPhrases();
61  $this->tree =& $tree;
62  $this->ref_id = $a_object->ref_id;
63  $this->ctrl->saveParameter($this, "p_id");
64  }
65 
69  public function &executeCommand()
70  {
71  $cmd = $this->ctrl->getCmd();
72  $next_class = $this->ctrl->getNextClass($this);
73 
74  $cmd = $this->getCommand($cmd);
75  switch($next_class)
76  {
77  default:
78  $ret =& $this->$cmd();
79  break;
80  }
81  return $ret;
82  }
83 
87  public function getCommand($cmd)
88  {
89  return $cmd;
90  }
91 
95  public function deletePhrase()
96  {
98 
99  $checked_phrases = $_POST['phrase'];
100  if (count($checked_phrases))
101  {
102  ilUtil::sendQuestion($this->lng->txt("qpl_confirm_delete_phrases"));
103  $this->deletePhrasesForm($checked_phrases);
104  return;
105  }
106  else
107  {
108  ilUtil::sendInfo($this->lng->txt("qpl_delete_phrase_select_none"));
109  $this->phrases();
110  return;
111  }
112  }
113 
119  public function phrases()
120  {
121  global $rbacsystem, $ilToolbar;
122 
123  $this->ctrl->setParameter($this, "p_id", "");
124 
125  if ($rbacsystem->checkAccess("write", $this->ref_id))
126  {
127  $ilToolbar->addButton($this->lng->txt('phrase_new'),
128  $this->ctrl->getLinkTarget($this, 'newPhrase'));
129 
130  include_once "./Modules/SurveyQuestionPool/classes/tables/class.ilSurveyPhrasesTableGUI.php";
131  $table_gui = new ilSurveyPhrasesTableGUI($this, 'phrases');
133  $data = array();
134  foreach ($phrases as $phrase_id => $phrase_array)
135  {
136  $categories =& ilSurveyPhrases::_getCategoriesForPhrase($phrase_id);
137  array_push($data, array('phrase_id' => $phrase_id, 'phrase' => $phrase_array["title"], 'answers' => join($categories, ", ")));
138  }
139  $table_gui->setData($data);
140  $this->tpl->setContent($table_gui->getHTML());
141  }
142  else
143  {
144  ilUtil::sendInfo($this->lng->txt("cannot_manage_phrases"));
145  }
146  }
147 
151  public function cancelDeletePhrase()
152  {
153  $this->ctrl->redirect($this, "phrases");
154  }
155 
159  public function confirmDeletePhrase()
160  {
161  $phrases = $_POST['phrase'];
162  $this->object->deletePhrases($phrases);
163  ilUtil::sendSuccess($this->lng->txt("qpl_phrases_deleted"), true);
164  $this->ctrl->redirect($this, "phrases");
165  }
166 
167  protected function getCategoriesForPhrase($phrase_id)
168  {
169  global $ilDB;
170 
171  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
172  $categories = new SurveyCategories();
173  $result = $ilDB->queryF("SELECT svy_category.title, svy_category.neutral, svy_phrase_cat.sequence FROM svy_phrase_cat, svy_category WHERE svy_phrase_cat.phrase_fi = %s AND svy_phrase_cat.category_fi = svy_category.category_id ORDER BY svy_phrase_cat.sequence ASC",
174  array('integer'),
175  array($phrase_id)
176  );
177  if ($result->numRows() > 0)
178  {
179  while ($data = $ilDB->fetchAssoc($result))
180  {
181  $categories->addCategory($data["title"], 0, $data["neutral"], null, $data['sequence']);
182  }
183  }
184  return $categories;
185  }
186 
187  protected function getPhraseTitle($phrase_id)
188  {
189  global $ilDB;
190 
191  $result = $ilDB->queryF("SELECT svy_phrase.title FROM svy_phrase WHERE svy_phrase.phrase_id = %s",
192  array('integer'),
193  array($phrase_id)
194  );
195  if ($result->numRows() > 0)
196  {
197  $row = $ilDB->fetchAssoc($result);
198  return $row['title'];
199  }
200  return null;
201  }
202 
208  public function deletePhrasesForm($checked_phrases)
209  {
210  include_once "./Modules/SurveyQuestionPool/classes/tables/class.ilSurveyPhrasesTableGUI.php";
211  $table_gui = new ilSurveyPhrasesTableGUI($this, 'phrases', true);
213  $data = array();
214  foreach ($checked_phrases as $phrase_id)
215  {
216  $phrase_array = $phrases[$phrase_id];
217  $categories =& ilSurveyPhrases::_getCategoriesForPhrase($phrase_id);
218  array_push($data, array('phrase_id' => $phrase_id, 'phrase' => $phrase_array["title"], 'answers' => join($categories, ", ")));
219  }
220  $table_gui->setData($data);
221  $this->tpl->setVariable('ADM_CONTENT', $table_gui->getHTML());
222  }
223 
224  public function cancelEditPhrase()
225  {
226  $this->ctrl->redirect($this, 'phrases');
227  }
228 
229  public function saveEditPhrase()
230  {
231  $result = $this->writePostData();
232  if ($result == 0)
233  {
234  if ($_GET['p_id'])
235  {
236  $this->object->updatePhrase($_GET['p_id']);
237  ilUtil::sendSuccess($this->lng->txt('phrase_saved'), true);
238  }
239  else
240  {
241  $this->object->savePhrase();
242  ilUtil::sendSuccess($this->lng->txt('phrase_added'), true);
243  }
244  $this->ctrl->redirect($this, 'phrases');
245  }
246  }
247 
254  function writePostData($always = false)
255  {
256  global $ilDB;
257  $hasErrors = (!$always) ? $this->phraseEditor(true) : false;
258  if (!$hasErrors)
259  {
260  $this->object->title = $_POST["title"];
261  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
262  $categories = new SurveyCategories();
263  foreach ($_POST['answers']['answer'] as $key => $value)
264  {
265  if (strlen($value)) $categories->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
266  }
267  if (strlen($_POST['answers']['neutral']))
268  {
269  $categories->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
270  }
271  $this->object->categories = $categories;
272  return 0;
273  }
274  else
275  {
276  return 1;
277  }
278  }
279 
280  public function newPhrase()
281  {
282  $this->ctrl->redirect($this, 'phraseEditor');
283  }
284 
285  public function editPhrase()
286  {
287  if (!array_key_exists('phrase', $_POST))
288  {
289  ilUtil::sendFailure($this->lng->txt('no_phrase_selected'), true);
290  $this->ctrl->redirect($this, 'phrases');
291  }
292  if ((array_key_exists('phrase', $_POST)) && count($_POST['phrase']) > 1)
293  {
294  ilUtil::sendFailure($this->lng->txt('select_max_one_item'), true);
295  $this->ctrl->redirect($this, 'phrases');
296  }
297  $phrase_id = (array_key_exists('phrase', $_POST)) ? $_POST['phrase'][key($_POST['phrase'])] : null;
298  if ($phrase_id)
299  {
300  $this->ctrl->setParameter($this, 'p_id', $phrase_id);
301  }
302  $this->ctrl->redirect($this, 'phraseEditor');
303  }
304 
305  public function phraseEditor($checkonly = FALSE)
306  {
307  $save = (strcmp($this->ctrl->getCmd(), "saveEditPhrase") == 0) ? TRUE : FALSE;
308 
309  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
310  $form = new ilPropertyFormGUI();
311  $form->setFormAction($this->ctrl->getFormAction($this, 'phraseEditor'));
312  $form->setTitle($this->lng->txt('edit_phrase'));
313  $form->setMultipart(FALSE);
314  $form->setTableWidth("100%");
315  $form->setId("phraseeditor");
316 
317  $phrase_id = $_GET['p_id'];
318 
319  // title
320  $title = new ilTextInputGUI($this->lng->txt("title"), "title");
321  $title->setValue($this->getPhraseTitle($phrase_id));
322  $title->setRequired(TRUE);
323  $form->addItem($title);
324 
325  // Answers
326  include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
327  $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
328  $answers->setRequired(true);
329  $answers->setAllowMove(true);
330  $answers->setShowWizard(false );
331  $answers->setShowSavePhrase(false);
332  $answers->setUseOtherAnswer(false);
333  $answers->setShowNeutralCategory(true);
334  $answers->setNeutralCategoryTitle($this->lng->txt('matrix_neutral_answer'));
335  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
336  $categories =& $this->getCategoriesForPhrase($phrase_id);
337  if (!$categories->getCategoryCount())
338  {
339  $categories->addCategory("");
340  }
341  $answers->setValues($categories);
342  $answers->setDisabledScale(true);
343  $form->addItem($answers);
344 
345  $form->addCommandButton("saveEditPhrase", $this->lng->txt("save"));
346  $form->addCommandButton("cancelEditPhrase", $this->lng->txt("cancel"));
347 
348  $errors = false;
349 
350  if ($save)
351  {
352  $form->setValuesByPost();
353  $errors = !$form->checkInput();
354  $form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
355  if ($errors) $checkonly = false;
356  }
357 
358  if (!$checkonly) $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
359  return $errors;
360  }
361 
362 }
363 ?>