Source of file ABTestingScheme.php
Size: 2,789 Bytes - Last Modified: 2021-12-23T10:33:31+00:00
/var/www/docs.ssmods.com/process/src/code/ABTestingScheme.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 | <?php /** * An AB testing scheme is one where there are a predefined number of variations, and one is chosen at random. */ class ABTestingScheme extends VaryingPersonalisationScheme { public static $db = array( // Name of a property in the property namespace (e.g. abtesting.pagetype.name) that we will use to choose // the variation. "VariationPropertyName" => "Varchar(255)" ); public static $is_abstract = false; public function canCreate($member = null) { return true; } public function personalise(Controller $controller = null) { $cp = $this->getContextProvider(); $var = $this->getVariation($cp, $this); $this->trackRender($var); return $var->render(null, $controller); } /** * Determine the variation that should be displayed. * @param ContextProvider $context * @param PersonalisationSource $source * @return PersonalisationVariation */ public function getVariation(ContextProvider $context, PersonalisationSource $source) { $v = null; if ($this->VariationPropertyName) { // Determine the existing property $vs = $context->getProperties(array($this->VariationPropertyName)); if ($vs && isset($vs[$this->VariationPropertyName][0])) { $v = $vs[$this->VariationPropertyName][0]->getValue(); } if (!$v || $v < 0) { // there is no value for this property, so create one $pick = $this->pickVariation(); if ($pick) { $v = $pick->ID; Tracker::track(array($this->VariationPropertyName => $v)); } } } // At this point, v should be the ID of one of the variations of this scheme, or should be null if ($v) { return PersonalisationVariation::get() ->filter("ParentID", $this->ID) ->filter("ID", $v) ->First(); } // We could not determine the variation for some reason, so just pick the first one. $var = PersonalisationVariation::get() ->filter("ParentID", $this->ID) ->First(); return $var; } /** * Pick one of the variations of this scheme at random. * @return void */ public function pickVariation() { // get all the variations in this scheme $vars = PersonalisationVariation::get() ->filter("ParentID", $this->ID) ->toArray(); if (!$vars || count($vars) == 0) { return null; } $rand = mt_rand(0, count($vars) - 1); return $vars[$rand]; } } |