function GetRating($type, $login = null, $width = null) { session_start(); $objResponse = new xajaxResponse(); $login = change_q_x($login, TRUE); $user = new users(); $user->GetUser($login); $uid = $user->uid; if (!$uid) { $uid = get_uid(false); } if (!$uid) { $objResponse->script('this.document.location.reload();'); return $objResponse; } $user->GetUserByUID($uid); $rating = new rating(); switch ($type) { case 'year': $TIME = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $pro_periods_date = date('Y-01-01', $TIME); $res = $rating->getRatingByYear($uid, date('Y', $TIME)); $periods = rating::getMonthParts(date('Y-01-01')); $data = array(); if ($res) { foreach ($periods as $m => $mm) { if ($m < date('m')) { foreach ($mm as $d) { if ($d > time() || $d < strtotime($user->reg_date)) { continue; } $data[$m][date('Y-m-d', $d)] = null; if ($d >= strtotime($user->reg_date)) { $data[$m][date('Y-m-d', $d)] = 0; } } } } } $start_r = null; if ($res) { if (date('Y', strtotime($res[0]['_date'])) == date('Y') - 1) { $start_r = $res[0]['rating']; if (isset($res[1]) && strtotime($res[1]['_date']) != $periods[0][0]) { $res[0]['_date'] = date('Y-m-d', $periods[0][0]); } else { $res = array_slice($res, 1); } } } else { $res = array(); } $verify_factor = 0; $verify_date = rating::GetVerifyDate($uid); foreach ($res as $row) { $t = strtotime($row['_date']); $m = (int) date('m', $t); $verify_factor = 0; if ($row['is_verify'] == 't') { if ($verify_date) { if (strtotime($verify_date) < $t) { $verify_factor = 0.2; } } else { $verify_factor = 0.2; } } $data[$m - 1][date('Y-m-d', $t)] = array('rating' => floatval($row['rating']), 'verify' => floatval($row['rating'] * $verify_factor), 'pro' => 0); } $lastval = null; foreach ($data as $i => $mon) { foreach ($mon as $d => $prt) { $vl = !$prt ? $lastval : $prt; $data[$i][$d] = $vl; // if($prt !== null) $lastval = $vl; } } $pro_periods = promotion::GetUserProPeriods($uid, $pro_periods_date, TRUE); if ($pro_periods) { $pro = array(); foreach ($pro_periods as $p => $period) { if (date('Y', strtotime($period['from_time'])) > date('Y', $TIME) && date('Y', strtotime($period['to_time']) > date('Y', $TIME))) { continue; } $d1 = (int) date('z', strtotime($period['from_time'])); $d2 = (int) date('z', strtotime($period['to_time'])); if (date('Y', strtotime($period['from_time'])) < date('Y', $TIME)) { $d1 = 0; } if (date('Y', strtotime($period['to_time'])) > date('Y', $TIME)) { $d2 = (int) date('z', mktime(0, 0, 0, 12, 31, date('Y'))); } $_factor = 0.2; if ($period['is_profi'] == 1) { $_factor = 0.4; } foreach ($data as $mon => $val) { foreach ($val as $per => $r) { $day = (int) date('z', strtotime($per)); if ($d1 < $day && $d2 >= $day) { $data[$mon][$per]['pro'] = floatval($data[$mon][$per]['rating'] * $_factor); } } } $pro[$p] = $d1 != $d2 ? array($d1, $d2) : array($d1); } $config['pro'] = $pro; } $new_data = array(); if ($data) { foreach ($data as $mon => $val) { foreach ($val as $per => $r) { $new_data[$mon][$per] = $r['rating'] + $r['verify'] + $r['pro']; } } } $config['data'] = $new_data; $config['cur'] = intval(date('m', $TIME)); $config['days'] = date('z', mktime(0, 0, 0, 12, 31, date('Y'))) + 1; $config['regdate'] = $user->reg_date; break; case 'prev': default: $config = array(); $data = array(); $config['hilight'] = array(); if ($type == 'prev') { $TIME = mktime(0, 0, 0, date('m') - 1, 1, date('Y')); $res = $rating->getRatingByMonth($uid, date('Y-m-d', $TIME)); if (!$res) { $res = array(); } $graphstart = strtotime($user->reg_date); if (count($res) && date('Ym', strtotime($res[0]['_date'])) == date('Ym', $TIME)) { $graphstart = strtotime($res[0]['_date']); } else { if (!count($res)) { $graphstart = time(); } } $rating_data = array(); $verify_date = null; $verify_factor = 0; $n = 0; foreach ($res as $d) { if (date('Y-m', strtotime($d['_date'])) != date('Y-m', $TIME)) { continue; } //$rating_data[intval(date('d', strtotime($d['_date'])))] = $d['rating']; $verify_factor = 0; if ($verify_date === null) { $verify_date = rating::GetVerifyDate($d['user_id']); } if ($d['is_verify'] == 't') { if ($verify_date) { if (strtotime($verify_date) < strtotime($d['_date'])) { $verify_factor = 0.2; } } else { $verify_factor = 0.2; } } if ($n == 0) { $res[0]['verify_factor'] = $verify_factor; } $rating_data[intval(date('d', strtotime($d['_date'])))] = array('rating' => floatval($d['rating']), 'verify' => floatval($d['rating'] * $verify_factor), 'pro' => 0); ++$n; } $last = null; for ($i = 0; $i < date('t', $TIME); ++$i) { if (strtotime(date('Y-m-' . ($i + 1), $TIME)) < $graphstart) { $last = null; } else { $last = $last !== null ? $last : 0; if ($i == 0 && !isset($rating_data[$i + 1])) { $last = array('rating' => floatval($res[0]['rating']), 'verify' => floatval($res[0]['rating'] * $res[0]['verify_factor']), 'pro' => 0); } } if (isset($rating_data[$i + 1])) { $last = $rating_data[$i + 1]; } $data[$i] = $last; } } else { $TIME = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $res = $rating->getRatingByMonth($uid, date('Y-m-d', $TIME)); if (!$res) { $res = array(); } $graphstart = strtotime($user->reg_date); if (count($res) && date('Ym', strtotime($res[0]['_date'])) == date('Ym', $TIME)) { $graphstart = strtotime($res[0]['_date']); } $rating_data = array(); $verify_date = null; $verify_factor = 0; $n = 0; foreach ($res as $d) { if (date('Y-m', strtotime($d['_date'])) != date('Y-m', $TIME)) { continue; } $verify_factor = 0; if ($verify_date === null) { $verify_date = rating::GetVerifyDate($d['user_id']); } if ($d['is_verify'] == 't') { if ($verify_date) { if (strtotime($verify_date) < strtotime($d['_date'])) { $verify_factor = 0.2; } } else { $verify_factor = 0.2; } } if ($n == 0) { $res[0]['verify_factor'] = $verify_factor; } $rating_data[intval(date('d', strtotime($d['_date'])))] = array('rating' => floatval($d['rating']), 'verify' => floatval($d['rating'] * $verify_factor), 'pro' => 0); ++$n; } $config['cur'] = intval(date('d', $TIME)); $last = 0; for ($i = 0; $i < date('d', $TIME); ++$i) { if (strtotime(date('Y-m-' . ($i + 1), $TIME)) < $graphstart) { $last = null; } else { $last = $last !== null ? $last : 0; if ($i == 0 && !isset($rating_data[$i + 1])) { $last = array('rating' => floatval($res[0]['rating']), 'verify' => floatval($res[0]['rating'] * $res[0]['verify_factor']), 'pro' => 0); } } if (isset($rating_data[$i + 1])) { $last = $rating_data[$i + 1]; } $data[$i] = $last; } } $pro_periods_date = date('Y-01-01', $TIME); for ($i = 1; $i <= date('t', $TIME); ++$i) { $t = mktime(0, 0, 0, date('m', $TIME), $i, date('Y', $TIME)); if (date('w', $t) == 0 || date('w', $t) == 6) { $config['hilight'][] = $i; } } $pro_periods = promotion::GetUserProPeriods($uid, $pro_periods_date, TRUE); if ($pro_periods) { $pro = array(); $tmp = array(); foreach ($pro_periods as $p => $period) { if (date('Ym', strtotime($period['from_time'])) > date('Ym', $TIME)) { continue; } if (date('Ym', strtotime($period['to_time'])) < date('Ym', $TIME)) { continue; } $d1 = (int) date('d', strtotime($period['from_time'])); $d2 = (int) date('d', strtotime($period['to_time'])); if (date('Ym', strtotime($period['from_time'])) < date('Ym', $TIME)) { $d1 = 1; } if (date('Ym', strtotime($period['to_time'])) > date('Ym', $TIME)) { $d2 = (int) date('t', $TIME); } $_factor = 0.2; //PRO ONLY if ($period['is_profi'] == 1) { $_factor = 0.4; //PROFI } foreach ($data as $day => $val) { if (isset($tmp[$day]) || $val === null) { continue; } if ($d1 <= $day + 1 && $d2 >= $day + 1) { $data[$day]['pro'] = floatval($data[$day]['rating'] * $_factor); //rating::PRO_FACTOR; $tmp[$day] = 1; } } $pro[$p] = $d1 != $d2 ? array($d1, $d2) : array($d1); } $config['pro'] = $pro; } if (strtotime($user->reg_date) > strtotime($pro_periods_date)) { $config['regdate'] = date('Y-m-d', strtotime($user->reg_date)); } $new_data = array(); if ($data) { foreach ($data as $day => $value) { $new_data[$day] = $value['rating'] + $value['verify'] + $value['pro']; } } // сегодняшний рейтинг берем из $user - тут он актуальный и не зависит от кэша if ($type === 'month') { array_pop($new_data); $new_data[] = floatval($user->rating); } $config['data'] = $new_data; $config['days'] = date('t', $TIME); $config['startdate'] = date('Y-m-01', $TIME); } if ($width) { $config['w'] = (int) $width; } $config = json_encode($config); $objResponse->script("loadGraph('{$type}', {$config});"); return $objResponse; }
/** * Строит график. */ public function createGraph() { $periods = rating::getMonthParts(date('Y-m-d', $this->_time)); foreach ($periods as $m => $p) { foreach ($this->_pro as $pr) { for ($c = 0; $c < 4; ++$c) { if ($p[$c] >= strtotime($pr['from_time']) && $p[$c] <= strtotime($pr['to_time'])) { $this->_ratingData[$m]['part' . ($c + 1)] *= rating::PRO_FACTOR; } } } } $dots = array(); // месяцы $labels_group = $this->doc->createElement('g'); $this->svg->appendChild($labels_group); $last = null; $c = count($this->_data); $max = $this->_getMax(); $min = $this->_min; //var_dump($min); if ($min < 0) { $h_diff = $min * -1; $h_diff *= 1.2; $max = ($max == $min || $max < 0 ? 0 : $max) + $h_diff; } $r_last = 0; for ($i = 0; $i < $this->_columns; ++$i) { $t = mktime(0, 0, 0, $i + 1, date('d', $this->_time), date('Y', $this->_time)); $m_params = array('fill' => '#B2B2B2'); $m_params['fill'] = date('Y-m', $this->_time) == date('Y-m', $t) ? '#666666' : $m_params['fill']; $label = $this->addText($this->_columnWidth * $i + $this->_columnWidth / 2, 155, $this->_labels[$i], $m_params); $labels_group->appendChild($label); if ($c <= 0) { continue; } for ($ii = 0; $ii < 4; ++$ii) { $x = $this->_columnWidth / 4 * $ii + $this->_columnWidth * $i + $this->_columnWidth / 8; $y = $this->_graphSize[1]; if (isset($this->_ratingData[$i])) { $field = 'part' . ($ii + 1); $y = $this->_graphSize[1] - ($this->_ratingData[$i][$field] + $h_diff) * $this->_graphSize[1] / $max * $this->_zoom; } if (!isset($this->_ratingData[$i]['_date']) && $last) { $y = $last; } if ($ii == 0 && $i == 0) { $this->_fillCoord[] = 'M0, ' . $this->_graphSize[1]; $this->_pathCoord[] = "M0, {$y}"; $this->_fillCoord[] = "L0, {$y}"; } $this->_pathCoord[] = "L{$x}, {$y}"; $this->_fillCoord[] = "L{$x}, {$y}"; $this->_path[] = array($x, $y); $rating = $r_last; if (isset($this->_ratingData[$i])) { $rating = $this->_ratingData[$i][$field]; } $date = date('d.m.Y', $periods[$i][$ii]); $dots[] = array($x, $y, floatval($rating), $date); $last = $y; $r_last = $rating; } if (isset($this->_ratingData[$i]['_date'])) { $c--; } } $this->drawPart(); // точки $this->_dots = $dots; $dots_group = $this->doc->createElement('g'); $dots_group->setAttribute('id', 'dots_group'); $dots_group->setIdAttribute('id', true); $this->svg->appendChild($dots_group); if ($dots) { foreach ($dots as $i => $dot) { $params = array(); if (isset($dot[2])) { $params['ratingvalue'] = $dot[2]; } if (isset($dot[3])) { $params['ratingdate'] = $dot[3]; } $dots_group->appendChild($this->addCircle($dot[0], $dot[1], $params)); } } $this->_setPro(); }
$graph_class = 'Rating_Svg_Daily'; $pro_periods_date = date('Y-m-01', $TIME); $file = "/users/" . substr($u_login, 0, 2) . "/{$u_login}/upload/{$file_name}.{$file_ext}"; $cfile = new CFile($file); $overwrite = date('Y-m') != date('Y-m', strtotime($cfile->modified)); break; case 'year': $TIME = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $file_name = 'rating_year'; $get_rating_by = 'getRatingByYear'; $get_rating_date = date('Y', $TIME); $graph_class = 'Rating_Svg_Monthly'; $pro_periods_date = date('Y-01-01', $TIME); $file = "/users/" . substr($u_login, 0, 2) . "/{$u_login}/upload/{$file_name}.{$file_ext}"; $cfile = new CFile($file); $periods = rating::getMonthParts(date('Y-01-01')); $cur_period = $periods[intval(date('m', $TIME)) - 1]; if ($TIME <= $cur_period[0]) { $file_maxtime = mktime(0, 0, 0, date('m', $cur_period[0]), 0, date('Y', $cur_period[0])); } elseif ($TIME > $cur_period[0] && $TIME <= $cur_period[1]) { $file_maxtime = $cur_period[0]; } elseif ($TIME > $cur_period[1] && $TIME <= $cur_period[2]) { $file_maxtime = $cur_period[1]; } elseif ($TIME > $cur_period[2] && $TIME <= $cur_period[3]) { $file_maxtime = $cur_period[2]; } $overwrite = strtotime($cfile->modified) < $file_maxtime; break; default: $TIME = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $file_name = 'rating';