<?php /*

 ocPortal
 Copyright (c) ocProducts, 2004-2012

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license		http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright	ocProducts Ltd
 * @package		awards
 */

/**
 * Module page class.
 */
class Module_awards
{

	/**
	 * Standard modular info function.
	 *
	 * @return ?array	Map of module info (NULL: module is disabled).
	 */
	function info()
	{
		$info=array();
		$info['author']='Chris Graham';
		$info['organisation']='ocProducts';
		$info['hacked_by']=NULL;
		$info['hack_version']=NULL;
		$info['version']=2;
		$info['locked']=false;
		return $info;
	}

	/**
	 * Standard modular entry-point finder function.
	 *
	 * @return ?array	A map of entry points (type-code=>language-code) (NULL: disabled).
	 */
	function get_entry_points()
	{
		return ($GLOBALS['SITE_DB']->query_value('award_types','COUNT(*)')==0)?array():array('misc'=>'AWARDS','overview'=>'AWARD_OVERVIEW');
	}
	
	/**
	 * Standard modular run function.
	 *
	 * @return tempcode	The result of execution.
	 */
	function run()
	{
		require_code('awards');
		require_lang('awards');

		// What are we doing?
		$type=get_param('type','misc');

		if ($type=='award') return $this->award();
		if ($type=='overview') return $this->award_overview();
		if ($type=='misc') return $this->choose_award();

		return new ocp_tempcode();
	}

	/**
	 * The UI to choose an award type to view.
	 *
	 * @return tempcode		The UI
	 */
	function choose_award()
	{
		$title=get_page_title('AWARDS');

		$rows=$GLOBALS['SITE_DB']->query_select('award_types',array('*'));
		$out=new ocp_tempcode();
		foreach ($rows as $myrow)
		{
			if ((!file_exists(get_file_base().'/sources/hooks/systems/awards/'.filter_naughty_harsh($myrow['a_content_type']).'.php')) && (!file_exists(get_file_base().'/sources_custom/hooks/systems/awards/'.filter_naughty_harsh($myrow['a_content_type']).'.php')))
				continue;

			require_code('hooks/systems/awards/'.filter_naughty_harsh($myrow['a_content_type']));
			$object=object_factory('Hook_awards_'.$myrow['a_content_type']);
			$info=$object->info();
			if (!is_null($info))
			{
				$url=build_url(array('page'=>'_SELF','type'=>'award','id'=>$myrow['id']),'_SELF');
				$_title=get_translated_text($myrow['a_title']);
				$description=get_translated_tempcode($myrow['a_description']);

				$out->attach(do_template('INDEX_SCREEN_FANCIER_ENTRY',array('_GUID'=>'0974df260d7521edebf33f5397cab7f4','NAME'=>$_title,'URL'=>$url,'DESCRIPTION'=>$description,'TITLE'=>'')));
			}
		}

		breadcrumb_set_self(do_lang_tempcode('CHOOSE'));

		$add_url=new ocp_tempcode();
		if (has_actual_page_access(get_member(),'admin_awards'))
			$add_url=build_url(array('page'=>'admin_awards','type'=>'ad'),get_module_zone('admin_awards'));

		return do_template('INDEX_SCREEN_FANCIER_SCREEN',array('ADD_URL'=>$add_url,'PRE'=>'','POST'=>'','TITLE'=>$title,'CONTENT'=>$out));
	}

	/**
	 * The UI to view the overview of all current award allocations.
	 *
	 * @return tempcode		The UI
	 */
	function award_overview()
	{
		$title=get_page_title('AWARD_OVERVIEW');
		
		$award_types=$GLOBALS['SITE_DB']->query_select('award_types',array('*'));

		$content=new ocp_tempcode();
		
		require_code('content');

		foreach ($award_types as $award_type_row)
		{
			require_code('hooks/systems/awards/'.filter_naughty_harsh($award_type_row['a_content_type']),true);
			$object=object_factory('Hook_awards_'.$award_type_row['a_content_type']);
			$info=$object->info();
			if (is_null($info)) continue;

			$_title=get_translated_text($award_type_row['a_title']);
			$description=paragraph(get_translated_tempcode($award_type_row['a_description']),'grdgdfghdfgodfs');

			$rows=$GLOBALS['SITE_DB']->query_select('award_archive',array('*'),array('a_type_id'=>$award_type_row['id']),'ORDER BY date_and_time DESC',1);
			foreach ($rows as $myrow)
			{
				$award_content_row=content_get_row($myrow['content_id'],$info);

				if (!is_null($award_content_row))
				{
					$rendered_content=$object->run($award_content_row,'_SEARCH');

					if (($award_type_row['a_hide_awardee']==1) || (is_guest($myrow['member_id'])))
					{
						$awardee='';
						$awardee_username='';
						$awardee_profile_url='';
					} else
					{
						$awardee=strval($myrow['member_id']);
						$awardee_username=$GLOBALS['FORUM_DRIVER']->get_username($myrow['member_id']);
						if (is_null($awardee_username)) $awardee_username=do_lang('UNKNOWN');
						$awardee_profile_url=$GLOBALS['FORUM_DRIVER']->member_profile_url($myrow['member_id'],false,true);
					}

					$rendered=do_template('AWARDED_CONTENT',array('_GUID'=>'1a2a5b6e9b53a99e303b7ed17070cea9','AWARDEE_PROFILE_URL'=>$awardee_profile_url,'AWARDEE'=>$awardee,'AWARDEE_USERNAME'=>$awardee_username,'RAW_AWARD_DATE'=>strval($myrow['date_and_time']),'AWARD_DATE'=>get_timezoned_date($myrow['date_and_time']),'CONTENT'=>$rendered_content));
					$archive_url=build_url(array('page'=>'_SELF','type'=>'award','id'=>$award_type_row['id']),'_SELF');
					$content->attach(do_template('INDEX_SCREEN_FANCIER_ENTRY',array('URL'=>$archive_url,'TITLE'=>$_title,'NAME'=>$_title,'DESCRIPTION'=>$rendered)));
				}
			}
		}

		return do_template('INDEX_SCREEN_FANCIER_SCREEN',array('TITLE'=>$title,'PRE'=>do_lang_tempcode('DESCRIPTION_AWARD_OVERVIEW'),'CONTENT'=>$content,'POST'=>''));
	}

