<?php

defined('_JEXEC') or die('Restricted Access');

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

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsDetails;

class plgQuickiconuserpointsicon extends CMSPlugin {

    public function onGetIcons($context) {
        if (0 == count(array_intersect(UserPoints::getUser()->groups, [7, 8]))) {
            return;
        }

        if (
                $context == $this->params->get('context', 'mod_quickicon') && UserPoints::getUser()->authorise('core.manage', 'com_userpoints')
        ) {

            $lang = Factory::getLanguage();
            $lang->load('com_userpoints', JPATH_ADMINISTRATOR);

            $label = Text::_('AUP_USERS_POINTS');
            $label2 = "";

            // check if unapproved item
            $count = PointsDetails::countUnapproved();

            if ($count) {
                $label2 = '<span class="small"> - <font color="red">' . Text::_('AUP_PENDING_APPROVAL') . ' (' . $count . ')</font></span>';
            }
            $icon = ($count) ? "warning" : "star-2";

            return array(array(
                    'link' => 'index.php?option=com_userpoints',
                    'image' => $icon,
                    'text' => $label . $label2,
                    'id' => 'plg_quickicon_userpointsicon'
            ));
        } else {
            return;
        }
    }
}
