/** * Sets the average rating of the package * Sets $this->data['stars'] that can be directly put to pages showing * the stars */ public function get_average(array $args) { try { $this->data['to'] = midgard_object_class::get_object_by_guid($args['to']); } catch (midgard_error_exception $e) { midgardmvc_core::get_instance()->log(__CLASS__, 'Package with guid: ' . $args['to'] . ' not found. ' . $e->getMessage(), 'error'); return false; } $this->data['repository'] = new com_meego_repository($this->data['to']->repository); com_meego_ratings_controllers_rating::get_read($args); $storage = new midgard_query_storage('com_meego_package_statistics_calculated'); $q = new midgard_query_select($storage); $q->set_constraint(new midgard_query_constraint(new midgard_query_property('packageguid'), '=', new midgard_query_value($this->data['to']->guid))); $q->execute(); $cache = $q->list_objects(); $this->data['average'] = 0; $this->data['numberofratings'] = 0; $this->data['numberofcomments'] = 0; $this->data['rated'] = false; //load data from cache if (count($cache) > 0) { $this->data['average'] = $cache[0]->ratingvalue; $this->data['numberofratings'] = $cache[0]->ratings; $this->data['numberofcomments'] = $cache[0]->comments; $this->data['rated'] = true; } com_meego_ratings_caching_controllers_rating::get_stars($this->data['average']); }
/** * Process vote posts */ public function post_vote(array $args) { $auth = false; $ocs = new com_meego_ocs_OCSWriter(); if ($this->user) { $auth = true; } else { // Voting requires authentication $auth = com_meego_ocs_utils::authenticate($args); } if ($auth) { $primary = new com_meego_package(); $primary->get_by_id((int) $args['contentid']); if (!$primary->guid) { $this->mvc->log(__CLASS__, 'Package with id: (with id:' . $args['contentid'] . ') can not be found', 'info'); $ocs->writeError('Content not found', 101); } else { $voted = false; // the multiple voting is configurable, pls check the config file if (!$this->mvc->configuration->allow_multiple_voting) { // if not allowed then check if the user has voted already if (com_meego_ocs_utils::user_has_voted($primary->id, $this->user->person)) { $this->mvc->log(__CLASS__, "{$this->user}->login has already voted for {$primary->name} (with id: {$primary->id}) and multiple votings are disabled", 'info'); $ocs->writeError('Multiple voting not allowed and user has already voted this object.', 103); } } if (!$ocs->error) { $rating = new com_meego_ratings_rating(); $rating->to = $primary->guid; $vote = $_POST['vote']; // incoming votes are ranging between 0 and 100 // our internal scale is different: 0 - 5 $vote = round($vote / 20); if ($vote > $this->mvc->configuration->maxrate) { $vote = $this->mvc->configuration->maxrate; } $rating->rating = $vote; // for votes only we have no comments $rating->comment = 0; if (!$rating->create()) { $this->mvc->log(__CLASS__, 'Failed to create rating object. User: '******', application: ' . $primary->name . ' (with id: ' . $primary->id . ')', 'info'); throw new midgardmvc_exception_notfound("Could not create rating object"); } $args = array('to' => $rating->to); com_meego_ratings_caching_controllers_rating::calculate_average($args); $ocs->writeMeta(0); $this->mvc->log(__CLASS__, 'Rating (' . $rating->rating . ') submitted by ' . $this->user->login . ' for ' . $primary->name . ' (with id: ' . $primary->id . ')', 'info'); // create activity object $verb = 'rate'; $summary = 'The user rated an application via OCS.'; $res = midgardmvc_account_controllers_activity::create_activity($rating->metadata->creator, $verb, $rating->to, $summary, 'Apps', $rating->metadata->created); } } } else { // extend the OCS spec with a custom status code $this->mvc->log(__CLASS__, 'Attempt to vote by anonymous. No luck.', 'info'); $ocs->writeError('Voting requires authentication. Please login first.', 102); } $ocs->endDocument(); self::output_xml($ocs); }