Report_views_weekly_model.php 3.16 KB
Newer Older
Phạm Văn Đoan committed
1 2 3 4
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report_views_weekly_model extends CI_Model
{
5 6
    protected $_table = 'report_views_weekly';
    protected $_table_new = 'report_views_weekly_new';
Phạm Văn Đoan committed
7 8 9 10

    public function __construct(){
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
11 12 13 14

        if (date('W') > 13) {
            $this->_table = $this->_table_new;
        }
Phạm Văn Đoan committed
15 16
    }

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    public function cmsCountAll($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination($record, $start, $name = null)
    {
32 33
        $this->db->select($this->_table . '.*, u1.full_name AS singer_name, track_user.name AS singer_name2,
        tracks.title AS name_song, GROUP_CONCAT(track_user.`name` SEPARATOR ",") AS all_name, tracks.art AS art');
34
        $this->db->from($this->_table);
35 36
        $this->db->join('track_user', 'track_user.track_id = ' . $this->_table . '.track_id', 'left');
        $this->db->join('tracks', 'tracks.id = ' . $this->_table . '.track_id', 'left');
37 38
        $this->db->join('users u1', 'u1.id = track_user.user_id', 'left');
        if ($name != null) {
39 40 41
            $data = explode('-', $name);  
            $this->db->where('week', $data[0]);
            $this->db->where('year', $data[1]);
42
        }
43
        $this->db->group_by($this->_table . '.id');
44 45 46 47 48 49
        $this->db->order_by($this->_table . '.total_point', 'desc');
        $this->db->limit($record);
        $query = $this->db->get();

        return $query->result_array();
    }
50 51 52

    public function selectSearch()
    {
53 54 55 56 57
        $this->db->select('week, year');
        $this->db->from($this->_table);
        $this->db->distinct($this->_table . '.week');
        $this->db->order_by( $this->_table . '.year desc');
        $this->db->order_by( $this->_table . '.week desc');
58
        $this->db->limit(60);
59 60 61 62 63
        $query = $this->db->get();

        return $query->result_array();
    }

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    /**
     * Hàm lấy xếp hạng tuần trước để so sánh tăng/giảm hạng
     *
     * @param $week_year
     * @return array
     */
    public function previousRanking($week_year)
    {
        $this->db->select('track_id, order, week, year');
        $this->db->from($this->_table);

        if ($week_year != null) {
            $data = explode('-', $week_year);
            $week = intval($data[0]);
            $year = intval($data[1]);
            if ($week == 1) {
                return [];
            } else {
                $week = $week - 1;
            }
            $this->db->where('week', $week);
            $this->db->where('year', $year);
        }
        $this->db->where('order <=', 100);
        $query = $this->db->get();
        $data = $query->result_array();

        $result = [];
        if ($data) {
            foreach ($data as $key => $val) {
                $result[$val['track_id']] = $val['order'];
            }
        }

        return $result;
    }

Phạm Văn Đoan committed
101
}