/** * Generate module contents * * @return void */ public function run() { require_once Component::path('com_answers') . DS . 'models' . DS . 'question.php'; // randomly choose one $rows = Question::all()->select('id')->whereEquals('state', 0)->ordered()->rows()->toArray(); $key = array_rand($rows); $row = Question::oneOrNew($rows[$key]['id']); // Did we have a result to display? if ($row->get('id')) { $this->cls = trim($this->params->get('moduleclass_sfx')); $this->txt_length = trim($this->params->get('txt_length')); $this->row = $row; $config = Component::params('com_answers'); $this->thumb = DS . trim($this->params->get('defaultpic', '/core/modules/mod_featuredquestion/assets/img/question_thumb.gif'), DS); if ($this->thumb == '/modules/mod_featuredquestion/question_thumb.gif') { $this->thumb = '/core/modules/mod_featuredquestion/assets/img/question_thumb.gif'; } require $this->getLayoutPath(); } }
/** * Calculate the market value * * @param integer $id Question ID * @param string $type Transaction type * @return mixed */ public function calculate_marketvalue($id, $type = 'regular') { if ($id === NULL) { $id = $this->qid; } if ($id === NULL) { return false; } require_once dirname(__DIR__) . DS . 'models' . DS . 'question.php'; // Get point values for actions $BC = Config::values(); $p_Q = $BC->get('ask'); $p_A = $BC->get('answer'); $p_R = $BC->get('answervote'); $p_RQ = $BC->get('questionvote'); $p_A_accepted = $BC->get('accepted'); $calc = 0; // Get actons and sum up $results = Response::all()->whereEquals('question_id', $id)->where('state', '!=', 2)->rows(); if ($type != 'royalty') { $calc += $p_Q; // ! this is different from version before code migration ! $calc += $results->count() * $p_A; } // Calculate as if there is at leat one answer if ($type == 'maxaward' && $results->count() == 0) { $calc += $p_A; } foreach ($results as $result) { $calc += $result->get('helpful') * $p_R; $calc += $result->get('nothelpful') * $p_R; if ($result->get('state') == 1 && $type != 'royalty') { $accepted = 1; } } if (isset($accepted) or $type == 'maxaward') { $calc += $p_A_accepted; } // Add question votes $aq = Question::oneOrNew($id); if ($aq->get('state') != 2) { $calc += $aq->get('helpful') * $p_RQ; } $calc = $calc ? $calc : '0'; return $calc; }
/** * Save a question * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option)) { App::abort(403, Lang::txt('JERROR_ALERTNOAUTHOR')); } // Incoming data $fields = Request::getVar('question', array(), 'post', 'none', 2); $tags = null; if (isset($fields['tags'])) { $tags = $fields['tags']; unset($fields['tags']); } // Initiate model $row = Question::oneOrNew($fields['id'])->set($fields); // Ensure we have at least one tag if (!$tags) { Notify::error(Lang::txt('COM_ANSWERS_ERROR_QUESTION_MUST_HAVE_TAGS')); return $this->editTask($row); } $row->set('email', isset($fields['email']) ? 1 : 0); $row->set('anonymous', isset($fields['anonymous']) ? 1 : 0); // Store content if (!$row->save()) { Notify::error($row->getError()); return $this->editTask($row); } // Add the tag(s) $row->tag($tags, User::get('id')); Notify::success(Lang::txt('COM_ANSWERS_QUESTION_SAVED')); if ($this->getTask() == 'apply') { return $this->editTask($row); } // Redirect back to the full questions list $this->cancelTask(); }
/** * Save a question * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Incoming data $fields = Request::getVar('question', array(), 'post', 'none', 2); $tags = null; if (isset($fields['tags'])) { $tags = $fields['tags']; unset($fields['tags']); } // Initiate model $row = Question::oneOrNew($fields['id'])->set($fields); // Ensure we have at least one tag if (!$tags) { Notify::error(Lang::txt('COM_ANSWERS_ERROR_QUESTION_MUST_HAVE_TAGS')); return $this->editTask($row); } $row->set('email', isset($fields['email']) ? 1 : 0); $row->set('anonymous', isset($fields['anonymous']) ? 1 : 0); // Store content if (!$row->save()) { Notify::error($row->getError()); return $this->editTask($row); } // Add the tag(s) $row->tag($tags, User::get('id')); Notify::success(Lang::txt('COM_ANSWERS_QUESTION_SAVED')); if ($this->getTask() == 'apply') { return $this->editTask($row); } // Redirect back to the full questions list App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); }
/** * Delete a question * * @apiMethod DELETE * @apiUri /answers/questions/{id} * @apiParameter { * "name": "id", * "description": "Question identifier", * "type": "integer", * "required": true, * "default": 0 * } * @return void */ public function deleteTask() { $this->requiresAuthentication(); $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; if (count($ids) <= 0) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_ID'), 500); } foreach ($ids as $id) { $row = Question::oneOrNew(intval($id)); if (!$row->get('id')) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_RECORD'), 404); } if (!$row->destroy()) { throw new Exception($row->getError(), 500); } } $this->send(null, 204); }
/** * Save a question * * @return void */ public function saveqTask() { // Check for request forgeries Request::checkToken(); // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); return $this->loginTask(); } if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option) && !User::authorise('core.manage', $this->_option)) { App::abort(403, Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN')); } // Incoming $fields = Request::getVar('fields', array(), 'post', 'none', 2); $tags = Request::getVar('tags', ''); if (!isset($fields['reward'])) { $fields['reward'] = 0; } // If offering a reward, do some checks if ($fields['reward']) { // Is it an actual number? if (!is_numeric($fields['reward'])) { App::abort(500, Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC')); } // Are they offering more than they can afford? if ($fields['reward'] > $fields['funds']) { App::abort(500, Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS')); } } unset($fields['funds']); // clean input array_walk($fields, function (&$field, $key) { $field = \Hubzero\Utility\Sanitize::clean($field); }); // Initiate class and bind posted items to database fields $row = Question::oneOrNew($fields['id'])->set($fields); if ($fields['reward'] && $this->config->get('banking')) { $row->set('reward', 1); } // Store new content if (!Request::checkHoneypot()) { $this->setError(Lang::txt('JLIB_APPLICATION_ERROR_INVALID_CONTENT')); return $this->newTask($row); } // Ensure the user added a tag if (!$tags) { $this->setError(Lang::txt('COM_ANSWERS_QUESTION_MUST_HAVE_TAG')); return $this->newTask($row); } // Store new content if (!$row->save()) { Request::setVar('tag', $tags); $this->setError($row->getError()); return $this->newTask($row); } // Hold the reward for this question if we're banking if ($fields['reward'] && $this->config->get('banking')) { $BTL = new Teller(User::get('id')); $BTL->hold($fields['reward'], Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id')); } // Add the tags $row->tag($tags); // Log activity $recipients = array($row->get('created_by')); $recipients = $this->recipients($recipients); Event::trigger('system.logActivity', ['activity' => ['action' => $fields['id'] ? 'updated' : 'created', 'scope' => 'question', 'scope_id' => $row->get('id'), 'anonymous' => $row->get('anonymous', 0), 'description' => Lang::txt('COM_ANSWERS_ACTIVITY_QUESTION_' . ($fields['id'] ? 'UPDATED' : 'CREATED'), '<a href="' . Route::url($row->link()) . '">' . $row->get('subject') . '</a>'), 'details' => array('title' => $row->get('title'), 'url' => $row->link())], 'recipients' => $recipients]); // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $row->get('id')), Lang::txt('COM_ANSWERS_NOTICE_QUESTION_POSTED_THANKS')); }
/** * Save a question and redirect to the main listing when done * * @return void */ private function _save() { // Login required if (User::isGuest()) { return $this->_browse(); } // Check for request forgeries Request::checkToken(); Lang::load('com_answers'); // Incoming $tags = Request::getVar('tags', ''); $funds = Request::getInt('funds', 0); $reward = Request::getInt('reward', 0); // If offering a reward, do some checks if ($reward) { // Is it an actual number? if (!is_numeric($reward)) { App::abort(500, Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC')); return; } // Are they offering more than they can afford? if ($reward > $funds) { App::abort(500, Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS')); return; } } // Initiate class and bind posted items to database fields $fields = Request::getVar('question', array(), 'post', 'none', 2); $row = \Components\Answers\Models\Question::oneOrNew($fields['id'])->set($fields); if ($reward && $this->banking) { $row->set('reward', 1); } // Store new content if (!$row->save()) { $this->setError($row->getError()); return $this->_new($row); } // Hold the reward for this question if we're banking if ($reward && $this->banking) { $BTL = new \Hubzero\Bank\Teller(User::get('id')); $BTL->hold($reward, Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id')); } // Add the tags $row->tag($tags); // Add the tag to link to the resource $tag = $this->model->isTool() ? 'tool:' . $this->model->resource->alias : 'resource:' . $this->model->resource->id; $row->addTag($tag, User::get('id'), $this->model->isTool() ? 0 : 1); // Get users who need to be notified on every question $config = Component::params('com_answers'); $apu = $config->get('notify_users', ''); $apu = explode(',', $apu); $apu = array_map('trim', $apu); $receivers = array(); // Get tool contributors if question is about a tool if ($tags) { $tags = explode(',', $tags); if (count($tags) > 0) { require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'author.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'version.php'; $TA = new \Components\Tools\Tables\Author($this->database); $objV = new \Components\Tools\Tables\Version($this->database); if ($this->model->isTool()) { $toolname = $this->model->resource->alias; $rev = $objV->getCurrentVersionProperty($toolname, 'revision'); $authors = $TA->getToolAuthors('', 0, $toolname, $rev); if (count($authors) > 0) { foreach ($authors as $author) { $receivers[] = $author->uidNumber; } } } } } if (!empty($apu)) { foreach ($apu as $u) { $user = User::getInstance($u); if ($user) { $receivers[] = $user->get('id'); } } } $receivers = array_unique($receivers); // Send the message if (!empty($receivers)) { // Send a message about the new question to authorized users (specified admins or related content authors) $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U'))); // Build the message subject $subject = Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('new question about content you author or manage'); // Build the message $eview = new \Hubzero\Mail\View(array('base_path' => PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'site', 'name' => 'emails', 'layout' => 'question_plaintext')); $eview->option = 'com_answers'; $eview->sitename = Config::get('sitename'); $eview->question = $row; $eview->id = $row->get('id', 0); $eview->boundary = $from['multipart']; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML message $eview->setLayout('question_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); if (!Event::trigger('xmessage.onSendMessage', array('new_question_admin', $subject, $message, $from, $receivers, 'com_answers'))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->option . '&id=' . $this->model->resource->id . '&active=' . $this->_name)); }
/** * Save a question and redirect to the main listing when done * * @return void */ private function _save() { // Login required if (User::isGuest()) { return $this->_browse(); } // Check for request forgeries Request::checkToken(); // Incoming $tags = Request::getVar('tags', ''); $funds = Request::getInt('funds', 0); $reward = Request::getInt('reward', 0); // If offering a reward, do some checks if ($reward) { // Is it an actual number? if (!is_numeric($reward)) { App::abort(500, Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC')); return; } // Are they offering more than they can afford? if ($reward > $funds) { App::abort(500, Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS')); return; } } // Initiate class and bind posted items to database fields $fields = Request::getVar('question', array(), 'post', 'none', 2); $row = \Components\Answers\Models\Question::oneOrNew($fields['id'])->set($fields); if ($reward && $this->banking) { $row->set('reward', 1); } // Store new content if (!$row->save()) { $this->setError($row->getError()); return $this->_new($row); } // Hold the reward for this question if we're banking if ($reward && $this->banking) { $BTL = new \Hubzero\Bank\Teller($this->database, User::get('id')); $BTL->hold($reward, Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id')); } // Add the tags $row->tag($tags); // Add the tag to link to the publication $identifier = $this->publication->get('alias') ? $this->publication->get('alias') : $this->publication->get('id'); $tag = $this->publication->isTool() ? 'tool' . $identifier : 'publication' . $identifier; $row->addTag($tag, User::get('id'), $this->publication->isTool() ? 0 : 1); // Redirect to the question App::redirect(Route::url($this->publication->link() . '&active=questions')); }
/** * Save a question * * @return void */ public function saveqTask() { // Check for request forgeries Request::checkToken(); // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); return $this->loginTask(); } if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option) && !User::authorise('core.manage', $this->_option)) { App::abort(403, Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN')); } // Incoming $fields = Request::getVar('fields', array(), 'post', 'none', 2); $tags = Request::getVar('tags', ''); if (!isset($fields['reward'])) { $fields['reward'] = 0; } // If offering a reward, do some checks if ($fields['reward']) { // Is it an actual number? if (!is_numeric($fields['reward'])) { throw new Exception(Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC'), 500); } // Are they offering more than they can afford? if ($fields['reward'] > $fields['funds']) { throw new Exception(Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS'), 500); } } unset($fields['funds']); // clean input array_walk($fields, function (&$field, $key) { $field = \Hubzero\Utility\Sanitize::clean($field); }); // Initiate class and bind posted items to database fields $row = Question::oneOrNew($fields['id'])->set($fields); if ($fields['reward'] && $this->config->get('banking')) { $row->set('reward', 1); } // Store new content if (!Request::checkHoneypot()) { $this->setError(Lang::txt('JLIB_APPLICATION_ERROR_INVALID_CONTENT')); return $this->newTask($row); } // Ensure the user added a tag if (!$tags) { $this->setError(Lang::txt('COM_ANSWERS_QUESTION_MUST_HAVE_TAG')); return $this->newTask($row); } // Store new content if (!$row->save()) { Request::setVar('tag', $tags); $this->setError($row->getError()); return $this->newTask($row); } // Hold the reward for this question if we're banking if ($fields['reward'] && $this->config->get('banking')) { $db = App::get('db'); $BTL = new Teller($db, User::get('id')); $BTL->hold($fields['reward'], Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id')); } // Add the tags $row->tag($tags); // Get users who need to be notified on every question $apu = $this->config->get('notify_users', ''); $apu = explode(',', $apu); $apu = array_map('trim', $apu); $receivers = array(); // Get tool contributors if question is about a tool if ($tags) { $tags = preg_split("/[,;]/", $tags); if (count($tags) > 0) { require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'author.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'version.php'; $db = App::get('db'); $TA = new \Components\Tools\Tables\Author($db); $objV = new \Components\Tools\Tables\Version($db); foreach ($tags as $tag) { if ($tag == '') { continue; } if (preg_match('/tool:/', $tag)) { $toolname = preg_replace('/tool:/', '', $tag); if (trim($toolname)) { $rev = $objV->getCurrentVersionProperty($toolname, 'revision'); $authors = $TA->getToolAuthors('', 0, $toolname, $rev); if (count($authors) > 0) { foreach ($authors as $author) { $receivers[] = $author->uidNumber; } } } } } } } if (!empty($apu)) { foreach ($apu as $u) { $user = User::getInstance($u); if ($user) { $receivers[] = $user->get('id'); } } } $receivers = array_unique($receivers); // Send the message if (!empty($receivers)) { // Send a message about the new question to authorized users (specified admins or related content authors) $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U'))); // Build the message subject $subject = Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('new question about content you author or manage'); $message = array(); // Plain text message $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'question_plaintext')); $eview->option = $this->_option; $eview->sitename = Config::get('sitename'); $eview->question = $row; $eview->id = $row->get('id', 0); $eview->boundary = $from['multipart']; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML message $eview->setLayout('question_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); if (!Event::trigger('xmessage.onSendMessage', array('new_question_admin', $subject, $message, $from, $receivers, $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $row->get('id')), Lang::txt('COM_ANSWERS_NOTICE_QUESTION_POSTED_THANKS')); }