Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
condition_user_frequency.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/conditions/condition/condition.inc';
18 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
64  public static function evaluate(Asset $asset, Array $condition_data)
65  {
66  $result = FALSE;
67 
68  // Retrieve the cookie or initialise it
69  if (isset($_COOKIE['user_frequency_cond_'.$condition_data['id_name']]) && (FALSE !== strpos($_COOKIE['user_frequency_cond_'.$condition_data['id_name']], '__'))) {
70  $data = Array();
71  list($data['value'], $data['last_date']) = explode('__', $_COOKIE['user_frequency_cond_'.$condition_data['id_name']]);
72  } else {
73  $data = Array(
74  'value' => 1,
75  'last_date' => time(),
76  );
77  }
78 
79  // time difference between now and last update in days
80  $difference = (time() - $data['last_date']) / 86400;
81  // EVERY X HITS
82  // Condition evaluates to TRUE every x hits, for example when
83  // x is 20, it will return TRUE on hit 20, 40, 60... etc
84  if ($condition_data['units'] == 'hits') {
85  $hits = (int) $condition_data['value'];
86  // if the current counter divides evenly into condition return TRUE
87  if (($hits != 0) && (($data['value'] % $hits) == 0)) {
88  $result = TRUE;
89  } else {
90  $result = FALSE;
91  }
92  $data['value']++;
93  }
94 
95  // EVERY X HITS LIMIT 1 PER DAY
96  // same as above limit 1 hit per day
97  if ($condition_data['units'] == 'session') {
98  $hits = (int) $condition_data['value'];
99 
100  // if the current counter divides evenly into condition return TRUE
101  if (($data['value'] % $hits) == 0) {
102  $result = TRUE;
103  } else {
104  $result = FALSE;
105  }
106 
107  // we only increment the count if a day has passed
108  if ((int) $difference) $data['value']++;
109  }
110 
111  // EVERY X TIME PERIOD UNITS
112  // Condition evaluates TRUE every x period of time
113  // for example when x is 3 days, condition will be TRUE
114  // if more than 3 days have passed since last check
115  if ($condition_data['units'] == 'days' || $condition_data['units'] == 'hours' || $condition_data['units'] == 'minutes') {
116  if ($condition_data['units'] == 'hours') {
117  $difference *= 60;
118  }
119 
120  if ($condition_data['units'] == 'minutes') {
121  $difference *= 3600;
122  }
123 
124  // if more units have passed than the required difference
125  if (((int) $difference) > $condition_data['value']) {
126  $result = TRUE;
127  } else {
128  $result = FALSE;
129  }
130  }
131  // update the time, and set the cookie
132  $data['last_date'] = time();
133  $root_url = str_replace(Array('http://', 'https://'), '', sq_web_path('root_url'));
134  $domain = $root_url;
135  $path = '/';
136  if (FALSE !== ($slashpos = strpos($root_url, '/'))) {
137  $domain = substr($root_url, 0, $slashpos);
138  $path = substr($root_url, $slashpos);
139  }
140  setcookie('user_frequency_cond_'.$condition_data['id_name'], $data['value'].'__'.$data['last_date'], strtotime('+3 months'), $path, $domain);
141 
142  return $result;
143 
144  }//end evaluate()
145 
146 
147 }//end class
148 
149 ?>