ILIAS  Release_4_2_x_branch Revision 61807
 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;
122 
123  if ($rbacsystem->checkAccess("write", $this->ref_id))
124  {
125  include_once "./Modules/SurveyQuestionPool/classes/tables/class.ilSurveyPhrasesTableGUI.php";
126  $table_gui = new ilSurveyPhrasesTableGUI($this, 'phrases');
128  $data = array();
129  foreach ($phrases as $phrase_id => $phrase_array)
130  {
131  $categories =& ilSurveyPhrases::_getCategoriesForPhrase($phrase_id);
132  array_push($data, array('phrase_id' => $phrase_id, 'phrase' => $phrase_array["title"], 'answers' => join($categories, ", ")));
133  }
134  $table_gui->setData($data);
135  $this->tpl->setVariable('ADM_CONTENT', $table_gui->getHTML());
136  }
137  else
138  {
139  ilUtil::sendInfo($this->lng->txt("cannot_manage_phrases"));
140  }
141  }
142 
146  public function cancelDeletePhrase()
147  {
148  $this->ctrl->redirect($this, "phrases");
149  }
150 
154  public function confirmDeletePhrase()
155  {
156  $phrases = $_POST['phrase'];
157  $this->object->deletePhrases($phrases);
158  ilUtil::sendSuccess($this->lng->txt("qpl_phrases_deleted"), true);
159  $this->ctrl->redirect($this, "phrases");
160  }
161 
162  protected function getCategoriesForPhrase($phrase_id)
163  {
164  global $ilDB;
165 
166  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
167  $categories = new SurveyCategories();
168  $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",
169  array('integer'),
170  array($phrase_id)
171  );
172  if ($result->numRows() > 0)
173  {
174  while ($data = $ilDB->fetchAssoc($result))
175  {
176  $categories->addCategory($data["title"], 0, $data["neutral"], null, $data['sequence']);
177  }
178  }
179  return $categories;
180  }
181 
182  protected function getPhraseTitle($phrase_id)
183  {
184  global $ilDB;
185 
186  $result = $ilDB->queryF("SELECT svy_phrase.title FROM svy_phrase WHERE svy_phrase.phrase_id = %s",
187  array('integer'),
188  array($phrase_id)
189  );
190  if ($result->numRows() > 0)
191  {
192  $row = $ilDB->fetchAssoc($result);
193  return $row['title'];
194  }
195  return null;
196  }
197 
203  public function deletePhrasesForm($checked_phrases)
204  {
205  include_once "./Modules/SurveyQuestionPool/classes/tables/class.ilSurveyPhrasesTableGUI.php";
206  $table_gui = new ilSurveyPhrasesTableGUI($this, 'phrases', true);
208  $data = array();
209  foreach ($checked_phrases as $phrase_id)
210  {
211  $phrase_array = $phrases[$phrase_id];
212  $categories =& ilSurveyPhrases::_getCategoriesForPhrase($phrase_id);
213  array_push($data, array('phrase_id' => $phrase_id, 'phrase' => $phrase_array["title"], 'answers' => join($categories, ", ")));
214  }
215  $table_gui->setData($data);
216  $this->tpl->setVariable('ADM_CONTENT', $table_gui->getHTML());
217  }
218 
219  public function cancelEditPhrase()
220  {
221  $this->ctrl->redirect($this, 'phrases');
222  }
223 
224  public function saveEditPhrase()
225  {
226  $result = $this->writePostData();
227  if ($result == 0)
228  {
229  if ($_GET['p_id'])
230  {
231  $this->object->updatePhrase($_GET['p_id']);
232  ilUtil::sendSuccess($this->lng->txt('phrase_saved'), true);
233  }
234  else
235  {
236  $this->object->savePhrase();
237  ilUtil::sendSuccess($this->lng->txt('phrase_added'), true);
238  }
239  $this->ctrl->redirect($this, 'phrases');
240  }
241  }
242 
249  function writePostData($always = false)
250  {
251  global $ilDB;
252  $hasErrors = (!$always) ? $this->phraseEditor(true) : false;
253  if (!$hasErrors)
254  {
255  $this->object->title = $_POST["title"];
256  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
257  $categories = new SurveyCategories();
258  foreach ($_POST['answers']['answer'] as $key => $value)
259  {
260  if (strlen($value)) $categories->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
261  }
262  if (strlen($_POST['answers']['neutral']))
263  {
264  $categories->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
265  }
266  $this->object->categories = $categories;
267  return 0;
268  }
269  else
270  {
271  return 1;
272  }
273  }
274 
275  public function newPhrase()
276  {
277  $this->ctrl->redirect($this, 'phraseEditor');
278  }
279 
280  public function editPhrase()
281  {
282  if (!array_key_exists('phrase', $_POST))
283  {
284  ilUtil::sendFailure($this->lng->txt('no_phrase_selected'), true);
285  $this->ctrl->redirect($this, 'phrases');
286  }
287  if ((array_key_exists('phrase', $_POST)) && count($_POST['phrase']) > 1)
288  {
289  ilUtil::sendFailure($this->lng->txt('select_max_one_item'), true);
290  $this->ctrl->redirect($this, 'phrases');
291  }
292  $phrase_id = (array_key_exists('phrase', $_POST)) ? $_POST['phrase'][key($_POST['phrase'])] : null;
293  if ($phrase_id)
294  {
295  $this->ctrl->setParameter($this, 'p_id', $phrase_id);
296  }
297  $this->ctrl->redirect($this, 'phraseEditor');
298  }
299 
300  public function phraseEditor($checkonly = FALSE)
301  {
302  $save = (strcmp($this->ctrl->getCmd(), "saveEditPhrase") == 0) ? TRUE : FALSE;
303 
304  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
305  $form = new ilPropertyFormGUI();
306  $form->setFormAction($this->ctrl->getFormAction($this, 'phraseEditor'));
307  $form->setTitle($this->lng->txt('edit_phrase'));
308  $form->setMultipart(FALSE);
309  $form->setTableWidth("100%");
310  $form->setId("phraseeditor");
311 
312  $phrase_id = $_GET['p_id'];
313 
314  // title
315  $title = new ilTextInputGUI($this->lng->txt("title"), "title");
316  $title->setValue($this->getPhraseTitle($phrase_id));
317  $title->setRequired(TRUE);
318  $form->addItem($title);
319 
320  // Answers
321  include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
322  $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
323  $answers->setRequired(true);
324  $answers->setAllowMove(true);
325  $answers->setShowWizard(false );
326  $answers->setShowSavePhrase(false);
327  $answers->setUseOtherAnswer(false);
328  $answers->setShowNeutralCategory(true);
329  $answers->setNeutralCategoryTitle($this->lng->txt('matrix_neutral_answer'));
330  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
331  $categories =& $this->getCategoriesForPhrase($phrase_id);
332  if (!$categories->getCategoryCount())
333  {
334  $categories->addCategory("");
335  }
336  $answers->setValues($categories);
337  $answers->setDisabledScale(true);
338  $form->addItem($answers);
339 
340  $form->addCommandButton("saveEditPhrase", $this->lng->txt("save"));
341  $form->addCommandButton("cancelEditPhrase", $this->lng->txt("cancel"));
342 
343  $errors = false;
344 
345  if ($save)
346  {
347  $form->setValuesByPost();
348  $errors = !$form->checkInput();
349  $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
350  if ($errors) $checkonly = false;
351  }
352 
353  if (!$checkonly) $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
354  return $errors;
355  }
356 
357 }
358 ?>