Example #1
0
 /**
  * Update a question
  *
  * @apiMethod PUT
  * @apiUri    /answers/questions/{id}
  * @apiParameter {
  * 		"name":        "id",
  * 		"description": "Question identifier",
  * 		"type":        "integer",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "email",
  * 		"description": "Notify user of responses",
  * 		"type":        "integer",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "anonymous",
  * 		"description": "List author as anonymous or not",
  * 		"type":        "integer",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "subject",
  * 		"description": "Short, one-line question",
  * 		"type":        "string",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "question",
  * 		"description": "Longer, detailed question",
  * 		"type":        "string",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "created",
  * 		"description": "Created timestamp (YYYY-MM-DD HH:mm:ss)",
  * 		"type":        "string",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "crated_by",
  * 		"description": "User ID of entry creator",
  * 		"type":        "integer",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "state",
  * 		"description": "Published state (0 = unpublished, 1 = published)",
  * 		"type":        "integer",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "reward",
  * 		"description": "Reward points",
  * 		"type":        "integer",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "tags",
  * 		"description": "Comma-separated list of tags",
  * 		"type":        "string",
  * 		"required":    false,
  * 		"default":     null
  * }
  * @return    void
  */
 public function updateTask()
 {
     $this->requiresAuthentication();
     $fields = array('id' => Request::getInt('id', 0, 'post'), 'email' => Request::getInt('email', null), 'anonymous' => Request::getInt('anonymous', null), 'subject' => Request::getVar('subject', null, '', 'none', 2), 'question' => Request::getVar('question', null, '', 'none', 2), 'created' => Request::getVar('created', null), 'created_by' => Request::getInt('created_by', null), 'state' => Request::getInt('state', null), 'reward' => Request::getInt('reward', null), 'tags' => Request::getVar('tags', null));
     $row = new Question($fields['id']);
     if (!$row->exists()) {
         throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_RECORD'), 404);
     }
     if (!$row->bind($fields)) {
         throw new Exception(Lang::txt('COM_ANSWERS_ERROR_BINDING_DATA'), 422);
     }
     $row->set('email', isset($fields['email']) ? 1 : 0);
     $row->set('anonymous', isset($fields['anonymous']) ? 1 : 0);
     if (!$row->store(true)) {
         throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_DATA'), 500);
     }
     if (isset($fields['tags'])) {
         if (!$row->tag($fields['tags'], User::get('id'))) {
             throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_TAGS'), 500);
         }
     }
     $this->send($row);
 }
Example #2
0
 /**
  * Save a question
  *
  * @return  void
  */
 public function saveTask()
 {
     // Check for request forgeries
     Request::checkToken();
     // Incoming data
     $fields = Request::getVar('question', array(), 'post', 'none', 2);
     // Initiate model
     $row = new Question($fields['id']);
     if (!$row->bind($fields)) {
         Notify::error($row->getError());
         return $this->editTask($row);
     }
     // Ensure we have at least one tag
     if (!isset($fields['tags']) || !$fields['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->store(true)) {
         Notify::error($row->getError());
         return $this->editTask($row);
     }
     // Add the tag(s)
     $row->tag($fields['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));
 }
Example #3
0
 /**
  * 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'));
         $this->loginTask();
         return;
     }
     if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option) && !User::authorise('core.manage', $this->_option)) {
         throw new Exception(Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
     }
     // 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);
         }
     }
     // clean input
     array_walk($fields, function (&$field, $key) {
         $field = \Hubzero\Utility\Sanitize::clean($field);
     });
     // Initiate class and bind posted items to database fields
     $row = new Question($fields['id']);
     if (!$row->bind($fields)) {
         throw new Exception($row->getError(), 500);
     }
     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'));
         $this->newTask($row);
         return;
     }
     // Ensure the user added a tag
     if (!$tags) {
         $this->setError(Lang::txt('COM_ANSWERS_QUESTION_MUST_HAVE_TAG'));
         $this->newTask($row);
         return;
     }
     // We need to temporarily set this so the store() method
     // has access to the tags string to be able to run it
     // through spam checkers and validation.
     $row->set('tags', $tags);
     // Store new content
     if (!$row->store(true)) {
         Request::setVar('tag', $tags);
         $this->setError($row->getError());
         $this->newTask($row);
         return;
     }
     // Hold the reward for this question if we're banking
     if ($fields['reward'] && $this->config->get('banking')) {
         $BTL = new Teller($this->database, 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';
             $TA = new \Components\Tools\Tables\Author($this->database);
             $objV = new \Components\Tools\Tables\Version($this->database);
             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'));
 }