public function getIndex()
 {
     //return response()->json(Session::get('mappings'));
     // get selected mapping
     $mappingid = Input::get('mappingid');
     $mappings = Session::get('mappings');
     $mapping = $mappings[$mappingid];
     // get selected columns name
     $header = [];
     $dataset = \App\Dataset::findOrFail($mapping->datasetid);
     $tablename = $dataset->table_name;
     $attributes = $dataset->attributes()->get();
     $header = $mapping->mappingname;
     // get project
     $project = \App\VisualizationProject::findOrFail(Session::get('visualizationid'));
     // select only selected columns only
     $datasetdata = DB::connection('dataset')->table($tablename)->select($header);
     $sortdata = null;
     foreach ($project->dataSelections as $selection) {
         if ($selection->operand == "##SORTBY##") {
             $sortdata = $selection;
         } else {
             $datasetdata = $datasetdata->where($selection->column_name, $selection->operator, $selection->operand);
         }
     }
     if ($sortdata !== null) {
         $sorttype = $sortdata->operator == '>' ? 'asc' : 'desc';
         $datasetdata = $datasetdata->orderBy($sortdata->column_name, $sorttype);
     }
     $datasetdata = $datasetdata->get();
     if (Session::get('aggregate')) {
         // aggregate dataset
         foreach ($header as $attribute) {
             // check if there is an attribute that contain aggregation information
             $categoryData = $dataset->categories()->where('name', $attribute)->first();
             if ($categoryData != null) {
                 $categories = DB::connection('dataset')->table($tablename)->select($attribute)->distinct()->get();
                 $rowsData = [];
                 foreach ($categories as $category) {
                     $rows = DB::connection('dataset')->table($tablename)->where($attribute, $category->{$attribute})->get();
                     $rowData = (object) [];
                     // initialize rowdata
                     foreach ($header as $att) {
                         $rowData->{$att} = 0;
                     }
                     foreach ($rows as $row) {
                         foreach ($header as $att) {
                             if ($att == $attribute) {
                                 $rowData->{$att} = $row->{$att};
                             } else {
                                 if (is_numeric($row->{$att})) {
                                     $rowData->{$att} += $row->{$att};
                                     //var_dump($row->$att);
                                 } else {
                                     $rowData->{$att} = $row->{$att};
                                 }
                             }
                         }
                     }
                     //exit();
                     $rowsData[] = $rowData;
                     if ($categoryData->type == "AVERAGE") {
                         foreach ($header as $att) {
                             if ($att != $attribute) {
                                 if (is_numeric($row->{$att})) {
                                     $rowData->{$att} /= count($rows);
                                 }
                             }
                         }
                     }
                 }
                 $datasetdata = $rowsData;
                 break;
             }
         }
     }
     // join dataset with header
     $data[] = $header;
     foreach ($datasetdata as $set) {
         $row = [];
         foreach ($set as $value) {
             $row[] = $value;
         }
         $data[] = $row;
     }
     // get visualization visual variable information
     $visualization = \App\Visualization::findOrFail($mapping->visualizationid);
     $category = [];
     // save which data/column is used as category
     for ($i = 0; $i < count($visualization->visualVariables); $i++) {
         if ($visualization->visualVariables[$i]->pivot->type == "category") {
             $category[] = $header[$i];
         }
         $test[] = [$i, $visualization->visualVariables[$i]->pivot->type];
     }
     //return response()->json($test);
     $visdata = ['visualization' => $visualization->name, 'category' => $category, 'header' => $header, 'data' => $data, 'activities' => $visualization->activities];
     Session::put('visdata', (object) $visdata);
     // Call the view and then wait for data request
     return view('visualization.view', ['projectname' => $project->name]);
 }
 public function postRemove()
 {
     $projectid = Input::get('projectid');
     $project = \App\VisualizationProject::findOrFail($projectid);
     $selectionid = Input::get('selectionid');
     $project->dataSelections()->where('id', $selectionid)->first()->delete();
     return Redirect::to('/dataset/selection/' . $projectid);
 }
 public function postRecommendation()
 {
     if (Session::get('visualizationid') == null) {
         return redirect('setup');
     } else {
         // load project
         $project = \App\VisualizationProject::findOrFail(Session::get('visualizationid'));
         // load selected dataset
         $dataset = $project->dataset;
         // get attribute selection
         $selection = Input::get('selection');
         // is exact match?
         $isExact = Input::get('exact');
         // has purpose type?
         $purpose = Input::get('purpose');
         // on mobile?
         $isMobile = Input::get('mobile');
         // aggregate?
         $isAggregate = false;
         //Input::get('aggregate');
         Session::put('aggregate', $isAggregate);
         $rating = [];
         $dumpdata = [];
         foreach (Visualization::all() as $visualization) {
             // PRE SELECTION
             if ($visualization->purpose_type == $purpose || $purpose == 'ALL') {
                 if (count($selection) < count($visualization->visualVariables)) {
                     // if the selected attributes is less than the number of visual variables needed
                 } else {
                     if (!$isExact || count($selection) == count($visualization->visualVariables)) {
                         // GENERATE MAPPINGS
                         $mappings = $this->matcher($selection, $visualization);
                         // [0, 1], [0,2], ...
                         $mappingsRating = null;
                         $bestrating = 0;
                         // GENERATE RATING FOR EACH MAPPING
                         foreach ($mappings as $map) {
                             $mapRating = 0;
                             $numFactors = 3;
                             // FACTUAL VISUALIZATION KNOWLEDGE
                             $factual = $this->factualVisualizationKnowledge($visualization, $dataset, $selection, $map);
                             $mapRating += $factual;
                             // USER & DEVICE INFORMATION
                             $userdevice = $this->userDeviceInformation($visualization, $isMobile);
                             $mapRating += $userdevice;
                             // USER-SHARED KNOWLEDGE
                             $vis = Visualization::where('name', $visualization->name)->first();
                             $usersharedrating = $this->userSharedKnowledge($vis);
                             if ($usersharedrating !== null) {
                                 $usershared = $usersharedrating * 100;
                                 $mapRating += $usershared;
                             } else {
                                 $numFactors--;
                             }
                             $mapRating /= $numFactors;
                             $temp[] = $map;
                             // select only the best mapping for each visualization
                             if ($mapRating > $bestrating) {
                                 $mappingsRating = (object) ['rating' => $mapRating, 'mapping' => $map, 'scores' => [$factual, $userdevice, $usershared]];
                                 $bestrating = $mapRating;
                             }
                         }
                         $mappingname = [];
                         foreach ($mappingsRating->mapping as $mapping) {
                             $mappingname[] = $selection[$mapping];
                         }
                         $visrating = (object) ['visualizationid' => $visualization->id, 'visualization' => $visualization->name, 'rating' => $mappingsRating->rating, 'scores' => $mappingsRating->scores, 'mapping' => $mappingsRating->mapping, 'mappingname' => $mappingname, 'datasetid' => $dataset->id];
                         // check if there is other version of visualization is used (barchart and 2-data barchart)
                         $exist = false;
                         for ($i = 0; $i < count($rating); $i++) {
                             if ($visrating->visualization == $rating[$i]->visualization) {
                                 if (count($visrating->mapping) >= count($rating[$i]->mapping)) {
                                     $rating[$i] = $visrating;
                                 }
                                 $exist = true;
                                 break;
                             }
                         }
                         if (!$exist) {
                             $rating[] = $visrating;
                         }
                     }
                 }
             }
         }
         // sort mapping
         usort($rating, array("App\\Http\\Controllers\\RatingController", "cmp"));
         // Save mappings to session
         Session::put('mappings', $rating);
         return response()->json(['mappings' => $rating]);
     }
 }