CMS  Version 3.9
QuestionnaireForm Class Reference
+ Inheritance diagram for QuestionnaireForm:

Public Member Functions

 QuestionnaireForm ($mgr, $readOnlyForm=false)
 
 makeSubordinate ($subordinate=true)
 
 getValidationEngine ()
 
 button ($text, $url, $confirm=null, $isScript=false)
 Adds a custom button to the form. More...
 
 drawButtons ()
 Draws any additional buttons specified in the calling script. More...
 
 getAnswer ($question)
 
 writeScript ()
 
 writeHTML ()
 
 writeOneQuestion ($question)
 
 fromPOST ()
 Put any updated or new answers in the $_POST array into the array of answers that are indexed by question id so that when we draw the form, the answers are shown, even if page is not reloaded. More...
 
 drawForm ()
 
 drawSubmitButtons ($submitLabel)
 
 drawReadOnly ()
 
 drawView ()
 
 getIndexedAnswers ()
 
 validate ()
 
 save ()
 

Static Public Member Functions

static incrementArray ($array)
 

Public Attributes

 $mgr
 
 $questions
 
 $answers
 
 $submitLabel = "Save"
 
 $validators = array()
 
 $requiredText = "<b>You must answer all required questions.</b><br/>"
 
 $msg
 
 $onSaveComplete = null
 Callback event handler that is fired after the AutoForm has finished saving data to the database. More...
 
 $readOnlyForm
 Specifies whether the entire form is read-only. More...
 
 $id
 
 $labelCSS = ""
 CSS Class to use for label cells. More...
 
 $valueCSS = ""
 CSS Class to use for field/value cells. More...
 
 $buttonCSS = "button"
 CSS Class to use for buttons. More...
 
 $inputCSS = ""
 CSS Class to use for input fields. More...
 
 $checkboxCSS = ""
 CSS Class to use for checkbox fields. More...
 
 $markRequiredFields = false
 Set to true to add an asterisk after required field labels. More...
 
 $onFormatLabel = null
 Label Processing hook. More...
 
 $buttons_at_top = false
 
 $requiredFieldsText = "* indicates required question"
 
 $subordinate = false
 
 $buttons = array()
 The custom buttons collection. More...
 
 $allowIncompleteSave = true
 Allows users to save partially completed forms. More...
 

Detailed Description

Definition at line 74 of file questionnaire_form.inc.

Member Function Documentation

◆ button()

QuestionnaireForm::button (   $text,
  $url,
  $confirm = null,
  $isScript = false 
)

Adds a custom button to the form.

Parameters
string$textthe button label text
string$urlthe URL to handle the button press
string$confirmoptional confirmation message
boolean$isScripttrue if the url is javascript code to execute, false if it is a URL to redirect to

Definition at line 161 of file questionnaire_form.inc.

162  {
163  $this->buttons[] = array('text' => $text, 'url' => $url, 'confirm' => $confirm, 'isScript' => $isScript);
164  }
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58

◆ drawButtons()

QuestionnaireForm::drawButtons ( )

Draws any additional buttons specified in the calling script.

Definition at line 170 of file questionnaire_form.inc.

171  {
172  foreach($this->buttons as $button)
173  {
174  $url = ($button['isScript']) ? $button['url'] : "go('{$button['url']}');";
175 
176  if ($button['confirm'])
177  {
178  $link = "if (confirm('".jsSafe($button['confirm'])."')) $url; return false;";
179  }
180  else
181  {
182  $link = "$url; return false;";
183  }
184 
185  echo "&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' class='{$this->buttonCSS}' onclick=\"$link\" value=\"{$button['text']}\"/>";
186  }
187 
188  }
$button
Definition: show.inc:6

◆ drawForm()

QuestionnaireForm::drawForm ( )

Definition at line 365 of file questionnaire_form.inc.

366  {
367  if ($this->readOnlyForm) return $this->drawReadOnly();
368 
369  echo "<div class='questionnaire_form'>";
370 
372 
373  $id = ($this->id) ? $this->id : "Questionnaire_form";
374 
375  if (!$this->subordinate)
376  {
377  echo "<form id=\"{$id}\" method=\"POST\" action=\"\" ";
378  echo "enctype='multipart/form-data' onsubmit='return onSubmitQuestionnaire_form(this);'>";
379  }
380 
381  if ($this->buttons_at_top && !$this->subordinate)
382  {
384  $this->drawButtons();
385  echo "<br/><br/>";
386  }
387 
388  $this->writeHTML();
389 
390  if (!$this->subordinate)
391  {
392  echo "<br/>";
394  $this->drawButtons();
395  echo "<br/>";
396  echo "</form>";
397  }
398 
399  echo "</div>";
400  }
drawButtons()
Draws any additional buttons specified in the calling script.
drawSubmitButtons($submitLabel)
$form readOnlyForm

