Exemplo n.º 1
0
 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();
 }
Exemplo n.º 2
0
 public function test_deleting_vote_should_invalidate_cache()
 {
     $q = self::factory()->question->create_and_get();
     self::factory()->vote->create(array('user_id' => 5, 'item' => $q, 'value' => 1));
     $v = new \WeBWorK\Server\Vote();
     $v->set_user_id(5);
     $v->set_item($q);
     $v->populate();
     $v->delete();
     $this->assertSame(0, $q->get_vote_count());
 }
Exemplo n.º 3
0
 public function test_delete_success()
 {
     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->assertTrue($v->exists());
     $this->assertTrue($v->delete());
     $v2 = new \WeBWorK\Server\Vote();
     $v2->set_user_id(5);
     $v2->set_item(new MockVoteable(10));
     $v2->populate();
     $this->assertFalse($v2->exists());
 }
Exemplo n.º 4
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;
 }