<?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		points
 */

/**
 * Module page class.
 */
class Module_leader_board
{

	/**
	 * 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('leader_board','COUNT(*)')==0)?array():array('!'=>'POINT_LEADERBOARD');
	}

	/**
	 * Standard modular run function.
	 *
	 * @return tempcode	The result of execution.
	 */
	function run()
	{
		require_lang('leader_board');
		require_code('points');
		require_css('points');

		$title=get_screen_title('POINT_LEADERBOARD');

		$start_date=intval(get_option('leaderboard_start_date'));

		// Are there any rank images going to display?
		$or_list='1=1';
		$admin_groups=$GLOBALS['FORUM_DRIVER']->get_super_admin_groups();
		$moderator_groups=$GLOBALS['FORUM_DRIVER']->get_moderator_groups();
		foreach (array_merge($admin_groups,$moderator_groups) as $group_id)
			$or_list.=' AND id<>'.strval($group_id);
		$has_rank_images=(get_forum_type()=='ocf') && ($GLOBALS['FORUM_DB']->query_value_null_ok_full('SELECT COUNT(*) FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_groups WHERE '.$or_list.' AND '.db_string_not_equal_to('g_rank_image',''))!=0);

		$weeks=$GLOBALS['SITE_DB']->query('SELECT DISTINCT date_and_time FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'leader_board WHERE date_and_time>='.strval($start_date).' ORDER BY date_and_time DESC');
		if (count($weeks)==0) warn_exit(do_lang_tempcode('NO_ENTRIES'));
		$first_week=$weeks[count($weeks)-1]['date_and_time'];
		$weeks=collapse_1d_complexity('date_and_time',$weeks);
		$out=new ocp_tempcode();
		foreach ($weeks as $week)
		{
			$rows=collapse_2d_complexity('lb_member','lb_points',$GLOBALS['SITE_DB']->query_select('leader_board',array('lb_member','lb_points'),array('date_and_time'=>$week)));
			$week_tpl=new ocp_tempcode();
			foreach ($rows as $member=>$points)
			{
				$points_url=build_url(array('page'=>'points','type'=>'member','id'=>$member),get_module_zone('points'));
				$profile_url=$GLOBALS['FORUM_DRIVER']->member_profile_url($member,false,true);
				$name=$GLOBALS['FORUM_DRIVER']->get_username($member);
				if (is_null($name)) $name=do_lang('UNKNOWN');
				$week_tpl->attach(do_template('POINTS_LEADERBOARD_ROW',array(
					'_GUID'=>'6d323b4b5abea0e82a14cb4745c4af4f',
					'ID'=>strval($member),
					'POINTS_URL'=>$points_url,
					'PROFILE_URL'=>$profile_url,
					'POINTS'=>integer_format($points),
					'NAME'=>$name,
					'HAS_RANK_IMAGES'=>$has_rank_images,
				)));
			}
			$nice_week=intval(($week-$first_week)/(7*24*60*60)+1);
			$out->attach(do_template('POINTS_LEADERBOARD_WEEK',array('_GUID'=>'3a0f71bf20f9098e5711e85cf25f6549','WEEK'=>integer_format($nice_week),'ROWS'=>$week_tpl)));
		}

		return do_template('POINTS_LEADERBOARD_SCREEN',array('_GUID'=>'bab5f7b661435b83800532d3eebd0d54','TITLE'=>$title,'WEEKS'=>$out));
	}

}


