function test_auto_marking_sc($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale'], '/new-question/errors'); try { $model = new Model(); $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die($msg->_('invalid-format'), 415); } $input_data = json_decode($raw_input, true); if (empty($input_data) || !isset($input_data['question']) || !isset($input_data['source-code']) || !is_string($input_data['source-code'])) { Util::output_errors_and_die($msg->_('invalid-format'), 400); } $extra = !empty($input_data['extra']) ? $input_data['extra'] : []; $qd = $input_data['question']; set_empty_if_undefined($qd['type']); if ($qd['type'] != 'source-code') { Util::output_errors_and_die('', 400); } $q = new QuestionSC($qd, Question::FROM_USER, $extra); $q->mark_automatically(array('source-code' => $input_data['source-code']), $log, $result); http_response_code(200); header('Content-Type: application/json'); echo my_json_encode($result); } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }
function test_auto_marking($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale'], '/new-question/errors'); try { $model = new Model(); $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die($msg->_('invalid-format'), 415); } $input_data = json_decode($raw_input, true); if (empty($input_data) || !isset($input_data['question']) || !isset($input_data['studentAnswer'])) { Util::output_errors_and_die($msg->_('invalid-format'), 400); } $extra = !empty($input_data['extra']) ? $input_data['extra'] : []; $qd = $input_data['question']; set_empty_if_undefined($qd['type']); if (!Validator::validate_question_type($qd['type'])) { Util::output_errors_and_die($msg->_('invalid-type'), 400); } switch ($qd['type']) { case 'short-answer': $q = new QuestionSA($qd, Question::FROM_USER, $extra); break; case 'essay': $q = new QuestionES($qd, Question::FROM_USER, $extra); break; case 'multiple-choice': $q = new QuestionMC($qd, Question::FROM_USER, $extra); break; case 'matching': $q = new QuestionMA($qd, Question::FROM_USER, $extra); break; case 'fitb-type': $q = new QuestionFT($qd, Question::FROM_USER, $extra); break; case 'fitb-select': $q = new QuestionFS($qd, Question::FROM_USER, $extra); break; case 'source-code': $q = new QuestionSC($qd, Question::FROM_USER, $extra); break; } http_response_code(200); header('Content-Type: application/json'); $mark = $q->mark_automatically($input_data['studentAnswer'], $log); foreach ($log as $i => $line) { $log[$i] = $msg->_('/auto-marking/' . $line[0], $line[1]); } $log = implode('<br/>', $log); echo my_json_encode(array('log' => $log, 'mark' => $mark)); } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }
function test_question($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale'], '/new-question/errors'); try { $model = new Model(); $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die($msg->_('invalid-format'), 415); } $input_data = json_decode($raw_input, true); if (empty($input_data)) { Util::output_errors_and_die($msg->_('invalid-format'), 400); } set_empty_if_undefined($input_data['type']); if (!Validator::validate_question_type($input_data['type'])) { Util::output_errors_and_die($msg->_('invalid-type'), 400); } switch ($input_data['type']) { case 'short-answer': $q = new QuestionSA($input_data, Question::FROM_USER); break; case 'essay': $q = new QuestionES($input_data, Question::FROM_USER); break; case 'multiple-choice': $q = new QuestionMC($input_data, Question::FROM_USER); break; case 'matching': $q = new QuestionMA($input_data, Question::FROM_USER); break; case 'fitb-type': $q = new QuestionFT($input_data, Question::FROM_USER); break; case 'fitb-select': $q = new QuestionFS($input_data, Question::FROM_USER); break; case 'source-code': $q = new QuestionSC($input_data, Question::FROM_USER); break; } http_response_code(200); header('Content-Type: application/json'); echo my_json_encode($q->to_auto_marking_test(true, true)); } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }