exportCompleteAnswers.php 6.36 KB
Newer Older
Chenu Denis's avatar
Chenu Denis committed
1 2 3 4 5 6
<?php
/**
 * exportCompleteAnswers Plugin for LimeSurvey
 * Export code and complete answer in CSV
 *
 * @author Denis Chenu <denis@sondages.pro>
7 8
 * @copyright 2014-2018 Denis Chenu <http://sondages.pro>
 * @copyright 2014-2018 Belgian Health Care Knowledge Centre (KCE) <http://kce.fgov.be>
9
 * @license AGPL v3
10
 * @version 1.0.0
Chenu Denis's avatar
Chenu Denis committed
11 12
 *
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as published by
Chenu Denis's avatar
Chenu Denis committed
14 15
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
16
 *
Chenu Denis's avatar
Chenu Denis committed
17 18 19
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Affero Public License for more details.
Chenu Denis's avatar
Chenu Denis committed
21 22
 *
 */
23
class exportCompleteAnswers extends PluginBase {
Chenu Denis's avatar
Chenu Denis committed
24 25 26 27 28
    protected $storage = 'DbStorage';
    static protected $name = 'Export all answers (code and text)';
    static protected $description = 'Allow to export code and text for answers. Give some ability to export it the way you want.';

    protected $settings = array(
29 30 31 32 33
        'title'=>array(
            'type'=>'string',
            'label'=>'Export Name',
            'default'=>'CSV from exportCompleteAnswers',
        ),
Chenu Denis's avatar
Chenu Denis committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        'default'=>array(
            'type'=>'boolean',
            'label'=>'Use as default export',
            'default'=>1,
        ),
        'exportAnswerCode'=>array(
            'type'=>'boolean',
            'label'=>'Export Answer code',
            'default'=>1,
        ),
        'exportAnswerText'=>array(
            'type'=>'boolean',
            'label'=>'Export Answer text',
            'default'=>1,
        ),
        'exportAnswerPosition'=>array(
            'type'=>'select',
            'label'=>'Answer code and text position',
            'options'=>array(
                'acodetext'=>'Code before text',
                'atextcode'=>'Text before code',
                'aseperatecodetext'=>'Code and text in seperate column (code before text)',
            ),
            'default'=>'aseperatecodetext',
        ),
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        'textStringForNull'=>array(
            'type'=>'string',
            'label'=>'String for null value for answer text',
            'default'=>"",
        ),
        'codeStringForNull'=>array(
            'type'=>'string',
            'label'=>'String for null value for answer code',
            'default'=>"N/A",
        ),
        /* Only for same column */
        'sameColumnInfo'=>array(
            'type'=>'info',
            'content' => '<legend style="display:bock">Options if answer and code are in same columns</legend>',
        ),
Chenu Denis's avatar
Chenu Denis committed
74 75
        'exportAnswerCodeBefore'=>array(
            'type'=>'string',
76
            'label'=>'String before answer code',
Chenu Denis's avatar
Chenu Denis committed
77 78 79 80
            'default'=>"[",
        ),
        'exportAnswerCodeAfter'=>array(
            'type'=>'string',
81
            'label'=>'String after answer code',
Chenu Denis's avatar
Chenu Denis committed
82 83 84 85
            'default'=>"]",
        ),
        'exportNullEmptyAnswerCode'=>array(
            'type'=>'select',
86
            'label'=>'Condition for adding string to code',
Chenu Denis's avatar
Chenu Denis committed
87
            'options'=>array(
88
                'notempty'=>'If answered',
Chenu Denis's avatar
Chenu Denis committed
89 90 91 92 93 94
                'notnull'=>'If shown to the user', // Not for 2.05 - build 140708
                'always'=>'Always',
            ),
            'default'=>"notnull",
            'help' => 'This settings use code before and after under condition, default adding the string if user see the question (except for numerical and date question).'
        ),
95 96 97 98
        /* Only for 2 columns */
        'differentColumnInfo'=>array(
            'type'=>'info',
            'content' => '<legend style="display:bock">Options if answer and code are in seperate columns.</legend><p>Using 2 column only of needed : code and text are different ( example: for list question or array question type, or for multi choice question type).</p>',
Chenu Denis's avatar
Chenu Denis committed
99 100 101
        ),
        'beforeHeadColumnCode'=>array(
            'type'=>'string',
102
            'label'=>'String before question for answer code column',
Chenu Denis's avatar
Chenu Denis committed
103 104 105 106
            'default'=>"[code]",
        ),
        'afterHeadColumnCode'=>array(
            'type'=>'string',
107
            'label'=>'String after question for answer code column',
Chenu Denis's avatar
Chenu Denis committed
108 109 110 111
            'default'=>"",
        ),
        'beforeHeadColumnFull'=>array(
            'type'=>'string',
112
            'label'=>'String before question for full answer column',
Chenu Denis's avatar
Chenu Denis committed
113 114 115 116
            'default'=>"",
        ),
        'afterHeadColumnFull'=>array(
            'type'=>'string',
117
            'label'=>'String after question for full answer column',
Chenu Denis's avatar
Chenu Denis committed
118 119 120 121
            'default'=>"[full answer]",
        ),
    );

122
    public function init() {
Chenu Denis's avatar
Chenu Denis committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        $this->subscribe('beforeActivate');
        $this->subscribe('listExportPlugins');
        $this->subscribe('listExportOptions');
        $this->subscribe('newExport');
    }
    public function beforeActivate()
    {
        $oEvent = $this->getEvent();
    }
    public function listExportOptions()
    {
        $event = $this->getEvent();
        $type = $event->get('type');

        switch ($type) {
            case 'csv-allanswer':
            default:
140
                $event->set('label',$this->get('title',null,null,$this->settings['title']['default']));
141
                if($this->get('default',null,null,$this->settings['default']['default'])) {
Chenu Denis's avatar
Chenu Denis committed
142
                    $event->set('default', true);
143
                }
Chenu Denis's avatar
Chenu Denis committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                break;
        }
    }
    /**
    * Registers this export type
    */
    public function listExportPlugins()
    {
        $event = $this->getEvent();
        $exports = $event->get('exportplugins');
        $newExport=array('csv'=>$exports['csv'],'csv-allanswer'=>get_class());
        unset($exports['csv']);
        $exports=$newExport+$exports;
        $event->set('exportplugins', $exports);
    }
    public function newExport()
    {
        $event = $this->getEvent();
        $type = $event->get('type');
        switch ($type) {
            case 'csv-allanswer':
            default:
                $writer = new exportCompleteAnswersWriter();
                break;
        }

        foreach ($this->settings as $name => $value)
        {
            $default=isset($this->settings[$name]['default'])?$this->settings[$name]['default']:NULL;
            $value = $this->get($name,null,null,$default);
            $writer->$name=$value;
        }
        $event->set('writer', $writer);
    }
}