/** * Retrieves a user's questions * * @param string $kind The kind of results to retrieve * @param array $interests Array of tags * @return array Database results */ private function _getQuestions($kind = 'open', $interests = array()) { // Get some classes we need require_once Component::path('com_answers') . DS . 'models' . DS . 'question.php'; require_once Component::path('com_answers') . DS . 'helpers' . DS . 'economy.php'; $limit = intval($this->params->get('limit', 10)); $tags = null; $records = \Components\Answers\Models\Question::all()->including(['responses', function ($response) { $response->select('id')->select('question_id'); }])->whereEquals('state', 0); if ($kind == 'mine') { $records->whereEquals('created_by', User::get('id')); } if ($kind == 'interest') { $tags = count($interests) <= 0 ? $this->_getInterests() : $interests; } if ($kind == 'assigned') { require_once Component::path('com_tools') . DS . 'tables' . DS . 'author.php'; $database = \App::get('db'); $TA = new \Components\Tools\Tables\Author($database); $tools = $TA->getToolContributions(User::get('id')); if ($tools) { foreach ($tools as $tool) { $tags .= 'tool' . $tool->toolname . ','; } $tags = rtrim($tags, ','); } } if ($tags) { $cloud = new \Components\Answers\Models\Tags(); $tags = $cloud->parse($tags); $records->select('#__answers_questions.*')->join('#__tags_object', '#__tags_object.objectid', '#__answers_questions.id')->join('#__tags', '#__tags.id', '#__tags_object.tagid')->whereEquals('#__tags_object.tbl', 'answers')->whereIn('#__tags.tag', $tags); } $data = $records->limit($limit)->ordered()->rows(); $results = array(); foreach ($data as $datum) { $datum->set('rcount', $datum->responses->count()); $results[] = $datum; } if ($this->banking && $results) { $database = \App::get('db'); $AE = new \Components\Answers\Helpers\Economy($database); $awards = array(); foreach ($results as $result) { // Calculate max award $result->set('marketvalue', round($AE->calculate_marketvalue($result->get('id'), 'maxaward'))); $result->set('maxaward', round(2 * ($result->get('marketvalue', 0) / 3))); if ($kind != 'mine') { $result->set('maxaward', $result->get('maxaward') + $result->get('reward')); } $awards[] = $result->get('maxaward', 0); } // re-sort by max reponses array_multisort($awards, SORT_DESC, $results); } return $results; }
/** * Retrieves a user's questions * * @param string $kind The kind of results to retrieve * @param array $interests Array of tags * @return array Database results */ private function _getQuestions($kind = 'open', $interests = array()) { $database = \App::get('db'); // Get some classes we need require_once Component::path('com_answers') . DS . 'models' . DS . 'question.php'; require_once Component::path('com_answers') . DS . 'tables' . DS . 'response.php'; require_once Component::path('com_answers') . DS . 'tables' . DS . 'log.php'; require_once Component::path('com_answers') . DS . 'tables' . DS . 'questionslog.php'; require_once Component::path('com_answers') . DS . 'helpers' . DS . 'economy.php'; $aq = new \Components\Answers\Tables\Question($database); if ($this->banking) { $AE = new \Components\Answers\Helpers\Economy($database); $BT = new \Hubzero\Bank\Transaction($database); } $params =& $this->params; $moduleclass = $params->get('moduleclass'); $limit = intval($params->get('limit', 10)); $limit = $limit ? $limit : 10; $filters = array('limit' => $limit, 'start' => 0, 'tag' => '', 'filterby' => 'open', 'sortby' => 'date'); switch ($kind) { case 'mine': $filters['mine'] = 1; $filters['sortby'] = 'responses'; break; case 'assigned': $filters['mine'] = 0; require_once Component::path('com_tools') . DS . 'tables' . DS . 'author.php'; $TA = new \Components\Tools\Tables\Author($database); $tools = $TA->getToolContributions(User::get('id')); if ($tools) { foreach ($tools as $tool) { $filters['tag'] .= 'tool' . $tool->toolname . ','; } } if (!$filters['tag']) { $filters['filterby'] = 'none'; } break; case 'interest': $filters['mine'] = 0; $interests = count($interests) <= 0 ? $this->_getInterests() : $interests; $filters['filterby'] = !$interests ? 'none' : 'open'; $filters['tag'] = $interests; break; } $results = $aq->getResults($filters); if ($this->banking && $results) { $awards = array(); foreach ($results as $result) { // Calculate max award $result->marketvalue = round($AE->calculate_marketvalue($result->id, 'maxaward')); $result->maxaward = round(2 * ($result->marketvalue / 3)); if ($kind != 'mine') { $result->maxaward = $result->maxaward + $result->reward; } $awards[] = $result->maxaward ? $result->maxaward : 0; } // re-sort by max reponses array_multisort($awards, SORT_DESC, $results); } foreach ($results as $k => $result) { $results[$k] = new \Components\Answers\Models\Question($result); } return $results; }
/** * Calculate royalties * * @return void */ public function royaltyTask() { $auto = Request::getInt('auto', 0); $action = 'royalty'; if (!$auto) { $who = User::get('id'); } else { $who = 0; } // What month/year is it now? $curmonth = Date::of('now')->format("F"); $curyear = Date::of('now')->format("Y-m"); $ref = strtotime($curyear); $this->_message = 'Royalties on Answers for ' . $curyear . ' were distributed successfully.'; $rmsg = 'Royalties on Reviews for ' . $curyear . ' were distributed successfully.'; $resmsg = 'Royalties on Resources for ' . $curyear . ' were distributed successfully.'; // Make sure we distribute royalties only once/ month $MH = new MarketHistory($this->database); $royaltyAnswers = $MH->getRecord('', $action, 'answers', $curyear, $this->_message); $royaltyReviews = $MH->getRecord('', $action, 'reviews', $curyear, $rmsg); $royaltyResources = $MH->getRecord('', $action, 'resources', $curyear, $resmsg); // Include economy classes if (is_file(PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'helpers' . DS . 'economy.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'helpers' . DS . 'economy.php'; } if (is_file(PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'economy.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'economy.php'; } $AE = new \Components\Answers\Helpers\Economy($this->database); $accumulated = 0; // Get Royalties on Answers if (!$royaltyAnswers) { $rows = $AE->getQuestions(); if ($rows) { foreach ($rows as $r) { $AE->distribute_points($r->id, $r->q_owner, $r->a_owner, $action); $accumulated = $accumulated + $AE->calculate_marketvalue($r->id, $action); } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = new MarketHistory($this->database); $data['itemid'] = $ref; $data['date'] = Date::toSql(); $data['market_value'] = $accumulated; $data['category'] = 'answers'; $data['action'] = $action; $data['log'] = $this->_message; if (!$MH->bind($data)) { $err = $MH->getError(); } if (!$MH->store()) { $err = $MH->getError(); } } } else { $this->_message = 'There were no questions eligible for royalty payment. '; } } else { $this->_message = 'Royalties on Answers for ' . $curyear . ' were previously distributed. '; } // Get Royalties on Resource Reviews if (!$royaltyReviews) { // get eligible $RE = new \Components\Resources\Helpers\Economy\Reviews($this->database); $reviews = $RE->getReviews(); // do we have ratings on reviews enabled? $plparam = Plugin::params('resources', 'reviews'); $voting = $plparam->get('voting'); $accumulated = 0; if ($reviews && $voting) { foreach ($reviews as $r) { $RE->distribute_points($r, $action); $accumulated = $accumulated + $RE->calculate_marketvalue($r, $action); } $this->_message .= $rmsg; } else { $this->_message .= 'There were no reviews eligible for royalty payment. '; } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = new MarketHistory($this->database); $data['itemid'] = $ref; $data['date'] = Date::toSql(); $data['market_value'] = $accumulated; $data['category'] = 'reviews'; $data['action'] = $action; $data['log'] = $rmsg; if (!$MH->bind($data)) { $err = $MH->getError(); } if (!$MH->store()) { $err = $MH->getError(); } } } else { $this->_message .= 'Royalties on Reviews for ' . $curyear . ' were previously distributed. '; } // Get Royalties on Resources if (!$royaltyResources) { // get eligible $ResE = new \Components\Resources\Helpers\Economy\Reviews($this->database); $cons = $ResE->getCons(); $accumulated = 0; if ($cons) { foreach ($cons as $con) { $ResE->distribute_points($con, $action); $accumulated = $accumulated + $con->ranking; } $this->_message .= $resmsg; } else { $this->_message .= 'There were no resources eligible for royalty payment.'; } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = new MarketHistory($this->database); $data['itemid'] = $ref; $data['date'] = Date::toSql(); $data['market_value'] = $accumulated; $data['category'] = 'resources'; $data['action'] = $action; $data['log'] = $resmsg; if (!$MH->bind($data)) { $err = $MH->getError(); } if (!$MH->store()) { $err = $MH->getError(); } } } else { $this->_message .= 'Royalties on Resources for ' . $curyear . ' were previously distributed.'; } if (!$auto) { // show output if run manually App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt($this->_message)); } }
/** * Calculate point royalties for members * * @param object $job \Components\Cron\Models\Job * @return boolean */ public function onPointRoyalties(\Components\Cron\Models\Job $job) { $this->database = App::get('db'); $action = 'royalty'; // What month/year is it now? $curmonth = Date::format("F"); $curyear = Date::format("Y-m"); $ref = strtotime($curyear); $this->_message = Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_DISTRIBUTED_ANSWERS', $curyear); $rmsg = Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_DISTRIBUTED_REVIEWS', $curyear); $resmsg = Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_DISTRIBUTED_RESOURCES', $curyear); // Make sure we distribute royalties only once/ month $royaltyAnswers = \Hubzero\Bank\MarketHistory::getRecord('', $action, 'answers', $curyear, $this->_message); $royaltyReviews = \Hubzero\Bank\MarketHistory::getRecord('', $action, 'reviews', $curyear, $rmsg); $royaltyResources = \Hubzero\Bank\MarketHistory::getRecord('', $action, 'resources', $curyear, $resmsg); // Include economy classes if (is_file(PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'helpers' . DS . 'economy.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'helpers' . DS . 'economy.php'; } if (is_file(PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'economy.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'economy.php'; } $AE = new \Components\Answers\Helpers\Economy($this->database); $accumulated = 0; // Get Royalties on Answers if (!$royaltyAnswers) { $rows = $AE->getQuestions(); if ($rows) { foreach ($rows as $r) { $AE->distribute_points($r->id, $r->q_owner, $r->a_owner, $action); $accumulated = $accumulated + $AE->calculate_marketvalue($r->id, $action); } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = \Hubzero\Bank\MarketHistory::blank()->set(array('itemid' => $ref, 'date' => Date::toSql(), 'market_value' => $accumulated, 'category' => 'answers', 'action' => $action, 'log' => $this->_message)); if (!$MH->save()) { $err = $MH->getError(); } } } else { $this->_message = Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_NO_QUESTIONS'); } } else { $this->_message = Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_ALREADY_DISTRIBUTED_ANSWERS', $curyear); } // Get Royalties on Resource Reviews if (!$royaltyReviews) { // get eligible $RE = new \Components\Resources\Helpers\Economy\Reviews($this->database); $reviews = $RE->getReviews(); // do we have ratings on reviews enabled? $param = Plugin::byType('resources', 'reviews'); $plparam = new \Hubzero\Config\Registry($param->params); $voting = $plparam->get('voting'); $accumulated = 0; if ($reviews && $voting) { foreach ($reviews as $r) { $RE->distribute_points($r, $action); $accumulated = $accumulated + $RE->calculate_marketvalue($r, $action); } $this->_message .= $rmsg; } else { $this->_message .= Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_NO_REVIEWS'); } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = \Hubzero\Bank\MarketHistory::blank()->set(array('itemid' => $ref, 'date' => Date::toSql(), 'market_value' => $accumulated, 'category' => 'reviews', 'action' => $action, 'log' => $rmsg)); if (!$MH->save()) { $err = $MH->getError(); } } } else { $this->_message .= Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_ALREADY_DISTRIBUTED_REVIEWS', $curyear); } // Get Royalties on Resources if (!$royaltyResources) { // get eligible $ResE = new \Components\Resources\Helpers\Economy($this->database); $cons = $ResE->getCons(); $accumulated = 0; if ($cons) { foreach ($cons as $con) { $ResE->distribute_points($con, $action); $accumulated = $accumulated + $con->ranking; } $this->_message .= $resmsg; } else { $this->_message .= Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_NO_RESOURCES'); } // make a record of royalty payment if (intval($accumulated) > 0) { $MH = \Hubzero\Bank\MarketHistory::blank()->set(array('itemid' => $ref, 'date' => Date::toSql(), 'market_value' => $accumulated, 'category' => 'resources', 'action' => $action, 'log' => $resmsg)); if (!$MH->save()) { $err = $MH->getError(); } } } else { $this->_message .= Lang::txt('PLG_CRON_MEMBERS_POINT_ROYALTIES_ALREADY_DISTRIBUTED_RESOURCES', $curyear); } return true; }