Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>optimal would be IMHO the the following:</p> <p>have an object for the user with functions that return user specific attributes/metrics that you initialise with the proper user id (you probably wanna make this a singleton/static for some elements...):</p> <pre><code>&lt;? class User { public function initUser($id) { /* initialise the user. maby load all metrics now, or if they are intensive on demand when the functions are called. you can cache them in a class variable*/ } public function getPostCount() { // return number of posts } public function getRegisterDate() { // return register date } public function getNumberOfLogins() { // return the number of logins the user has made over time } } ?&gt; </code></pre> <p>have a badge object that is initialised with an id/key and loads dependencies from your database:</p> <pre><code>&lt;? class Badge { protected $dependencies = array(); public function initBadge($id) { $this-&gt;loadDependencies($id); } protected function loadDependencies() { // load data from mysql and store it into dependencies like so: $dependencies = array(array( 'value' =&gt; 300, 'type' =&gt; 'PostCount', 'compare =&gt; 'greater', ),...); $this-&gt;dependencies = $dependencies; } public function getDependencies() { return $this-&gt;dependencies; } } ?&gt; </code></pre> <p>then you could have a class that controls the awarding of batches (you can also do it inside user...) and checks dependencies and prints failed dependencies etc...</p> <pre><code> &lt;? class BadgeAwarder { protected $badge = null; protected $user = null; public function awardBadge($userid,$badge) { if(is_null($this-&gt;badge)) { $this-&gt;badge = new Badge; // or something else for strange freaky badges, passed by $badge } $this-&gt;badge-&gt;initBadge($badge); if(is_null($this-&gt;user)) { $this-&gt;user = new User; $this-&gt;user-&gt;initUser($userid); } $allowed = $this-&gt;checkDependencies(); if($allowed === true) { // grant badge, print congratulations } else if(is_array($failed)) { // sorry, you failed tu full fill thef ollowing dependencies: print_r($failed); } else { echo "error?"; } } protected function checkDependencies() { $failed = array(); foreach($this-&gt;badge-&gt;getDependencies() as $depdency) { $value = call_user_func(array($this-&gt;badge, 'get'.$depdency['type'])); if(!$this-&gt;compare($value,$depdency['value'],$dependency['compare'])) { $failed[] = $dependency; } } if(count($failed) &gt; 0) { return $failed; } else { return true; } } protected function compare($val1,$val2,$operator) { if($operator == 'greater') { return ($val1 &gt; $val2); } } } ?&gt; </code></pre> <p>you can extend to this class if you have very custom batches that require weird calculations.</p> <p>hope i brought you on the right track. untested andp robably full of syntax errors. welcome to the world of object oriented programming. still wanna do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload