/**
  * Calculates the sum of all shifts in a given period for a given person id.
  *
  * @return int 		$subjectTotal
  */
 public function countStatisticsById($id, $from, $till)
 {
     // get all events for the month, currently jan15 for testing
     $events = ClubEvent::where('evnt_date_start', '>', $from)->where('evnt_date_start', '<', $till)->get();
     $subject = Person::find($id);
     $subjectTotal = 0;
     foreach ($events as $event) {
         // for each event - get its schedule
         $schedule = Schedule::findOrFail($event->getSchedule->id);
         // for each schedule - get its entries that have this person's id
         $entries = ScheduleEntry::where('schdl_id', '=', $schedule->id)->where('prsn_id', '=', $subject->id)->get();
         // for each entry - get its job type weight
         foreach ($entries as $entry) {
             $weight = $entry->getJobType->jbtyp_statistical_weight;
             //and add it to subject's total
             $subjectTotal += $weight;
         }
     }
     return $subjectTotal;
 }
 /**
  * Updates schedule entries of a specific schedule.
  *
  * If a password is needed, check it's correct and throw an error to the session if it's not,
  * update all entries in bulk otherwise.
  *
  * @param int $id
  *
  * @return RedirectResponse
  */
 private function onUpdate($id)
 {
     $schedule = Schedule::findOrFail($id);
     $entries = ScheduleEntry::where('schdl_id', '=', $id)->get();
     // Check if that schedule needs a password
     if ($schedule->schdl_password !== '') {
         //get password for specific id here, similar to enty->id
         if (!Hash::check(Input::get('password'), $schedule->schdl_password)) {
             Session::put('message', Config::get('messages_de.schedule-pw-needed'));
             Session::put('msgType', 'danger');
             return Redirect::back();
         }
     }
     foreach ($entries as $entry) {
         // Remember old value for logging
         $oldPerson = $entry->getPerson;
         // Entry was empty
         if (!isset($entry->prsn_id)) {
             // Entry is not empty now
             if (!Input::get('userName' . $entry->id) == '') {
                 // Add new entry data
                 $this->onAdd($entry);
                 // log revision
                 ScheduleController::logRevision($entry->getSchedule, $entry, "Dienst eingetragen", $oldPerson, $entry->getPerson()->first());
                 // new value
             }
             // Otherwise no change found - do nothing
         } else {
             // Same person there?
             if ($entry->getPerson->prsn_name == Input::get('userName' . $entry->id) and Person::where('id', '=', $entry->prsn_id)->first()->prsn_ldap_id == Input::get('ldapId' . $entry->id)) {
                 // Was comment updated?
                 if ($entry->entry_user_comment != Input::get('comment' . $entry->id)) {
                     $entry->entry_user_comment = Input::get('comment' . $entry->id);
                 }
                 // Otherwise no change found - do nothing
             } else {
                 // Was entry deleted?
                 if (Input::get('userName' . $entry->id) == '') {
                     $this->onDelete($entry);
                     // log revision
                     ScheduleController::logRevision($entry->getSchedule, $entry, "Dienst ausgetragen", $oldPerson, $entry->getPerson()->first());
                     // new value
                 } else {
                     // delete old data
                     $this->onDelete($entry);
                     // add new data
                     $this->onAdd($entry);
                     // log revision
                     ScheduleController::logRevision($entry->getSchedule, $entry, "Dienst geändert", $oldPerson, $entry->getPerson()->first());
                     // new value
                 }
             }
         }
     }
 }
 public function run()
 {
     Eloquent::unguard();
     /**
      * Clearing table
      */
     DB::table('schedule_entries')->delete();
     /**
      * Creating event schedule entries
      */
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '1', 'prsn_id' => '1'));
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '2', 'prsn_id' => '2'));
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '3'));
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '4', 'entry_user_comment' => 'Thou didst not reckon with the might of Thor, knave!', 'prsn_id' => '4'));
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '5', 'entry_user_comment' => 'komme 10 Min später', 'prsn_id' => '3'));
     ScheduleEntry::create(array('schdl_id' => '1', 'jbtyp_id' => '1'));
     /**
      * Creating task schedule
      */
     ScheduleEntry::create(array('schdl_id' => '2', 'jbtyp_id' => '6', 'prsn_id' => '3'));
     ScheduleEntry::create(array('schdl_id' => '2', 'jbtyp_id' => '7'));
     /**
      * reporting result to console
      */
     $this->command->info('Added schedule entries to example event and task on 01.04.2015.');
 }
 /**
  * Generates the view for a specific event, including the schedule.
  *
  * @param  int $id
  * @return view ClubEventView
  * @return ClubEvent $clubEvent
  * @return ScheduleEntry[] $entries
  * @return RedirectResponse
  */
 public function showId($id)
 {
     $clubEvent = ClubEvent::with('getPlace')->findOrFail($id);
     if (!Session::has('userId') and $clubEvent->evnt_is_private == 1) {
         Session::put('message', Config::get('messages_de.access-denied'));
         Session::put('msgType', 'danger');
         return Redirect::action('MonthController@showMonth', array('year' => date('Y'), 'month' => date('m')));
     }
     $schedule = Schedule::findOrFail($clubEvent->getSchedule->id);
     $entries = ScheduleEntry::where('schdl_id', '=', $schedule->id)->with('getJobType', 'getPerson', 'getPerson.getClub')->get();
     $clubs = Club::orderBy('clb_title')->lists('clb_title', 'id');
     $persons = Cache::remember('personsForDropDown', 10, function () {
         $timeSpan = new DateTime("now");
         $timeSpan = $timeSpan->sub(DateInterval::createFromDateString('3 months'));
         return Person::whereRaw("prsn_ldap_id IS NOT NULL AND (prsn_status IN ('aktiv', 'kandidat') OR updated_at>='" . $timeSpan->format('Y-m-d H:i:s') . "')")->orderBy('clb_id')->orderBy('prsn_name')->get();
     });
     $revisions = json_decode($clubEvent->getSchedule->entry_revisions, true);
     if (!is_null($revisions)) {
         // deleting ip adresses from output for privacy reasons
         foreach ($revisions as $entry) {
             unset($entry["from ip"]);
         }
     }
     return View::make('clubEventView', compact('clubEvent', 'entries', 'clubs', 'persons', 'revisions'));
 }