/** This function is used by the question engine to prevent regrading of * unchanged submissions. * * @param array $prevresponse * @param array $newresponse * @return boolean */ public function is_same_response(array $prevresponse, array $newresponse) { if (!question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer') || !question_utils::arrays_same_at_key_integer($prevresponse, $newresponse, 'rating')) { return false; } return true; }
/** * Creates a textual representation of a question for display. * * @param object $question A question object from the database questions table * @param bool $showicon If true, show the question's icon with the question. False by default. * @param bool $showquestiontext If true (default), show question text after question name. * If false, show only question name. * @param bool $return If true (default), return the output. If false, print it. */ function quiz_question_tostring($question, $showicon = false, $showquestiontext = true, $return = true) { global $COURSE; $result = ''; $result .= '<span class="questionname">'; if ($showicon) { $result .= print_question_icon($question, true); echo ' '; } $result .= shorten_text(format_string($question->name), 200) . '</span>'; if ($showquestiontext) { $questiontext = question_utils::to_plain_text($question->questiontext, $question->questiontextformat, array('noclean' => true, 'para' => false)); $questiontext = shorten_text($questiontext, 200); $result .= '<span class="questiontext">'; if (!empty($questiontext)) { $result .= s($questiontext); } else { $result .= '<span class="error">'; $result .= get_string('questiontextisempty', 'quiz'); $result .= '</span>'; } $result .= '</span>'; } if ($return) { return $result; } else { echo $result; } }
/** * Validate the manual mark for a question. * @param unknown $currentmark the user input (e.g. '1,0', '1,0' or 'invalid'. * @return string any errors with the value, or '' if it is OK. */ public function validate_manual_mark($currentmark) { if ($currentmark === null || $currentmark === '') { return ''; } $mark = question_utils::clean_param_mark($currentmark); if ($mark === null) { return get_string('manualgradeinvalidformat', 'question'); } $maxmark = $this->get_max_mark(); if ($mark > $maxmark * $this->get_max_fraction() || $mark < $maxmark * $this->get_min_fraction()) { return get_string('manualgradeoutofrange', 'question'); } return ''; }
public function is_same_response(array $prevresponse, array $newresponse) { foreach ($this->places as $place => $notused) { $fieldname = $this->field($place); if (!question_utils::arrays_same_at_key_integer($prevresponse, $newresponse, $fieldname)) { return false; } } return true; }
/** * @param int $num The number, starting at 0. * @param string $style The style to render the number in. One of the * options returned by {@link qtype_multichoice:;get_numbering_styles()}. * @return string the number $num in the requested style. */ public static function number_answer($num, $style) { switch ($style) { case 'abc': $number = chr(ord('a') + $num); break; case 'ABCD': $number = chr(ord('A') + $num); break; case '123': $number = $num + 1; break; case 'iii': $number = question_utils::int_to_roman($num + 1); break; case 'IIII': $number = strtoupper(question_utils::int_to_roman($num + 1)); break; case 'none': return ''; default: return 'ERR'; } return "({$number})"; }
public function is_same_response(array $prevresponse, array $newresponse) { if (!question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer')) { return false; } if ($this->has_separate_unit_field()) { return question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'unit'); } return true; }
public function is_same_response(array $prevresponse, array $newresponse) { foreach ($this->order as $key => $notused) { $fieldname = $this->field($key); if (!question_utils::arrays_same_at_key($prevresponse, $newresponse, $fieldname)) { return false; } } return true; }
/** * Get a particular parameter from the current request. A wrapper round * {@link optional_param()}, except that the results is returned without * slashes. * @param string $name the paramter name. * @param int $type one of the standard PARAM_... constants, or one of the * special extra constands defined by this class. * @param array $postdata (optional, only inteded for testing use) take the * data from this array, instead of from $_POST. * @return mixed the requested value. */ public function get_submitted_var($name, $type, $postdata = null) { switch ($type) { case self::PARAM_MARK: // Special case to work around PARAM_FLOAT converting '' to 0. return question_utils::clean_param_mark($this->get_submitted_var($name, PARAM_RAW_TRIMMED, $postdata)); case self::PARAM_FILES: return $this->process_response_files($name, $name, $postdata); case self::PARAM_RAW_FILES: $var = $this->get_submitted_var($name, PARAM_RAW, $postdata); return $this->process_response_files($name, $name . ':itemid', $postdata, $var); default: if (is_null($postdata)) { $var = optional_param($name, null, $type); } else if (array_key_exists($name, $postdata)) { $var = clean_param($postdata[$name], $type); } else { $var = null; } return $var; } }
public function is_same_response(array $prevresponse, array $newresponse) { foreach ($this->get_parameters() as $param) { if (!question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer_' . $param)) { return false; } } return true; }
/** * Updating output to include a graph of multiple choice answer possibilities * with the percentage of students that answered that option * * @param \mod_activequiz\activequiz_question $question The realtime quiz question * @param array $attempts An array of \mod_activequiz\activequiz_attempt classes * @param string $output The current output from getting the results * @return string Return the updated output to be passed to the client */ public function modify_questionresults_duringquiz($question, $attempts, $output) { global $DB; // store the possible answersid as the key of the array, and then a count // for the number of times it was answered $answers = array(); $dbanswers = array(); foreach ($attempts as $attempt) { /** @var \mod_activequiz\activequiz_attempt $attempt */ // only count attempts where they have "responded" if ($attempt->responded == 0) { continue; } $quba = $attempt->get_quba(); $slot = $attempt->get_question_slot($question); $qa = $quba->get_question_attempt($slot); // now get question definition $questiondef = $qa->get_question(); // if dbanswers is empty get them from the question definition (as this will be the same for all attempts for this slot // also save a db query if (empty($dbanswers)) { $dbanswers = $questiondef->answers; } // single and multi answers are handled differently for steps if ($questiondef instanceof \qtype_multichoice_single_question) { $this->update_answers_single($answers, $qa, $questiondef); } else { if ($questiondef instanceof \qtype_multichoice_multi_question) { $this->update_answers_multi($answers, $qa, $questiondef); } else { } } } $xaxis = array(); foreach ($dbanswers as $dbanswer) { $xaxis[$dbanswer->id] = \question_utils::to_plain_text($dbanswer->answer, $dbanswer->answerformat); } $newoutput = $this->add_chart($output, $xaxis, $answers); return $newoutput; }
public function is_same_response(array $prevresponse, array $newresponse) { if (!question_utils::arrays_same_at_key_missing_is_blank( $prevresponse, $newresponse, 'answer')) { return false; } if ($this->unitdisplay == qtype_numerical::UNITSELECT) { return question_utils::arrays_same_at_key_missing_is_blank( $prevresponse, $newresponse, 'unit'); } return false; }
/** * @return string a summary of a manual comment action. * @param unknown_type $step */ protected function summarise_manual_comment($step) { $a = new stdClass(); if ($step->has_behaviour_var('comment')) { $a->comment = shorten_text(html_to_text($this->format_comment($step->get_behaviour_var('comment')), 0, false), 200); } else { $a->comment = ''; } $mark = question_utils::clean_param_mark($step->get_behaviour_var('mark')); if (is_null($mark) || $mark === '') { return get_string('commented', 'question', $a->comment); } else { $a->mark = $mark / $step->get_behaviour_var('maxmark') * $this->qa->get_max_mark(); return get_string('manuallygraded', 'question', $a); } }
/** * @param object $question question data. * @return string HTML of question text, ready for display. */ protected function render_question_text_plain($question, $showimages = true) { global $OUTPUT; if ($showimages) { $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $this->context->id, 'quiz_statistics'); } else { $text = $question->questiontext; } $questiontext = question_utils::to_plain_text($text, $question->questiontextformat, array('noclean' => true, 'para' => false, 'overflowdiv' => true)); return ' ' . $questiontext; }
/** * Creates a textual representation of a question for display. * * @param object $question A question object from the database questions table * @param bool $showicon If true, show the question's icon with the question. False by default. * @param bool $showquestiontext If true (default), show question text after question name. * If false, show only question name. * @return string */ function quiz_question_tostring($question, $showicon = false, $showquestiontext = true) { $result = ''; $name = shorten_text(format_string($question->name), 200); if ($showicon) { $name .= print_question_icon($question) . ' ' . $name; } $result .= html_writer::span($name, 'questionname'); if ($showquestiontext) { $questiontext = question_utils::to_plain_text($question->questiontext, $question->questiontextformat, array('noclean' => true, 'para' => false)); $questiontext = shorten_text($questiontext, 200); if ($questiontext) { $result .= ' ' . html_writer::span(s($questiontext), 'questiontext'); } } return $result; }
public function get_possible_responses($questiondata) { if ($questiondata->options->single) { $responses = array(); foreach ($questiondata->options->answers as $aid => $answer) { $responses[$aid] = new question_possible_response(question_utils::to_plain_text($answer->answer, $answer->answerformat), $answer->fraction); } $responses[null] = question_possible_response::no_response(); return array($questiondata->id => $responses); } else { $parts = array(); foreach ($questiondata->options->answers as $aid => $answer) { $parts[$aid] = array($aid => new question_possible_response(question_utils::to_plain_text($answer->answer, $answer->answerformat), $answer->fraction)); } return $parts; } }
public function is_same_response(array $prevresponse, array $newresponse) { return question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer'); }
/** * Creates a textual representation of a question for display. * * @param object $question A question object from the database questions table * @param bool $showicon If true, show the question's icon with the question. False by default. * @param bool $showquestiontext If true (default), show question text after question name. * If false, show only question name. * @param bool $return If true (default), return the output. If false, print it. */ function offlinequiz_question_tostring($question, $showicon = false, $showquestiontext = true, $return = true, $shorttitle = false) { global $COURSE; $result = ''; $formatoptions = new stdClass(); $formatoptions->noclean = true; $formatoptions->para = false; $questiontext = strip_tags(question_utils::to_plain_text($question->questiontext, $question->questiontextformat, array('noclean' => true, 'para' => false))); $questiontitle = strip_tags(format_text($question->name, $question->questiontextformat, $formatoptions, $COURSE->id)); $result .= '<span class="questionname" title="' . $questiontitle . '">'; if ($shorttitle && strlen($questiontitle) > 25) { $questiontitle = shorten_text($questiontitle, 25, false, '...'); } if ($showicon) { $result .= print_question_icon($question, true); echo ' '; } if ($shorttitle) { $result .= $questiontitle; } else { $result .= shorten_text(format_string($question->name), 200) . '</span>'; } if ($showquestiontext) { $result .= '<span class="questiontext" title="' . $questiontext . '">'; $questiontext = shorten_text($questiontext, 200); if (!empty($questiontext)) { $result .= $questiontext; } else { $result .= '<span class="error">'; $result .= get_string('questiontextisempty', 'offlinequiz'); $result .= '</span>'; } $result .= '</span>'; } if ($return) { return $result; } else { echo $result; } }
public function is_same_response(array $prevresponse, array $newresponse) { return question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer') && ($this->attachments == 0 || question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'attachments')); }
/** * Convert some part of the question text to plain text. This might be used, * for example, by get_response_summary(). * @param string $text The HTML to reduce to plain text. * @param int $format the FORMAT_... constant. * @return string the equivalent plain text. */ public function html_to_text($text, $format) { return question_utils::to_plain_text($text, $format); }
/** * Validate that the manual grade submitted for a particular question is in range. * @param int $qubaid the question_usage id. * @param int $slot the slot number within the usage. * @return bool whether the submitted data is in range. */ public static function is_manual_grade_in_range($qubaid, $slot) { $prefix = 'q' . $qubaid . ':' . $slot . '_'; $mark = question_utils::optional_param_mark($prefix . '-mark'); $maxmark = optional_param($prefix . '-maxmark', null, PARAM_FLOAT); $minfraction = optional_param($prefix . ':minfraction', null, PARAM_FLOAT); $maxfraction = optional_param($prefix . ':maxfraction', null, PARAM_FLOAT); return is_null($mark) || $mark >= $minfraction * $maxmark && $mark <= $maxfraction * $maxmark; }
protected function is_same_prt_input($index, $prtinput1, $prtinput2) { foreach ($this->prts[$index]->get_required_variables(array_keys($this->inputs)) as $name) { if (!question_utils::arrays_same_at_key_missing_is_blank($prtinput1, $prtinput2, $name)) { return false; } } return true; }
/** * Convert the question text to plain text, so it can safely be displayed * during import to let the user see roughly what is going on. */ protected function format_question_text($question) { return question_utils::to_plain_text($question->questiontext, $question->questiontextformat); }
public function test_int_to_roman_not_int() { $this->setExpectedException('moodle_exception'); question_utils::int_to_roman(1.5); }
public function is_same_response(array $prevresponse, array $newresponse) { if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) { $value1 = (string) $prevresponse['answer']; } else { $value1 = ''; } if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) { $value2 = (string) $newresponse['answer']; } else { $value2 = ''; } return $value1 === $value2 && ($this->attachments == 0 || question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'attachments')); }
public function test_clean_param_mark() { $this->assertNull(question_utils::clean_param_mark(null)); $this->assertNull(question_utils::clean_param_mark('frog')); $this->assertSame('', question_utils::clean_param_mark('')); $this->assertSame(0.0, question_utils::clean_param_mark('0')); $this->assertSame(1.5, question_utils::clean_param_mark('1.5')); $this->assertSame(1.5, question_utils::clean_param_mark('1,5')); $this->assertSame(-1.5, question_utils::clean_param_mark('-1.5')); $this->assertSame(-1.5, question_utils::clean_param_mark('-1,5')); }
/** * @param int $num The number, starting at 0. * @param string $style The style to render the number in. One of the * options returned by {@link qtype_multichoice:;get_numbering_styles()}. * @return string the number $num in the requested style. */ protected function number_in_style($num, $style) { switch($style) { case 'abc': $number = chr(ord('a') + $num); break; case 'ABCD': $number = chr(ord('A') + $num); break; case '123': $number = $num + 1; break; case 'iii': $number = question_utils::int_to_roman($num + 1); break; case 'IIII': $number = strtoupper(question_utils::int_to_roman($num + 1)); break; case 'none': return ''; default: return 'ERR'; } return $this->number_html($number); }
public function test_int_to_roman_not_int() { $this->expectException(); question_utils::int_to_roman(1.5); }