File: /home4/cca63905/public_html/nueva/modules/pmproductvideoreviews/src/Ratings.php
<?php
/**
* rec.reviews
*
* @author Presta-Module.com <support@presta-module.com> - https://www.presta-module.com
* @copyright Presta-Module - https://www.presta-module.com
* @license see file: LICENSE.txt
*
* ____ __ __
* | _ \ | \/ |
* | |_) | | |\/| |
* | __/ | | | |
* |_| |_| |_|
*/
namespace PrestaModule\ProductVideoReviews;
use Db;
use Context;
use DbQuery;
if (!defined('_PS_VERSION_')) {
exit;
}
class Ratings extends \ObjectModel
{
public $id;
public $id_api;
public $id_shop;
public $id_product;
public $id_lang;
public $note;
public $status;
public $id_customer;
public $id_order;
public $video_url;
public $video_prw_img;
public $date_publish;
public $date_unpublish;
public $data;
public $date_add;
public $date_upd;
public static $definition = [
'table' => 'pvr_rating',
'primary' => 'id_rating',
'fields' => [
'id_api' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'id_shop' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'id_product' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'id_lang' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'note' => ['type' => self::TYPE_STRING, 'validate' => 'isInt'],
'status' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'id_order' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true],
'video_url' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'video_prw_img' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'date_publish' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
'date_unpublish' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
'data' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
],
];
public static function getProductReviews($product_id, $limit = null, $sort = null, $active = true)
{
$query = new DbQuery();
$query->select('*');
$query->from('pvr_rating', 'pr');
$query->where('pr.`id_product` = ' . (int)$product_id);
$query->where('pr.`id_shop` = ' . (int)Context::getContext()->shop->id);
if (!empty($active)) {
$query->where('pr.`status` = 1');
}
if (!empty($limit)) {
$query->limit($limit);
}
if (!empty($sort)) {
if ($sort == 'best') {
$query->orderby('pr.`note` DESC');
}
if ($sort == 'worst') {
$query->orderby('pr.`note` ASC');
}
if ($sort == 'older') {
$query->orderby('pr.`date_add` ASC');
}
} else {
if ($sort == 'recent') {
$query->orderby('pr.`date_add` DESC');
}
}
$query->orderby('pr.`id_rating` DESC');
$datas = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
return $datas;
}
public static function getProductRatingGroupByNote($product_id)
{
$query = new DbQuery();
$query->select('pr.note as note, count(id_product) as nb');
$query->from('pvr_rating', 'pr');
$query->where('pr.`id_product` = ' . (int)$product_id);
$query->where('pr.`id_shop` = ' . (int)Context::getContext()->shop->id);
$query->where('pr.`status` = 1');
$query->groupby('note');
$query->orderby('note DESC');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
}
public static function getProductRatingCount($id_product)
{
$query = new DbQuery();
$query->select('count(id_product) as count');
$query->from('pvr_rating', 'pr');
$query->where('pr.`id_product` = ' . (int)$id_product);
$query->where('pr.`id_shop` = ' . (int)Context::getContext()->shop->id);
$query->where('pr.`status` = 1');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
}
public static function getBattle($product_id, $type = 'worst')
{
$query = new DbQuery();
$query->select('*');
$query->from('pvr_rating', 'pr');
$query->where('pr.`id_product` = ' . (int)$product_id);
$query->where('pr.`id_shop` = ' . (int)Context::getContext()->shop->id);
$query->where('pr.`video_url` != ""');
$query->where('pr.`status` = 1');
$query->where('pr.`note` != "0"');
if ($type == 'worst') {
$query->orderby('note ASC');
} else {
$query->orderby('note DESC');
}
$query->orderby('date_add DESC');
return Db::getInstance()->getRow($query);
}
public static function getUnplishedReviewsId($id_order)
{
$query = new DbQuery();
$query->select('id_rating');
$query->from('pvr_rating', 'pr');
$query->where('pr.`id_shop` = ' . (int)Context::getContext()->shop->id);
$query->where('pr.`video_url` != ""');
$query->where('pr.`status` = 2');
$query->where('pr.`id_order` = ' . (int)$id_order);
return Db::getInstance()->getValue($query);
}
}