/**
  * @param Application $app
  * @return DBRecommendationRepository
  */
 public function register(Application $app)
 {
     if (!isset($app['odbc_aster.configs'])) {
         $app['odbc_aster.configs'] = array('driver' => '{AsterDriver}', 'host' => '192.168.100.100', 'database' => 'beehive', 'username' => 'db_superuser', 'password' => 'db_superuser');
     }
     $app['odbc_aster'] = $app->share(function ($app) {
         $dbRepository = new DBRecommendationRepository($app['odbc_aster.configs']);
         if ($dbRepository->connect() === false) {
             return false;
         }
         return $dbRepository;
     });
 }
 /**
  * Loads all recommended events for a given user. It is limited to a number of results
  * otherwise the web server will fail.
  *
  * @param $user_id int
  * @param $num int number of elements to retrieve
  * @param $offset int offset
  *
  * @return array of Event|null
  */
 public function getUserRecommendations($user_id, $num = 20, $offset = 0)
 {
     /*
     $events = $this->db->getByEqCondsAndLimitedAndOrdered(
         'events.*',
         self::ENTITY_NAME . " RIGHT JOIN events ON (".self::ENTITY_NAME.".event_id = events.event_id) ",
         array(self::ENTITY_NAME . '.user_id' => $user_id),
         $num, $offset, 'events.event_id'
     );
     */
     $query = "\n        SELECT  e.*, ui.interested, ui.not_interested\n        FROM    " . self::ENTITY_NAME . " ur JOIN (events e LEFT JOIN user_interest ui ON (e.event_id = ui.event_id)) ON (ur.event_id = e.event_id)\n        WHERE   ur.user_id = '{$user_id}'\n        AND     ui.user_id = '{$user_id}' OR ui.user_id IS NULL\n        ORDER BY e.event_id\n        LIMIT {$num} OFFSET {$offset};";
     $events = $this->db->executeQueryAndFetch($query);
     if ($events === false) {
         return false;
     }
     // Retrieve all events as Event Object
     $tmp = new \ReflectionClass('App\\Model\\Event');
     foreach ($events as $key => $event) {
         // calculate interests as single value
         $int = -1;
         $not_int = -1;
         if (array_key_exists("interested", $event)) {
             $int = $event['interested'];
         }
         if (array_key_exists("not_interested", $event)) {
             $not_int = $event['not_interested'];
         }
         if ($int == -1 && $not_int == -1) {
             $intval = -1;
         } else {
             if ($int == 0) {
                 if ($not_int == 0) {
                     $intval = 1;
                 } else {
                     $intval = 0;
                 }
             } else {
                 $intval = 2;
             }
         }
         // create new Event obj
         try {
             $events[$key] = EventProvider::toArrayForCvalues($events[$key]);
             $events[$key][0] = $tmp->newInstanceArgs($events[$key]);
             $events[$key][1] = $intval;
         } catch (InvalidArgumentException $e) {
             $events[$key] = null;
         }
     }
     return $events;
 }
 /**
  * Delete an Event
  *
  * @param mixed $event_id
  * @param mixed $user_id
  *
  * @return bool
  */
 public function delEvent($event_id, $user_id)
 {
     if (is_null($this->db)) {
         return false;
     }
     return $this->db->deleteObj(self::ENTITY_NAME, array('event_id' => $event_id, 'user_id' => $user_id));
 }
 /**
  * Signup user with a given username and password
  *
  * @param string $userid user ID
  * @param string $username The username
  * @param string $password The password
  *
  * @return bool
  */
 public function signupUser($userid, $username, $password)
 {
     if (is_null($this->db)) {
         return false;
     }
     $users = $this->db->getByEqualConditions(self::ENTITY_NAME, array('email' => $username));
     if (is_array($users) && !empty($users)) {
         $user = $users[0];
         if ($user !== false) {
             return false;
         }
     }
     $password = $this->encoder->encodePassword($password, '');
     $user = new User($userid, $username, $password);
     return $this->db->insertObj(self::ENTITY_NAME, $user);
 }
 /**
  * Update info regarding an event interests for a user
  *
  * @param mixed $userId user id
  * @param mixed $eventId event id
  * @param int $interested user interested
  * @param int $not_interested user not interested
  *
  * @return bool
  */
 public function updateUserInterest($userId, $eventId, $interested = 0, $not_interested = 0)
 {
     if (is_null($this->db)) {
         return false;
     }
     $userInt = new UserInterest($userId, $eventId, $interested, $not_interested);
     return $this->db->updateObj(self::ENTITY_NAME, $eventId, $userId, $userInt);
 }