Example #1
0
 public function getChartData()
 {
     $testId = Input::get('test');
     $startDate = date('Y-m-d', strtotime(Input::get('startDate')));
     $endDate = date('Y-m-d', strtotime(Input::get('endDate') . ' + 1 days'));
     $sportId = Input::get("sport");
     $genderId = Input::get("gender");
     $filteredTests = $this->getFilteredTests($testId, $startDate, $endDate, $sportId, $genderId);
     $test = $filteredTests['test'];
     $answeredTests = $filteredTests['answeredTests'];
     $ranges = $test->scales[0]->ranges;
     $scales = $test->scales;
     $responseData = array('cols' => array(), 'rows' => array());
     // Inserción de columnas
     $responseData['cols'][] = $this->createColumn('Escalas', 'string');
     $ranges = $test->scales[0]->ranges;
     foreach ($ranges as $range) {
         $label = 'Deportistas en: ' . $range->description . ' (' . $range->min . ', ' . $range->max . ')';
         $responseData['cols'][] = $this->createColumn($label, 'number');
     }
     // Preparación de datos de renglones
     $scalesResults = array();
     foreach ($scales as $scale) {
         $rangesValues = array();
         for ($i = 0; $i < $ranges->count(); $i++) {
             $rangesValues[] = 0;
         }
         $scalesResults[$scale->description] = $rangesValues;
     }
     // Cálculo de valores
     foreach ($answeredTests as $answeredTest) {
         $resultsByScale = $this->getResultsByScale($scales, $answeredTest->userAnswers);
         foreach ($resultsByScale as $resultByScale) {
             $scale = $resultByScale['scale'];
             $result = $resultByScale['result'];
             $rangeIndex = $this->getRangeIndex($ranges, $result);
             $scalesResults[$scale->description][$rangeIndex]++;
         }
     }
     // Inserción de renglones
     foreach ($scalesResults as $scaleName => $results) {
         $row = array();
         $row[] = $scaleName;
         foreach ($results as $result) {
             $row[] = $result;
         }
         $responseData['rows'][] = $this->createRow($row);
     }
     // Preparar encabezado y subtítulos
     $title = $test->name;
     $date = 'De ' . Input::get('startDate') . ' a ' . Input::get('endDate');
     $sport;
     $gender;
     if ($sportId != -1) {
         $sport = ' Deporte: ' . Sport::find($sportId)->description;
     } else {
         $sport = ' Deporte: Sin Especificar';
     }
     if ($genderId != -1) {
         $gender = ' Género: ' . Gender::find($genderId)->description;
     } else {
         $gender = ' Género: Sin Especificar';
     }
     return array('title' => $title, 'date' => $date, 'sport' => $sport, 'gender' => $gender, 'table' => $responseData);
 }