<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site\Models;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;
use BlackSheepResearch\Component\UserPoints\Site\Problems\Medal;

class Medals extends UserModel {

    function __construct() {
        parent::__construct();
    }

    function _getMedalsList() {
        // Get the pagination request variables
        list($limitstart, $limit) = Medal::getLimitsFromRequest();
        $q = "SELECT l.*, IFNULL(m.medalcount,0) AS nummedals FROM #__alpha_userpoints_levelrank AS l LEFT JOIN "
            . " (SELECT medal, COUNT(medal) AS medalcount FROM #__alpha_userpoints_medals GROUP BY medal) AS m "
            . " ON l.id = m.medal WHERE typerank='1' ORDER BY typerank ASC, ordering ASC, levelpoints DESC";
        $total = $this->_getListCount($q);
        $results = $this->_getList($q, $limitstart, $limit);
        return array($results, $total, $limit, $limitstart);
    }

    function _getDetailsMedalsListUsers() {
        $app = Factory::getApplication();
        // Get the pagination request variables
        list($limitstart, $limit) = Medal::getLimitsFromRequest();

        $cid = $app->input->get('cid', 0, 'int');
        $q = "SELECT aup.referreid, m.medaldate AS dateawarded, m.reason , u.name, u.username, lv.id AS cid, lv.typerank, lv.icon, lv.rank FROM #__alpha_userpoints_medals AS m"
                . " LEFT JOIN #__alpha_userpoints_levelrank AS lv ON m.medal=lv.id"
                . " LEFT JOIN #__alpha_userpoints AS aup ON m.rid=aup.id"
                . " LEFT JOIN #__users AS u ON aup.userid=u.id"
                . " WHERE m.medal=$cid "
                . " ORDER BY m.medaldate DESC";
        $total = $this->_getListCount($q);
        $results = $this->_getList($q, $limitstart, $limit);
        return array($results, $total, $limit, $limitstart);
    }

}