◆ drawReadOnly()

QuestionnaireForm::drawReadOnly ( )

Definition at line 407 of file questionnaire_form.inc.

408  {
409  $this->writeHTML();
410  }

◆ drawSubmitButtons()

QuestionnaireForm::drawSubmitButtons (   $submitLabel)

Definition at line 402 of file questionnaire_form.inc.

403  {
404  echo "<input type=\"submit\" style=\"float: left\" name=\"save\" class=\"{$this->buttonCSS}\" value=\"&nbsp;&nbsp;{$submitLabel}&nbsp;&nbsp;\"/>";
405  }

◆ drawView()

QuestionnaireForm::drawView ( )

Definition at line 413 of file questionnaire_form.inc.

414  {
415  $this->writeHTML();
416  }

◆ fromPOST()

QuestionnaireForm::fromPOST ( )

Put any updated or new answers in the $_POST array into the array of answers that are indexed by question id so that when we draw the form, the answers are shown, even if page is not reloaded.

Also called from function save.

Definition at line 294 of file questionnaire_form.inc.

295  {
296  global $_POST;
297 
298  $pk = $this->mgr->item->getPrimaryKey();
299  $qPk = $this->mgr->getQuestionKey();
300  $aPk = $this->mgr->getAnswerKey();
301  $answerClass = $this->mgr->getAnswerClass();
302 
303  foreach($_POST as $name => $value)
304  {
305  if (!strncmp("question_", $name, 9))
306  {
307  // convert array of checks to comma delimated string
308  $valueAnswer = (is_array($value)) ? implode(",", array_values($value)) : $value;
309 
310  $question_id = substr($name, 9);
311 
312  if(array_key_exists($question_id, $this->answers))
313  {
314  $this->answers[$question_id]->value = $valueAnswer;
315  }
316  else
317  {
318  $answer = new $answerClass();
319  // e.g., response_id
320  $answer->$pk = $this->mgr->item->$pk;
321  $answer->$qPk = $question_id;
322  $answer->value = $valueAnswer;
323  $this->answers[$question_id] = $answer;
324  }
325  }
326  }
327 
328  // Need to handle deleted checkbox answers which don't appear in $_POST
329  $questions = $this->mgr->getQuestions();
330 
331  if(count($questions) > 0)
332  {
333  $indexed = regroupList($questions, "question_type_id");
334  $checkListQuestions = array();
335  // CheckListView
336  if(array_key_exists(5, $indexed))
337  {
338  $checkListQuestions = $indexed[5];
339  if(!is_array($checkListQuestions))
340  {
341  $checkListQuestions = array($checkListQuestions);
342  }
343  }
344 
345  if(count($checkListQuestions) > 0)
346  {
347  foreach($checkListQuestions as $question)
348  {
349  $answer = $this->answers[$question->$qPk];
350  if($answer && $answer->value)
351  {
352  $postAnswer = $_POST["question_{$question->$qPk}"];
353 
354  if(!$postAnswer && !is_array($postAnswer))
355  {
356  $answer->value = "";
357  }
358  }
359  }
360  }
361  }
362 
363  }
$_POST["owner_id"]
Definition: blog_form.inc:54
$name
Definition: upload.inc:54
$question_id
$question

◆ getAnswer()

QuestionnaireForm::getAnswer (   $question)

Definition at line 189 of file questionnaire_form.inc.

190  {
191  $qPk = $this->mgr->getQuestionKey();
192  $answer = $this->answers[$question->$qPk];
193  return ($answer) ? $answer->value : "";
194  }

◆ getIndexedAnswers()

QuestionnaireForm::getIndexedAnswers ( )

Definition at line 418 of file questionnaire_form.inc.

419  {
420  $answers = $this->mgr->getAnswers();
421 
422  if(count($answers) > 0)
423  $answers = reindexList($answers, $this->mgr->getQuestionKey());
424 
425  return $answers;
426  }

◆ getValidationEngine()

QuestionnaireForm::getValidationEngine ( )

Definition at line 147 of file questionnaire_form.inc.

148  {
149  return $this;
150  }

◆ incrementArray()

static QuestionnaireForm::incrementArray (   $array)
static

Definition at line 481 of file questionnaire_form.inc.

482  {
483  if(count($array) > 0)
484  {
485  $idx = 0;
486  while($idx < (count($array)))
487  {
488  $newArray[$idx+1] = $array[$idx];
489  $idx++;
490  }
491  }
492  return $newArray;
493  }

◆ makeSubordinate()

QuestionnaireForm::makeSubordinate (   $subordinate = true)

Definition at line 142 of file questionnaire_form.inc.

143  {
144  $this->subordinate = $subordinate;
145  }

◆ QuestionnaireForm()

QuestionnaireForm::QuestionnaireForm (   $mgr,
  $readOnlyForm = false 
)

Definition at line 101 of file questionnaire_form.inc.

102  {
103  global $auto_form_defaults;
104 
105  $this->mgr = $mgr;
106  $this->readOnlyForm = $readOnlyForm;
107 
108  foreach($auto_form_defaults as $field => $value)
109  {
110  $this->$field = $value;
111  }
112  $this->requiredFieldsText = preg_replace("/field/i", "question", $this->requiredFieldsText);
113 
114  $questions = $this->mgr->getQuestions();
115 
116  if(count($questions) > 0)
117  {
118  $this->answers = $this->getIndexedAnswers();
119 
120  foreach($questions as $question)
121  {
122  $this->questions[] = QuestionField::create($this, $question);
123  }
124 
125  $num = 1;
126 
127  foreach($this->questions as $question)
128  {
129  if (!$question->skipNumbering)
130  {
131  $question->question->question_number = $num++;
132  }
133 
134  if($question->question->required)
135  {
136  array_push($this->validators, $question->getRequiredValidator());
137  }
138  }
139  }
140  }
static create($parent, $question)
$readOnlyForm
Specifies whether the entire form is read-only.

◆ save()

QuestionnaireForm::save ( )

Definition at line 444 of file questionnaire_form.inc.

445  {
446  $this->fromPOST();
447  $aPk = $this->mgr->getAnswerKey();
448  $qPk = $this->mgr->getQuestionKey();
449 
450  foreach($this->answers as $question_id => $answer)
451  {
452  // If saved answer, just update value
453  if($answer->$aPk)
454  {
455  $answer->filter = new InclusionFilter("value");
456  }
457  // new answer - save all fields
458  $answer->save();
459  }
460 
461  // Even if invalid, we still want to save their answers
462  $this->msg = $this->validate();
463  if ($this->msg != "")
464  {
465  return false;
466  }
467 
468  // onComplete event is fired once all processing has been completed
469  if ($this->onSaveComplete)
470  {
471  $onComplete = $this->onSaveComplete;
472  $onComplete($this);
473  }
474 
475  return true;
476 
477  }
fromPOST()
Put any updated or new answers in the $_POST array into the array of answers that are indexed by ques...
$onSaveComplete
Callback event handler that is fired after the AutoForm has finished saving data to the database.
$form onSaveComplete

◆ validate()

QuestionnaireForm::validate ( )

Definition at line 429 of file questionnaire_form.inc.

430  {
431  foreach($this->validators as $validator)
432  {
433  $msg = $validator->validate();
434  if ($msg)
435  {
436  $result .= $msg."<br>";
437  }
438  }
439  $this->msg = $msg;
440  return $result;
441  }
$validator
$result

◆ writeHTML()

QuestionnaireForm::writeHTML ( )

Reimplemented in QuizForm.

Definition at line 235 of file questionnaire_form.inc.

236  {
237  if(count($this->questions) == 0)
238  {
239  echo "There are no questions.<br/>";
240  return;
241  }
242 
243  if($this->requiredText)
244  {
245  if ($this->msg)
246  {
247  echo "<tr>\n <td colspan='2'><span class='error'>{$this->msg}</span</td></tr>\n";
248  $this->msg = "";
249  echo "</span>\n";
250  }
251  echo $this->requiredText;
252  }
253 
254  echo "<b>{$this->requiredFieldsText}</b><br/><br/>";
255 
256  echo "<dl>\n";
257 
258  foreach($this->questions as $question)
259  {
260  $this->writeOneQuestion($question);
261  }
262 
263  echo "</dl>\n";
264  }

◆ writeOneQuestion()

QuestionnaireForm::writeOneQuestion (   $question)

Reimplemented in QuizForm, QuestionnaireResponseView, and QuestionnairePreview.

Definition at line 266 of file questionnaire_form.inc.

267  {
268  $required = $question->getRequired();
269  $qPk = $question->getID();
270  $num = ($question->question->question_number) ? $question->question->question_number : $this->mgr->getQuestionNumber();
271 
272  echo " <dt><label for='question_{$qPk}'>";
273 
274  if (!$question->skipNumbering)
275  {
276  echo "<b>{$num}.</b> ";
277  }
278 
279  echo "{$question->question->question}{$required}</label></dt>\n";
280 
281  echo " <dd>";
282 
283  $question->writeHTML();
284 
285  echo " <br/></dd>";
286  }

◆ writeScript()

QuestionnaireForm::writeScript ( )

Reimplemented in QuestionnairePreview.

Definition at line 196 of file questionnaire_form.inc.

197  {
198  $script = "";
199 
200  if(count($this->questions) == 0)
201  return;
202 
203  foreach($this->questions as $question)
204  {
205  $script .= $question->writeScript();
206  }
207 
208 
209  $script .= "function validate_Questionnaire_form(form)\n";
210  $script .= "{";
211 
212  foreach($this->validators as $validator)
213  {
214  $script .= $validator->writeClient();
215  }
216 
217  $script .= "\n\n return true;\n}";
218  $script .= "\n\n";
219  $script .= "function onSubmitQuestionnaire_form(form)\n{\n";
220 
221  if ($this->allowIncompleteSave)
222  {
223  // return true because we need to save their answers, even if they
224  // didn't answer all questions (invalid form)
225  $script .= "\t validate_Questionnaire_form(form); return true\n}";
226  }
227  else
228  {
229  $script .= "\t return validate_Questionnaire_form(form);\n}";
230  }
231  if ($script) $script = "<script type='text/javascript'>\n$script\n</script>";
232  return $script;
233  }

Member Data Documentation

◆ $allowIncompleteSave

QuestionnaireForm::$allowIncompleteSave = true

Allows users to save partially completed forms.

Definition at line 99 of file questionnaire_form.inc.

◆ $answers

QuestionnaireForm::$answers

Definition at line 78 of file questionnaire_form.inc.

◆ $buttonCSS

QuestionnaireForm::$buttonCSS = "button"

CSS Class to use for buttons.

Definition at line 90 of file questionnaire_form.inc.

◆ $buttons

QuestionnaireForm::$buttons = array()

The custom buttons collection.

Definition at line 98 of file questionnaire_form.inc.

◆ $buttons_at_top

QuestionnaireForm::$buttons_at_top = false

Definition at line 95 of file questionnaire_form.inc.

◆ $checkboxCSS

QuestionnaireForm::$checkboxCSS = ""

CSS Class to use for checkbox fields.

Definition at line 92 of file questionnaire_form.inc.

◆ $id

QuestionnaireForm::$id

Definition at line 85 of file questionnaire_form.inc.

◆ $inputCSS

QuestionnaireForm::$inputCSS = ""

CSS Class to use for input fields.

Definition at line 91 of file questionnaire_form.inc.

◆ $labelCSS

QuestionnaireForm::$labelCSS = ""

CSS Class to use for label cells.

Definition at line 88 of file questionnaire_form.inc.

◆ $markRequiredFields

QuestionnaireForm::$markRequiredFields = false

Set to true to add an asterisk after required field labels.

Definition at line 93 of file questionnaire_form.inc.

◆ $mgr

QuestionnaireForm::$mgr

Definition at line 76 of file questionnaire_form.inc.

◆ $msg

QuestionnaireForm::$msg

Definition at line 82 of file questionnaire_form.inc.

◆ $onFormatLabel

QuestionnaireForm::$onFormatLabel = null

Label Processing hook.

Definition at line 94 of file questionnaire_form.inc.

◆ $onSaveComplete

QuestionnaireForm::$onSaveComplete = null

Callback event handler that is fired after the AutoForm has finished saving data to the database.

Definition at line 83 of file questionnaire_form.inc.

◆ $questions

QuestionnaireForm::$questions

Definition at line 77 of file questionnaire_form.inc.

◆ $readOnlyForm

QuestionnaireForm::$readOnlyForm

Specifies whether the entire form is read-only.

Definition at line 84 of file questionnaire_form.inc.

◆ $requiredFieldsText

QuestionnaireForm::$requiredFieldsText = "* indicates required question"

Definition at line 96 of file questionnaire_form.inc.

◆ $requiredText

QuestionnaireForm::$requiredText = "<b>You must answer all required questions.</b><br/>"

Definition at line 81 of file questionnaire_form.inc.

◆ $submitLabel

QuestionnaireForm::$submitLabel = "Save"

Definition at line 79 of file questionnaire_form.inc.

◆ $subordinate

QuestionnaireForm::$subordinate = false

Definition at line 97 of file questionnaire_form.inc.

◆ $validators

QuestionnaireForm::$validators = array()

Definition at line 80 of file questionnaire_form.inc.

◆ $valueCSS

QuestionnaireForm::$valueCSS = ""

CSS Class to use for field/value cells.

Definition at line 89 of file questionnaire_form.inc.


The documentation for this class was generated from the following file: