/**
  * Gets the total number of volunteer places associated with this organization
  * during a period.
  *
  * @param int $start begin timestamp
  * @param int $start end timestamp
  *
  * @return int
  */
 public function numPlaces($start = false, $end = false)
 {
     if (!$start) {
         $start = 0;
     }
     if (!$end) {
         $end = time();
     }
     return VolunteerPlace::totalRecords(['organization' => $this->id(), 'created_at >= ' . $start, 'created_at <= ' . $end]);
 }
 /**
  * @depends testCreate
  */
 public function testFind()
 {
     // try with the organization supplied
     $places = VolunteerPlace::find(['where' => ['organization' => self::$org->id()], 'sort' => 'id ASC'])['models'];
     $this->assertCount(2, $places);
     // look for our known models
     $find = [self::$place->id(), self::$place2->id()];
     foreach ($places as $m) {
         if (($key = array_search($m->id(), $find)) !== false) {
             unset($find[$key]);
         }
     }
     $this->assertCount(0, $find);
 }
 private function getOrgForAdmin($req, $res)
 {
     $org = $this->getOrg($req, $res);
     if (is_object($org)) {
         if ($org->can('admin', $this->app['user'])) {
             // calculate the number of unapproved volunteers, places, and hours
             $unapprovedVolunteers = Volunteer::totalRecords(['organization' => $org->id(), 'role' => Volunteer::ROLE_AWAITING_APPROVAL]);
             $unapprovedHours = VolunteerHour::totalRecords(['organization' => $org->id(), 'approved' => false]);
             $unapprovedPlaces = VolunteerPlace::totalRecords(['organization' => $org->id(), 'place_type' => VolunteerPlace::EXTERNAL, 'verify_approved' => false]);
             $this->app['view_engine']->setGlobalParameters(['volunteersAwaitingApproval' => $unapprovedVolunteers, 'hoursAwaitingApproval' => $unapprovedHours, 'placesAwaitingApproval' => $unapprovedPlaces]);
         } else {
             $res->setCode(401);
             return false;
         }
     }
     return $org;
 }
 public function reportHoursStep3($req, $res)
 {
     $org = $this->getOrg($req, $res);
     if (!is_object($org)) {
         return $org;
     }
     $currentUser = $this->app['user'];
     // make sure the user is logged in
     if (!$currentUser->isLoggedIn()) {
         setcookie('redirect', $org->url() . '/hours/report', time() + 3600, '/');
         return $res->redirect('/login');
     }
     $place = $req->query('place');
     if ($place) {
         $place = new VolunteerPlace($place);
     }
     if (!$place || !$place->exists()) {
         return $res->redirect($org->manageUrl() . '/hours/add');
     }
     // validate tags
     $tags = $req->request('tags');
     foreach ((array) explode(' ', $tags) as $tag) {
         if (!Validate::is($tag, 'alpha_dash')) {
             $this->app['errors']->push(['error' => 'invalid_volunteer_hour_tags']);
             return $this->reportHoursStep2($req, $res);
         }
     }
     // validate day
     $date = \DateTime::createFromFormat('M j, Y', $req->request('timestamp'));
     if (!$date) {
         $this->app['errors']->push(['error' => 'validation_failed', 'params' => ['field_name' => 'Day']]);
         return $this->reportHoursStep2($req, $res);
     }
     $uid = $currentUser->id();
     $hour = new VolunteerHour();
     $success = $hour->create(['organization' => $org->id(), 'uid' => $uid, 'hours' => $req->request('hours'), 'timestamp' => $date->getTimestamp(), 'place' => $place->id(), 'approved' => false, 'tags' => $tags]);
     if ($success) {
         $res->redirect($org->url() . '/hours/thanks');
     } else {
         return $this->reportHoursStep2($req, $res);
     }
 }