function _validate() { $vm = self::createValidationManager(); $vm->register('value', new Validator\RequireValidator($this->value), 'Value of the rating is required'); $vm->register('value', new Validator\FunctionValidator(function () { return in_array((int) $this->value, Rating::getScalar()); }), 'Rating value is invalid'); $vm->doValidate(); }
public function rate($id) { $this->respondTo('html', function () use($id) { try { $userSession = UserSession::getInstance(); if (!$userSession->isSignedIn()) { throw new ActionNotAuthorizedException(); } Rating::create($userSession->getUser(), Product::find($id), $this->getRequest()->getParams()); $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ResourceNotFoundException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ValidationException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (DuplicateResourceException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } catch (ActionNotAuthorizedException $e) { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'show', [$id]); } }); }
function positiveRatingPercent() { $ratings = Rating::findBy(array('product' => $this)); $positive_ratings = array_filter($ratings, function ($rating) { return (int) $rating->value >= 4; }); if (!count($ratings)) { return 0; } return 100 * count($positive_ratings) / count($ratings); }