type === self::TYPE; } /** * Add the type to the type list. * * @param array $types Type key => label. */ public static function register_type($types) { $types[self::TYPE] = __('Quiz question', '3task-polls'); return $types; } /** * Add the type to a list of type keys. * * @param string[] $types Type keys. */ public static function add_type($types) { $types[] = self::TYPE; return $types; } /** * The solution is shown after the vote, whatever the poll setting says, and never before * (the page HTML must not give it away). A closed quiz shows it to everyone. * * @param bool $visible Result of the poll setting. * @param TPoll_Poll $poll Poll. * @param string $show_results Poll setting. * @param bool $has_voted Visitor has voted. */ public static function results_visible($visible, $poll, $show_results, $has_voted) { if (!self::is_quiz($poll)) { return $visible; } return $has_voted || $poll->is_closed(); } /** * The correct answer of a quiz question. * * @param TPoll_Poll $poll Poll. * @return object|null Answer row. */ public static function correct_answer($poll) { $id = (int) $poll->get_setting('quiz_correct', 0); foreach ($poll->get_answers() as $answer) { if ((int) $answer->id === $id) { return $answer; } } return null; } /** * Tags allowed in the explanation. * * @return array */ private static function allowed_html() { return array( 'a' => array('href' => true), 'strong' => array(), 'em' => array(), 'br' => array(), ); } /** * Explanation shown after the vote (sanitized when saved). * * @param TPoll_Poll $poll Poll. * @return string */ public static function explanation($poll) { return (string) $poll->get_setting('quiz_explanation', ''); } /** * Row index of the marked answer, counted among the answers that are kept (empty rows are dropped). * * @param array $raw Unslashed POST data. * @return int|null Position or null when no kept answer is marked. */ private static function marked_position($raw) { if (!isset($raw['quiz_correct'], $raw['answers']) || !is_array($raw['answers']) || $raw['quiz_correct'] === '') { return null; } $marked = (string) $raw['quiz_correct']; $position = 0; foreach ($raw['answers'] as $key => $answer) { if (!is_array($answer) || !isset($answer['text']) || trim((string) $answer['text']) === '') { continue; } if ((string) $key === $marked) { return $position; } $position++; } return null; } /** * A quiz question is answered, not voted on. * * @param string $text Button text. * @param TPoll_Poll $poll Poll. */ public static function button_text($text, $poll) { return self::is_quiz($poll) ? _x('Answer', 'quiz button', '3task-polls') : $text; } /** * Editor box for the explanation, shown for quiz questions only. * * @param TPoll_Poll $poll Poll being edited. */ public static function render_edit_box($poll) { ?>