	/**
	 * The UI to view the archive for an award type.
	 *
	 * @return tempcode		The UI
	 */
	function award()
	{
		breadcrumb_set_parents(array(array('_SELF:_SELF:misc',do_lang_tempcode('CHOOSE'))));

		require_css('awards');

		$id=get_param_integer('id');
		$_award_type_row=$GLOBALS['SITE_DB']->query_select('award_types',array('*'),array('id'=>$id),'',1);
		if (!array_key_exists(0,$_award_type_row)) warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
		$award_type_row=$_award_type_row[0];
		require_code('hooks/systems/awards/'.filter_naughty_harsh($award_type_row['a_content_type']),true);
		$object=object_factory('Hook_awards_'.$award_type_row['a_content_type']);
		$info=$object->info();
		if (is_null($info)) fatal_exit(do_lang_tempcode('INTERNAL_ERROR'));

		$start=get_param_integer('start',0);
		$max=get_param_integer('max',20);
		$title=get_page_title('_AWARD',true,array(escape_html(get_translated_text($award_type_row['a_title']))));
		$description=paragraph(get_translated_tempcode($award_type_row['a_description']),'grdgdfghdfgodfs');

		$rows=$GLOBALS['SITE_DB']->query_select('award_archive',array('*'),array('a_type_id'=>$id),'ORDER BY date_and_time DESC',$max,$start);
		$max_rows=$GLOBALS['SITE_DB']->query_value('award_archive','COUNT(*)',array('a_type_id'=>$id));
		$content=new ocp_tempcode();
		foreach ($rows as $myrow)
		{
			require_code('content');
			$award_content_row=content_get_row($myrow['content_id'],$info);

			if (!is_null($award_content_row))
			{
				$rendered_content=$object->run($award_content_row,'_SEARCH');

				if (($award_type_row['a_hide_awardee']==1) || (is_guest($myrow['member_id'])))
				{
					$awardee='';
					$awardee_username='';
					$awardee_profile_url='';
				} else
				{
					$awardee=strval($myrow['member_id']);
					$awardee_username=$GLOBALS['FORUM_DRIVER']->get_username($myrow['member_id']);
					if (is_null($awardee_username)) $awardee_username=do_lang('UNKNOWN');
					$awardee_profile_url=$GLOBALS['FORUM_DRIVER']->member_profile_url($myrow['member_id'],false,true);
				}

				$content->attach(do_template('AWARDED_CONTENT',array('_GUID'=>'1a2a5b6e9b53a99e303b7ed17070cea9','AWARDEE_PROFILE_URL'=>$awardee_profile_url,'AWARDEE'=>$awardee,'AWARDEE_USERNAME'=>$awardee_username,'RAW_AWARD_DATE'=>strval($myrow['date_and_time']),'AWARD_DATE'=>get_timezoned_date($myrow['date_and_time'],false,false,false,true),'CONTENT'=>$rendered_content)));

				$content->attach(do_template('BLOCK_SEPARATOR',array()));
			}
		}
		if ($content->is_empty())
		{
			if (has_category_access(get_member(),'award',strval($id)))
				inform_exit(do_lang_tempcode('NO_ENTRIES_AWARDS',escape_html($info['title'])));
			inform_exit(do_lang_tempcode('NO_ENTRIES'));
		}

		$page_num=intval(floor(floatval($start)/floatval($max)))+1;
		$num_pages=intval(ceil(floatval($max_rows)/floatval($max)));

		$previous_url=($start==0)?new ocp_tempcode():build_url(array('page'=>'_SELF','start'=>($start-$max==0)?NULL:$start-$max),'_SELF');
		$next_url=(count($rows)!=$max)?new ocp_tempcode():build_url(array('page'=>'_SELF','start'=>$start+$max),'_SELF');
		$browse=do_template('NEXT_BROWSER_BROWSE_NEXT',array('_GUID'=>'54690cd3d6ea1ee0722271ab428e6a31','NEXT_LINK'=>$next_url,'PREVIOUS_LINK'=>$previous_url,'PAGE_NUM'=>integer_format($page_num),'NUM_PAGES'=>integer_format($num_pages)));

		$sub_title=do_lang_tempcode('AWARD_HISTORY');

		return do_template('NEXT_BROWSER_SCREEN',array('_GUID'=>'b9cf3a37300aced490003f79d7bb4914','TITLE'=>$title,'SUB_TITLE'=>$sub_title,'DESCRIPTION'=>$description,'CONTENT'=>$content,'BROWSE'=>$browse));
	}

}


