/** * 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!'))); }
/** * checks to see if the current user has rated this object * by checking against the rating SessionID and MemberID * @return Boolean **/ public function UserHasRated() { return $this->rateableService->userHasRated($this->owner->ClassName, $this->owner->ID); }