Advertisement
Guest User

Untitled

a guest
Apr 14th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.93 KB | None | 0 0
  1. <?php
  2.  
  3. class goodStar
  4. {
  5.     /** @var modX $modx */
  6.     public $modx;
  7.  
  8.  
  9.     /**
  10.      * @param modX $modx
  11.      * @param array $config
  12.      */
  13.     function __construct(modX &$modx, array $config = [])
  14.     {
  15.         $this->modx =& $modx;
  16.         $corePath = $this->modx->getOption('goodstar_core_path', '', MODX_CORE_PATH . 'components/goodstar/');
  17.         $assetsUrl = $this->modx->getOption('goodstar_assets_url', '', MODX_ASSETS_URL . 'components/goodstar/');
  18.  
  19.         $this->config = array_merge([
  20.             'corePath' => $corePath,
  21.             'modelPath' => $corePath . 'model/',
  22.  
  23.             'connectorUrl' => $assetsUrl . 'connector.php',
  24.             'assetsUrl' => $assetsUrl,
  25.             'cssUrl' => $assetsUrl . 'css/',
  26.             'jsUrl' => $assetsUrl . 'js/',
  27.         ], $config);
  28.  
  29.         $this->modx->addPackage('goodstar', $this->config['modelPath']);
  30.         $this->modx->lexicon->load('goodstar:default');
  31.     }
  32.  
  33.     function initialize($ctx = 'web', $scriptProperties = array()){
  34.  
  35.         $this->modx->regClientScript($this->modx->getOption('goodstar_jsUrl'));
  36.         $this->modx->regClientScript($this->modx->getOption('goodstar_jsCustom'));
  37.         $this->modx->regClientCSS('assets/components/goodstar/js/jquery-bar-rating/dist/themes/' . $this->modx->getOption('goodstar_theme') . '.css');
  38.  
  39.         $data = json_encode(array_merge($this->config, array(
  40.             'theme' => $this->modx->getOption('goodstar_theme'),
  41.             'selector' => $this->modx->getOption('goodstar_selector')
  42.         )));
  43.  
  44.         $this->modx->regClientStartupScript('<script type="text/javascript">goodStarConfig='.$data.';</script>');
  45.     }
  46.  
  47.     function saveVoice(array $data){
  48.  
  49.         $user = $this->modx->user->id ? $this->modx->user->id : md5(time() . md5(rand(1,10000)));
  50.  
  51.         $save = array(
  52.             'thread' => $data['thread'],
  53.             'vote' => $data['vote'],
  54.             'group' => $data['group'],
  55.             'user' => $user
  56.         );
  57.         //Если не голосовал
  58.         $where = array('thread' => $data['thread'], 'user' => $save['user']);
  59.         if(!$this->modx->getCount('goodStarItem', $where)){
  60.             $q = $this->modx->newObject('goodStarItem');
  61.             $q->fromArray($save);
  62.             $q->save();
  63.         }else{
  64.             $q = $this->modx->getObject('goodStarItem', $where);
  65.             $q->set('vote', $data['vote']);
  66.             $q->save();
  67.         }
  68.  
  69.  
  70.         // Считаем сумму голосов
  71.  
  72.         $q = $this->modx->newQuery('goodStarItem');
  73.         $q->where(array(
  74.             'thread' => $data['thread']
  75.         ));
  76.         $q->select('SUM(vote) as sum');
  77.         $sum = $this->modx->getIterator('goodStarItem', $q);
  78.  
  79.         foreach ($sum as $s){
  80.             $sumVotes = $s->get('sum');
  81.         }
  82.  
  83.         // Считаем всего голосов
  84.         $totalVotes = $this->modx->getCount('goodStarItem', $q);
  85.  
  86.         $wilson = $this->wilsonScore($sumVotes, $totalVotes);
  87.  
  88.         $where = array('thread' => $data['thread']);
  89.  
  90.         if(!$this->modx->getCount('goodStarVoteCount', $where)){
  91.             $arr = array(
  92.                 'thread' => $save['thread'],
  93.                 'count' => $wilson,
  94.                 'countaverage' => $save['vote']
  95.             );
  96.             $q = $this->modx->newObject('goodStarVoteCount');
  97.             $q->fromArray($arr);
  98.             $q->save();
  99.         }else{
  100.             $countaverage = $sumVotes/$totalVotes;
  101.             $q = $this->modx->getObject('goodStarVoteCount', $where);
  102.             $q->set('count', $wilson);
  103.             $q->set('countaverage', $countaverage);
  104.             $q->save();
  105.         }
  106.  
  107.         //Ставим куку от повторного голосования
  108.  
  109.         $coockie = json_encode(array(
  110.             'thread' => $data['thread'],
  111.             'user' => $save['user']
  112.         ));
  113.         $name = 'goodStar-'.$data['thread'];
  114.         setcookie($name, $coockie, time()+(60*60*24*30*6), '/');
  115.  
  116.         return true;
  117.  
  118.     }
  119.  
  120.     function getCurrentRating($thread = '', $conclusion){
  121.         if($thread){
  122.             $currentRating = $this->modx->getObject('goodStarVoteCount', array('thread' => $thread));
  123.             if(is_object($currentRating)){
  124.                 switch ($conclusion){
  125.  
  126.                     //Проверям, какой рейтинг выводить
  127.                     case 'wilson':
  128.                         return $currentRating->get('count');
  129.                         break;
  130.  
  131.                     case 'average':
  132.                         return $currentRating->get('countaverage');
  133.                         break;
  134.  
  135.                     case 'user':
  136.                         $coockie = json_decode($_COOKIE['goodStar-'.$thread], true);
  137.                         $countVoite = $this->modx->getObject('goodStarItem', array(
  138.                             'thread' => $coockie['thread'],
  139.                             'user' => $coockie['user']
  140.                         ));
  141.                         if(is_object($countVoite)){
  142.                             return $countVoite->get('vote');
  143.                         }else{
  144.                             return 0;
  145.                         }
  146.                         break;
  147.  
  148.                     default:
  149.                         return 0;
  150.                         break;
  151.                 }
  152.             }
  153.         }else{
  154.             return 0;
  155.         }
  156.     }
  157.  
  158.     function getCountVoite($thread = ''){
  159.         if($thread){
  160.             $count = $this->modx->getCount('goodStarItem', array('thread' => $thread));
  161.             return $count;
  162.         }else{
  163.             return 0;
  164.         }
  165.     }
  166.  
  167.     function getReadOnly($thread = ''){
  168.         if($thread){
  169.             $coockie = json_decode($_COOKIE['goodStar-'.$thread], true);
  170.  
  171.             if($coockie['thread'] == $thread && $this->modx->getCount('goodStarItem', array('thread' => $thread, 'user' => $coockie['user']))){
  172.                 return "true";
  173.             }
  174.         }
  175.  
  176.         return;
  177.     }
  178.  
  179.     /**
  180.      * Расчет рейтинга на основе шкалы голосов
  181.      *
  182.      * @param $sumVotes Сумма всех голосов
  183.      * @param $totalVotes Кол-во голосов
  184.      * @param array $votesRange Диапазон возможных значений голосов
  185.      * @return float|int
  186.      */
  187.     function wilsonScore($sumVotes, $totalVotes, $votesRange = [1, 2, 3, 4, 5])
  188.     {
  189.         if ($sumVotes > 0 && $totalVotes > 0) {
  190.             $z = 1.64485;
  191.             $vMin = min($votesRange);
  192.             $vWidth = floatval(max($votesRange) - $vMin);
  193.             $phat = ($sumVotes - $totalVotes * $vMin) / $vWidth / floatval($totalVotes);
  194.             $rating = ($phat + $z * $z / (2 * $totalVotes) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $totalVotes)) / $totalVotes)) / (1 + $z * $z / $totalVotes);
  195.             return round($rating * $vWidth + $vMin, 6);
  196.         }
  197.         return 0;
  198.     }
  199.  
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement