<?php

	class AKB_RECENTQUESTIONS_PANEL  extends AKB_PANEL
	{
		/**
		* SetPanelSettings
		* This function is a chance to set any last minute global variables for
		* the template to use
		*
		* @return void
		*/
		function SetPanelSettings()
		{
			// Parse the panel of tokens, etc
			$GLOBALS['RecentQuestionList'] = $this->GetRecentQuestionList();
			$GLOBALS['Link'] = GetUrl('rss', '', 't=recent');
			if ((empty($GLOBALS['frontendUser']) || empty($GLOBALS['frontendPass']))) {
				$GLOBALS['RssButton'] = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RssLink');
			} else {
				$GLOBALS['RssButton'] = '';
			}
		}

		/**
		* GetRecentQuestionList
		* Get a list of recently updated questions and generate the html for
		* displaying the list to the user
		*
		* @return string The html for the list
		*/
		function GetRecentQuestionList()
		{
			$categoryRestrictSQL = "";
			if (isset($GLOBALS['categoryRestrictOption']) && $GLOBALS['categoryRestrictOption'] == "1") {
				$accessible_cats = $GLOBALS['AKB_CLASS_HELPER']->GetPermittedCats();
				$categoryRestrict = implode(", ", $accessible_cats);
				if ($categoryRestrict == '') {
					$categoryRestrict = "''";
				}
				$categoryRestrictSQL = "AND c.categoryid IN (".$categoryRestrict.")";
			}
			
			if ((isset($GLOBALS['recentQuestionNumber'])) && ((int)$GLOBALS['recentQuestionNumber'] > 0)) {
				$recentQuestionNumber = (int)$GLOBALS['recentQuestionNumber'];
			} else {
				$recentQuestionNumber = DEFAULT_RECENT;
			}

			// Get a list of recent questions sorted by age
			$workflowQry = '';
			if (validUrl()) {
			$workflowQry = 'AND ( q.workflowstatus = \''.WF_STATUS_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_AUTO_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_REPENDING.'\')';
			}
			$query = 'SELECT q.questionid, title, lastupdated, q.workflowstatus
			FROM '.$GLOBALS['tablePrefix'].'categoryassociations a, '.$GLOBALS['tablePrefix'].'categories c,
			('.$GLOBALS['tablePrefix'].'question_auth qa	RIGHT JOIN '.$GLOBALS['tablePrefix'].'questions q ON qa.questionid = q.questionid)
			
			WHERE q.questionid = a.questionid
			AND a.categoryid = c.categoryid
			AND visible=1
			'.$workflowQry.'
			AND
				(
				((q.startdate < \''.$GLOBALS['currDatetime'].'\') AND ((q.enableexpiry = 1) AND q.expirydate > \''.$GLOBALS['currDatetime'].'\'))
				OR
				((q.startdate < \''.$GLOBALS['currDatetime'].'\') AND (q.enableexpiry = 0))
				)			
			'.$categoryRestrictSQL.'
			AND (qa.groupid is null '.AKB_AUTH::deriveGroupSQL(false).') 
			GROUP BY q.questionid, title, lastupdated, q.workflowstatus
			ORDER BY lastupdated DESC
			'.$GLOBALS['AKB_DB']->AddLimit(0, $recentQuestionNumber);

			$result = $GLOBALS['AKB_DB']->Query($query);

			if ($GLOBALS['AKB_DB']->CountResult($query) > 0) {
				$tmpRow = array();
				$historyQuestionIds = array();
				while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
					if (validUrl()) {
						if ($row['workflowstatus'] != WF_STATUS_REPENDING) {
							$tmpRow[] = $row;
						} else {
							$row['title'] = '';
							$tmpRow[] = $row;
							$historyQuestionIds[] = trim($row['questionid']);
						}
					} else {
						$tmpRow[] = $row;
					}
				}
				
				if (sizeof($historyQuestionIds)) {
					$historyQuestionIdsStr = "'" . implode("','", $historyQuestionIds) . "'";
					$historyQuestionIdsQry = "SELECT questionid, title, lastupdated, workflowstatus 
					FROM {$GLOBALS['tablePrefix']}questions_history
					WHERE (workflowstatus = '".WF_STATUS_AUTO_APPROVED."' OR workflowstatus = '".WF_STATUS_APPROVED."')
					ORDER BY lastupdated ASC
					";
					$historyQuestionIdsQryRes = $GLOBALS['AKB_DB']->Query($historyQuestionIdsQry);
					$historyQuestion = array();
					if ($GLOBALS['AKB_DB']->CountResult($historyQuestionIdsQry) > 0) {
						while ($row = $GLOBALS['AKB_DB']->Fetch($historyQuestionIdsQryRes)) {
							$historyQuestion[$row['questionid']] = $row;
						}
					}
				}
				
				
				$output = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RecentQuestionsListHeader');
				foreach ($tmpRow as $row) {
					if (isset($historyQuestion[$row['questionid']])) {
						$row['title'] = $historyQuestion[$row['questionid']]['title'];
						$row['lastupdated'] = $historyQuestion[$row['questionid']]['lastupdated'];
						$row['workflowstatus'] = $historyQuestion[$row['questionid']]['workflowstatus'];
					}
					if ($row['title'] != '') {
						$link = GetUrl('question', $row['questionid']);
						if ($GLOBALS['cleanUrls']) {
							$link .= $GLOBALS['AKB_CLASS_HELPER']->_MakeURLSafe($row['title']);
						}
						$GLOBALS['Link'] = $link;
						$GLOBALS['Text'] = htmlspecialchars($GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['title']),ENT_QUOTES,$GLOBALS['charset']);
						$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RecentQuestionsListRow');
					}
				}

				$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RecentQuestionsListFooter');
			} else {
				$output = GetLang('hpNoQuestions');
			}
			return $output;
		}
	}

?>
