/**
  * Create a new controller instance.
  *
  * @return void
  */
 public function __construct()
 {
     $this->middleware('auth');
     $this->viewData['user_id'] = (int) Auth::User()->id;
     $this->viewData['controller_heading'] = 'Initiatives';
     $this->viewData['controller_name'] = $this->controller;
     $this->viewData['whatisit'] = 'Initiative';
     $this->viewData['plans'] = Plan::where('user_id', $this->viewData['user_id'])->where('status', 0)->orderBy('name')->lists('name', 'id');
     $this->viewData['currentPlan'] = NULL;
     if (!empty($_GET['plan_id'])) {
         $this->viewData['currentPlan'] = Plan::find($_GET['plan_id']);
     } else {
         if (!empty($this->viewData['plans'])) {
             $this->viewData['currentPlan'] = Plan::where('user_id', $this->viewData['user_id'])->where('status', 0)->orderBy('name')->get()->first();
         }
     }
     $this->viewData['dimensions'] = Dimension::leftJoin('plans', 'plans.id', '=', 'dimensions.plan_id')->where('plans.user_id', $this->viewData['user_id'])->where('dimensions.plan_id', $this->viewData['currentPlan']->id)->where('dimensions.status', 0)->orderBy('dimensions.name')->select('dimensions.*')->lists('dimensions.name', 'dimensions.id');
     $this->viewData['currentDimension'] = NULL;
     if (!empty($_GET['dimension_id'])) {
         $this->viewData['currentDimension'] = Dimension::find($_GET['dimension_id']);
     } else {
         if (!empty($this->viewData['dimensions'])) {
             $this->viewData['currentDimension'] = Dimension::where('dimensions.plan_id', $this->viewData['currentPlan']->id)->where('dimensions.status', 0)->orderBy('dimensions.name')->get()->first();
         }
     }
     $this->viewData['currentDimensionId'] = !empty($this->viewData['currentDimension']) ? $this->viewData['currentDimension']->id : '';
     $this->viewData['objectives'] = Objective::leftJoin('dimensions', 'dimensions.id', '=', 'objectives.dimension_id')->leftJoin('plans', 'plans.id', '=', 'dimensions.plan_id')->where('plans.user_id', '=', $this->viewData['user_id'])->where('objectives.dimension_id', '=', $this->viewData['currentDimensionId'])->where('objectives.status', 0)->orderBy('objectives.name')->select('objectives.*')->lists('objectives.name', 'objectives.id');
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 private function populateObjectiveTabularAndGraph($objective_id)
 {
     $list = _MODEL::leftJoin('dimensions', 'dimensions.id', '=', 'objectives.dimension_id')->leftJoin('plans', 'plans.id', '=', 'dimensions.plan_id')->where('objectives.id', '=', (int) $objective_id)->select('objectives.*')->get();
     $this->viewData['graphData'] = [];
     $this->viewData['graphData']['title'] = "BSC Report - Objective: " . $list[0]->name;
     $this->viewData['graphData']['subtitle'] = "Report for " . $list[0]->name . "'s intiatives";
     $this->viewData['graphData']['data'] = [];
     $objective_count = 0;
     foreach ($list as $objective) {
         $objective->AVERAGE = 0;
         //get initiatives related to objective
         $initiatives = $objective->initiatives;
         $initiative_AVERAGE = 0;
         $initiative_count = 0;
         $total_initiatives = 0;
         foreach ($initiatives as $initiative) {
             // get measures related to initiative
             $measures = $initiative->measures;
             $measure_count = 0;
             $percent = 0;
             foreach ($measures as $measure) {
                 $measure->percent = 0;
                 $measure->actual = ActualMeasure::where('actual_measures.measure_id', '=', (int) $measure->id)->sum('actual_measures.actual_measure');
                 if ($measure->target != 0) {
                     $percent += $measure->actual / $measure->target * 100;
                     $measure->AVERAGE = $measure->actual / $measure->target * 100;
                 }
                 $measure_count++;
             }
             $initiative->measures = $measures;
             //end measures
             if ($measure_count != 0) {
                 $initiative_AVERAGE += $percent / $measure_count;
                 $initiative->AVERAGE = $percent / $measure_count;
             }
             $initiative_count++;
         }
         //end initiative
         if ($initiative_count != 0) {
             $objective->AVERAGE += $initiative_AVERAGE / $initiative_count;
         }
         $objective_count++;
     }
     $this->viewData['objectives'] = $list;
     self::populateObjectiveGraph();
 }