<commit_before><?php
/**
 * HUBzero CMS
 *
 * Copyright 2005-2011 Purdue University. All rights reserved.
 *
 * This file is part of: The HUBzero(R) Platform for Scientific Collaboration
 *
 * The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
 * software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * HUBzero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * HUBzero is a registered trademark of Purdue University.
 *
 * @package   hubzero-cms
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2005-2011 Purdue University. All rights reserved.
 * @license   http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

/**
 * Answers controller class for questions
 */
class AnswersControllerQuestions extends \Hubzero\Component\SiteController
{
	/**
	 * Execute a task
	 * 
	 * @return     void
	 */
	public function execute()
	{
		$this->config->set('banking', JComponentHelper::getParams('com_members')->get('bankAccounts'));

		$this->registerTask('__default', 'search');
		$this->registerTask('latest', 'latest.rss');

		parent::execute();
	}

	/**
	 * Build the document pathway (breadcrumbs)
	 * 
	 * @param      object $question AnswersTableQuestion
	 * @return     void
	 */
	protected function _buildPathway($question=null)
	{
		$pathway = JFactory::getApplication()->getPathway();

		if (count($pathway->getPathWay()) <= 0) 
		{
			$pathway->addItem(
				JText::_(strtoupper($this->_option)),
				'index.php?option=' . $this->_option
			);
		}
		if ($this->_task && in_array($this->_task, array('new', 'myquestions', 'search'))) 
		{
			$pathway->addItem(
				JText::_(strtoupper($this->_option) . '_' . strtoupper($this->_task)),
				'index.php?option=' . $this->_option . '&task=' . $this->_task
			);
		}
		if (is_object($question) && $question->get('subject')) 
		{
			$pathway->addItem(
				\Hubzero\Utility\String::truncate($question->subject('clean'), 50),
				$question->link()
			);
		}
	}

	/**
	 * Build the document title
	 * 
	 * @param      object $question AnswersTableQuestion
	 * @return     void
	 */
	protected function _buildTitle($question=null)
	{
		$this->view->title = JText::_(strtoupper($this->_option));
		if ($this->_task && $this->_task != 'view') 
		{
			$this->view->title .= ': ' . JText::_(strtoupper($this->_option) . '_' . strtoupper($this->_task));
		}
		if (is_object($question) && $question->get('subject')) 
		{
			$this->view->title .= ': ' . \Hubzero\Utility\String::truncate($question->subject('clean'), 50);
		}
		$document = JFactory::getDocument();
		$document->setTitle($this->view->title);
	}

	/**
	 * Display the latest entries
	 * 
	 * @return     void
	 */
	public function displayTask()
	{
		$this->view->setLayout('search');
		return $this->searchTask();
	}

	/**
	 * Redirect to login form
	 * 
	 * @return     void
	 */
	public function loginTask()
	{
		$rtrn = JRequest::getVar('REQUEST_URI', JRoute::_('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false, true), 'server');

		$this->setRedirect(
			JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode($rtrn), false)
		);
	}

	/**
	 * Save a reply
	 * 
	 * @return     void
	 */
	public function savereplyTask()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			$this->setError(JText::_('COM_ANSWERS_LOGIN_TO_COMMENT'));
			$this->loginTask();
			return;
		}

		// Incoming
		$comment = JRequest::getVar('comment', array(), 'post', 'none', 2);

		if (!$comment['item_id']) 
		{
			JError::raiseError(500, JText::_('COM_ANSWERS_ERROR_QUESTION_ID_NOT_FOUND'));
			return;
		}

		if ($comment['item_type']) 
		{
			$row = new AnswersModelComment(0);
			if (!$row->bind($comment)) 
			{
				JError::raiseError(500, $row->getError());
				return;
			}

			// Perform some text cleaning, etc.
			$row->set('content', nl2br($row->get('content')));
			$row->set('anonymous', ($row->get('anonymous') ? 1 : 0));
			$row->set('created', JFactory::getDate()->toSql());
			$row->set('state', 0);
			$row->set('created_by', $this->juser->get('id'));

			// Save the data
			if (!$row->store(true)) 
			{
				JError::raiseError(500, $row->getError());
				return;
			}
		}

