Example #1
0
/**
 * Trigger certain actions when leader or teacher of a plan was changed
 *
 * a) send notification to each invovled
 * b) change the team accordingly
 *
 * @param Request $request all data from the HTTP request (the new data)
 * @param EloquentModel $plan  (the old data)
 */
function checkIfLeaderOrTeacherWasChanged($request, $plan)
{
    $msg = false;
    // check if LEADER was changed
    if ($plan->leader_id != $request->leader_id) {
        // check if reason was given for the change
        if (!$request->has('reasonForChange')) {
            flashError('Please provide reason for the change of leader!');
            return false;
        }
        // find the corresponding team record for the leader
        $leader = Team::where([['plan_id', $plan->id], ['role_id', env('LEADER_ID', 4)]]);
        if ($leader->count()) {
            // update the team record
            $leader->update(['user_id' => $request->leader_id]);
        } else {
            // create a new team member...
            $plan->leader_id = $request->leader_id;
            addDefaultRolesAndResourcesToPlan($plan);
        }
        // affected users must be notified of this change accordingly
        $new_leader = User::find($request->leader_id);
        $recipient = $new_leader->id;
        $subject = 'Leader changed for Event on ' . Carbon::parse($plan->date)->format('l, jS \\of F Y');
        $msg = Auth::user()->name . ' changed the leader for this ' . $plan->type->name . ' from ' . $plan->leader->name . ' to ' . $new_leader->name;
    }
    // check if TEACHER was changed
    if ($plan->teacher_id != $request->teacher_id) {
        // check if a reason was given for the change
        if (!$request->has('reasonForChange')) {
            flashError('Please provide reason for the change of teacher!');
            return false;
        }
        // find the corresponding team record for the teacher
        $teacher = Team::where([['plan_id', $plan->id], ['role_id', env('TEACHER_ID', 5)]]);
        if ($teacher->count()) {
            // update the team record
            $teacher->update(['user_id' => $request->teacher_id]);
        } else {
            // create a new team member...
            $plan->teacher_id = $request->teacher_id;
            addDefaultRolesAndResourcesToPlan($plan);
        }
        // affected users must be notified of this change accordingly
        $new_teacher = User::find($request->teacher_id);
        $recipient = $new_teacher->id;
        $subject = 'Teacher changed for Event on ' . Carbon::parse($plan->date)->format('l, jS \\of F Y');
        $msg = Auth::user()->name . ' changed the teacher for this ' . $plan->type->name . ' from ' . $plan->teacher->name . ' to ' . $new_teacher->name;
    }
    if ($msg) {
        // send internal notification and message
        sendInternalMessage($subject, $msg, $recipient);
        Log::info($subject . ' - ' . $msg);
        // also create a history record for this change
        $history = new History(['user_id' => Auth::user()->id, 'changes' => $subject . "\n" . $msg, 'reason' => $request->reasonForChange]);
        $plan->histories()->save($history);
    }
    return true;
}
Example #2
0
 /**
  * Store a newly created plan in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(StorePlanRequest $request)
 {
     // create new record
     $plan = Plan::create($request->except(['start', 'end']));
     // set some defaults
     $plan->changer = Auth::user()->first_name;
     $plan->state = 1;
     $planDate = Carbon::instance($plan->date);
     // insert default service TIMES if requested
     if ($request->input('defaultValues') == 'Y') {
         $type = Type::find($plan->type_id);
         if (count($type)) {
             // default end time is only the time of day. We need to combine this with the plan date
             $startTme = Carbon::parse($type->start);
             $endTime = Carbon::parse($type->end);
             $plan->date = $planDate->copy()->addHour($startTme->hour)->addMinute($startTme->minute);
             $plan->date_end = $planDate->addHour($endTime->hour)->addMinute($endTime->minute);
         }
     } else {
         // request contains custom start and end times
         $startTme = Carbon::parse($request->start);
         $endTime = Carbon::parse($request->end);
         $plan->date = $planDate->copy()->addHour($startTme->hour)->addMinute($startTme->minute);
         $plan->date_end = $planDate->addHour($endTime->hour)->addMinute($endTime->minute);
     }
     $plan->save();
     addDefaultRolesAndResourcesToPlan($plan);
     // insert default items if requested
     if ($request->input('defaultItems') == 'Y') {
         $dItems = DefaultItem::where('type_id', $plan->type_id)->get();
         // $newItems = [];
         foreach ($dItems as $dItem) {
             // get single default item to create a nwe Item object
             $iNew = new Item(['seq_no' => $dItem->seq_no, 'comment' => $dItem->text, 'forLeadersEyesOnly' => $dItem->forLeadersEyesOnly]);
             // save the new item to the new plan
             $plan->items()->save($iNew);
             // if default item contains a default image, link the new Plan item to the image
             if ($dItem->file_id) {
                 $file = File::find($dItem->file_id);
                 $iNew->files()->save($file);
             }
             // array_push( $newItems, $iNew );
         }
     }
     flash('New Plan added with id ' . $plan->id);
     // redirect back to the plan editor to create another plan
     if ($request->input('addAnother') == 'Y') {
         // use heper function to calculate next date for this plan
         $newDate = getNextPlanDate($plan);
         // send the default values to the View
         $request->session()->flash('defaultValues', ['type_id' => $plan->type_id, 'date' => $newDate, 'start' => $startTme->toTimeString(), 'end' => $endTime->toTimeString(), 'leader_id' => $plan->leader_id]);
         // get list of service types
         $types = Type::get();
         // get list of users
         $users = User::orderBy('first_name')->get();
         return view($this->view_one, array('types' => $types, 'users' => $users));
     }
     return \Redirect::route('plans.edit', $plan->id);
 }