function create_topic($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale']); try { $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die('', 415); } $input_data = json_decode($raw_input, true); if (empty($input_data)) { Util::output_errors_and_die('', 400); } $model = new Model(); if (!isset($input_data['name'])) { $input_data['name'] = ''; } $topic_id = $model->create_topic($input_data['name']); if ($topic_id) { http_response_code(201); header('Content-Type: text/plain'); echo '/topics/' . $topic_id; die; } else { Util::output_errors_and_die('', 400); } } catch (ConflictException $e) { Util::output_errors_and_die($e->getMessage(), 409); } 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_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 create_question($request, $assignment_id = null) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale']); try { $model = new Model(); $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die('', 415); } $input_data = json_decode($raw_input, true); if (empty($input_data)) { Util::output_errors_and_die('', 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; } $qid = $model->create_question($q); header('Content-Type: text/plain'); echo '/question_bank/questions/' . $qid; http_response_code(201); die; } catch (ConflictException $e) { Util::output_errors_and_die($e->getMessage(), 409); } 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); } }
function get_programming_languages($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale']); try { $model = new Model(); $result = $model->get_programming_languages(); http_response_code(200); header('Content-Type: application/json'); echo my_json_encode($result); die; } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }
function delete_topic($topic_id, $request) { $msg = new Messages($GLOBALS['locale']); Authenticator::assert_manager_or_professor($request->cookies['authToken']); try { $model = new Model(); $result = $model->delete_topic($topic_id); if ($result) { http_response_code(204); die; } else { Util::output_errors_and_die('', 404); } } catch (ConflictException $e) { Util::output_errors_and_die('', 409); } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }
<?php $user_data = Authenticator::assert_manager_or_professor($_COOKIE['authToken']); $msg = new Messages($GLOBALS['locale']); $type_new = req_data('GET', 'new', false); $edit_id = req_data('GET', 'edit', false) or null; switch ($type_new) { case 'short-answer': $methods = ['binary', 'none']; break; case 'essay': $methods = ['none']; break; case 'fitb-select': $methods = ['binary', 'success-rate', 'none']; break; case 'fitb-type': $methods = ['binary', 'success-rate', 'none']; break; case 'matching': $methods = ['binary', 'success-rate', 'none']; break; case 'multiple-choice': $methods = ['binary', 'success-rate', 'none']; break; case 'source-code': $methods = ['input-output-bin', 'input-output-avg', 'none']; break; default: $methods = ['none']; }