generateform.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once('../templates/templates.php');
  3. $cacheQuestions = array();
  4. $cacheChoices = array();
  5. function loadQuestionTemplate ($type) {
  6. global $cacheQuestions;
  7. if (!isset($cacheQuestions[$type])) {
  8. $cacheQuestions[$type] = getTemplate(strtolower("question_$type.html"));
  9. }
  10. return $cacheQuestions[$type];
  11. }
  12. function loadChoicesTemplate ($type) {
  13. global $cacheChoices;
  14. if (!isset($cacheChoices[$type])) {
  15. $cacheChoices[$type] = getTemplate(strtolower("choice_$type.html"));
  16. }
  17. return $cacheChoices[$type];
  18. }
  19. function generateFormHTML ($form) {
  20. $pageCount = 1;
  21. $formText = "";
  22. $content = "";
  23. $questions = $form['questions'];
  24. $pageTemplate = getTemplate('form_page.html');
  25. foreach ($questions as $question) {
  26. if ($question->type === 'PAGEBREAK') {
  27. if (strlen($content) > 0) {
  28. $context = array('page_number'=>$pageCount,'questions'=>$content);
  29. $formText = $formText . parseTemplate($pageTemplate, $context);
  30. $pageCount++;
  31. $content = "";
  32. }
  33. continue;
  34. }
  35. $questionTemplate = loadQuestionTemplate($question->type);
  36. $required = "";
  37. if (property_exists($question,'required')) {
  38. $required = "required";
  39. }
  40. $context = array('id'=>$question->number,'text'=>$question->text, 'required'=>$required);
  41. if (in_array($question->type, array('S','M'))) {
  42. $context['choices'] = generateChoices($question);
  43. }
  44. $content = $content . parseTemplate($questionTemplate, $context);
  45. }
  46. if (strlen($content) > 0) {
  47. $context = array('page_number'=>$pageCount,'questions'=>$content);
  48. $formText = $formText . parseTemplate($pageTemplate, $context);
  49. }
  50. $formTemplate = getTemplate('form.html');
  51. $context = ['title'=>$form['title'], 'description'=>$form['description'], 'pages'=>$formText, 'form_id'=>$form['id']];
  52. return parseTemplate($formTemplate, $context);
  53. }
  54. function generateChoices ($question) {
  55. $choices = $question->choices;
  56. $content = "";
  57. $template = loadChoicesTemplate($question->type);
  58. for ($i = 0; $i < count($choices); $i++) {
  59. $choice = $choices[$i];
  60. $context = ['id' => $question->number, 'count' => $i, 'value'=>$choice->value, 'text'=>$choice->text];
  61. $content = $content . parseTemplate($template, $context);
  62. }
  63. return $content;
  64. }