public function from_user($qd, $extra = []) { parent::from_user($qd, $extra); $err = []; if ($this->auto_marking_method == 'binary') { if (!isset($qd['answer']) || empty($qd['answer'])) { $err['answer'] = $this->msg->_('sa-missing-answer'); } elseif (!Validator::validate_answer($qd['answer'])) { $err['answer'] = $this->msg->_('answer-too-long'); } else { $this->raw_answer = $qd['answer']; } $r = Validator::build_regex($qd['answer'], 'iu', true); if (!$r) { $err['answer'] = $this->msg->_('invalid-regex', [$qd['answer']]); } else { $this->answer = $r; } } else { set_empty_if_undefined($qd['answer']); if (!Validator::validate_answer($qd['answer'])) { $err['answer'] = $this->msg->_('answer-too-long'); } else { $this->answer = $qd['answer']; } } if ($err) { throw new Exception(my_json_encode($err)); } }
public function from_user($qd, $extra = []) { parent::from_user($qd, $extra); $err = []; set_empty_if_undefined($qd['answer']); if (!Validator::validate_answer($qd['answer'])) { $err['answer'] = $this->msg->_('answer-too-long'); } else { $this->answer = $qd['answer']; } if ($err) { throw new Exception(my_json_encode($err)); } }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); if (!isset($qd['fields']) || !is_array($qd['fields'])) { throw new Exception($this->msg->_('/new-question/errors/invalid-format')); } $this->n_answers = count($this->answers); if (!empty($extra)) { if (!is_array($extra) || count($extra) != count($qd['fields'])) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } $this->n_real_answers = preg_match_all(':<\\s*f\\s*/>:', $this->body); $err = array(); foreach ($qd['fields'] as $i => $f) { if (!is_string($f)) { $err['fields'] = $this->msg->_('invalid-format', [$f]); } else { if (!$extra) { $hex_strings = []; do { $hex_id = '#' . random_hex_string(8); } while (in_array($hex_id, $hex_strings)); // just in case $hex_strings[] = $hex_id; } else { $hex_id = $extra[$i]; } $real = $i < $this->n_real_answers; $this->answers[] = array('text' => $f, 'hex_id' => $hex_id, 'real' => $real); $this->answers_by_hex_id[$hex_id] = array('text' => $f, 'real' => $real); $this->n_answers++; } } if ($err) { throw new Exception(my_json_encode($err)); } if ($this->n_real_answers > $this->n_answers) { throw new Exception($this->msg->_('/new-question/errors/ft-num-fields')); } }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); $err = array(); if (!isset($qd['fields']) || !is_array($qd['fields'])) { throw new Exception($this->msg->_('/new-question/errors/invalid-format')); } foreach ($qd['fields'] as $f) { $r = Validator::build_regex($f, 'iu', true); if (!$r) { $err['fields'] = $this->msg->_('invalid-regex', [$f]); } else { $this->answers[] = $r; $this->raw_answers[] = $f; } } if ($err) { throw new Exception(my_json_encode($err)); } $this->n_answers = count($this->answers); if (preg_match_all(':<\\s*f\\s*/>:', $this->body) != $this->n_answers) { throw new Exception($this->msg->_('/new-question/errors/ft-num-fields')); } }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); $err = []; if (!isset($qd['shuffle'])) { $qd['shuffle'] = ''; } switch ($qd['shuffle']) { case 'left': $this->shuffle_left = true; $this->shuffle_right = false; break; case 'right': $this->shuffle_left = false; $this->shuffle_right = true; break; case 'both': $this->shuffle_left = true; $this->shuffle_right = true; break; default: $err['shuffle'] = $this->msg->_('invalid-format'); } if (!isset($qd['items'])) { $qd['items'] = array(); } if (!is_array($qd['items'])) { $err['items'] = $this->msg->_('invalid-format'); } $this->n_rows = count($qd['items']); if (!empty($extra)) { if (!is_array($extra) || count($extra) != 2 || !is_array($extra[0]) || !is_array($extra[1]) || count($extra[0]) != $this->n_rows || count($extra[1]) != $this->n_rows) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } foreach ($qd['items'] as $i => $a) { if (!is_array($a) || !array_key_exists('left', $a) || !is_string($a['left']) || !array_key_exists('right', $a) || !is_string($a['right'])) { $err['items'] = $this->msg->_('invalid-format'); break; } if (!$extra) { $hex_strings = []; do { $left_hex_id = '#' . random_hex_string(8); $right_hex_id = '#' . random_hex_string(8); } while (in_array($left_hex_id, $hex_strings) || in_array($right_hex_id, $hex_strings)); // just in case $hex_strings[] = $left_hex_id; $hex_strings[] = $right_hex_id; } else { if (!is_string($extra[0][$i]) || !is_string($extra[1][$i])) { $err['items'] = $this->msg->_('invalid-format'); break; } $left_hex_id = $extra[0][$i]; $right_hex_id = $extra[1][$i]; } $this->left_column[] = array('text' => $a['left'], 'hex_id' => $left_hex_id); $this->right_column[] = array('text' => $a['right'], 'hex_id' => $right_hex_id); $this->left_column_by_hex_id[$left_hex_id] = $a['left']; $this->right_column_by_hex_id[$right_hex_id] = $a['right']; } if ($err) { throw new Exception(my_json_encode($err)); } }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); $err = []; if (!isset($qd['answers'])) { $qd['answers'] = array(); } if (!is_array($qd['answers'])) { $err['answers'] = $msg->_('invalid-format'); } if (!empty($extra)) { if (!is_array($extra) || count($extra) != count($qd['answers'])) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } foreach ($qd['answers'] as $i => $a) { if (!is_array($a) || !(!empty($a['text']) && isset($a['fixed']) && isset($a['correct'])) || !is_string($a['text']) || !Validator::validate_boolean($a['fixed']) || !Validator::validate_boolean($a['correct'])) { $err['answers'] = $this->msg->_('invalid-format'); break; } if (!$extra) { $hex_strings = []; do { $hex_id = '#' . random_hex_string(8); } while (in_array($hex_id, $hex_strings)); // just in case $hex_strings[] = $hex_id; } else { $hex_id = $extra[$i]; } $this->answers[] = array('text' => $a['text'], 'correct' => $a['correct'], 'fixed' => $a['fixed'], 'hex_id' => $hex_id); $this->answers_by_hex_id[$hex_id] = array('text' => $a['text'], 'correct' => $a['correct'], 'fixed' => $a['fixed']); if ($a['correct']) { $this->correct_answers++; } $this->total_answers++; } if ($this->total_answers == 0) { $err['answers'] = $this->msg->_('mc-at-least-one'); } if ($err) { throw new Exception(my_json_encode($err)); } set_empty_if_undefined($qd['min_answers']); set_empty_if_undefined($qd['max_answers']); if (!is_int($qd['min_answers'])) { $err['min_answers'] = $this->msg->_('invalid-format'); } elseif (!($qd['min_answers'] >= 0) || !($qd['min_answers'] <= $this->correct_answers)) { $err['min_answers'] = $this->msg->_('mc-min'); } elseif ($qd['min_answers'] == $this->total_answers) { $err['min-answers'] = $this->msg->_('mc-min-lt-total'); } else { $this->min_answers = $qd['min_answers']; } if (!is_int($qd['max_answers'])) { $err['max_answers'] = $this->msg->_('invalid-format'); } elseif (!($qd['max_answers'] >= $this->correct_answers) || !($qd['max_answers'] <= $this->total_answers)) { $err['max_answers'] = $this->msg->_('mc-max'); } elseif ($qd['max_answers'] == 0) { $err['max-answers'] = $this->msg->_('mc-max-gt-zero'); } else { $this->max_answers = $qd['max_answers']; } if ($err) { throw new Exception(my_json_encode($err)); } }