PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/mautic/app/bundles/FormBundle/Helper/AbTestHelper.php

https://gitlab.com/randydanniswara/website
PHP | 119 lines | 74 code | 19 blank | 26 comment | 12 complexity | f140c7a2a84adfb7325bcd03ad704ce8 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\FormBundle\Helper;
  10. use Mautic\CoreBundle\Factory\MauticFactory;
  11. use Mautic\EmailBundle\Entity\Email;
  12. use Mautic\PageBundle\Entity\Page;
  13. /**
  14. * Class AbTestHelper
  15. */
  16. class AbTestHelper
  17. {
  18. /**
  19. * Determines the winner of A/B test based on number of form submissions
  20. *
  21. * @param MauticFactory $factory
  22. * @param Page $parent
  23. * @param $children
  24. *
  25. * @return array
  26. */
  27. public static function determineSubmissionWinner ($factory, $parent, $children)
  28. {
  29. $repo = $factory->getEntityManager()->getRepository('MauticFormBundle:Submission');
  30. //if this is an email A/B test, then link email to page to form submission
  31. //if it is a page A/B test, then link form submission to page
  32. $type = ($parent instanceof Email) ? 'email' : 'page';
  33. $ids = array($parent->getId());
  34. foreach ($children as $c) {
  35. if ($c->isPublished()) {
  36. $id = $c->getId();
  37. $ids[] = $id;
  38. }
  39. }
  40. $startDate = $parent->getVariantStartDate();
  41. if ($startDate != null && !empty($ids)) {
  42. $counts = ($type == "page") ? $repo->getSubmissionCountsByPage($ids, $startDate) : $repo->getSubmissionCountsByEmail($ids, $startDate);
  43. $translator = $factory->getTranslator();
  44. if ($counts) {
  45. $submissions = $support = $data = array();
  46. $hasResults = array();
  47. $submissionLabel = $translator->trans('mautic.form.abtest.label.submissions');
  48. $hitLabel = ($type == 'page') ? $translator->trans('mautic.form.abtest.label.hits') : $translator->trans('mautic.form.abtest.label.sentemils');
  49. foreach ($counts as $stats) {
  50. $submissionRate = ($stats['total']) ? round(($stats['count'] / $stats['total']) * 100, 2) : 0;
  51. $submissions[$stats['id']] = $submissionRate;
  52. $data[$submissionLabel][] = $stats['count'];
  53. $data[$hitLabel][] = $stats['total'];
  54. $support['labels'][] = $stats['id'] . ':' . $stats['name'] . ' (' . $submissionRate . '%)';
  55. $hasResults[] = $stats['id'];
  56. }
  57. //make sure that parent and published children are included
  58. if (!in_array($parent->getId(), $hasResults)) {
  59. $data[$submissionLabel][] = 0;
  60. $data[$hitLabel][] = 0;
  61. $support['labels'][] = (($type == 'page') ? $parent->getTitle() : $parent->getName()) . ' (0%)';
  62. }
  63. foreach ($children as $c) {
  64. if ($c->isPublished()) {
  65. if (!in_array($c->getId(), $hasResults)) {
  66. $data[$submissionLabel][] = 0;
  67. $data[$hitLabel][] = 0;
  68. $support['labels'][] = (($type == 'page') ? $c->getTitle() : $c->getName()) . ' (0%)';
  69. }
  70. }
  71. }
  72. $support['data'] = $data;
  73. //set max for scales
  74. $maxes = array();
  75. foreach ($support['data'] as $label => $data) {
  76. $maxes[] = max($data);
  77. }
  78. $top = max($maxes);
  79. $support['step_width'] = (ceil($top / 10) * 10);
  80. //put in order from least to greatest just because
  81. asort($submissions);
  82. //who's the winner?
  83. $max = max($submissions);
  84. //get the page ids with the most number of submissions
  85. $winners = array_keys($submissions, $max);
  86. return array(
  87. 'winners' => $winners,
  88. 'support' => $support,
  89. 'basedOn' => 'form.submissions',
  90. 'supportTemplate' => 'MauticPageBundle:SubscribedEvents\AbTest:bargraph.html.php'
  91. );
  92. }
  93. }
  94. return array(
  95. 'winners' => array(),
  96. 'support' => array(),
  97. 'basedOn' => 'form.submissions'
  98. );
  99. }
  100. }