		$this->setRedirect(
			JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . JRequest::getInt('rid', 0))
		);
	}

	/**
	 * Reply to an answer
	 * 
	 * @return     void
	 */
	public function replyTask()
	{
		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			$this->setError(JText::_('COM_ANSWERS_LOGIN_TO_COMMENT'));
			$this->loginTask();
			return;
		}

		// Retrieve a review or comment ID and category
		$id    = JRequest::getInt('id', 0);
		$refid = JRequest::getInt('refid', 0);
		$cat   = JRequest::getVar('category', '');

		// Do we have an ID?
		if (!$id) 
		{
			// Cannot proceed
			$this->setRedirect(
				JRoute::_('index.php?option=' . $this->_option)
			);
			return;
		}

		// Do we have a category?
		if (!$cat) 
		{
			// Cannot proceed
			$this->setRedirect(
				JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $id)
			);
			return;
		}

		// Store the comment object in our registry
		$this->category = $cat;
		$this->referenceid = $refid;
		$this->qid = $id;
		$this->questionTask();
	}

	/**
	 * Rate an item
	 * 
	 * @return     void
	 */
	public function rateitemTask()
	{
		$no_html = JRequest::getInt('no_html', 0);

		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			if (!$no_html) 
			{
				$this->addComponentMessage(JText::_('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE'));
				$this->loginTask();
			}
			return;
		}

		// Incoming
		$id      = JRequest::getInt('refid', 0);
		$cat     = JRequest::getVar('category', '');
		$vote    = JRequest::getVar('vote', '');
		$ip      = JRequest::ip();

		// Check for reference ID
		if (!$id) 
		{
			// cannot proceed
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option),
					JText::_('No ID provided.'),
					'error'
				);
			}
			return;
		}

		// load answer
		$row = new AnswersModelResponse($id);

		$qid = $row->get('question_id');

		// Can't vote for your own comment
		if ($row->get('created_by') == $this->juser->get('username'))
		{
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $qid),
					JText::_('Cannot vote for your own entries.'),
					'warning'
				);
			}
			return;
		}

		// Can't vote for your own comment
		if (!$vote)
		{
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $qid),
					JText::_('No vote provided.'),
					'warning'
				);
			}
			return;
		}

		// Get vote log
		$al = new AnswersTableLog($this->database);
		$al->loadByIp($id, $ip);

		if (!$al->id) 
		{
			// new vote;
			// record if it was helpful or not
			switch ($vote) 
			{
				case 'yes':
				case 'like':
				case 'up':
				case 1:
					$row->set('helpful', $row->get('helpful') + 1);
				break;

				case 'no':
				case 'dislike':
				case 'down':
				case -1:
					$row->set('nothelpful', $row->get('nothelpful') + 1);
				break;
			} 
		}
		else if ($al->helpful != $vote)
		{
			// changing vote;
			// Adjust values to reflect vote change
			switch ($vote) 
			{
				case 'yes':
				case 'like':
				case 'up':
				case 1:
					$row->set('helpful', $row->get('helpful') + 1);
					$row->set('nothelpful', $row->get('nothelpful') - 1);
				break;

				case 'no':
				case 'dislike':
				case 'down':
				case -1:
					$row->set('helpful', $row->get('helpful') - 1);
					$row->set('nothelpful', $row->get('nothelpful') + 1);
				break;
			} 
		}
		else
		{
			// no vote change;
		}

		if (!$row->store()) 
		{
			$this->setError($row->getError());
			return;
		}

		// Record user's vote (old way)
		$al->rid     = $row->get('id');
		$al->ip      = $ip;
		$al->helpful = $vote;
		if (!$al->check()) 
		{
			$this->setError($al->getError());
			return;
		}
		if (!$al->store()) 
		{
			$this->setError($al->getError());
			return;
		}

		// Record user's vote (new way)
		if ($cat) 
		{
			require_once(JPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . $this->_option . DS . 'vote.class.php');

			$v = new Vote($this->database);
			$v->referenceid = $row->get('id');
			$v->category    = $cat;
			$v->voter       = $this->juser->get('id');
			$v->ip          = $ip;
			$v->voted       = JFactory::getDate()->toSql();
			$v->helpful     = $vote;
			if (!$v->check()) 
			{
				$this->setError($v->getError());
				return;
			}
			if (!$v->store()) 
			{
				$this->setError($v->getError());
				return;
			}
		}

		// update display
		if ($no_html) 
		{
			//$response = $row->getResponse($id, $ip);
			$row->set('vote', $vote);

			$this->view->option = $this->_option;
			$this->view->item   = $row; //new AnswersModelResponse($response[0]);
			if ($this->getError()) 
			{
				foreach ($this->getErrors() as $error)
				{
					$this->view->setError($error);
				}
			}
			$this->view->display();
		} 
		else 
		{
			$this->setRedirect(<commit_msg>[HUBZERO][#4220][COM_ANSWERS] Fixed issue caused by a misnamed property (due to DB table column being renamed)<commit_after><?php
/**
 * HUBzero CMS
 *
 * Copyright 2005-2011 Purdue University. All rights reserved.
 *
 * This file is part of: The HUBzero(R) Platform for Scientific Collaboration
 *
 * The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
 * software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * HUBzero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * HUBzero is a registered trademark of Purdue University.
 *
 * @package   hubzero-cms
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2005-2011 Purdue University. All rights reserved.
 * @license   http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

/**
 * Answers controller class for questions
 */
class AnswersControllerQuestions extends \Hubzero\Component\SiteController
{
	/**
	 * Execute a task
	 * 
	 * @return     void
	 */
	public function execute()
	{
		$this->config->set('banking', JComponentHelper::getParams('com_members')->get('bankAccounts'));

		$this->registerTask('__default', 'search');
		$this->registerTask('latest', 'latest.rss');

		parent::execute();
	}

	/**
	 * Build the document pathway (breadcrumbs)
	 * 
	 * @param      object $question AnswersTableQuestion
	 * @return     void
	 */
	protected function _buildPathway($question=null)
	{
		$pathway = JFactory::getApplication()->getPathway();

		if (count($pathway->getPathWay()) <= 0) 
		{
			$pathway->addItem(
				JText::_(strtoupper($this->_option)),
				'index.php?option=' . $this->_option
			);
		}
		if ($this->_task && in_array($this->_task, array('new', 'myquestions', 'search'))) 
		{
			$pathway->addItem(
				JText::_(strtoupper($this->_option) . '_' . strtoupper($this->_task)),
				'index.php?option=' . $this->_option . '&task=' . $this->_task
			);
		}
		if (is_object($question) && $question->get('subject')) 
		{
			$pathway->addItem(
				\Hubzero\Utility\String::truncate($question->subject('clean'), 50),
				$question->link()
			);
		}
	}

	/**
	 * Build the document title
	 * 
	 * @param      object $question AnswersTableQuestion
	 * @return     void
	 */
	protected function _buildTitle($question=null)
	{
		$this->view->title = JText::_(strtoupper($this->_option));
		if ($this->_task && $this->_task != 'view') 
		{
			$this->view->title .= ': ' . JText::_(strtoupper($this->_option) . '_' . strtoupper($this->_task));
		}
		if (is_object($question) && $question->get('subject')) 
		{
			$this->view->title .= ': ' . \Hubzero\Utility\String::truncate($question->subject('clean'), 50);
		}
		$document = JFactory::getDocument();
		$document->setTitle($this->view->title);
	}

	/**
	 * Display the latest entries
	 * 
	 * @return     void
	 */
	public function displayTask()
	{
		$this->view->setLayout('search');
		return $this->searchTask();
	}

	/**
	 * Redirect to login form
	 * 
	 * @return     void
	 */
	public function loginTask()
	{
		$rtrn = JRequest::getVar('REQUEST_URI', JRoute::_('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false, true), 'server');

		$this->setRedirect(
			JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode($rtrn), false)
		);
	}

	/**
	 * Save a reply
	 * 
	 * @return     void
	 */
	public function savereplyTask()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			$this->setError(JText::_('COM_ANSWERS_LOGIN_TO_COMMENT'));
			$this->loginTask();
			return;
		}

		// Incoming
		$comment = JRequest::getVar('comment', array(), 'post', 'none', 2);

		if (!$comment['item_id']) 
		{
			JError::raiseError(500, JText::_('COM_ANSWERS_ERROR_QUESTION_ID_NOT_FOUND'));
			return;
		}

		if ($comment['item_type']) 
		{
			$row = new AnswersModelComment(0);
			if (!$row->bind($comment)) 
			{
				JError::raiseError(500, $row->getError());
				return;
			}

			// Perform some text cleaning, etc.
			$row->set('content', nl2br($row->get('content')));
			$row->set('anonymous', ($row->get('anonymous') ? 1 : 0));
			$row->set('created', JFactory::getDate()->toSql());
			$row->set('state', 0);
			$row->set('created_by', $this->juser->get('id'));

			// Save the data
			if (!$row->store(true)) 
			{
				JError::raiseError(500, $row->getError());
				return;
			}
		}

		$this->setRedirect(
			JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . JRequest::getInt('rid', 0))
		);
	}

	/**
	 * Reply to an answer
	 * 
	 * @return     void
	 */
	public function replyTask()
	{
		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			$this->setError(JText::_('COM_ANSWERS_LOGIN_TO_COMMENT'));
			$this->loginTask();
			return;
		}

		// Retrieve a review or comment ID and category
		$id    = JRequest::getInt('id', 0);
		$refid = JRequest::getInt('refid', 0);
		$cat   = JRequest::getVar('category', '');

		// Do we have an ID?
		if (!$id) 
		{
			// Cannot proceed
			$this->setRedirect(
				JRoute::_('index.php?option=' . $this->_option)
			);
			return;
		}

		// Do we have a category?
		if (!$cat) 
		{
			// Cannot proceed
			$this->setRedirect(
				JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $id)
			);
			return;
		}

		// Store the comment object in our registry
		$this->category = $cat;
		$this->referenceid = $refid;
		$this->qid = $id;
		$this->questionTask();
	}

	/**
	 * Rate an item
	 * 
	 * @return     void
	 */
	public function rateitemTask()
	{
		$no_html = JRequest::getInt('no_html', 0);

		// Is the user logged in?
		if ($this->juser->get('guest')) 
		{
			if (!$no_html) 
			{
				$this->addComponentMessage(JText::_('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE'));
				$this->loginTask();
			}
			return;
		}

		// Incoming
		$id      = JRequest::getInt('refid', 0);
		$cat     = JRequest::getVar('category', '');
		$vote    = JRequest::getVar('vote', '');
		$ip      = JRequest::ip();

		// Check for reference ID
		if (!$id) 
		{
			// cannot proceed
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option),
					JText::_('No ID provided.'),
					'error'
				);
			}
			return;
		}

		// load answer
		$row = new AnswersModelResponse($id);

		$qid = $row->get('question_id');

		// Can't vote for your own comment
		if ($row->get('created_by') == $this->juser->get('username'))
		{
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $qid),
					JText::_('Cannot vote for your own entries.'),
					'warning'
				);
			}
			return;
		}

		// Can't vote for your own comment
		if (!$vote)
		{
			if (!$no_html)
			{
				$this->setRedirect(
					JRoute::_('index.php?option=' . $this->_option . '&task=question&id=' . $qid),
					JText::_('No vote provided.'),
					'warning'
				);
			}
			return;
		}

		// Get vote log
		$al = new AnswersTableLog($this->database);
		$al->loadByIp($id, $ip);

		if (!$al->id) 
		{
			// new vote;
			// record if it was helpful or not
			switch ($vote) 
			{
				case 'yes':
				case 'like':
				case 'up':
				case 1:
					$row->set('helpful', $row->get('helpful') + 1);
				break;

				case 'no':
				case 'dislike':
				case 'down':
				case -1:
					$row->set('nothelpful', $row->get('nothelpful') + 1);
				break;
			} 
		}
		else if ($al->helpful != $vote)
		{
			// changing vote;
			// Adjust values to reflect vote change
			switch ($vote) 
			{
				case 'yes':
				case 'like':
				case 'up':
				case 1:
					$row->set('helpful', $row->get('helpful') + 1);
					$row->set('nothelpful', $row->get('nothelpful') - 1);
				break;

				case 'no':
				case 'dislike':
				case 'down':
				case -1:
					$row->set('helpful', $row->get('helpful') - 1);
					$row->set('nothelpful', $row->get('nothelpful') + 1);
				break;
			} 
		}
		else
		{
			// no vote change;
		}

		if (!$row->store(false)) 
		{
			$this->setError($row->getError());
			return;
		}

		// Record user's vote (old way)
		$al->response_id = $row->get('id');
		$al->ip      = $ip;
		$al->helpful = $vote;
		if (!$al->check()) 
		{
			echo $al->getError();
			$this->setError($al->getError());
			return;
		}
		if (!$al->store()) 
		{
			echo $al->getError();
			$this->setError($al->getError());
			return;
		}

		// Record user's vote (new way)
		if ($cat) 
		{
			require_once(JPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . $this->_option . DS . 'vote.class.php');

			$v = new Vote($this->database);
			$v->referenceid = $row->get('id');
			$v->category    = $cat;
			$v->voter       = $this->juser->get('id');
			$v->ip          = $ip;
			$v->voted       = JFactory::getDate()->toSql();
			$v->helpful     = $vote;
			if (!$v->check()) 
			{
				echo $v->getError();
				$this->setError($v->getError());
				return;
			}
			if (!$v->store()) 
			{
				echo $v->getError();
				$this->setError($v->getError());
				return;
			}
		}

		// update display
		if ($no_html) 
		{
			//$response = $row->getResponse($id, $ip);
			$row->set('vote', $vote);

			$this->view->option = $this->_option;
			$this->view->item   = $row; //new AnswersModelResponse($response[0]);
			if ($this->getError()) 
			{
				foreach ($this->getErrors() as $error)
				{
					$this->view->setError($error);
				}
			}
			$this->view->display();
		} 
		else 
		{
			$this->setRedirect(
{'fraction_non_alphanumeric': 0.19425368612401536, 'fraction_numerical': 0.0060593819430418095, 'mean_word_length': 2.953883010580954, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 315, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1684184', 'n_tokens_mistral': 8300, 'n_tokens_neox': 6944, 'n_words': 1495}