Example #1
0
 /**
  * Send an email to the user to request his confirmation for being a member of the team
  *
  * @return \Illuminate\Http\Response
  */
 public function sendrequest($plan_id, $id, AppMailer $mailer)
 {
     // check access rights
     if (!Auth::user()->ownsPlan($plan_id)) {
         return redirect('home')->with('error', 'You are unauthorized for this request.');
     }
     // get the resource handle
     $team = Team::find($id);
     if ($team) {
         if ($team->requested) {
             $error = 'Request Email was already sent to this user!';
             return \Redirect::back()->with(['error' => $error]);
         }
         $team->requested = True;
         $team->remember_token = str_random(32);
         // send internal message to user
         $message = 'Please open <a href="' . url('cspot/plans/' . $plan_id) . '/team"> this plan </a> and confirm if you accept the given role.';
         $thread_id = sendInternalMessage('You have been assigned a role in a Service plan', $message, $team->user_id, false);
         $team->thread_id = $thread_id;
         $team->save();
         // also send an email to the user
         $recipient = User::find($team->user_id);
         $plan = Plan::find($team->plan_id);
         $mailer->getPlanMemberConfirmation($recipient, $plan, $team);
         $status = 'Email with membership request was sent to user.';
         return \Redirect::route('team.index', ['plan_id' => $plan_id])->with(['status' => $status]);
     }
     $error = 'Wrong team member id!';
     return \Redirect::back()->with(['error' => $error]);
 }
Example #2
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;
}