<?php

/*
 * @component AltaUserPoints
 * @copyright Copyright (C) 2008-2016 Bernard Gilly - Adrien Roussel
 * @license : GNU/GPL
 * @Website : https://www.nordmograph.com/extensions
 */
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class PlgUserUp_login extends CMSPlugin {

    public function onUserLogin($userparm, $options = array()) {
        // *** Login rule available only on frontend ***
        if (Factory::getApplication()->isClient('site')) {
            $user = UserPoints::getUserByUsername($userparm['username']);
            // *** Daily login rule available only on frontend ***
            if (is_object($user) AND !((int) $user->block)) {
                UserPoints::getReferreid((int) $user->id);
                $lang = Factory::getLanguage();
                $lang->load('com_userpoints', JPATH_SITE);
                $datareference = date(Text::_('DATE_FORMAT_LC2'), strtotime(SQL::dateSQL()));
                    // HTMLHelper::_('date', date('Y-m-d'), Text::_('DATE_FORMAT_LC2'), false);
                $keyreference = UserPoints::buildKeyreference('up_login', '', (int) $user->id);
                UserPoints::newpoints('up_login', '', $keyreference, $datareference);
            }
        }
    }
}
