CMS  Version 3.9
question_helper.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFile("framework/field_renderers/string_field_renderer.inc");
40 
42 {
44  {
45 
46  }
47 
52  function formatOptions($obj)
53  {
54  if(!$obj->hasField("question_type_id") && $obj->hasRelation(Question))
55  {
56  $question = $obj->Question();
57  if (!$question) return "";
58  }
59  else
60  {
61  $question = $obj;
62  }
63 
64  if($question->isFreeText() || $question->isShortText())
65  {
66  $text .= "Character Limit: ";
67  $text .= ($question->char_limit) ? $question->char_limit : "None";
68  $text .= "<br>";
69  }
70  else
71  {
72  $text = preg_replace("/\s+\n/", "; ", $question->options);
73  }
74 
75  if($question->isFreeText())
76  $text .= "Number of Rows: " . $question->num_rows . "<br>";
77 
78  return $text;
79  }
80 
81 
86  function formatRequired($obj)
87  {
88  if(!$obj->hasField("question_type_id") && $obj->hasRelation(Question))
89  {
90  $question = $obj->Question();
91  if (!$question) return "";
92  }
93  else
94  {
95  $question = $obj;
96  }
97 
98  if($question->isCheckList())
99  return ($question->required > 0) ? $question->required : "No";
100  else
101  return ($question->required == 1) ? "Yes" : "No";
102  }
103 
105  {
106  if(!$question->question_name)
107  $question->question_name = substr($question->question, 0, 79);
108 
109  $pk = $question->getPrimaryKey();
110  return $question->format("<input type='text' name=\"question_{$question->$pk}\"
111  value=\"{question_name}\" size=\"40\"/>");
112  }
113 
114 
115  static function formatSortOrder($question)
116  {
117  return $question->format("<input type='text' name=\"question_{question_id}\"value=\"{question_number}\" size=\"4\"/>");
118  }
119 
129  static function formatDelete($question)
130  {
131  $pk = $question->getPrimaryKey();
132 
133  if($question->allowDelete())
134  {
135  $out = $question->format("<a href=\"delete_question?{$pk}={$question->$pk}\" onclick=\"questionnaireMgr.deleteQuestion({{$pk}}); return false;\">
136  <img class=\"icon\" alt=\"delete\" src=\"/fakoli/images/icon_delete_red.gif\"></a>\n");
137  }
138  else
139  {
140  $out = "";
141  }
142  return $out;
143  }
144 
157  static function formatRemove($xref)
158  {
159  $xrefPk = $xref->getPrimaryKey();
160  return $xref->format("<a href=\"#\" onclick=\"questionnaireMgr.removeQuestion({{$xrefPk}}); return false;\">
161  <img class=\"icon\" alt=\"delete\" src=\"/fakoli/images/icon_delete_red.gif\"></a>\n");
162  }
163 }
164 
165 
167 {
169  {
170 
171  }
172 
177  function formatOptions($obj)
178  {
179  if(!$obj->hasField("question_type_id") && $obj->hasRelation(Question))
180  {
181  $question = $obj->Question();
182  if (!$question) return "";
183  }
184  else
185  {
186  $question = $obj;
187  }
188 
189  if($question->isFreeText() || $question->isShortText())
190  {
191  $text .= "Character Limit: ";
192  $text .= ($question->char_limit) ? $question->char_limit : "None";
193  $text .= "<br>";
194  }
195  else
196  {
197  $text = preg_replace("/\s+\n/", "; ", $question->options);
198  }
199 
200  if($question->isFreeText())
201  $text .= "Number of Rows: " . $question->num_rows . "<br>";
202 
203  return $text;
204  }
205 
206 
208  {
209  if(!$question->hasField("answer") || !$question->answer || !$question->options)
210  {
211  return "";
212  }
213 
214  $options = explode("\n", $question->options);
215  return $options[$question->answer-1];
216  }
217 }
218 
219 
220 
221 
222 class QuestionFormHelper
223 {
224 
225  function QuestionFormHelper()
226  {
227  }
228 
246  static function questionTypeSetup(&$form, $editable)
247  {
248  $question = $form->data;
249  $pk = $question->getPrimaryKey();
250 
251  if($question->isRatings() || $editable)
252  {
253  $form->add(new RatingQuestionFieldRenderer($form), "label_for_lowest");
254  $form->add(new StringFieldRenderer($form), "label_for_highest");
255  $form->add(new NumberFieldRenderer($form), "number_of_steps");
256  }
257 
258  if($editable || $question->isFreeText() || $question->isShortText())
259  {
260  $form->getRenderer("num_rows")->size = 5;
261  $form->getRenderer("char_limit")->size = 5;
262  }
263  else
264  $form->hide("num_rows", "char_limit");
265 
266  if($question->$pk && $question->isRatings())
267  $form = QuestionFormHelper::setOptionsToRatings($form);
268  elseif($editable)
269  $form = QuestionFormHelper::setDefaultRatings($form);
270 
271  $form->labelCSS = "question_form_label";
272 
273  $typeSelect = new SelectFieldRenderer($form, "question_type_id", "Question Type", QuestionType::getQuestionTypeList());
274 
275  if(!$editable)
276  {
277  $form->readOnly("question_type_id", "options");
278  }
279  return $form;
280  }
281 
282  static function setRatingsToOptions()
283  {
284  global $_POST;
285 
286  $options = $_POST["label_for_lowest"] . "\r\n";
287  $options .= $_POST["label_for_highest"] . "\r\n";
288  $options .= $_POST["number_of_steps"] . "\r\n";
289 
290  $_POST["options"] = $options;
291  }
292 
293  static function setOptionsToRatings($form)
294  {
295  list($from, $to, $steps) = explode("\n", $form->data->get("options"));
296 
297  $form->data->set("label_for_lowest", $from);
298  $form->data->set("label_for_highest", $to);
299  $form->data->set("number_of_steps", $steps);
300 
301  return $form;
302  }
303 
304  static function setDefaultRatings($form)
305  {
306  $form->data->set("label_for_lowest", "Lowest");
307  $form->data->set("label_for_highest", "Highest");
308  $form->data->set("number_of_steps", 5);
309 
310  return $form;
311  }
312 
320  static function writeScript($form)
321  {
322  $question_type_id = $form->data->get("question_type_id");
323 
324  ob_start();
325 ?>
326 <script type="text/javascript" src="/components/questionnaire/js/question.js"></script>
327 <script type="text/javascript">
328 
329  var questionMgr;
330 
331  window.addEvent('domready', function()
332  {
333  questionMgr = new QuestionManager(<?php echo $question_type_id ?>, '<?php echo $form->id ?>');
334  });
335  </script>
336 <?php
337  $script .= ob_get_contents();
338  ob_end_clean();
339 
340  return $script;
341  }
342 
343 
344 }
345 
346 
347 
348 /*
349  * Use this custom renderer to copy the pseudo field values
350  * Label for Lowest, Label for Highest, and Number of Steps
351  * into the options text field before saving the form.
352  */
353 class RatingQuestionFieldRenderer extends StringFieldRenderer
354 {
355  function RatingQuestionFieldRenderer(&$parent)
356  {
357  $this->FieldRenderer($parent);
358  }
359 
360  function preProcess($field = "")
361  {
362  global $_POST;
363 
364  $question = $this->parent->data;
365  if($question->isRatings())
366  {
367  $lowest = $_POST["label_for_lowest"];
368  trace("RatingQuestionFieldRenderer: lowest $lowest", 3);
369  $highest = $_POST["label_for_highest"];
370  $steps = $_POST["number_of_steps"];
371  $this->parent->data->set("options", $lowest . "\n" . $highest . "\n" . $steps);
372  }
373  }
374 } // end RatingQuestionFieldRenderer?>
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$out
Definition: page.inc:66
$parent
Definition: templates.inc:42
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
static getQuestionTypeList()
static formatRemove($xref)
From question list page, format link to remove a question that is linked to the questionnaire item th...
static formatCorrectAnswer($question)
static formatQuestionNameInputField($question)
static formatSortOrder($question)
static formatDelete($question)
From question list page, format link to delete a question.
$question
$editable
$question_type_id