Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
Hello and Greetings to one and all and KK Sir.

I am trying to do a Rating and Review system for an ecommerce site.

I am using the following:
1. Comment System (viewtopic.php?f=8&t=8397)
2. Votes, Ratings & Polls Module (viewtopic.php?f=5&t=8133)

What I have:
1. product.php (holding the products, clonable template and the couch cart)
2. review.php (holding the product id on which review is being posted, user id of the user posting the review, the review, star rating given by the user)

review.php template
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Product Comments' clonable='1' parent="_product_" executable='0' order="4" >
   <cms:editable name='ticket_comments' type='relation' label="Product Reviewed" masterpage='product.php' has='one' order="1" required="1" />
   <cms:editable name='username' type='relation' label='Username' masterpage='users/index.php' has='one' order="2" required="1" />
   <cms:editable name='review' type='textarea' label='Review' order="3" required="1" />
   <cms:editable name='rating_by_user' label="Rating by User" type='text' order="4" />
</cms:template>
<?php COUCH::invoke(); ?>


Snippet from the product.php template that is implementing the review and rating
Code: Select all
<cms:editable name="rating" label="Product Rating" type="group" order="12" />
    <cms:editable name='rating_product' label="Product Rating" type='vote_stars' search_type='decimal' allow_zero_stars='0' group="rating" order="1" />

Code: Select all
<cms:form 
    masterpage='review.php'
    mode='create'
    enctype='multipart/form-data'
    method='post'
    anchor='0'
>
    <cms:if k_success >
      <!-- New Review Added -->
      <!-- k_page_title = Product Page Id - User Id -->
       <cms:db_persist_form
           _invalidate_cache='0'
           _auto_title='0'
           k_page_title = "<cms:show product_id_rating />-<cms:show k_user_id />"
           username=k_user_id
            ticket_comments=k_page_id
            rating_by_user = frm_rating_by_user
       />

       <cms:if k_success>
          <!-- Star Rating Update -->
          <cms:db_persist
             _masterpage="product.php"
             _mode='edit'
             _page_id=product_id_rating
            
             rating_product = frm_rating_by_user
         />                                          
                        <cms:set_flash name='submit_success' value='1' />
         <cms:redirect k_page_link />
      </cms:if>
   </cms:if>


    <cms:if k_error >
        <div class="row">
            <cms:each k_error >
               <div class="col-md-12">
                   <div class="alert alert-danger shadow-z-1">
                      <cms:show item />
                   </div>
               </div>
            </cms:each>
        </div>
    </cms:if>   


   <cms:if "<cms:not submit_success />" >
      <div class="col-md-3">
            <strong>Your Rating</strong>
            <div class="gxcpl-ptop-5"></div>
          <cms:input type='bound' name='rating_by_user' opt_values='1 | 2 | 3 | 4 | 5' />
            <div class="gxcpl-ptop-10"></div>
        </div>
       
        <div class="col-md-9">
           <cms:hide>
                <cms:input type="bound" name="review" placeholder="Your Review" class="form-control" />
            </cms:hide>
            <textarea name="f_review" class="form-control" placeholder="Your Review" validator_msg="required=You cannot submit a blank review" required="1"></textarea>
            <div class="gxcpl-ptop-10"></div>                       
        </div>
        <div class="col-md-12 text-center">
            <cms:input type="submit" class="btn gxcpl-btn-grey-blue" value="SUBMIT REVIEW" name="submit"/>
            <div class="gxcpl-ptop-40"></div>
        </div>
   </cms:if>
</cms:form>


The Issue
The review gets saved as intended, but the rating doesnot. What happens is that only the latest rating gets saved. while the remaining do not.
For example, if I give a 4-star rating, its saves in the backend. Now when I give a new rating, lets say 1 star, this saves in the back end, but the total votes are always shown to be 1.
How can all the star ratings be saved in the backend?

Thanks in advance.

Regards,
GenXCoders
Image
where innovation meets technology
Hi Aashish,

The 'votes' module was coded to work with the 'members' module - the 'extended-users' module has long since supplanted the 'members' module but the votes module, unfortunately, has not been updated to work with the new addon.

That said, the votes module only uses the info of whether or not a 'member' is logged-in to prevent users from casting their votes multiple times.
If a member ID is not found, the visitor is considered to be anonymous and then her IP address is used instead to prevent multiple voting (how long the same visitor is not allowed a second vote can be set from the config file present within the votes folder - for anonymous it is 6 hours by default).

So, the votes module can still work but will consider all visitors to be anonymous and will try to stop visitors from the same IP to vote multiple times.

That is what seems to be happening in your case -
you are testing the votes from the same IP and the module considers it a vote of the same user and so will update the last vote instead of creating a new entry.

Since you are using Couch's other features (relations etc.) to allow only logged-in users to vote and vote only once on a product, you don't need the votes module to exercise its default constraints.

To do that, edit addons/votes/config.php and set the 'define' shown below to '0' -
Code: Select all
// 2.
// Same as above but applies to anonymous voters (i.e. visitors that are not registered members).
// Since non-members can only be tracked by their IPs, this window should be kept small enough to allow
// visitors potentially sharing the same IP address to vote,
define( 'K_VOTE_WINDOW_ANON', 6 * 60 * 60 ); // time in seconds. Default 6 hours

i.e. make it as follows -
Code: Select all
define( 'K_VOTE_WINDOW_ANON', 0 );

And that should now allow multiple votes from the same IP address too.

Hope this helps.
@KK Sir,
Thanks a lot for posting the solution and its detailed explanation. As always, it indeed helps. I confirm that the review and rating module is working perfectly.
I will post the final barebone code here, so that it comes in handy for others.
Regards,
Aashish
Image
where innovation meets technology
3 posts Page 1 of 1