Is there any way to create a rating system?

#1

Can Cockpit do this? I’m thinking about a system that user can click on star or thumbs button and this is sent to backend. When another user load the page, the rating code shows the new rating.

Check box in a form?
#2

Yes. create a collection "ratings" with a rating field (text field).

Also install the custom “Moderation” addon if you want an admin to moderate before a rating is published. In that case, 1) add a "published" field to the ratings collection with fieldtype “moderation” and 2) add the following to /addons/Moderation/bootstrap.php in order to make sure that the field never can be set to published without moderation:

// Include actions.
if (COCKPIT_API_REQUEST) {
  // continue logic.
  include_once __DIR__ . '/actions.php';
  // force the Draft status when posted by API:
  $app->on('collections.save.before.ratings', function($name, &$entry, $isUpdate) {
    $entry['published'] = 'Draft';
  });
}

Create an additional user i.e. “api-rating” in the cockpit user accounts. Add a new users group in config.yaml, call the group i.e. “apipostcalls”. Make sure to add this user to the “apipostcalls” group in the user accounts --> user (api-rating) --> groups on the right side. Also, copy the APITOKEN from this user.

Now, go to the permissions tab on the collections settings (collections --> edit). There you see the newly created group and have to activate "create entries" on this group.

You can create new entries with a POST call, i.e. with axios:

    axios
      .post(
        "/api/collections/save/ratings?token=ADDYOURAPIRATINGUSERTOKENHERE",
        {
          headers: { "Content-Type": "application/json" },
          data: {
            name: this.name,
            rating: 5,
            published: "Draft"
          }
        }
      )
      .then( response => {
        console.log(response)
      })
#3

Thank you for your help!
I forgot to ask. Is it possible to limit how many times a user can vote on each product? What I mean is a way to use click once and it’s done. Maybe with cookie or ip?