function watch($id = null)
 {
     if (is_null($id)) {
         $this->Session->setFlash('Repository not found');
         $this->redirect('/');
     }
     if (!$this->isLoggedIn()) {
         $this->redirect(array('controller' => 'login', 'action' => 'index'));
     }
     $user = $this->getConnectedUser();
     $repo = $this->RepositoriesUser->find('first', array('conditions' => array('user_id' => $user['User']['id'], 'repository_id' => $id), 'recursive' => -1));
     if (empty($repo)) {
         $this->Session->setFlash('Repository not found');
         $this->redirect('/');
     }
     $watching = $repo['RepositoriesUser']['watching'];
     $this->RepositoriesUser->read(null, $repo['RepositoriesUser']['id']);
     $this->RepositoriesUser->set('watching', !$watching);
     $this->RepositoriesUser->save();
     if ($watching) {
         $msg = "Repository removed from watchlist";
     } else {
         $msg = "Repository added to watchlist";
     }
     $this->Session->setFlash($msg);
     $this->redirect(array('action' => 'set_repository_by_id', $id));
 }
Exemplo n.º 2
0
 /**
  * check if user points are enough to action required
  * or anon user who doesnt require any points
  * - upload
  * - download
  * - earn (does not require any points)
  * 
  * redirects if unexpected behavior
  * 
  * @return true if user needs a challenge, false otherwise	 
  */
 function _check_if_challenge()
 {
     $action = $this->Session->read('Action.type');
     $repo = $this->getCurrentRepository();
     $user = $this->getConnectedUser();
     /*
      * anon doesnt need points check
      * always need a challenge
      */
     if ($user == $this->anonymous) {
         return true;
     }
     $upload = strcmp($action, 'upload') == 0;
     $download = strcmp($action, 'download') == 0;
     $earn = strcmp($action, 'earn') == 0;
     if ($earn) {
         /*
          * earn points:
          * go to challenge
          */
         return true;
     } else {
         /*
          * download or upload:
          * check points
          */
         if ($download) {
             if ($this->Session->check('Search.count')) {
                 $count = $this->Session->read('Search.count');
                 // determine $cost
                 $cost = $repo['Repository']['download_cost'] * $count;
             } else {
                 $this->_cancel_everything('Please, first perform a search to download documents');
             }
         } elseif ($upload) {
             // determine $cost
             $cost = $repo['Repository']['upload_cost'];
         } else {
             // ERROR invalid action
             $this->_cancel_everything('Action not recognized');
         }
         // check $cost against user points
         $user_points = $this->RepositoriesUser->find('first', array('conditions' => array('RepositoriesUser.repository_id' => $repo['Repository']['id'], 'RepositoriesUser.user_id' => $user['User']['id']), 'fields' => array('points'), 'recursive' => -1));
         $user_points = $user_points['RepositoriesUser']['points'];
         //if user points > cost, doesnt need a challenge
         if ($user_points >= $cost) {
             return false;
         } else {
             return true;
         }
     }
 }