<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 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    backup
 */

/**
 * Hook class.
 */
class Hook_cron_star
{
    /**
     * Run function for CRON hooks. Searches for tasks to perform.
     */
    public function run()
    {
		/* Uncomment the below section and edit as necessary to define when the cron should NOT run */
		
        //if (intval(date('i')) > 4) {
        //    return;
        //}
		
		/* 	Set the below variable to the number of seconds in the past action logs should have an impact on popularity.
			The number 2592000 represents 30 days, which was the default.
		*/
		$lookbackseconds = 2592000;
		
		/*	Set the below variable to the ID of the custom field for each member's popularity, in format 'field_(ID)'. */
		$cpf = 'field_44';

		// Prepare the where string for SQL. Edit to taste. The default tz_time makes activity within last 30 days count.
		$wherestring = "(the_type LIKE 'ADD%' OR the_type LIKE 'FILEDUMP_UPLOAD' OR the_type LIKE 'GIVE%' OR the_type LIKE 'MAKE%' OR the_type LIKE 'WIKI_ADD_PAGE') AND date_and_time > " . tz_time(time() - $lookbackseconds, get_site_timezone());
		
		//get the number of registered users. Add 1 because forum stats does not count Guest.
		require_code('cns_general');
		$stats = cns_get_forums_stats();
		$nummembers = integer_format($stats['num_members']) + 1;
		
		$popularityarray = array();
		$totalpopularity = 0;
		
		//Get the action logs for our conditions. Then run all of them through a loop to process.
		$actions = $GLOBALS['SITE_DB']->query_select('actionlogs', array('*'), null, 'WHERE ' . $wherestring);
		foreach ($actions as $action)
		{
			if(!array_key_exists($action['member_id'],$popularityarray))
				$popularityarray[$action['member_id']] = 0;
			
			/* 	Edit the below switch to your taste. You may want to add action log types not mentioned here.
				You may also wish to define your own weight for each action.
			*/
			$weight = 0;
			switch ($action['the_type'])
			{
				case 'ADD_AWARD_TYPE':
					$weight = 10;
					break;
				case 'ADD_BANNER':
					$weight = 25;
					break;
				case 'ADD_BANNER_TYPE':
					$weight = 10;
					break;
				case 'ADD_CALENDAR_EVENT':
					$weight = 50;
					break;
				case 'ADD_CATALOGUE':
					$weight = 50;
					break;
				case 'ADD_CATALOGUE_CATEGORY':
					$weight = 0;
					break;
				case 'ADD_CATALOGUE_ENTRY':
					$weight = 50;
					break;
				case 'ADD_CHATROOM':
					$weight = 50;
					break;
				case 'ADD_CUSTOM_COMCODE_TAG':
					$weight = 0;
					break;
				case 'ADD_CUSTOM_PROFILE_FIELD':
					$weight = 0;
					break;
				case 'ADD_DOWNLOAD_CATEGORY':
					$weight = 10;
					break;
				case 'ADD_EVENT_TYPE':
					$weight = 10;
					break;
				case 'ADD_FORUM':
					$weight = 100;
					break;
				case 'ADD_FORUM_GROUPING':
					$weight = 10;
					break;
				case 'ADD_GALLERY':
					$weight = 25;
					break;
				case 'ADD_GROUP':
					$weight = 100;
					break;
				case 'ADD_IMAGE':
					$weight = 25;
					break;
				case 'ADD_MENU_ITEM':
					$weight = 0;
					break;
				case 'ADD_NEWS':
					$weight = 50;
					break;
				case 'ADD_NEWS_CATEGORY':
					$weight = 10;
					break;
				case 'ADD_POLL':
					$weight = 25;
					break;
				case 'ADD_POST':
					$weight = 10;
					$forum_id = $GLOBALS['SITE_DB']->query_select_value_if_there('f_topics', 't_forum_id', array('id' => $action['param_b']));
					if ($forum_id === null)
						$weight = 2;
					break;
				case 'ADD_QUIZ':
					$weight = 50;
					break;
				case 'ADD_THEME':
					$weight = 0;
					break;
				case 'ADD_TICKET_TYPE':
					$weight = 0;
					break;
				case 'ADD_TOPIC':
					$weight = 50;
					if ($action['param_b'] === '')
						$weight = 10;
					break;
				case 'ADD_VIDEO':
					$weight = 50;
					break;
				case 'ADD_WORDFILTER':
					$weight = 0;
					break;
				case 'ADD_ZONE':
					$weight = 0;
					break;
				case 'FILEDUMP_UPLOAD':
					$weight = 10;
					break;
				case 'GIVE_AWARD':
					$weight = 25;
					break;
				case 'MAKE_FRIEND':
					$weight = 10;
					break;
				case 'WIKI_ADD_PAGE':
					$weight = 25;
					break;
				default: // this is for all action log types returned by our WHERE condition but didn't have a switch case.
					$weight = 25;
					break;
			}
			
			// the below two lines will adjust the weight... the older the action, the less weight it has.
			$beginningtime = tz_time(time() - $lookbackseconds, get_site_timezone());
			$weight = $weight * (($action['date_and_time'] - $beginningtime) / $lookbackseconds);
			
			// Now, add the action's final weight to total, and to the specific member's.
			$totalpopularity = $totalpopularity + $weight;
			$popularityarray[$action['member_id']] = $popularityarray[$action['member_id']] + $weight;
		}
		
		// Define the average weight for a member
		$averagemember = float_format($totalpopularity / $nummembers);
		
		// Go through each member and determine / set their popularity in the form of a multiplier rounded to 2 decimal places.
		foreach ($popularityarray as $key => $value)
		{
			$popularity = ($value / $averagemember);
			$multiplier = round($popularity,2);
			$updater = $GLOBALS['SITE_DB']->query_update('f_member_custom_fields',array($cpf => $multiplier),array('mf_member_id' => $key));
		}
			
		return;
    }
}
