<?php

class presentComponents extends sfComponents {

  public function executeCreatedirect() {
    $catItem = blogStore::$catTree->getCatItem($this->cat_id);
    $this->exts = $catItem->getAllowExts();
    $this->catType = $catItem->type;
    $this->doc = blogStore::getCatTypeName($this->catType);
    $this->zipExts = sfConfig::get('app_zip_extensions');
  }

  public function executeCreateselect() {
    $c = new Criteria();
    $c->add(TblpresentationPeer::PR_USER, blogStore::$userId);
    $c->add(TblpresentationPeer::PR_STATUS, 0);
    $c->addDescendingOrderByColumn(TblpresentationPeer::PR_DATE);
    $this->page = $this->getRequestParameter('page', 1);
    $this->pager = new PropelPager($c, 'TblpresentationPeer', 'doSelect', $this->page, 18);
  }

  public function executeCreatepaste() {
  }

  public function executeSame() {
    $this->same = $this->getRequestParameter('same', 'show');
    $this->page = $this->getRequestParameter('page', 1);

    if ($this->same == 'author') {
      $c = new Criteria();
      $c->add(TblblogentryPeer::ENTRY_USER, $this->tblblogentry->getEntryUser());
      $c->add(TblblogentryPeer::ENTRY_TYPE, $this->tblblogentry->getEntryType());
      $c->addDescendingOrderByColumn(TblblogentryPeer::ENTRY_MODIFYDATE);
      $c->addIndex('userindex');
      $this->pager = new PropelPager($c, 'TblblogentryPeer', 'doSelect', $this->page, 18);
      $this->tblblogentrys = $this->pager->getResult();
    }
    elseif ($this->same == 'downloader') {
      $c = new Criteria();
      $c->add(TbluserpresentationPeer::USPRE_PRESENTATION, $this->tblblogentry->getEntryLink());
      $c->addDescendingOrderByColumn(TbluserpresentationPeer::USPRE_DATE);
      $this->pager = new PropelPager($c, 'TbluserpresentationPeer', 'doSelect', $this->page, 18);
    }

    $this->entry_id = $this->tblblogentry->getEntryId();
    $this->tblpresentation = $this->tblblogentry->getTblpresentation();
  }

  public function parsePackageData($jsdata) {
    $nodes = array();
    if ($jsdata->item == null) $nodes[$jsdata->foreignId] = $jsdata->title;
    else foreach ($jsdata->item as $item) $nodes += $this->parsePackageData($item);
    return $nodes;
  }

  public function executeReportsco() {
    $package = TblscormcppackagePeer::retrieveByPk($this->pr_id);
    $nodetTitles = $this->parsePackageData(json_decode($package->getJsdata())->item);

    $this->totalHit = 0;
    $this->totalTime = 0;
    $this->learners = array();
    $this->nodes = array();

    $c = new Criteria();
    $c->add(TblscormcmicustomPeer::OBJ_ID, $this->pr_id);
    $c->add(TblscormcmicustomPeer::LVALUE, 'package_attempts');
    $this->tblcustom = TblscormcmicustomPeer::doSelect($c);
    foreach ($this->tblcustom as $c) $this->totalHit += $c->getRvalue();

    $c = new Criteria();
    $c->add(TblscormcpnodePeer::SLM_ID, $this->pr_id);
    $tblcminode = TblscormcminodePeer::doSelectJoinTblscormcpnode($c);

    foreach ($tblcminode as $cminode) {
      preg_match('/T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/', $cminode->getTotalTime(), $match);
      $time = $match[1]*3600 + $match[2]*60 + $match[3];
      $this->totalTime += $time;

      $name = $cminode->getLearnerName();
      $userId = $cminode->getUserId();
      $nodeId = $cminode->getCpNodeId();
      $score = $cminode->getScaled();

      if (!$this->learners[$userId]) {
        $this->learners[$userId] = array('name'=>$name, 'time'=>0, 'score'=>0, 'detail'=>array());
      }
      $this->learners[$userId]['time'] += $time;
      $this->learners[$userId]['score'] += $score;
      $this->learners[$userId]['detail'][$nodeId] = array($nodetTitles[$nodeId], $time, $score);

      if (!$this->nodes[$nodeId]) {
        $this->nodes[$nodeId] = array('time'=>0, 'hit'=>0, 'score'=>0, 'scorehit'=>0, 'detail'=>array());
        $this->nodes[$nodeId]['name'] = $nodetTitles[$nodeId];
      }
      $this->nodes[$nodeId]['hit'] += 1;
      $this->nodes[$nodeId]['time'] += $time;
      $this->nodes[$nodeId]['score'] += $score;
      $this->nodes[$nodeId]['scorehit'] += ($score > 0)? 1: 0;
      $this->nodes[$nodeId]['detail'][$userId] = array($name, $time, $score);
    }
  }

  public function executeConfirmlesson() {
    if (!$this->getUser()->isAuthenticated()) return sfView::NONE;
    $lesson = $this->tblpresentation->getPrLesson();
    if ($lesson != null && $lesson == $this->tblblogentry->getEntryLesson()) return sfView::NONE;
    $this->cat_id = $this->tblblogentry->getEntryCategory();
    $this->cat_base = blogStore::$catTree->getCatItem($this->cat_id)->base;
    $this->entry_id = $this->tblblogentry->getEntryId();
    if (!lessonList::allowTag($this->cat_id)) return sfView::NONE;

    $resExts = sfConfig::get('app_resource_extension');
    $this->is_doc = in_array($this->tblpresentation->getPrType(), $resExts['lessonplan']);
  }

  public function executeModtab() {
    if (!in_array(blogStore::$blogId, array(12, 763773))) return sfView::NONE;
    if (!$this->tblblogentry->getEntryLesson() || !$this->tblblogentry->canMod()) return sfView::NONE;
    $this->entry_id = $this->tblblogentry->getEntryId();
    $this->cat_id = $this->tblblogentry->getEntryCategory();
  }

  public function executeLessondropdown() {
    $catItem = blogStore::$catTree->getCatItem($this->cat_id);
    $this->lessonList = commonData::$lessons->getCatList($catItem->base);
    array_pop($this->lessonList);
  }

  public function executeSearchlesson() {
  }
}
?>