예제 #1
0
 public function test_creation_sets_values_properly()
 {
     self::factory()->vote->create(array('user_id' => 5, 'item' => new MockVoteable(10), 'value' => 15));
     $v = new \WeBWorK\Server\Vote();
     $v->set_user_id(5);
     $v->set_item(new MockVoteable(10));
     $v->populate();
     $this->assertSame(5, $v->get_user_id());
     $this->assertSame(10, $v->get_item_id());
     $this->assertSame(15, $v->get_value());
 }
예제 #2
0
 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;
 }