public function testUserSumRating() { Rating::unguard(); $post1 = Post::find(1); $post2 = Post::find(2); $user1 = User::find(1); Rating::create(['id' => 5, 'rating' => 1, 'rateable_id' => $post1->id, 'rateable_type' => 'Post', 'user_id' => $user1->id]); Rating::create(['id' => 6, 'rating' => 1, 'rateable_id' => $post2->id, 'rateable_type' => 'Post', 'user_id' => $user1->id]); Rating::reguard(); $this->assertEquals(6, Post::find(1)->userSumRating); $this->assertEquals(6, Post::find(1)->userSumRating()); $this->assertEquals(2, Post::find(2)->userSumRating); $this->assertEquals(2, Post::find(2)->userSumRating()); }
public function run() { DB::table('ratings')->delete(); $user_id = User::where('email', '*****@*****.**')->first()->user_id; $user_1 = User::where('email', '*****@*****.**')->first()->user_id; $user_2 = User::where('email', '*****@*****.**')->first()->user_id; $wine_1 = Wine::where('wine_id', 1)->first()->wine_unique_id; $wine_2 = Wine::where('wine_id', 2)->first()->wine_unique_id; $wine_3 = Wine::where('wine_id', 3)->first()->wine_unique_id; Rating::create(array('id' => '1', 'user_id' => $user_id, 'wine_unique_id' => $wine_1, 'rate' => '0.5', 'content' => 'this is rating 1', 'like_count' => '5', 'comment_count' => '5', 'is_my_wine' => '1')); Rating::create(array('id' => '2', 'user_id' => $user_id, 'wine_unique_id' => $wine_2, 'rate' => '1.5', 'content' => 'this is rating 2', 'like_count' => '5', 'comment_count' => '5', 'is_my_wine' => '1')); Rating::create(array('id' => '3', 'user_id' => $user_id, 'wine_unique_id' => $wine_3, 'rate' => '4.5', 'content' => 'this is rating 3', 'like_count' => '8', 'comment_count' => '5', 'is_my_wine' => '1')); Rating::create(array('id' => '4', 'user_id' => $user_1, 'wine_unique_id' => $wine_1, 'rate' => '5', 'content' => 'this is rating 7', 'like_count' => '6', 'comment_count' => '15', 'is_my_wine' => '1')); Rating::create(array('id' => '5', 'user_id' => $user_2, 'wine_unique_id' => $wine_2, 'rate' => '5', 'content' => 'this is rating 8', 'like_count' => '14', 'comment_count' => '8', 'is_my_wine' => '1')); }
public function run() { DB::table('ratings')->delete(); Rating::unguard(); $post1 = Post::find(1); $post2 = Post::find(2); $post3 = Post::find(3); $user1 = User::find(1); $user2 = User::find(2); Rating::create(['id' => 1, 'rating' => 5, 'rateable_id' => $post1->id, 'rateable_type' => 'Post', 'user_id' => $user1->id]); Rating::create(['id' => 2, 'rating' => 5, 'rateable_id' => $post1->id, 'rateable_type' => 'Post', 'user_id' => $user2->id]); Rating::create(['id' => 3, 'rating' => 1, 'rateable_id' => $post2->id, 'rateable_type' => 'Post', 'user_id' => $user1->id]); Rating::create(['id' => 4, 'rating' => 5, 'rateable_id' => $post2->id, 'rateable_type' => 'Post', 'user_id' => $user2->id]); Rating::reguard(); }
/** * action for rating an object * @return JSON **/ public function rate($request) { $class = $request->param('ObjectClassName'); $id = (int) $request->param('ObjectID'); $score = (int) $request->getVar('score'); // check we have all the params if (!class_exists($class) || !$id || !$score || !($object = $class::get()->byID($id))) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORMESSAGE', 'Sorry, there was an error rating this item'))); } // check the object exists if (!$object || !$object->checkRatingsEnabled()) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORNOTFOUNT', 'Sorry, the item you are trying to rate could not be found'))); } // check the user can rate the object $ratingRecord = $this->rateableService->userGetRating($class, $id); if ($ratingRecord) { if (!$object->canChangeRating()) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORALREADYRATED', 'Sorry, You have already rated this item'))); } // If clicked same score as before, remove rating if ($score == $ratingRecord->Score) { // Remove rating $ratingRecord->delete(); // Success return Convert::raw2json(array('status' => 'success', 'isremovingrating' => 1, 'averagescore' => $object->getAverageScore(), 'numberofratings' => $object->getNumberOfRatings(), 'message' => _t('RateableController.RATINGREMOVED', 'Your rating has been removed!'))); } } // check if score is valid $isScoreValid = false; $scoreOptions = $object->getRatingOptions(); if ($scoreOptions) { foreach ($scoreOptions as $scoreOption) { $isScoreValid = $isScoreValid || $score == $scoreOption->Score; } } if (!$isScoreValid) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORINVALIDRATING', 'You sent an invalid rating.'))); } // create the rating $isRatingNew = !$ratingRecord; if (!$ratingRecord) { $ratingRecord = Rating::create(array('ObjectID' => $id, 'ObjectClass' => $class)); } $ratingRecord->Score = $score; $ratingRecord->write(); // success return Convert::raw2json(array('status' => 'success', 'isnew' => $isRatingNew, 'averagescore' => $object->getAverageScore(), 'numberofratings' => $object->getNumberOfRatings(), 'message' => $isRatingNew ? _t('RateableController.THANKYOUMESSAGE', 'Thanks for rating!') : _t('RateableController.CHANGEMESSAGE', 'Your rating has been changed!'))); }
public function run() { DB::table('ratings')->delete(); // Going to randomize all our ratings for our users, so throw everything in // an array. $toRate = array(array('item' => Doctor::where('number', 9)->first(), 'type' => 'doctor'), array('item' => Doctor::where('number', 10)->first(), 'type' => 'doctor'), array('item' => Doctor::where('number', 11)->first(), 'type' => 'doctor'), array('item' => Doctor::where('number', 12)->first(), 'type' => 'doctor'), array('item' => Enemy::where('name', 'Cybermen')->first(), 'type' => 'enemy'), array('item' => Enemy::where('name', 'Daleks')->first(), 'type' => 'enemy'), array('item' => Enemy::where('name', 'Sontarans')->first(), 'type' => 'enemy'), array('item' => Enemy::where('name', 'The Silence')->first(), 'type' => 'enemy'), array('item' => Enemy::where('name', 'Weeping Angels')->first(), 'type' => 'enemy'), array('item' => Companion::where('name', 'Amy Pond')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Captain Jack')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Clara Oswald')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Donna Noble')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Martha Jones')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Mickey Smith')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'River Song')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Rory Williams')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Rose Tyler')->first(), 'type' => 'companion'), array('item' => Companion::where('name', 'Wilfred Mott')->first(), 'type' => 'companion'), array('item' => Episode::where('season', 1)->where('episode', 1)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 1)->where('episode', 6)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 1)->where('episode', 10)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 1)->where('episode', 12)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 2)->where('episode', 5)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 2)->where('episode', 12)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 3)->where('episode', 9)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 3)->where('episode', 10)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 4)->where('episode', 4)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 4)->where('episode', 13)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 5)->where('episode', 3)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 5)->where('episode', 4)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 5)->where('episode', 12)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 6)->where('episode', 1)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 6)->where('episode', 10)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 6)->where('episode', 13)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 7)->where('episode', 5)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 7)->where('episode', 12)->first(), 'type' => 'episode'), array('item' => Episode::where('season', 8)->where('episode', 2)->first(), 'type' => 'episode')); $users = User::get(); // Loop through our users foreach ($users as $user) { // Loop through things to be rated foreach ($toRate as $item) { $rating = rand(1, 5); Rating::create(array('user_id' => $user->id, 'item_id' => $item['item']->id, 'item_type' => $item['type'], 'rating' => $rating)); } } }
/** * action for rating an object * @return JSON **/ public function rate($request) { $class = $request->param('ObjectClassName'); $id = (int) $request->param('ObjectID'); $score = (int) $request->getVar('score'); // check we have all the params if (!class_exists($class) || !$id || !$score || !($object = $class::get()->byID($id))) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORMESSAGE', 'Sorry, there was an error rating this item'))); } // check the object exists if (!$object && !$object->checkRatingsEnabled()) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORNOTFOUNT', 'Sorry, the item you are trying to rate could not be found'))); } // check the user can rate the object if ($this->rateableService->userHasRated($class, $id)) { return Convert::raw2json(array('status' => 'error', 'message' => _t('RateableController.ERRORALREADYRATED', 'Sorry, You have already rated this item'))); } // create the rating $rating = Rating::create(array('Score' => $score, 'ObjectID' => $id, 'ObjectClass' => $class)); $rating->write(); // success return Convert::raw2json(array('status' => 'success', 'averagescore' => $object->getAverageScore(), 'message' => _t('RateableController.THANKYOUMESSAGE', 'Thanks for rating!'))); }
public function issueVote() { $politician_id = Input::get('politician_id'); $issue_id = Input::get('issue_id'); $user_id = Auth::user()->id; $vote_value = Input::get('vote'); $new_rating_array = array('politician_id' => $politician_id, 'user_id' => $user_id, 'issue_id' => $issue_id, 'value' => $vote_value); $rating = Rating::create($new_rating_array); //adjust politician rank $politician = Politician::find($politician_id); $politician->rank += $vote_value; $politician->save(); $title = $politician->full_name . "'s Approval Over Last 7 Days"; $chartController = new ChartController(); $response = $chartController->generatePoliticianChart($politician_id, $title); return Response::json($response); // return Response::json($new_rating_array); }