first commit
This commit is contained in:
219
modules/gamification/classes/Advice.php
Normal file
219
modules/gamification/classes/Advice.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2016 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2016 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class Advice extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
|
||||
public $id_ps_advice;
|
||||
|
||||
public $id_tab;
|
||||
|
||||
public $validated;
|
||||
|
||||
public $hide;
|
||||
|
||||
public $selector;
|
||||
|
||||
public $location;
|
||||
|
||||
public $html;
|
||||
|
||||
public $start_day;
|
||||
|
||||
public $stop_day;
|
||||
|
||||
public $weight;
|
||||
|
||||
/**
|
||||
* @see ObjectModel::$definition
|
||||
*/
|
||||
public static $definition = [
|
||||
'table' => 'advice',
|
||||
'primary' => 'id_advice',
|
||||
'multilang' => true,
|
||||
'fields' => [
|
||||
'id_ps_advice' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'id_tab' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'selector' => ['type' => self::TYPE_STRING],
|
||||
'location' => ['type' => self::TYPE_STRING],
|
||||
'validated' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
'start_day' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'stop_day' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'weight' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
|
||||
// Lang fields
|
||||
'html' => ['type' => self::TYPE_HTML, 'lang' => true, 'required' => true, 'validate' => 'isString'],
|
||||
],
|
||||
];
|
||||
|
||||
public static function getIdByIdPs($id_ps_advice)
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select('id_advice');
|
||||
$query->from('advice', 'b');
|
||||
$query->where('`id_ps_advice` = ' . (int) $id_ps_advice);
|
||||
|
||||
return (int) Db::getInstance()->getValue($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $idTab
|
||||
* @param bool $includePremium [default=false] True to include Premium as well
|
||||
* @param bool $includeAddons [default=false] True to include Addons as well
|
||||
*
|
||||
* @return array[]
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public static function getValidatedByIdTab($idTab, $includePremium = false, $includeAddons = false)
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select('a.`id_ps_advice`, a.`selector`, a.`location`, al.`html`, a.`weight`');
|
||||
$query->from('advice', 'a');
|
||||
$query->join('
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'advice_lang` al ON al.`id_advice` = a.`id_advice`
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'tab_advice` at ON at.`id_advice` = a.`id_advice`
|
||||
');
|
||||
|
||||
$selectorsToExclude = [];
|
||||
if (!$includePremium) {
|
||||
$selectorsToExclude[] = '#dashtrends';
|
||||
}
|
||||
if (!$includeAddons) {
|
||||
$selectorsToExclude[] = 'addons';
|
||||
}
|
||||
|
||||
$selectorsToExcludeSql = '';
|
||||
if (!empty($selectorsToExclude)) {
|
||||
$selectorsToExcludeSql = 'AND a.selector NOT IN("' . implode('","', $selectorsToExclude) . '")';
|
||||
}
|
||||
|
||||
$query->where(sprintf(
|
||||
"a.validated = 1
|
||||
AND a.hide = 0
|
||||
AND al.id_lang = %d
|
||||
AND at.id_tab = %d
|
||||
$selectorsToExcludeSql
|
||||
AND (
|
||||
(a.start_day = 0 AND a.stop_day = 0)
|
||||
OR (%s BETWEEN a.start_day AND a.stop_day)
|
||||
)",
|
||||
(int) Context::getContext()->language->id,
|
||||
$idTab,
|
||||
date('d')
|
||||
));
|
||||
|
||||
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
$advices = [];
|
||||
if (is_array($result)) {
|
||||
foreach ($result as $res) {
|
||||
$advices[] = [
|
||||
'selector' => $res['selector'],
|
||||
'location' => $res['location'],
|
||||
'html' => $res['html'],
|
||||
'id_ps_advice' => $res['id_ps_advice'],
|
||||
'weight' => $res['weight'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $advices;
|
||||
}
|
||||
|
||||
public static function getValidatedPremiumOnlyByIdTab($id_tab)
|
||||
{
|
||||
$advices = self::getValidatedByIdTab($id_tab, true);
|
||||
|
||||
foreach ($advices as $k => $a) {
|
||||
if ($a['selector'] != '#dashtrends') {
|
||||
unset($advices[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $advices;
|
||||
}
|
||||
|
||||
public static function getValidatedAddonsOnlyByIdTab($id_tab)
|
||||
{
|
||||
$advices = self::getValidatedByIdTab($id_tab, false, true);
|
||||
foreach ($advices as $k => $a) {
|
||||
if ($a['selector'] != 'addons') {
|
||||
unset($advices[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $advices;
|
||||
}
|
||||
|
||||
public static function getIdsAdviceToValidate()
|
||||
{
|
||||
$ids = [];
|
||||
$query = new DbQuery();
|
||||
$query->select('a.`id_advice`');
|
||||
$query->from('advice', 'a');
|
||||
$query->join('
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition_advice` ca ON ca.`id_advice` = a.`id_advice` AND ca.`display` = 1
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition` c ON c.`id_condition` = ca.`id_condition` AND c.`validated` = 1');
|
||||
$query->where('a.`validated` = 0');
|
||||
$query->groupBy('a.`id_advice`');
|
||||
$query->having('count(*) = SUM(c.`validated`)');
|
||||
|
||||
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
|
||||
if (is_array($result)) {
|
||||
foreach ($result as $advice) {
|
||||
$ids[] = $advice['id_advice'];
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public static function getIdsAdviceToUnvalidate()
|
||||
{
|
||||
$ids = [];
|
||||
$query = new DbQuery();
|
||||
$query->select('a.`id_advice`');
|
||||
$query->from('advice', 'a');
|
||||
$query->join('
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition_advice` ca ON ca.`id_advice` = a.`id_advice` AND ca.`display` = 0
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition` c ON c.`id_condition` = ca.`id_condition` AND c.`validated` = 1');
|
||||
$query->where('a.`validated` = 1');
|
||||
$query->groupBy('a.`id_advice`');
|
||||
$query->having('count(*) = SUM(c.`validated`)');
|
||||
|
||||
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
|
||||
if (is_array($result)) {
|
||||
foreach ($result as $advice) {
|
||||
$ids[] = $advice['id_advice'];
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
131
modules/gamification/classes/Badge.php
Normal file
131
modules/gamification/classes/Badge.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2016 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2016 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class Badge extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
|
||||
public $id_ps_badge;
|
||||
|
||||
public $type;
|
||||
|
||||
public $id_group;
|
||||
|
||||
public $group_position;
|
||||
|
||||
public $group_name;
|
||||
|
||||
public $scoring;
|
||||
|
||||
public $validated;
|
||||
|
||||
public $name;
|
||||
|
||||
public $description;
|
||||
|
||||
public $awb;
|
||||
|
||||
const BAGDE_IMG_URL = __PS_BASE_URI__ . 'modules/gamification/views/img/badges/';
|
||||
|
||||
/**
|
||||
* @see ObjectModel::$definition
|
||||
*/
|
||||
public static $definition = [
|
||||
'table' => 'badge',
|
||||
'primary' => 'id_badge',
|
||||
'multilang' => true,
|
||||
'fields' => [
|
||||
'id_ps_badge' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'type' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 32],
|
||||
'id_group' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 32],
|
||||
'group_position' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'scoring' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'validated' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
'awb' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
|
||||
// Lang fields
|
||||
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 64],
|
||||
'description' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 255],
|
||||
'group_name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 255],
|
||||
],
|
||||
];
|
||||
|
||||
public function getBadgeImgUrl()
|
||||
{
|
||||
return self::BAGDE_IMG_URL . (int) $this->id_ps_badge . '_' . (int) $this->validated . '.png';
|
||||
}
|
||||
|
||||
public function validate()
|
||||
{
|
||||
$this->validated = 1;
|
||||
$this->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getIdByIdPs($id_ps_badge)
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select('id_badge');
|
||||
$query->from('badge', 'b');
|
||||
$query->where('`id_ps_badge` = ' . (int) $id_ps_badge);
|
||||
|
||||
return (int) Db::getInstance()->getValue($query);
|
||||
}
|
||||
|
||||
public static function getIdsBadgesToValidate()
|
||||
{
|
||||
$ids = [];
|
||||
$query = new DbQuery();
|
||||
$query->select('b.`id_badge`');
|
||||
$query->from('badge', 'b');
|
||||
$query->join('
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition_badge` cb ON cb.`id_badge` = b.`id_badge`
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'condition` c ON c.`id_condition` = cb.`id_condition` AND c.`validated` = 1');
|
||||
$query->where('b.validated = 0');
|
||||
$query->groupBy('b.`id_badge`');
|
||||
$query->having('count(*) = SUM(c.validated)');
|
||||
|
||||
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
|
||||
foreach ($result as $badge) {
|
||||
$ids[] = $badge['id_badge'];
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public function getNextBadgeId()
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select('b.`id_badge`');
|
||||
$query->from('badge', 'b');
|
||||
$query->where('b.id_group = \'' . pSQL($this->id_group) . '\' AND b.validated = 0');
|
||||
$query->orderBy('b.group_position');
|
||||
|
||||
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue($query);
|
||||
}
|
||||
}
|
||||
353
modules/gamification/classes/Condition.php
Normal file
353
modules/gamification/classes/Condition.php
Normal file
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2016 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2016 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class Condition extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
|
||||
public $id_ps_condition;
|
||||
|
||||
public $type;
|
||||
|
||||
public $request;
|
||||
|
||||
public $operator;
|
||||
|
||||
public $value;
|
||||
|
||||
public $result;
|
||||
|
||||
public $calculation_type;
|
||||
|
||||
public $calculation_detail;
|
||||
|
||||
public $validated;
|
||||
|
||||
public $date_add;
|
||||
|
||||
public $date_upd;
|
||||
|
||||
public static $unauthorized = ['DELETE', 'DROP'];
|
||||
|
||||
/**
|
||||
* @see ObjectModel::$definition
|
||||
*/
|
||||
public static $definition = [
|
||||
'table' => 'condition',
|
||||
'primary' => 'id_condition',
|
||||
'fields' => [
|
||||
'id_ps_condition' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
'type' => ['type' => self::TYPE_STRING, 'size' => 32],
|
||||
'request' => ['type' => self::TYPE_STRING],
|
||||
'operator' => ['type' => self::TYPE_NOTHING],
|
||||
'value' => ['type' => self::TYPE_STRING],
|
||||
'result' => ['type' => self::TYPE_STRING],
|
||||
'calculation_type' => ['type' => self::TYPE_STRING],
|
||||
'calculation_detail' => ['type' => self::TYPE_STRING],
|
||||
'validated' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
|
||||
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
|
||||
],
|
||||
];
|
||||
|
||||
public static function getIdByIdPs($id_ps_condition)
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select('id_condition');
|
||||
$query->from('condition', 'c');
|
||||
$query->where('`id_ps_condition` = ' . (int) $id_ps_condition);
|
||||
|
||||
return (int) Db::getInstance()->getValue($query);
|
||||
}
|
||||
|
||||
public static function getIdsByHookCalculation($hook_name)
|
||||
{
|
||||
$ids = [];
|
||||
$in = [];
|
||||
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_badge');
|
||||
$sub_query->from('badge', 'b');
|
||||
$sub_query->where('b.`validated` = 0');
|
||||
|
||||
$sub_results = Db::getInstance()->executeS($sub_query);
|
||||
|
||||
foreach ($sub_results as $sub_result) {
|
||||
$in[] = $sub_result['id_badge'];
|
||||
}
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_badge` cb ON cb.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`calculation_type` = \'hook\'');
|
||||
$query->where('c.`calculation_detail` = \'' . pSQL($hook_name) . '\'');
|
||||
$query->where('c.`validated` = 0');
|
||||
if (count($in)) {
|
||||
$query->where('cb.`id_badge` IN (' . implode(',', $in) . ')');
|
||||
}
|
||||
$query->groupBy('c.`id_condition`');
|
||||
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_advice');
|
||||
$sub_query->from('advice', 'a');
|
||||
|
||||
$sub_results = Db::getInstance()->executeS($sub_query);
|
||||
|
||||
$in = [];
|
||||
|
||||
foreach ($sub_results as $sub_result) {
|
||||
$in[] = $sub_result['id_advice'];
|
||||
}
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_advice` ca ON ca.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`calculation_type` = \'hook\'');
|
||||
$query->where('c.`calculation_detail` = \'' . pSQL($hook_name) . '\'');
|
||||
$query->where('c.`validated` = 0');
|
||||
if (count($in)) {
|
||||
$query->where('ca.`id_advice` IN (' . implode(',', $in) . ')');
|
||||
}
|
||||
$query->groupBy('c.`id_condition`');
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
return array_unique($ids);
|
||||
}
|
||||
|
||||
public static function getIdsDailyCalculation()
|
||||
{
|
||||
$ids = [];
|
||||
$in = [];
|
||||
|
||||
//badges conditions validation
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_badge');
|
||||
$sub_query->from('badge', 'b');
|
||||
|
||||
$sub_results = Db::getInstance()->executeS($sub_query);
|
||||
|
||||
foreach ($sub_results as $sub_result) {
|
||||
$in[] = $sub_result['id_badge'];
|
||||
}
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_badge` cb ON cb.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`calculation_type` = \'time\'');
|
||||
$query->where('DATEDIFF(NOW(), `date_upd`) >= `calculation_detail`');
|
||||
$query->where('c.`validated` = 0');
|
||||
if (count($in)) {
|
||||
$query->where('cb.`id_badge` IN (' . implode(',', $in) . ')');
|
||||
}
|
||||
$query->groupBy('c.`id_condition`');
|
||||
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
//advice conditions validation
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_advice');
|
||||
$sub_query->from('advice', 'a');
|
||||
|
||||
$sub_results = Db::getInstance()->executeS($sub_query);
|
||||
|
||||
$in = [];
|
||||
|
||||
foreach ($sub_results as $sub_result) {
|
||||
$in[] = $sub_result['id_advice'];
|
||||
}
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_advice` ca ON ca.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`calculation_type` = \'time\'');
|
||||
$query->where('DATEDIFF(NOW(), `date_upd`) >= `calculation_detail`');
|
||||
$query->where('c.`validated` = 0');
|
||||
if (count($in)) {
|
||||
$query->where('ca.`id_advice` IN (' . implode(',', $in) . ')');
|
||||
}
|
||||
$query->groupBy('c.`id_condition`');
|
||||
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
return array_unique($ids);
|
||||
}
|
||||
|
||||
public static function getIdsByBadgeGroupPosition($badge_group_position)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_badge');
|
||||
$sub_query->from('badge', 'b');
|
||||
$sub_query->where('b.`group_position` = ' . (int) $badge_group_position);
|
||||
$sub_query->where('b.`validated` = 0');
|
||||
$sub_query->groupBy('b.`id_group`');
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_badge` cb ON cb.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`validated` = 0');
|
||||
$query->where('cb.`id_badge` IN (' . $sub_query . ')');
|
||||
$query->groupBy('c.`id_condition`');
|
||||
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public static function getIdsByBadgeGroup($badge_group)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
$sub_query = new DbQuery();
|
||||
$sub_query->select('id_badge');
|
||||
$sub_query->from('badge', 'b');
|
||||
$sub_query->where('b.`id_group` = ' . (int) $badge_group);
|
||||
$sub_query->where('b.`validated` = 0');
|
||||
$sub_query->groupBy('b.`id_group`');
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('c.`id_condition`');
|
||||
$query->from('condition', 'c');
|
||||
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_badge` cb ON cb.`id_condition` = c.`id_condition`');
|
||||
$query->where('c.`validated` = 0');
|
||||
$query->where('cb.`id_badge` IN (' . $sub_query . ')');
|
||||
$query->groupBy('c.`id_condition`');
|
||||
|
||||
$result = Db::getInstance()->executeS($query);
|
||||
foreach ($result as $r) {
|
||||
$ids[] = $r['id_condition'];
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public function processCalculation()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case 'configuration':
|
||||
$this->processConfiguration();
|
||||
break;
|
||||
case 'install':
|
||||
$this->processInstall();
|
||||
break;
|
||||
case 'sql':
|
||||
$this->processSql();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function processSql()
|
||||
{
|
||||
if (preg_match('/' . implode('|', self::$unauthorized) . '/', $this->request)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->result = (int) Db::getInstance()->getValue(GamificationTools::parseMetaData($this->request));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->makeCalculation($this->operator, $this->result, $this->value)) {
|
||||
$this->validated = 1;
|
||||
}
|
||||
$this->save();
|
||||
}
|
||||
|
||||
protected function processConfiguration()
|
||||
{
|
||||
$this->result = Configuration::get($this->request);
|
||||
if ($this->result = $this->makeCalculation($this->operator, $this->result, $this->value)) {
|
||||
$this->validated = 1;
|
||||
}
|
||||
$this->save();
|
||||
}
|
||||
|
||||
protected function processInstall()
|
||||
{
|
||||
$install = strtotime(_PS_CREATION_DATE_ . ' 00:00:00');
|
||||
$value = strtotime('+ ' . (int) $this->value . ' day', $install);
|
||||
$this->result = $this->makeCalculation($this->operator, (time() - $install), $value - $install);
|
||||
if ($this->result) {
|
||||
$this->validated = 1;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
protected function makeCalculation($operator, $arg1, $arg2)
|
||||
{
|
||||
$result = false;
|
||||
switch ($operator) {
|
||||
case '>':
|
||||
$result = $arg1 > $arg2;
|
||||
break;
|
||||
case '>=':
|
||||
$result = $arg1 >= $arg2;
|
||||
break;
|
||||
case '<':
|
||||
$result = $arg1 < $arg2;
|
||||
break;
|
||||
case '<=':
|
||||
$result = $arg1 <= $arg2;
|
||||
break;
|
||||
case '==':
|
||||
$result = $arg1 == $arg2;
|
||||
break;
|
||||
case '!=':
|
||||
$result = $arg1 != $arg2;
|
||||
break;
|
||||
}
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
}
|
||||
87
modules/gamification/classes/GamificationTools.php
Normal file
87
modules/gamification/classes/GamificationTools.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2016 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2016 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class GamificationTools
|
||||
{
|
||||
public static function parseMetaData($content)
|
||||
{
|
||||
$meta_data = [
|
||||
'PREFIX_' => _DB_PREFIX_,
|
||||
];
|
||||
//replace define
|
||||
$content = str_replace(array_keys($meta_data), array_values($meta_data), $content);
|
||||
|
||||
//replace meta data
|
||||
$content = preg_replace_callback('#\{config\}([a-zA-Z0-9_-]*)\{/config\}#', function ($matches) {
|
||||
return Configuration::get($matches[1]);
|
||||
}, $content);
|
||||
$content = preg_replace_callback('#\{link\}(.*)\{/link\}#', function ($matches) {
|
||||
return Context::getContext()->link->getAdminLink($matches[1]);
|
||||
}, $content);
|
||||
$content = preg_replace_callback('#\{employee\}(.*)\{/employee\}#', function ($matches) {
|
||||
return Context::getContext()->employee->$matches[1];
|
||||
}, $content);
|
||||
$content = preg_replace_callback('#\{language\}(.*)\{/language\}#', function ($matches) {
|
||||
return Context::getContext()->language->$matches[1];
|
||||
}, $content);
|
||||
$content = preg_replace_callback('#\{country\}(.*)\{/country\}#', function ($matches) {
|
||||
return Context::getContext()->country->$matches[1];
|
||||
}, $content);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Json api file, forcing gzip compression to save bandwith.
|
||||
*
|
||||
* @param string $url
|
||||
* @param bool $withResponseHeaders
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function retrieveJsonApiFile($url, $withResponseHeaders = false)
|
||||
{
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
|
||||
// @see https://cloud.google.com/appengine/kb/#compression
|
||||
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, 'gzip');
|
||||
curl_setopt($curl, CURLOPT_HEADER, $withResponseHeaders);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$content = curl_exec($curl);
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user