/** * Process an attribute setting POST * args['context'] might be: installed * args['key'] holds the package ID * @param args see above */ public function post_setattribute(array $args) { $summary = ''; $success = true; if (!$this->user) { // this operation requires authentication $auth = com_meego_ocs_utils::authenticate($args); if (!$auth) { com_meego_ocs_utils::end_with_error('This interface is available for authenticated users only', 199); } } if (!isset($args['context'])) { com_meego_ocs_utils::end_with_error('Mandatory context missing (e.g. installed)', 102); } if (!isset($args['key'])) { com_meego_ocs_utils::end_with_error('Mandatory package ID missing', 103); } // check if the context is supported switch ($args['context']) { case 'save': $summary = 'User succesfully installed an application.'; break; case 'unsave': $summary = 'User succesfully uninstalled an application.'; break; case 'savefail': $summary = 'Application installation failed.'; break; default: com_meego_ocs_utils::end_with_error('This context: ' . $args['context'] . ' is not supported', 104); } $package = new com_meego_package(); try { $package->get_by_id((int) $args['key']); } catch (Exception $e) { $success = false; com_meego_ocs_utils::end_with_error('Package with id: ' . $args['key'] . ' not found', 105); } if ($success) { $res = midgardmvc_account_controllers_activity::create_activity($this->user->person, $args['context'], $package->guid, $summary, 'Apps'); if (!$res) { com_meego_ocs_utils::end_with_error('Failed to create activity object.', 106); } } // everything went fine $ocs = new com_meego_ocs_OCSWriter(); $ocs->writeMeta(null, null, 'Attribute setting succeded.', 'ok', 100); $ocs->endDocument(); com_meego_ocs_utils::output_xml($ocs); }
/** * 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); }