<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=users.details.tags
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

require_once cot_incfile('reviews', 'plug');

$reviews_cnt = array(
  'pos' => $db->query("SELECT COUNT(*) FROM $db_reviews WHERE item_score=1 AND item_type='review' AND item_touserid=".$urr['user_id'])->fetchColumn(),
  'neg' => $db->query("SELECT COUNT(*) FROM $db_reviews WHERE item_score=0 AND item_type='review' AND item_touserid=".$urr['user_id'])->fetchColumn()
);

$reviews = $db->query("SELECT * FROM $db_reviews WHERE item_score=1 AND item_type='review' AND item_touserid=".$urr['user_id'].' ORDER BY item_date DESC LIMIT 5')->fetchAll();
foreach($reviews as $review) {
  if($review['item_area'] == 'market') { // && $review['item_mcode'] > 0) {
    $t->assign(cot_generate_markettags($review['item_mcode'], 'PRD_ROW_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
  }

  $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $review['item_date'])));
  $t->assign(cot_generate_usertags($review['item_userid'], 'REVIEW_ROW_OWNER_'));
  $t->assign(array(
  	'REVIEW_ROW_ID' => $review['item_id'],
  	'REVIEW_ROW_TEXT' => $review['item_text'],
  	'REVIEW_ROW_SCORE' => $review['item_score'],
    'REVIEW_ROW_AREA' => $review['item_area'],
  	'REVIEW_ROW_DATE' => $review['item_date'],
    'REVIEW_ROW_TIMEAGO' => $timeago,
  ));

  $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='".$review['item_area']."' AND item_type='review_answer' AND item_code='".$review['item_code']."' LIMIT 1")->fetch();
  if($mreview['item_id'] > 0) {
    $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $mreview['item_date'])));
    $t->assign(array(
    	'REVIEW_ANSW_ID' => $mreview['item_id'],
    	'REVIEW_ANSW_TEXT' => $mreview['item_text'],
    	'REVIEW_ANSW_SCORE' => $mreview['item_score'],
    	'REVIEW_ANSW_DATE' => $mreview['item_date'],
      'REVIEW_ANSW_TIMEAGO' => $timeago
    ));
    if($usr['id'] == $urr['user_id']) $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER.CAN_EDIT');
    $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER');
  } elseif($usr['id'] == $urr['user_id']) {
    $t->parse('MAIN.REVIEWS_ROWS.CAN_ANSWER');
  }

  $t->parse('MAIN.REVIEWS_ROWS');
}

$t->assign(array(
  'USERS_DETAILS_REVIEWS_CNT_POS' => $reviews_cnt['pos'],
  'USERS_DETAILS_REVIEWS_CNT_NEG' => $reviews_cnt['neg'],
));

?>