public function survey_print_render($message = '', $referer = '', $courseid, $rid = 0, $blankquestionnaire = false)
 {
     global $USER, $DB, $OUTPUT, $CFG;
     if (!($course = $DB->get_record("course", array("id" => $courseid)))) {
         print_error('incorrectcourseid', 'questionnaire');
     }
     $this->course = $course;
     if (!empty($rid)) {
         // If we're viewing a response, use this method.
         $this->view_response($rid, $referer, $blankquestionnaire);
         return;
     }
     if (empty($section)) {
         $section = 1;
     }
     if (isset($this->questionsbysec)) {
         $numsections = count($this->questionsbysec);
     } else {
         $numsections = 0;
     }
     if ($section > $numsections) {
         return false;
         // Invalid section.
     }
     $hasrequired = $this->has_required();
     // Find out what question number we are on $i.
     $i = 1;
     for ($j = 2; $j <= $section; $j++) {
         $i += count($this->questionsbysec[$j - 1]);
     }
     $action = $CFG->wwwroot . '/mod/questionnaire/preview.php?id=' . $this->cm->id;
     echo '<form id="phpesp_response" method="post" action="' . $action . '">
                             ';
     // Print all sections.
     $formdata = new stdClass();
     $errors = 1;
     if (data_submitted()) {
         $formdata = data_submitted();
         $pageerror = '';
         $s = 1;
         $errors = 0;
         foreach ($this->questionsbysec as $section) {
             $errormessage = $this->response_check_format($s, $formdata);
             if ($errormessage) {
                 if ($numsections > 1) {
                     $pageerror = get_string('page', 'questionnaire') . ' ' . $s . ' : ';
                 }
                 echo '<div class="notifyproblem">' . $pageerror . $errormessage . '</div>';
                 $errors++;
             }
             $s++;
         }
     }
     echo $OUTPUT->box_start();
     $this->print_survey_start($message, $section = 1, 1, $hasrequired, $rid = '');
     $descendantsandchoices = array();
     if ($referer == 'preview' && questionnaire_has_dependencies($this->questions)) {
         $descendantsandchoices = questionnaire_get_descendants_and_choices($this->questions);
     }
     if ($errors == 0) {
         echo '<div class="message">' . get_string('submitpreviewcorrect', 'questionnaire') . '</div>';
     }
     $page = 1;
     foreach ($this->questionsbysec as $section) {
         if ($numsections > 1) {
             echo '<div class="surveyPage">' . get_string('page', 'questionnaire') . ' ' . $page . '</div>';
             $page++;
         }
         foreach ($section as $question) {
             $descendantsdata = array();
             if ($question->type_id == QUESSECTIONTEXT) {
                 $i--;
             }
             if ($referer == 'preview' && $descendantsandchoices && ($question->type_id == QUESYESNO || $question->type_id == QUESRADIO || $question->type_id == QUESDROP)) {
                 if (isset($descendantsandchoices['descendants'][$question->id])) {
                     $descendantsdata['descendants'] = $descendantsandchoices['descendants'][$question->id];
                     $descendantsdata['choices'] = $descendantsandchoices['choices'][$question->id];
                 }
             }
             $question->survey_display($formdata, $descendantsdata, $i++, $usehtmleditor = null, $blankquestionnaire, $referer);
         }
     }
     // End of questions.
     if ($referer == 'preview' && !$blankquestionnaire) {
         $url = $CFG->wwwroot . '/mod/questionnaire/preview.php?id=' . $this->cm->id;
         echo '
                 <div>
                     <input type="submit" name="submit" value="' . get_string('submitpreview', 'questionnaire') . '" />
                     <a href="' . $url . '">' . get_string('reset') . '</a>
                 </div>
             ';
     }
     echo $OUTPUT->box_end();
     return;
 }
 public function definition()
 {
     global $CFG, $questionnaire, $SESSION, $OUTPUT;
     global $DB;
     $sid = $questionnaire->survey->id;
     $mform =& $this->_form;
     $mform->addElement('header', 'questionhdr', get_string('addquestions', 'questionnaire'));
     $mform->addHelpButton('questionhdr', 'questiontypes', 'questionnaire');
     $strremove = get_string('remove', 'questionnaire');
     $strmove = get_string('move');
     $strmovehere = get_string('movehere');
     $strposition = get_string('position', 'questionnaire');
     if (!isset($questionnaire->questions)) {
         $questionnaire->questions = array();
     }
     if ($this->moveq) {
         $moveqposition = $questionnaire->questions[$this->moveq]->position;
     }
     $pos = 0;
     $select = '';
     if (!($qtypes = $DB->get_records_select_menu('questionnaire_question_type', $select, null, '', 'typeid,type'))) {
         $qtypes = array();
     }
     // Get the names of each question type in the appropriate language.
     foreach ($qtypes as $key => $qtype) {
         // Do not allow "Page Break" to be selected as first element of a Questionnaire.
         if (empty($questionnaire->questions) && $qtype == 'Page Break') {
             unset($qtypes[$key]);
         } else {
             $qtypes[$key] = questionnaire_get_type($key);
         }
     }
     natsort($qtypes);
     $addqgroup = array();
     $addqgroup[] =& $mform->createElement('select', 'type_id', '', $qtypes);
     // The 'sticky' type_id value for further new questions.
     if (isset($SESSION->questionnaire->type_id)) {
         $mform->setDefault('type_id', $SESSION->questionnaire->type_id);
     }
     $addqgroup[] =& $mform->createElement('submit', 'addqbutton', get_string('addselqtype', 'questionnaire'));
     $questionnairehasdependencies = questionnaire_has_dependencies($questionnaire->questions);
     $mform->addGroup($addqgroup, 'addqgroup', '', ' ', false);
     if (isset($SESSION->questionnaire->validateresults) && $SESSION->questionnaire->validateresults != '') {
         $mform->addElement('static', 'validateresult', '', '<div class="qdepend warning">' . $SESSION->questionnaire->validateresults . '</div>');
         $SESSION->questionnaire->validateresults = '';
     }
     $qnum = 0;
     // JR skip logic :: to prevent moving child higher than parent OR parent lower than child
     // we must get now the parent and child positions.
     if ($questionnairehasdependencies) {
         $parentpositions = questionnaire_get_parent_positions($questionnaire->questions);
         $childpositions = questionnaire_get_child_positions($questionnaire->questions);
     }
     $mform->addElement('header', 'manageq', get_string('managequestions', 'questionnaire'));
     $mform->addHelpButton('manageq', 'managequestions', 'questionnaire');
     $mform->addElement('html', '<div class="qcontainer">');
     foreach ($questionnaire->questions as $question) {
         $manageqgroup = array();
         $qid = $question->id;
         $tid = $question->type_id;
         $qtype = $question->type;
         $required = $question->required;
         // Does this questionnaire contain branching questions already?
         $dependency = '';
         if ($questionnairehasdependencies) {
             if ($question->dependquestion != 0) {
                 $parent = questionnaire_get_parent($question);
                 $dependency = '<strong>' . get_string('dependquestion', 'questionnaire') . '</strong> : ' . $strposition . ' ' . $parent[$qid]['parentposition'] . ' (' . $parent[$qid]['parent'] . ')';
             }
         }
         $pos = $question->position;
         // No page break in first position!
         if ($tid == QUESPAGEBREAK && $pos == 1) {
             $DB->set_field('questionnaire_question', 'deleted', 'y', array('id' => $qid, 'survey_id' => $sid));
             if ($records = $DB->get_records_select('questionnaire_question', $select, null, 'position ASC')) {
                 foreach ($records as $record) {
                     $DB->set_field('questionnaire_question', 'position', $record->position - 1, array('id' => $record->id));
                 }
             }
             redirect($CFG->wwwroot . '/mod/questionnaire/questions.php?id=' . $questionnaire->cm->id);
         }
         if ($tid != QUESPAGEBREAK && $tid != QUESSECTIONTEXT) {
             $qnum++;
         }
         // Needed for non-English languages JR.
         $qtype = '[' . questionnaire_get_type($tid) . ']';
         $content = '';
         // If question text is "empty", i.e. 2 non-breaking spaces were inserted, do not display any question text.
         if ($question->content == '<p>  </p>') {
             $question->content = '';
         }
         if ($tid != QUESPAGEBREAK) {
             // Needed to print potential media in question text.
             $content = format_text(file_rewrite_pluginfile_urls($question->content, 'pluginfile.php', $question->context->id, 'mod_questionnaire', 'question', $question->id), FORMAT_HTML);
         }
         $moveqgroup = array();
         $spacer = $OUTPUT->pix_url('spacer');
         if (!$this->moveq) {
             $mform->addElement('html', '<div class="qn-container">');
             // Begin div qn-container.
             $mextra = array('value' => $question->id, 'alt' => $strmove, 'title' => $strmove);
             $eextra = array('value' => $question->id, 'alt' => get_string('edit', 'questionnaire'), 'title' => get_string('edit', 'questionnaire'));
             $rextra = array('value' => $question->id, 'alt' => $strremove, 'title' => $strremove);
             if ($tid == QUESPAGEBREAK) {
                 $esrc = $CFG->wwwroot . '/mod/questionnaire/images/editd.gif';
                 $eextra = array('disabled' => 'disabled');
             } else {
                 $esrc = $CFG->wwwroot . '/mod/questionnaire/images/edit.gif';
             }
             if ($tid == QUESPAGEBREAK) {
                 $esrc = $spacer;
                 $eextra = array('disabled' => 'disabled');
             } else {
                 $esrc = $OUTPUT->pix_url('t/edit');
             }
             $rsrc = $OUTPUT->pix_url('t/delete');
             // Question numbers.
             $manageqgroup[] =& $mform->createElement('static', 'qnums', '', '<div class="qnums">' . $strposition . ' ' . $pos . '</div>');
             // Need to index by 'id' since IE doesn't return assigned 'values' for image inputs.
             $manageqgroup[] =& $mform->createElement('static', 'opentag_' . $question->id, '', '');
             $msrc = $OUTPUT->pix_url('t/move');
             if ($questionnairehasdependencies) {
                 // Do not allow moving parent question at position #1 to be moved down if it has a child at position < 4.
                 if ($pos == 1) {
                     if (isset($childpositions[$qid])) {
                         $maxdown = $childpositions[$qid];
                         if ($maxdown < 4) {
                             $strdisabled = get_string('movedisabled', 'questionnaire');
                             $msrc = $OUTPUT->pix_url('t/block');
                             $mextra = array('value' => $question->id, 'alt' => $strdisabled, 'title' => $strdisabled);
                             $mextra += array('disabled' => 'disabled');
                         }
                     }
                 }
                 // Do not allow moving or deleting a page break if immediately followed by a child question
                 // or immediately preceded by a question with a dependency and followed by a non-dependent question.
                 if ($tid == QUESPAGEBREAK) {
                     if ($nextquestion = $DB->get_record('questionnaire_question', array('survey_id' => $sid, 'position' => $pos + 1, 'deleted' => 'n'), $fields = 'dependquestion, name, content')) {
                         if ($previousquestion = $DB->get_record('questionnaire_question', array('survey_id' => $sid, 'position' => $pos - 1, 'deleted' => 'n'), $fields = 'dependquestion, name, content')) {
                             if ($nextquestion->dependquestion != 0 || $previousquestion->dependquestion != 0 && $nextquestion->dependquestion == 0) {
                                 $strdisabled = get_string('movedisabled', 'questionnaire');
                                 $msrc = $OUTPUT->pix_url('t/block');
                                 $mextra = array('value' => $question->id, 'alt' => $strdisabled, 'title' => $strdisabled);
                                 $mextra += array('disabled' => 'disabled');
                                 $rsrc = $msrc;
                                 $strdisabled = get_string('deletedisabled', 'questionnaire');
                                 $rextra = array('value' => $question->id, 'alt' => $strdisabled, 'title' => $strdisabled);
                                 $rextra += array('disabled' => 'disabled');
                             }
                         }
                     }
                 }
             }
             $manageqgroup[] =& $mform->createElement('image', 'movebutton[' . $question->id . ']', $msrc, $mextra);
             $manageqgroup[] =& $mform->createElement('image', 'editbutton[' . $question->id . ']', $esrc, $eextra);
             $manageqgroup[] =& $mform->createElement('image', 'removebutton[' . $question->id . ']', $rsrc, $rextra);
             if ($tid != QUESPAGEBREAK && $tid != QUESSECTIONTEXT) {
                 if ($required == 'y') {
                     $reqsrc = $OUTPUT->pix_url('t/stop');
                     $strrequired = get_string('required', 'questionnaire');
                 } else {
                     $reqsrc = $OUTPUT->pix_url('t/go');
                     $strrequired = get_string('notrequired', 'questionnaire');
                 }
                 $strrequired .= ' ' . get_string('clicktoswitch', 'questionnaire');
                 $reqextra = array('value' => $question->id, 'alt' => $strrequired, 'title' => $strrequired);
                 $manageqgroup[] =& $mform->createElement('image', 'requiredbutton[' . $question->id . ']', $reqsrc, $reqextra);
             }
             $manageqgroup[] =& $mform->createElement('static', 'closetag_' . $question->id, '', '');
         } else {
             $manageqgroup[] =& $mform->createElement('static', 'qnum', '', '<div class="qnums">' . $strposition . ' ' . $pos . '</div>');
             $moveqgroup[] =& $mform->createElement('static', 'qnum', '', '');
             $display = true;
             if ($questionnairehasdependencies) {
                 // Prevent moving child to higher position than its parent.
                 if (isset($parentpositions[$this->moveq])) {
                     $maxup = $parentpositions[$this->moveq];
                     if ($pos <= $maxup) {
                         $display = false;
                     }
                 }
                 // Prevent moving parent to lower position than its (first) child.
                 if (isset($childpositions[$this->moveq])) {
                     $maxdown = $childpositions[$this->moveq];
                     if ($pos >= $maxdown) {
                         $display = false;
                     }
                 }
             }
             $typeid = $DB->get_field('questionnaire_question', 'type_id', array('id' => $this->moveq));
             if ($display) {
                 // Do not move a page break to first position.
                 if ($typeid == QUESPAGEBREAK && $pos == 1) {
                     $manageqgroup[] =& $mform->createElement('static', 'qnums', '', '');
                 } else {
                     if ($this->moveq == $question->id) {
                         $moveqgroup[] =& $mform->createElement('cancel', 'cancelbutton', get_string('cancel'));
                     } else {
                         $mextra = array('value' => $question->id, 'alt' => $strmove, 'title' => $strmovehere . ' (position ' . $pos . ')');
                         $msrc = $OUTPUT->pix_url('movehere');
                         $moveqgroup[] =& $mform->createElement('static', 'opentag_' . $question->id, '', '');
                         $moveqgroup[] =& $mform->createElement('image', 'moveherebutton[' . $pos . ']', $msrc, $mextra);
                         $moveqgroup[] =& $mform->createElement('static', 'closetag_' . $question->id, '', '');
                     }
                 }
             } else {
                 $manageqgroup[] =& $mform->createElement('static', 'qnums', '', '');
                 $moveqgroup[] =& $mform->createElement('static', 'qnums', '', '');
             }
         }
         if ($question->name) {
             $qname = '(' . $question->name . ')';
         } else {
             $qname = '';
         }
         $manageqgroup[] =& $mform->createElement('static', 'qinfo_' . $question->id, '', $qtype . ' ' . $qname);
         if ($dependency) {
             $mform->addElement('static', 'qdepend_' . $question->id, '', '<div class="qdepend">' . $dependency . '</div>');
         }
         if ($tid != QUESPAGEBREAK) {
             if ($tid != QUESSECTIONTEXT) {
                 $qnumber = '<div class="qn-info"><h2 class="qn-number">' . $qnum . '</h2></div>';
             } else {
                 $qnumber = '';
             }
         }
         if ($this->moveq && $pos < $moveqposition) {
             $mform->addGroup($moveqgroup, 'moveqgroup', '', '', false);
         }
         if ($this->moveq) {
             if ($this->moveq == $question->id && $display) {
                 $mform->addElement('html', '<div class="moving" title="' . $strmove . '">');
                 // Begin div qn-container.
             } else {
                 $mform->addElement('html', '<div class="qn-container">');
                 // Begin div qn-container.
             }
         }
         $mform->addGroup($manageqgroup, 'manageqgroup', '', '&nbsp;', false);
         if ($tid != QUESPAGEBREAK) {
             $mform->addElement('static', 'qcontent_' . $question->id, '', $qnumber . '<div class="qn-question">' . $content . '</div>');
         }
         $mform->addElement('html', '</div>');
         // End div qn-container.
         if ($this->moveq && $pos >= $moveqposition) {
             $mform->addGroup($moveqgroup, 'moveqgroup', '', '', false);
         }
     }
     if ($this->moveq) {
         $mform->addElement('hidden', 'moveq', $this->moveq);
     }
     // Hidden fields.
     $mform->addElement('hidden', 'id', 0);
     $mform->setType('id', PARAM_INT);
     $mform->addElement('hidden', 'sid', 0);
     $mform->setType('sid', PARAM_INT);
     $mform->addElement('hidden', 'action', 'main');
     $mform->setType('action', PARAM_RAW);
     $mform->setType('moveq', PARAM_RAW);
     $mform->addElement('html', '</div>');
 }
 /**
  * Create and update question data from the forms.
  */
 public function form_update($formdata, $questionnaire)
 {
     global $DB;
     $this->form_preprocess_data($formdata);
     if (!empty($formdata->qid)) {
         // Update existing question.
         // Handle any attachments in the content.
         $formdata->itemid = $formdata->content['itemid'];
         $formdata->format = $formdata->content['format'];
         $formdata->content = $formdata->content['text'];
         $formdata->content = file_save_draft_area_files($formdata->itemid, $questionnaire->context->id, 'mod_questionnaire', 'question', $formdata->qid, array('subdirs' => true), $formdata->content);
         $fields = array('name', 'type_id', 'length', 'precise', 'required', 'content', 'dependquestion', 'dependchoice');
         $questionrecord = new \stdClass();
         $questionrecord->id = $formdata->qid;
         foreach ($fields as $f) {
             if (isset($formdata->{$f})) {
                 $questionrecord->{$f} = trim($formdata->{$f});
             }
         }
         $result = $this->update($questionrecord, false);
         if (questionnaire_has_dependencies($questionnaire->questions)) {
             questionnaire_check_page_breaks($questionnaire);
         }
     } else {
         // Create new question:
         // Need to update any image content after the question is created, so create then update the content.
         $formdata->survey_id = $formdata->sid;
         $fields = array('survey_id', 'name', 'type_id', 'length', 'precise', 'required', 'position', 'dependquestion', 'dependchoice');
         $questionrecord = new \stdClass();
         foreach ($fields as $f) {
             if (isset($formdata->{$f})) {
                 $questionrecord->{$f} = trim($formdata->{$f});
             }
         }
         $questionrecord->content = '';
         $this->add($questionrecord);
         // Handle any attachments in the content.
         $formdata->itemid = $formdata->content['itemid'];
         $formdata->format = $formdata->content['format'];
         $formdata->content = $formdata->content['text'];
         $content = file_save_draft_area_files($formdata->itemid, $questionnaire->context->id, 'mod_questionnaire', 'question', $this->qid, array('subdirs' => true), $formdata->content);
         $result = $DB->set_field('questionnaire_question', 'content', $content, array('id' => $this->qid));
     }
     if ($this->has_choices()) {
         // Now handle any choice updates.
         $cidx = 0;
         if (isset($this->choices) && !isset($formdata->makecopy)) {
             $oldcount = count($this->choices);
             $echoice = reset($this->choices);
             $ekey = key($this->choices);
         } else {
             $oldcount = 0;
         }
         $newchoices = explode("\n", $formdata->allchoices);
         $nidx = 0;
         $newcount = count($newchoices);
         while ($nidx < $newcount && $cidx < $oldcount) {
             if ($newchoices[$nidx] != $echoice->content) {
                 $choicerecord = new \stdClass();
                 $choicerecord->id = $ekey;
                 $choicerecord->question_id = $this->qid;
                 $choicerecord->content = trim($newchoices[$nidx]);
                 $r = preg_match_all("/^(\\d{1,2})(=.*)\$/", $newchoices[$nidx], $matches);
                 // This choice has been attributed a "score value" OR this is a rate question type.
                 if ($r) {
                     $newscore = $matches[1][0];
                     $choicerecord->value = $newscore;
                 } else {
                     // No score value for this choice.
                     $choicerecord->value = null;
                 }
                 $this->update_choice($choicerecord);
             }
             $nidx++;
             $echoice = next($this->choices);
             $ekey = key($this->choices);
             $cidx++;
         }
         while ($nidx < $newcount) {
             // New choices...
             $choicerecord = new \stdClass();
             $choicerecord->question_id = $this->qid;
             $choicerecord->content = trim($newchoices[$nidx]);
             $r = preg_match_all("/^(\\d{1,2})(=.*)\$/", $choicerecord->content, $matches);
             // This choice has been attributed a "score value" OR this is a rate question type.
             if ($r) {
                 $choicerecord->value = $matches[1][0];
             }
             $this->add_choice($choicerecord);
             $nidx++;
         }
         while ($cidx < $oldcount) {
             end($this->choices);
             $ekey = key($this->choices);
             $this->delete_choice($ekey);
             $cidx++;
         }
     }
 }
Пример #4
0
             }
             while ($cidx < $oldcount) {
                 $result = $DB->delete_records('questionnaire_quest_choice', array('id' => $ekey));
                 $echoice = next($question->choices);
                 $ekey = key($question->choices);
                 $cidx++;
             }
         }
         // Make these field values 'sticky' for further new questions.
         if (!isset($qformdata->required)) {
             $qformdata->required = 'n';
         }
         // Need to reload questions.
         $questions = $DB->get_records('questionnaire_question', array('survey_id' => $sid, 'deleted' => 'n'), 'id');
         $questionnairehasdependencies = questionnaire_has_dependencies($questions);
         if (questionnaire_has_dependencies($questions)) {
             questionnaire_check_page_breaks($questionnaire);
         }
         $SESSION->questionnaire->required = $qformdata->required;
         $SESSION->questionnaire->type_id = $qformdata->type_id;
         // Switch to main screen.
         $action = 'main';
         $reload = true;
     }
 }
 // Log question created event.
 if (isset($qformdata)) {
     $context = context_module::instance($questionnaire->cm->id);
     $questiontype = $qtypenames[$qformdata->type_id];
     $params = array('context' => $context, 'courseid' => $questionnaire->course->id, 'other' => array('questiontype' => $questiontype));
     $event = \mod_questionnaire\event\question_created::create($params);