/** * @todo Check this function * * @global object * @global object * @global int * @global void This is never defined * @global object This is defined twice? * @param object $question */ function survey_print_multi($question) { global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT; //TODO: this is sloppy globals abuse $stripreferthat = get_string("ipreferthat", "survey"); $strifoundthat = get_string("ifoundthat", "survey"); $strdefault = get_string('notyetanswered', 'survey'); $strresponses = get_string('responses', 'survey'); echo $OUTPUT->heading($question->text, 3); echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">"; $options = explode( ",", $question->options); $numoptions = count($options); // COLLES Actual (which is having questions of type 1) and COLLES Preferred (type 2) // expect just one answer per question. COLLES Actual and Preferred (type 3) expects // two answers per question. ATTLS (having a single question of type 1) expects one // answer per question. CIQ is not using multiquestions (i.e. a question with subquestions). // Note that the type of subquestions does not really matter, it's the type of the // question itself that determines everything. $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false; // COLLES Preferred (having questions of type 2) will use the radio elements with the name // like qP1, qP2 etc. COLLES Actual and ATTLS have radios like q1, q2 etc. if ($question->type == 2) { $P = "P"; } else { $P = ""; } echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>"; echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>"; while (list ($key, $val) = each ($options)) { echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n"; } echo "</tr>\n"; echo "<tr><th scope=\"col\" colspan=\"7\">$question->intro</th></tr>\n"; $subquestions = survey_get_subquestions($question); foreach ($subquestions as $q) { $qnum++; if ($oneanswer) { $rowclass = survey_question_rowclass($qnum); } else { $rowclass = survey_question_rowclass(round($qnum / 2)); } if ($q->text) { $q->text = get_string($q->text, "survey"); } echo "<tr class=\"$rowclass rblock\">"; if ($oneanswer) { echo "<th scope=\"row\" class=\"optioncell\">"; echo "<b class=\"qnumtopcell\">$qnum</b> "; echo $q->text ."</th>\n"; $default = get_accesshide($strdefault); echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</label></td>"; for ($i=1;$i<=$numoptions;$i++) { $hiddentext = get_accesshide($options[$i-1]); $id = "q$P" . $q->id . "_$i"; echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; } $checklist["q$P$q->id"] = 0; } else { echo "<th scope=\"row\" class=\"optioncell\">"; echo "<b class=\"qnumtopcell\">$qnum</b> "; $qnum++; echo "<span class=\"preferthat\">$stripreferthat</span> "; echo "<span class=\"option\">$q->text</span></th>\n"; $default = get_accesshide($strdefault); echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" />'.$default.'</label></td>'; for ($i=1;$i<=$numoptions;$i++) { $hiddentext = get_accesshide($options[$i-1]); $id = "qP" . $q->id . "_$i"; echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; } echo "</tr>"; echo "<tr class=\"$rowclass rblock\">"; echo "<th scope=\"row\" class=\"optioncell\">"; echo "<b class=\"qnumtopcell\">$qnum</b> "; echo "<span class=\"foundthat\">$strifoundthat</span> "; echo "<span class=\"option\">$q->text</span></th>\n"; $default = get_accesshide($strdefault); echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>'; for ($i=1;$i<=$numoptions;$i++) { $hiddentext = get_accesshide($options[$i-1]); $id = "q" . $q->id . "_$i"; echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; } $checklist["qP$q->id"] = 0; $checklist["q$q->id"] = 0; } echo "</tr>\n"; } echo "</table>"; }
/** * Test survey_save_answers */ public function test_survey_save_answers() { global $DB; $this->resetAfterTest(); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id)); $context = context_module::instance($survey->cmid); // Build our questions and responses array. $realquestions = array(); $questions = survey_get_questions($survey); $i = 5; foreach ($questions as $q) { if ($q->type > 0) { if ($q->multi) { $subquestions = survey_get_subquestions($q); foreach ($subquestions as $sq) { $key = 'q' . $sq->id; $realquestions[$key] = $i % 5 + 1; $i++; } } else { $key = 'q' . $q->id; $realquestions[$key] = $i % 5 + 1; $i++; } } } $sink = $this->redirectEvents(); survey_save_answers($survey, $realquestions, $course, $context); // Check the stored answers, they must match. $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $survey->id), '', 'question, answer1'); foreach ($realquestions as $key => $value) { $id = str_replace('q', '', $key); $this->assertEquals($value, $dbanswers[$id]); } // Check events. $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_survey\\event\\response_submitted', $event); $this->assertEquals($context, $event->get_context()); $this->assertEquals($survey->id, $event->other['surveyid']); }
/** * Get the complete list of questions for the survey, including subquestions. * * @param int $surveyid the survey instance id * @return array of warnings and the question list * @since Moodle 3.0 * @throws moodle_exception */ public static function get_questions($surveyid) { global $DB, $USER; $params = self::validate_parameters(self::get_questions_parameters(), array('surveyid' => $surveyid)); $warnings = array(); // Request and permission validation. $survey = $DB->get_record('survey', array('id' => $params['surveyid']), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($survey, 'survey'); $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/survey:participate', $context); $mainquestions = survey_get_questions($survey); foreach ($mainquestions as $question) { if ($question->type >= 0) { // Parent is used in subquestions. $question->parent = 0; $questions[] = survey_translate_question($question); // Check if the question has subquestions. if ($question->multi) { $subquestions = survey_get_subquestions($question); foreach ($subquestions as $sq) { $sq->parent = $question->id; $questions[] = survey_translate_question($sq); } } } } $result = array(); $result['questions'] = $questions; $result['warnings'] = $warnings; return $result; }
/** * Test submit_answers */ public function test_submit_answers() { global $DB; // Test user with full capabilities. $this->setUser($this->student); // Build our questions and responses array. $realquestions = array(); $questions = survey_get_questions($this->survey); $i = 5; foreach ($questions as $q) { if ($q->type >= 0) { if ($q->multi) { $subquestions = survey_get_subquestions($q); foreach ($subquestions as $sq) { $realquestions[] = array('key' => 'q' . $sq->id, 'value' => $i % 5 + 1); $i++; } } else { $realquestions[] = array('key' => 'q' . $q->id, 'value' => $i % 5 + 1); $i++; } } } $result = mod_survey_external::submit_answers($this->survey->id, $realquestions); $result = external_api::clean_returnvalue(mod_survey_external::submit_answers_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $this->survey->id), '', 'question, answer1'); foreach ($realquestions as $q) { $id = str_replace('q', '', $q['key']); $this->assertEquals($q['value'], $dbanswers[$id]); } // Submit again, we expect an error here. try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to answers already submitted.'); } catch (moodle_exception $e) { $this->assertEquals('alreadysubmitted', $e->errorcode); } // Test user with no capabilities. // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. assign_capability('mod/survey:participate', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); accesslib_clear_all_caches_for_unit_testing(); try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } // Test not-enrolled user. $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to not enrolled user.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } }
break; } } foreach ($questionorder as $key => $val) { $question = $questions[$val]; if ($question->type < 0) { // We have some virtual scales. DON'T show them. continue; } $question->text = get_string($question->text, "survey"); if ($question->multi) { echo $OUTPUT->heading($question->text . ':', 4); $subquestions = survey_get_subquestions($question); foreach ($subquestions as $subquestion) { if ($subquestion->type > 0) { echo "<p class=\"centerpara\">"; echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$subquestion->id\">"; survey_print_graph("id=$id&qid=$subquestion->id&group=$currentgroup&type=question.png"); echo "</a></p>"; } } } else if ($question->type > 0 ) { echo "<p class=\"centerpara\">"; echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$question->id\">"; survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=question.png"); echo "</a></p>"; } else {