<?php

class userStatistics {

  public static function recordDownloadPresent($user, $presentation) {
    if ($presentation->getPrUser() == $user->getUsId()) return;

    if ($price = $presentation->getPrPrice()) {
      if (!$presentation->getUserLicense($user->getUsId())) {
        $user->doTransaction(-$price, 'pay', 'present '.$presentation->getPrId());
        $presentation->getTbluser()->doTransaction($price, 'sell', 'present '.$presentation->getPrId());
        $presentation->createUserLicense($user->getUsId());
      }
      return;
    }

    try {
      $tbluserpresentation = new Tbluserpresentation();
      $tbluserpresentation->setUsPreUser($user->getUsId());
      $tbluserpresentation->setUsPrePresentation($presentation->getPrId());
      $tbluserpresentation->setUsPreDate(date('Y-m-d H:i:s'));
      $tbluserpresentation->save();

      $presentation->setPrHit( $presentation->getPrHit() + 1 );
      $presentation->save();

      $user->incDownload(false, 1);
      $user->setUsScore($user->getUsScore() - 1);
      $user->save();

      $author = $presentation->getTbluser();
      $author->setUsScore($author->getUsScore() + 3);
      $author->save();
      self::incUserPoint($author->getUsId(), 2);
    } catch (Exception $e) {
    }
  }

  public static function recordDownloadDocument($user, $document) {
    if ($document->getDocUser() == $user->getUsId()) return;

    try {
      $tbluserdocument = new Tbluserdocument();
      $tbluserdocument->setUsDocUser($user->getUsId());
      $tbluserdocument->setUsDocdocument($document->getDocId());
      $tbluserdocument->setUsDocDate(date('Y-m-d H:i:s'));
      $tbluserdocument->save();

      $document->setDocHit( $document->getDocHit() + 1 );
      $document->save();

      $user->incDownload();

      $author = $document->getTbluser();
      $author->setUsScore($author->getUsScore() + 1);
      $author->save();
      self::incUserPoint($author->getUsId(), 1);
    } catch (Exception $e) {
    }
  }

  /**
   * Return the day that will be updated score next
   *
   */
  public function getNextUpdateDate() {
    $memcache = sfMemcacheCache::getInstance();
    $ret = $memcache->get('nextUpdateDate', 'blog');
    if ($ret==null) {
      $ret = myUtility::execSql("select max(udsc_date) from tbluserdayscore");
      $ret = $ret[0][0];
      if ($ret == null) $ret = date('2000-1-1');
      else $ret = date('Y-m-d', strtotime("$ret +1 day"));
      $memcache->set('nextUpdateDate', 'blog', $ret);
    }
    return $ret;
  }

  /**
   * Move all temporary points from tempoint table to daypoint table
   *
   */
  public function moveTempPointToDayPoint() {
    $currentDate = date('Y-m-d');
    if ($currentDate<=self::getNextUpdateDate()) return false;
    $memcache = sfMemcacheCache::getInstance();
    $memcache->set('nextUpdateDate', 'blog', $currentDate);

    myUtility::execSql("insert ignore into tbluserdayscore select utsc_user, utsc_date, utsc_score from tblusertempscore where utsc_date<'$currentDate'", true);
    myUtility::execSql("delete from tblusertempscore where utsc_date<'$currentDate'", true);
    return true;
  }

  public static function incUserPoint($user_id, $addPoint) {
    if ($user_id==0) return;
    $today = date('Y-m-d');
    self::moveTempPointToDayPoint();
    $tblusertempscore = TblusertempscorePeer::retrieveByPK($user_id, $today);
    if (!$tblusertempscore) {
      $tblusertempscore = new Tblusertempscore();
      $tblusertempscore->setUtscUser($user_id);
      $tblusertempscore->setUtscDate($today);
      $tblusertempscore->setUtscScore($addPoint);
    } else {
      $tblusertempscore->setUtscScore($tblusertempscore->getUtscScore()+$addPoint);
    }
    $tblusertempscore->save();
  }
}
?>
