protected function question_survey_display($data, $descendantsdata, $blankquestionnaire = false) { // Date. $datemess = html_writer::start_tag('div', array('class' => 'qn-datemsg')); $datemess .= get_string('dateformatting', 'questionnaire'); $datemess .= html_writer::end_tag('div'); if (!empty($data->{'q' . $this->id})) { $dateentered = $data->{'q' . $this->id}; $setdate = questionnaire_check_date($dateentered, false); if ($setdate == 'wrongdateformat') { $msg = get_string('wrongdateformat', 'questionnaire', $dateentered); questionnaire_notify($msg); } else { if ($setdate == 'wrongdaterange') { $msg = get_string('wrongdaterange', 'questionnaire'); questionnaire_notify($msg); } else { $data->{'q' . $this->id} = $setdate; } } } echo $datemess; echo html_writer::start_tag('div', array('class' => 'qn-date')); echo '<input onkeypress="return event.keyCode != 13;" type="text" size="12" name="q' . $this->id . '" maxlength="10" value="' . (isset($data->{'q' . $this->id}) ? $data->{'q' . $this->id} : '') . '" />'; echo html_writer::end_tag('div'); }
protected function question_survey_display($data, $descendantsdata, $blankquestionnaire = false) { // Numeric. $precision = $this->precise; $a = ''; if (isset($data->{'q' . $this->id})) { $mynumber = $data->{'q' . $this->id}; if ($mynumber != '') { $mynumber0 = $mynumber; if (!is_numeric($mynumber)) { $msg = get_string('notanumber', 'questionnaire', $mynumber); questionnaire_notify($msg); } else { if ($precision) { $pos = strpos($mynumber, '.'); if (!$pos) { if (strlen($mynumber) > $this->length) { $mynumber = substr($mynumber, 0, $this->length); } } $this->length += 1 + $precision; // To allow for n numbers after decimal point. } $mynumber = number_format($mynumber, $precision, '.', ''); if ($mynumber != $mynumber0) { $a->number = $mynumber0; $a->precision = $precision; $msg = get_string('numberfloat', 'questionnaire', $a); questionnaire_notify($msg); } } } if ($mynumber != '') { $data->{'q' . $this->id} = $mynumber; } } echo '<input onkeypress="return event.keyCode != 13;" type="text" size="' . $this->length . '" name="q' . $this->id . '" maxlength="' . $this->length . '" value="' . (isset($data->{'q' . $this->id}) ? $data->{'q' . $this->id} : '') . '" id="' . $this->type . $this->id . '" />'; }
public function view() { global $CFG, $USER, $PAGE, $OUTPUT; $PAGE->set_title(format_string($this->name)); $PAGE->set_heading(format_string($this->course->fullname)); // Initialise the JavaScript. $PAGE->requires->js_init_call('M.mod_questionnaire.init_attempt_form', null, false, questionnaire_get_js_module()); echo $OUTPUT->header(); $questionnaire = $this; if (!$this->cm->visible && !$this->capabilities->viewhiddenactivities) { notice(get_string("activityiscurrentlyhidden")); } if (!$this->capabilities->view) { echo '<br/>'; questionnaire_notify(get_string("noteligible", "questionnaire", $this->name)); echo '<div><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $this->course->id . '">' . get_string("continue") . '</a></div>'; exit; } // Print the main part of the page. if (!$this->is_active()) { echo '<div class="notifyproblem">' . get_string('notavail', 'questionnaire') . '</div>'; } else { if (!$this->is_open()) { echo '<div class="notifyproblem">' . get_string('notopen', 'questionnaire', userdate($this->opendate)) . '</div>'; } else { if ($this->is_closed()) { echo '<div class="notifyproblem">' . get_string('closed', 'questionnaire', userdate($this->closedate)) . '</div>'; } else { if (!$this->user_is_eligible($USER->id)) { echo '<div class="notifyproblem">' . get_string('noteligible', 'questionnaire') . '</div>'; } else { if ($this->user_can_take($USER->id)) { $quser = $USER->id; if ($this->survey->realm == 'template') { print_string('templatenotviewable', 'questionnaire'); echo $OUTPUT->footer($this->course); exit; } $msg = $this->print_survey($USER->id, $quser); // If Questionnaire was submitted with all required fields completed ($msg is empty), // then record the submittal. $viewform = data_submitted($CFG->wwwroot . "/mod/questionnaire/complete.php"); if (!empty($viewform->rid)) { $viewform->rid = (int) $viewform->rid; } if (!empty($viewform->sec)) { $viewform->sec = (int) $viewform->sec; } if (data_submitted() && confirm_sesskey() && isset($viewform->submit) && isset($viewform->submittype) && $viewform->submittype == "Submit Survey" && empty($msg)) { $this->response_delete($viewform->rid, $viewform->sec); $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser); $this->response_commit($this->rid); // If it was a previous save, rid is in the form... if (!empty($viewform->rid) && is_numeric($viewform->rid)) { $rid = $viewform->rid; // Otherwise its in this object. } else { $rid = $this->rid; } questionnaire_record_submission($this, $USER->id, $rid); if ($this->grade != 0) { $questionnaire = new stdClass(); $questionnaire->id = $this->id; $questionnaire->name = $this->name; $questionnaire->grade = $this->grade; $questionnaire->cmidnumber = $this->cm->idnumber; $questionnaire->courseid = $this->course->id; questionnaire_update_grades($questionnaire, $quser); } // Update completion state. $completion = new completion_info($this->course); if ($completion->is_enabled($this->cm) && $this->completionsubmit) { $completion->update_state($this->cm, COMPLETION_COMPLETE); } // Log this submitted response. $context = context_module::instance($this->cm->id); $anonymous = $this->respondenttype == 'anonymous'; $params = array('context' => $context, 'courseid' => $this->course->id, 'relateduserid' => $USER->id, 'anonymous' => $anonymous, 'other' => array('questionnaireid' => $questionnaire->id)); $event = \mod_questionnaire\event\attempt_submitted::create($params); $event->trigger(); $this->response_send_email($this->rid); $this->response_goto_thankyou(); } } else { switch ($this->qtype) { case QUESTIONNAIREDAILY: $msgstring = ' ' . get_string('today', 'questionnaire'); break; case QUESTIONNAIREWEEKLY: $msgstring = ' ' . get_string('thisweek', 'questionnaire'); break; case QUESTIONNAIREMONTHLY: $msgstring = ' ' . get_string('thismonth', 'questionnaire'); break; default: $msgstring = ''; break; } echo '<div class="notifyproblem">' . get_string("alreadyfilled", "questionnaire", $msgstring) . '</div>'; } } } } } // Finish the page. echo $OUTPUT->footer($this->course); }
protected function question_survey_display($data, $descendantsdata, $blankquestionnaire = false) { $disabled = ''; if ($blankquestionnaire) { $disabled = ' disabled="disabled"'; } if (!empty($data) && (!isset($data->{'q' . $this->id}) || !is_array($data->{'q' . $this->id}))) { $data->{'q' . $this->id} = array(); } $isna = $this->precise == 1; $osgood = $this->precise == 3; // Check if rate question has one line only to display full width columns of choices. $nocontent = false; $nameddegrees = 0; $n = array(); $v = array(); $mods = array(); $maxndlen = 0; foreach ($this->choices as $cid => $choice) { $content = $choice->content; if (!$nocontent && $content == '') { $nocontent = true; } // Check for number from 1 to 3 digits, followed by the equal sign = (to accomodate named degrees). if (preg_match("/^([0-9]{1,3})=(.*)\$/", $content, $ndd)) { $n[$nameddegrees] = format_text($ndd[2], FORMAT_HTML); if (strlen($n[$nameddegrees]) > $maxndlen) { $maxndlen = strlen($n[$nameddegrees]); } $v[$nameddegrees] = $ndd[1]; $this->choices[$cid] = ''; $nameddegrees++; } else { $contents = questionnaire_choice_values($content); if ($contents->modname) { $choice->content = $contents->text; } } } // The 0.1% right margin is needed to avoid the horizontal scrollbar in Chrome! // A one-line rate question (no content) does not need to span more than 50%. $width = $nocontent ? "50%" : "99.9%"; echo '<table style="width:' . $width . '">'; echo '<tbody>'; echo '<tr>'; // If Osgood, adjust central columns to width of named degrees if any. if ($osgood) { if ($maxndlen < 4) { $width = '45%'; } else { if ($maxndlen < 13) { $width = '40%'; } else { $width = '30%'; } } $nn = 100 - $width * 2; $colwidth = $nn / $this->length . '%'; $textalign = 'right'; } else { if ($nocontent) { $width = '0%'; $colwidth = 100 / $this->length . '%'; $textalign = 'right'; } else { $width = '59%'; $colwidth = 40 / $this->length . '%'; $textalign = 'left'; } } echo '<td style="width: ' . $width . '"></td>'; if ($isna) { $na = get_string('notapplicable', 'questionnaire'); } else { $na = ''; } if ($this->precise == 2) { $order = ' onclick="other_rate_uncheck(name, value)" '; } else { $order = ''; } if ($this->precise != 2) { $nbchoices = count($this->choices) - $nameddegrees; } else { // If "No duplicate choices", can restrict nbchoices to number of rate items specified. $nbchoices = $this->length; } // Display empty td for Not yet answered column. if ($nbchoices > 1 && $this->precise != 2 && !$blankquestionnaire) { echo '<td></td>'; } for ($j = 0; $j < $this->length; $j++) { if (isset($n[$j])) { $str = $n[$j]; $val = $v[$j]; } else { $str = $j + 1; $val = $j + 1; } if ($blankquestionnaire) { $val = '<br />(' . $val . ')'; } else { $val = ''; } echo '<td style="width:' . $colwidth . '; text-align:center;" class="smalltext">' . $str . $val . '</td>'; } if ($na) { echo '<td style="width:' . $colwidth . '; text-align:center;" class="smalltext">' . $na . '</td>'; } echo '</tr>'; $num = 0; foreach ($this->choices as $cid => $choice) { $str = 'q' . "{$this->id}_{$cid}"; $num += isset($data->{$str}) && $data->{$str} != -999; } $notcomplete = false; if ($num != $nbchoices && $num != 0) { questionnaire_notify(get_string('checkallradiobuttons', 'questionnaire', $nbchoices)); $notcomplete = true; } $row = 0; foreach ($this->choices as $cid => $choice) { if (isset($choice->content)) { $row++; $str = 'q' . "{$this->id}_{$cid}"; echo '<tr class="raterow">'; $content = $choice->content; if ($osgood) { list($content, $contentright) = array_merge(preg_split('/[|]/', $content), array(' ')); } echo '<td style="text-align: ' . $textalign . ';">' . format_text($content, FORMAT_HTML) . ' </td>'; $bg = 'c0 raterow'; if ($nbchoices > 1 && $this->precise != 2 && !$blankquestionnaire) { $checked = ' checked="checked"'; $completeclass = 'notanswered'; $title = ''; if ($notcomplete && isset($data->{$str}) && $data->{$str} == -999) { $completeclass = 'notcompleted'; $title = get_string('pleasecomplete', 'questionnaire'); } // Set value of notanswered button to -999 in order to eliminate it from form submit later on. echo '<td title="' . $title . '" class="' . $completeclass . '" style="width:1%;"><input name="' . $str . '" type="radio" value="-999" ' . $checked . $order . ' /></td>'; } for ($j = 0; $j < $this->length + $isna; $j++) { $checked = isset($data->{$str}) && ($j == $data->{$str} || $j == $this->length && $data->{$str} == -1) ? ' checked="checked"' : ''; $checked = ''; if (isset($data->{$str}) && ($j == $data->{$str} || $j == $this->length && $data->{$str} == -1)) { $checked = ' checked="checked"'; } echo '<td style="text-align:center" class="' . $bg . '">'; $i = $j + 1; echo html_writer::tag('span', get_string('option', 'questionnaire', $i), array('class' => 'accesshide')); // If isna column then set na choice to -1 value. $value = $j < $this->length ? $j : -1; echo '<input name="' . $str . '" type="radio" value="' . $value . '"' . $checked . $disabled . $order . ' id="' . $str . '_' . $value . '" />' . '<label for="' . $str . '_' . $value . '" class="accesshide">Choice ' . $i . ' for row ' . $row . '</label></td>'; if ($bg == 'c0 raterow') { $bg = 'c1 raterow'; } else { $bg = 'c0 raterow'; } } if ($osgood) { echo '<td> ' . format_text($contentright, FORMAT_HTML) . '</td>'; } echo '</tr>'; } } echo '</tbody>'; echo '</table>'; }
protected function question_survey_display($data, $descendantsdata, $blankquestionnaire = false) { // Radio buttons global $idcounter; // To make sure all radio buttons have unique ids. // JR 20 NOV 2007. $otherempty = false; $output = ''; // Find out which radio button is checked (if any); yields choice ID. if (isset($data->{'q' . $this->id})) { $checked = $data->{'q' . $this->id}; } else { $checked = ''; } $horizontal = $this->length; $ischecked = false; // To display or hide dependent questions on Preview page. $onclickdepend = array(); if ($descendantsdata) { $descendants = implode(',', $descendantsdata['descendants']); foreach ($descendantsdata['choices'] as $key => $choice) { $choices[$key] = implode(',', $choice); $onclickdepend[$key] = ' onclick="depend(\'' . $descendants . '\', \'' . $choices[$key] . '\')"'; } } // End dependents. foreach ($this->choices as $id => $choice) { $other = strpos($choice->content, '!other'); if ($horizontal) { $output .= ' <span style="white-space:nowrap;">'; } // To display or hide dependent questions on Preview page. $onclick = ''; if ($onclickdepend) { if (isset($onclickdepend[$id])) { $onclick = $onclickdepend[$id]; } else { // In case this dependchoice is not used by any child question. $onclick = ' onclick="depend(\'' . $descendants . '\', \'\')"'; } } else { $onclick = ' onclick="other_check_empty(name, value)"'; } // End dependents. if ($other !== 0) { // This is a normal radio button. $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter); $output .= '<input name="q' . $this->id . '" id="' . $htmlid . '" type="radio" value="' . $id . '"' . $onclick; if ($id == $checked) { $output .= ' checked="checked"'; $ischecked = true; } $value = ''; if ($blankquestionnaire) { $output .= ' disabled="disabled"'; $value = ' (' . $choice->value . ') '; } $content = $choice->content; $contents = questionnaire_choice_values($choice->content); $output .= ' /><label for="' . $htmlid . '" >' . $value . format_text($contents->text, FORMAT_HTML) . $contents->image . '</label>'; } else { // Radio button with associated !other text field. $othertext = preg_replace(array("/^!other=/", "/^!other/"), array('', get_string('other', 'questionnaire')), $choice->content); $cid = 'q' . $this->id . '_' . $id; $otherempty = false; $otherid = 'q' . $this->id . '_' . $checked; if (substr($checked, 0, 6) == 'other_') { // Fix bug CONTRIB-222. $checked = substr($checked, 6); } $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter); $output .= '<input name="q' . $this->id . '" id="' . $htmlid . '" type="radio" value="other_' . $id . '"' . $onclick; if ($id == $checked || !empty($data->{$cid})) { $output .= ' checked="checked"'; $ischecked = true; if (!$data->{$cid}) { $otherempty = true; } } $output .= ' /><label for="' . $htmlid . '" >' . format_text($othertext, FORMAT_HTML) . '</label>'; $choices['other_' . $cid] = $othertext; $output .= '<input type="text" size="25" name="' . $cid . '" id="' . $htmlid . '-other" onclick="other_check(name)"'; if (isset($data->{$cid})) { $output .= ' value="' . stripslashes($data->{$cid}) . '"'; } $output .= ' />'; $output .= '<label for="' . $htmlid . '-other" class="accesshide">Text for ' . format_text($othertext, FORMAT_HTML) . '</label> '; } if ($horizontal) { // Added a zero-width space character to make MSIE happy! $output .= '</span>​'; } else { $output .= '<br />'; } } // CONTRIB-846. if ($this->required == 'n') { $id = ''; $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter); if ($horizontal) { $output .= ' <span style="white-space:nowrap;">'; } // To display or hide dependent questions on Preview page. $onclick = ''; if ($onclickdepend) { $onclick = ' onclick="depend(\'' . $descendants . '\', \'\')"'; } else { $onclick = ' onclick="other_check_empty(name, value)"'; } // End dependents. $output .= '<input name="q' . $this->id . '" id="' . $htmlid . '" type="radio" value="' . $id . '"' . $onclick; if (!$ischecked && !$blankquestionnaire) { $output .= ' checked="checked"'; } $content = get_string('noanswer', 'questionnaire'); $output .= ' /><label for="' . $htmlid . '" >' . format_text($content, FORMAT_HTML) . '</label>'; if ($horizontal) { $output .= '</span> '; } else { $output .= '<br />'; } } // End CONTRIB-846. echo $output; if ($otherempty) { questionnaire_notify(get_string('otherempty', 'questionnaire')); } }
} $currentsection = 1; $SESSION->questionnaire->currentfbsection = 1; redirect($CFG->wwwroot . '/mod/questionnaire/fbsettings.php?id=' . $questionnaire->cm->id . '¤tsection=' . $currentsection, '', 0); } } $PAGE->set_url($url); // Print the page header. $PAGE->set_title(get_string('feedbackeditingsections', 'questionnaire')); $PAGE->set_heading(format_string($course->fullname)); $PAGE->navbar->add(get_string('feedbackeditingsections', 'questionnaire')); echo $OUTPUT->header(); echo '<form id="fbsections" method="post">'; $feedbacksections = $questionnaire->survey->feedbacksections + 1; if ($errormsg != '') { questionnaire_notify($errormsg); } $n = 0; $bg = 'c0'; echo $OUTPUT->box_start(); echo $OUTPUT->help_icon('feedbacksectionsselect', 'questionnaire'); echo '<b>Sections:</b><br /><br />'; $formdata = new stdClass(); $descendantsdata = array(); foreach ($questionnaire->questions as $question) { $qtype = $question->type_id; $qname = $question->name; $qprecise = $question->precise; $required = $question->required; $qid = $question->id; // Questions to be included in feedback sections must be required, have a name
function numeric_survey_display($data) { // Numeric $precision = $this->precise; $a = ''; if (isset($data->{'q' . $this->id})) { $mynumber = $data->{'q' . $this->id}; if ($mynumber != '') { $mynumber0 = $mynumber; if (!is_numeric($mynumber)) { $msg = get_string('notanumber', 'questionnaire', $mynumber); questionnaire_notify($msg); } else { if ($precision) { $pos = strpos($mynumber, '.'); if (!$pos) { if (strlen($mynumber) > $this->length) { $mynumber = substr($mynumber, 0, $this->length); } } $this->length += 1 + $precision; // to allow for n numbers after decimal point } $mynumber = number_format($mynumber, $precision, '.', ''); if ($mynumber != $mynumber0) { $a->number = $mynumber0; $a->precision = $precision; $msg = get_string('numberfloat', 'questionnaire', $a); questionnaire_notify($msg); } } } if ($mynumber != '') { $data->{'q' . $this->id} = $mynumber; } } echo '<input type="text" size="' . $this->length . '" name="q' . $this->id . '" maxlength="' . $this->length . '" value="' . (isset($data->{'q' . $this->id}) ? $data->{'q' . $this->id} : '') . '" />'; }
function view() { global $CFG, $USER, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_WHENANSWERED, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_WHENCLOSED, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_ALWAYS, $QUESTIONNAIRE_TYPES, $SESSION; $currentcss = ''; if (!empty($this->survey->theme)) { $currentcss = '<link rel="stylesheet" type="text/css" href="' . $CFG->wwwroot . '/mod/questionnaire/css/' . $this->survey->theme . '" />'; } $navigation = build_navigation('', $this->cm); print_header_simple($this->name, "", $navigation, '', $currentcss, true, update_module_button($this->cm->id, $this->course->id, $this->strquestionnaire), navmenu($this->course, $this->cm)); /// print the tabs $questionnaire = $this; include 'tabs.php'; $strprint = get_string('print', 'questionnaire'); $strprinttooltip = get_string('printtooltip', 'questionnaire'); $strprintblank = get_string('printblank', 'questionnaire'); $strprintblanktooltip = get_string('printblanktooltip', 'questionnaire'); $blankquestionnaire = true; if (!$this->cm->visible) { notice(get_string("activityiscurrentlyhidden")); } if (!$this->capabilities->view) { echo '<br/>'; questionnaire_notify(get_string("guestsno", "questionnaire", $this->name)); echo '<div class="returnLink"><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $this->course->id . '">' . get_string("continue") . '</a></div>'; exit; } /// Print the main part of the page if (!$this->is_active()) { echo '<div class="message">' . get_string('notavail', 'questionnaire') . '</div>'; } else { if (!$this->is_open()) { echo '<div class="message">' . get_string('notopen', 'questionnaire', userdate($this->opendate)) . '</div>'; } else { if ($this->is_closed()) { echo '<div class="message">' . get_string('closed', 'questionnaire', userdate($this->closedate)) . '</div>'; } else { if (!$this->user_is_eligible($USER->id)) { echo '<div class="message">' . get_string('noteligible', 'questionnaire') . '</div>'; } else { if ($this->user_can_take($USER->id)) { $sid = $this->sid; $quser = $USER->id; if ($this->survey->realm == 'template') { print_string('templatenotviewable', 'questionnaire'); print_footer($this->course); exit; } echo '<div class="box generalbox boxaligncenter boxwidthwide">'; echo '<div style="text-align:right; margin-right:5px; margin-top:-5px; margin-bottom:5px;">'; if (isset($this->questions) && $this->capabilities->printblank) { // open print friendly as popup window to fix XML strict bug contrib-200 $linkname = get_string('printblank', 'questionnaire'); $height = ''; $width = ''; $title = get_string('printblanktooltip', 'questionnaire'); $options = 'menubar=1, location=0, scrollbars, resizable'; $image_url = $CFG->wwwroot . '/mod/questionnaire/images/'; $name = 'popup'; $linkname = '<img src="' . $image_url . 'print.gif" alt="Printer-friendly version" />'; $url = '/mod/questionnaire/print.php?qid=' . $this->id . '&rid=0&' . 'courseid=' . $this->course->id . '&sec=1'; link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, false); } echo '</div>'; $msg = $this->print_survey($USER->id, $quser); $viewform = data_submitted($CFG->wwwroot . "/mod/questionnaire/view.php"); /// If Survey was submitted with all required fields completed ($msg is empty), /// then record the submittal. if (isset($viewform->submit) && isset($viewform->submittype) && $viewform->submittype == "Submit Survey" && empty($msg)) { $this->response_delete($viewform->rid, $viewform->sec); $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser, $viewform); $this->response_commit($this->rid); /// If it was a previous save, rid is in the form... if (!empty($viewform->rid) && is_numeric($viewform->rid)) { $rid = $viewform->rid; /// Otherwise its in this object. } else { $rid = $this->rid; } questionnaire_record_submission($this, $USER->id, $rid); $questionnaire = new Object(); $questionnaire->id = $this->id; $questionnaire->name = $this->name; $questionnaire->grade = $this->grade; $questionnaire->cmidnumber = $this->cm->idnumber; $questionnaire->courseid = $this->course->id; questionnaire_update_grades($questionnaire, $quser); add_to_log($this->course->id, "questionnaire", "submit", "view.php?id={$this->cm->id}", "{$this->name}", $this->cm->id, $USER->id); $this->response_send_email($this->rid); $this->response_goto_thankyou(); } echo '</div>'; //div class="box generalbox boxaligncenter boxwidthwide" } else { switch ($this->qtype) { case QUESTIONNAIREDAILY: $msgstring = ' ' . get_string('today', 'questionnaire'); break; case QUESTIONNAIREWEEKLY: $msgstring = ' ' . get_string('thisweek', 'questionnaire'); break; case QUESTIONNAIREMONTHLY: $msgstring = ' ' . get_string('thismonth', 'questionnaire'); break; default: $msgstring = ''; break; } echo '<div class="message">' . get_string("alreadyfilled", "questionnaire", $msgstring) . '</div>'; } } } } } /// Finish the page print_footer($this->course); }
protected function question_survey_display($data, $descendantsdata, $blankquestionnaire = false) { // Check boxes. $otherempty = false; if (!empty($data)) { if (!isset($data->{'q' . $this->id}) || !is_array($data->{'q' . $this->id})) { $data->{'q' . $this->id} = array(); } // Verify that number of checked boxes (nbboxes) is within set limits (length = min; precision = max). if ($data->{'q' . $this->id}) { $otherempty = false; $boxes = $data->{'q' . $this->id}; $nbboxes = count($boxes); foreach ($boxes as $box) { $pos = strpos($box, 'other_'); if (is_int($pos) == true) { $otherchoice = substr($box, 6); $resp = 'q' . $this->id . '' . substr($box, 5); if (!$data->{$resp}) { $otherempty = true; } } } $nbchoices = count($this->choices); $min = $this->length; $max = $this->precise; if ($max == 0) { $max = $nbchoices; } if ($min > $max) { $min = $max; // Sanity check. } $min = min($nbchoices, $min); $msg = ''; if ($nbboxes < $min || $nbboxes > $max) { $msg = get_string('boxesnbreq', 'questionnaire'); if ($min == $max) { $msg .= ' ' . get_string('boxesnbexact', 'questionnaire', $min); } else { if ($min && $nbboxes < $min) { $msg .= get_string('boxesnbmin', 'questionnaire', $min); if ($nbboxes > $max) { $msg .= ' & ' . get_string('boxesnbmax', 'questionnaire', $max); } } else { if ($nbboxes > $max) { $msg .= get_string('boxesnbmax', 'questionnaire', $max); } } } questionnaire_notify($msg); } } } foreach ($this->choices as $id => $choice) { $other = strpos($choice->content, '!other'); if ($other !== 0) { // This is a normal check box. $contents = questionnaire_choice_values($choice->content); $checked = false; if (!empty($data)) { $checked = in_array($id, $data->{'q' . $this->id}); } echo html_writer::checkbox('q' . $this->id . '[]', $id, $checked, format_text($contents->text, FORMAT_HTML) . $contents->image); echo '<br />'; } else { // Check box with associated !other text field. // In case length field has been used to enter max number of choices, set it to 20. $othertext = preg_replace(array("/^!other=/", "/^!other/"), array('', get_string('other', 'questionnaire')), $choice->content); $cid = 'q' . $this->id . '_' . $id; if (!empty($data) && !empty($data->{$cid})) { $checked = true; } else { $checked = false; } $name = 'q' . $this->id . '[]'; $value = 'other_' . $id; echo html_writer::checkbox($name, $value, $checked, format_text($othertext . '', FORMAT_HTML)); $othertext = ' <input type="text" size="25" name="' . $cid . '" onclick="other_check(name)"'; if ($cid) { $othertext .= ' value="' . (!empty($data->{$cid}) ? stripslashes($data->{$cid}) : '') . '"'; } $othertext .= ' />'; echo $othertext . '<br />'; } } if ($otherempty) { questionnaire_notify(get_string('otherempty', 'questionnaire')); } }