public function create_object($args) { $vote = new \WeBWorK\Server\Vote(); $vote->set_item($args['item']); $vote->set_user_id($args['user_id']); $vote->set_value($args['value']); $vote->save(); return $vote->get_id(); }
public function test_successful_save_for_new_item() { $v = new \WeBWorK\Server\Vote(); $v->set_user_id(5); $v->set_item(new MockVoteable(10)); $v->set_value(15); $saved = $v->save(); $this->assertTrue($saved); }
public function create_item($request) { $params = $request->get_params(); $item_id = $params['item_id']; $raw_value = $params['value']; $value = null; if ('up' === $raw_value) { $value = 1; } elseif ('down' === $raw_value) { $value = -1; } $item = null; switch ($params['item_type']) { case 'question': $item = new \WeBWorK\Server\Question($item_id); break; case 'response': $item = new \WeBWorK\Server\Response($item_id); break; } $vote = new \WeBWorK\Server\Vote(); $vote->set_user_id(get_current_user_id()); $vote->set_item($item); $vote->populate(); // Don't allow duplicate votes. // This is not really RESTful. On a successful lookup, perform an update. $retval = false; if ($vote->exists() && $value === $vote->get_value()) { // do something } elseif ($value) { $vote->set_value($value); $retval = $vote->save(); } elseif ($vote->exists()) { $retval = $vote->delete(); } $response = rest_ensure_response($retval); if ($retval) { $response->set_status(201); } else { // We return 200 anyway. Not sure how to give good error feedback here. $response->set_status(200); } return $response; }