Changeset 0462ffe in iPeer


Ignore:
Timestamp:
2012-12-23T21:26:27-08:00 (6 years ago)
Author:
Compass <pan.luo@…>
Branches:
3.1.x, dev, hotfix, master, pagodabox, ticket463
Children:
4d43e34
Parents:
57c4d8a
Message:

Use fputcsv to export csv file

Using fputcsv to export csv file with correct escape. Changed the format
of generated grid for fputcsv

Location:
app/controllers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • app/controllers/components/export_base_new.php

    r57c4d8a r0462ffe  
    9090        $this->Group = ClassRegistry::init('Group');
    9191        $group = $this->Group->getGroupWithMemberRoleByGroupIdEventId($groupEvent['group_id'], $event['Event']['id']);
    92         $csv = '';
     92        $grid = array();
    9393        $responsesByEvaluatee = Set::combine($results, '{n}.'.$this->responseModelName.'.evaluator', '{n}', '{n}.'.$this->responseModelName.'.evaluatee');
    9494        foreach ($group['Member'] as $member) {
     
    9797                continue;
    9898            }
    99 
    100             $csv .= $this->buildScoreTableByEvaluatee($params, $group, $member, $event, $responsesByEvaluatee);
    101         }
    102         return $csv;
     99            $grid = array_merge($grid, $this->buildScoreTableByEvaluatee($params, $group, $member, $event, $responsesByEvaluatee));
     100        }
     101
     102        return $grid;
    103103    }
    104104
     
    115115    function buildEvaluationScoreTableByEvent($params, $event, $results)
    116116    {
    117         $csv  = '';
     117        $grid = array();
    118118        if (empty($event['GroupEvent'])) {
    119             return $csv;
     119            return $grid;
    120120        }
    121121        foreach ($event['GroupEvent'] as $ge) {
    122             $resultTable =  $this->buildEvaluationScoreTableByGroup($params, $ge, $event, $results[$ge['id']]);
    123             $csv .= $resultTable."\n";
    124         }
    125         return $csv;
     122            $grid = array_merge($grid, $this->buildEvaluationScoreTableByGroup($params, $ge, $event, $results[$ge['id']]));
     123        }
     124        return $grid;
    126125    }
    127126
     
    148147        $xDimension = $this->calcDimensionX($params, $event);
    149148        $yDimensions = count($group['Member']);
    150         $grid = $this->ExportHelper2->buildExporterGrid($xDimension, $yDimensions);
     149        $grid = array();
    151150
    152151        foreach ($group['Member'] as $evaluator) {
     
    182181            // check if we have a reponse for this evaluator
    183182            if (!isset($responses[$evaluatee['id']]) || !array_key_exists($evaluator['id'], $responses[$evaluatee['id']])) {
    184                 $this->ExportHelper2->fillGridHorizonally($grid, $xPosition, $yPosition+$yInc, $row);
    185                 $yInc++;
     183                array_push($row, array_fill(0, $xDimension - count($row), ''));
    186184                continue;
    187185            }
     
    227225                array_push($row, $finalGrade);
    228226            }
    229             $this->ExportHelper2->fillGridHorizonally($grid, $xPosition, $yPosition + $yInc, $row);
     227            $grid[] = $row;
    230228            $yInc++;
    231229        }
    232230
    233         return $this->ExportHelper2->arrayDraw($grid);
     231        return $grid;
    234232    }
    235233
  • app/controllers/components/export_csv.php

    r57c4d8a r0462ffe  
    9797    function createCsv($params, $event)
    9898    {
    99         $csv = '';
     99        $grid = array();
    100100        $groupEvents = $event['GroupEvent'];
    101101        $groupEventIds = Set::extract($groupEvents, '/id');
     
    111111        $header = $this->generateHeader($params, $event);
    112112
    113         $csv .= $header."\n\n";
    114         $csv .= $this->buildEvaluationScoreTableByEvent($params, $event, $results);
    115 
    116         return $csv;
     113        $grid = $this->buildEvaluationScoreTableByEvent($params, $event, $results);
     114
     115        $this->render($header, $grid);
    117116    }
    118117
     
    168167                if ((isset($params['include']['grade_tables']) && $question['question_type'] == 'S') ||
    169168                (isset($params['include']['comments']) && $question['question_type'] == 'T')) {
    170                     $header[] = "Q".($key+1)." ( /".$question['multiplier']."), ";
     169                    $header[] = "Q".($key+1)." ( /".$question['multiplier'].")";
    171170                }
    172171            } else {
    173                 $header[] = "Q".($key+1)." ( /".$question['multiplier']."), ";
     172                $header[] = "Q".($key+1)." ( /".$question['multiplier'].")";
    174173            }
    175174        }
     
    181180        }
    182181
    183         return join(', ', $header);
     182        return $header;
    184183    }
    185184
     
    314313    }
    315314
     315    /**
     316     * render
     317     *
     318     * @param mixed $header csv header
     319     * @param mixed $grid   data grid
     320     *
     321     * @access public
     322     * @return void
     323     */
     324    function render($header, $grid)
     325    {
     326        $target = array();
     327        $resource = fopen('php://output', 'a');
     328        fputcsv($resource, $header);
     329        foreach ($grid as $row) {
     330            fputcsv($resource, $row);
     331        }
     332        fclose($resource);
     333    }
    316334}
  • app/controllers/components/export_helper2.php

    r57c4d8a r0462ffe  
    154154        $yLim = count($grid[0]);
    155155        for ($y=0; $y<$yLim; $y++) {
    156             for ($x=0; $x<$xLim; $x++) {
     156            for ($x=0; $x<$xLim-1; $x++) {
    157157                $formatCSV .= $grid[$x][$y].",";
    158158            }
    159159            $formatCSV .= "\n";
    160160        }
    161 
    162161        return $formatCSV;
    163162    }
  • app/controllers/evaluations_controller.php

    r57c4d8a r0462ffe  
    294294
    295295            $fileName = isset($this->params['form']['file_name']) && !empty($this->params['form']['file_name']) ? $this->params['form']['file_name']:date('m.d.y');
     296            header('Content-Type: application/csv');
     297            header('Content-Disposition: attachment; filename=' . $fileName . '.csv');
    296298            switch($this->params['form']['export_type']) {
    297299            case "csv" :
    298                 $fileContent = $this->ExportCsv->createCsv($this->params['form'], $event);
     300                $this->ExportCsv->createCsv($this->params['form'], $event);
    299301                break;
    300302            case "excel" :
     
    304306                throw new Exception("Invalid evaluation selection.");
    305307            }
    306             header('Content-Type: application/csv');
    307             header('Content-Disposition: attachment; filename=' . $fileName . '.csv');
    308308            $this->log($this->User->getDataSource()->getLog(false, false), 'debug');
    309             echo $fileContent;
    310309        } else {
    311310            // Set up data
Note: See TracChangeset for help on using the changeset viewer.