如何检查用户是否对评价进行了评分?

时间:2015-01-26 00:40:15

标签: php html mysql symfony rating-system

我目前正在开发一个使用5星评级的书评系统。我想为每个评论用户帖子添加评级系统。因此,我创建了一个表来存储每个费率和当前被评级的bookId,以及id,review和发布的userId如下:

id  Review  rating  AppUser_id  Book_id
46  reviw1  2           6          23
53  reviw2  3           6         23 

以下是明星的html:

    {% if UserId >0 %}
    <form class="form-signin" action="/BookReview{% if ReviewId != -1 %}/{{ ReviewId }}{% endif %}/{{ act }}" method="POST">

        <h2 class="form-signin-heading">Please Post Your Review</h2>
        <label for="Review" class="sr-only">Review</label>

        <textarea name="review" id="Review" class="form-control" placeholder="Your Review Here" required autofocus>{{ review }}</textarea>

        <button class="btn btn-lg btn-primary btn-block" type="submit" >Submit</button>
        <fieldset class="rating">
            <input type="radio" id="star5" name="rating5" value="5" /><label for="star5" title="Rocks!">5 stars</label>
            <input type="radio" id="star4" name="rating4" value="4" /><label for="star4" title="Pretty good">4 stars</label>
            <input type="radio" id="star3" name="rating3" value="3" /><label for="star3" title="Meh">3 stars</label>
            <input type="radio" id="star2" name="rating2" value="2" /><label for="star2" title="Kinda bad">2 stars</label>
            <input type="radio" id="star1" name="rating1" value="1" /><label for="star1" title="Sucks big time">1 star</label>
        </fieldset>
    </form>
    {% endif %}

以下是控制器:

    if ($request->getMethod() == 'POST' and $act == -1) {
        $em = $this->getDoctrine()->getManager();
        $review = $request->request->get('review');
        $review = $request->request->get('BookId');
        $rate1 = $request->request->get('rating1');
        $rate2 = $request->request->get('rating2');
        $rate3 = $request->request->get('rating3');
        $rate4 = $request->request->get('rating4');
        $rate5 = $request->request->get('rating5');


        $br = new BookReview();
        $br->setReview($review);
        $repository = $this->getDoctrine()->getRepository('AppBundle:AppUser');
        $User = $repository->findOneBy(array('id' => $userId));
        $br->setAppUser($User);
        $br->setBook($book);


            if($rate1)
            {
                $br->setRating(1);

            }elseif($rate2)
            {
                $br->setRating(2);
            }elseif($rate3)
            {
                $br->setRating(3);
            }elseif($rate4)
            {
                $br->setRating(4);
            }elseif($rate5)
            {
                $br->setRating(5);
            }


        $em->persist($br);
        $em->flush();
        $request->getSession()->getFlashBag()->add('success', 'Review Added');

        return array('book' => $book, 'reviews' => $book->getBookReviews(), 'Title' => '', 'Message' => '', 'id' => $userId, 'review' => '', 'ReviewId' => $ReviewId, 'act' => $act);
    }

这是我的问题,如何检查用户之前是否对同一本书进行了评分,如果他之前已经对该书进行了评分,那么使用上述控制器上完全提供的代码部分设置费率:

if($rate1)
                {
                    $br->setRating(1);

                }elseif($rate2)
                {
                    $br->setRating(2);
                }elseif($rate3)
                {
                    $br->setRating(3);
                }elseif($rate4)
                {
                    $br->setRating(4);
                }elseif($rate5)
                {
                    $br->setRating(5);
                }

第二个问题是如何计算书籍的总体平均评分。我正在使用symfony2(PHP)

以下是我的尝试似乎不起作用:

        $br = new BookReview();

        $br->setReview($review);
        $repository = $this->getDoctrine()->getRepository('AppBundle:AppUser');
        $User = $repository->findOneBy(array('id' => $userId));
        $br->setAppUser($User);
        $br->setBook($book);
        $book_id = $br->getBook(); $request->request->get('book_id');
        $user_id = $br->getAppUser(); $request->request->get('user_id');
        $query = $em->createQuery('SELECT br FROM AppBundle:BookReview br WHERE (br.Book = ' . $book_id . ' and br.AppUser = ' . $user_id . ')');

        $results = $query->getResult();



        if ($results){
            $request->getSession()->getFlashBag()->add('success', 'no Added');

        }


        else{

            if($rate1)
            {
                $br->setRating(1);

            }elseif($rate2)
            {
                $br->setRating(2);
            }elseif($rate3)
            {
                $br->setRating(3);
            }elseif($rate4)
            {
                $br->setRating(4);
            }elseif($rate5)
            {
                $br->setRating(5);
            }
        }

3 个答案:

答案 0 :(得分:1)

首先你的控制器和视图看起来有点凌乱。您需要为评级字段提供不同的名称?它是单选按钮。只需为所有单选按钮设置相同的名称即可。然后,您将不需要if-else条件来确定选择了哪个评级值。

要知道用户是否已经查看了某些书籍,您是否需要知道是否存在具有此类user_id和book_id的条目。这种检索不需要返回结果中的对象,并且执行简单的sql以获得具有适当值的行数要容易得多。您可以使用控制器中的database_connection服务来执行此操作:

$this->get('database_connection')
     ->fetchColumn('select count(*) as cnt 
                    from book_reviews 
                    where user_id = ? and book_id = ?', array($userId, $bookId));

或者来自存储库:

public function userHasAlreadyReviewedBook($userId, $bookId)
{
    $connection = $this->getEntityManager()->getConnection();
    $query = 'select count(*) as cnt 
              from book_reviews 
              where user_id = ? and book_id = ?';

    return $connection->fetchColumn($query, array($userId, $bookId)) > 0;
}

或者您可以为此类操作指定特殊服务并注入&#39; database_connection&#39;进入它。

如果上述请求将返回大于零的内容,则用户已经查看了该书。

要获得书评评级的平均值,您可以再次使用database_connection服务(当然,如果您不需要在某些复杂的数据集中使用此值):

对于评论中的每本书:

$connection->fetchAll('select avg(rating) as average_rating 
                       from book_reviews 
                       group by book_id');

或某些特别的书:

$connection->fetchColumn('select avg(rating) as average_rating 
                          from book_reviews 
                          where book_id = ?', array($bookId));

希望这会有所帮助。

答案 1 :(得分:0)

在您的存储库中,您可以添加以下内容:

public function getCurrentRated($bookId, $userId)
{
$sql = 'SELECT avg(rating) as totalRated FROM your_table WHERE AppUser_id = :userId and Book_id = :bookId group by Book_id';
$params = array(
    'userId' => $userId,
    'bookId' => $bookId,
);

return $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchAll();
}

然后在你的控制器中调用它:

$currentRated = $repository->getCurrentRated($bookId, $userId);

答案 2 :(得分:0)

首先,我强烈建议您为所有收音机输入提供rating(不是rating1rating2,...)的名称。这样,您就可以通过获取$request->request->get('rating')的值来了解用户对该图书的评分。

现在如何检查用户是否已对该图书进行评分?使用此查询:

$repository = $this->getDoctrine()->getRepository('AppBundle:BookReview');
$bookReview = $repository->findOneBy(array('AppUser_id' => $userId, 'Book_id' => $bookId));

if (!$bookReview) {
    $bookReview = new BookReview();
    $bookReview->setUser($user);
    $bookReview->setBook($book);
    $this->getDoctrine()->getManager()->persist();
}

$bookReview->setRating($request->request->get('rating'));

$this->getDoctrine()->getManager()->flush();