upgrade
This commit is contained in:
42
plugin/surveyexportcsv/README.md
Normal file
42
plugin/surveyexportcsv/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Survey Export CSV
|
||||
|
||||
Exports survey results to a CSV file with a very specific format.
|
||||
|
||||
This plugin will add a new action button in the surveys list, allowing the
|
||||
teacher to export the survey in a CSV format meant at exchanging with external
|
||||
analysis tools.
|
||||
|
||||
The CSV format looks this way:
|
||||
|
||||
```
|
||||
DATID;P01;P02;P03;P04;P05;P06;P07;P08;DATOBS
|
||||
1;"1";"2";"26";"10";"2";"2";"2";"4";"2"
|
||||
2;"1";"2";"32";"10";"6";"4";"4";"5";"2"
|
||||
3;"2";"3";"27";"8";"5";"5";"2";"5";"1"
|
||||
4;"1";"3";"33";"11";"1";"4";"1";"6";"1"
|
||||
```
|
||||
|
||||
Where:
|
||||
- DATID represents a sequential ID for the participants (not related to
|
||||
their internal user ID)
|
||||
- P01,P02,... represent the sequential ID of each question inside the survey
|
||||
- DATOBS represents the free answer of the user to an open remarks form at
|
||||
the end of the survey
|
||||
|
||||
**Setup instructions**
|
||||
|
||||
- Install plugin
|
||||
- Set enabled in configuration
|
||||
- Edit `configuration.php` file
|
||||
```php
|
||||
$_configuration['survey_additional_teacher_modify_actions'] = [
|
||||
// ...
|
||||
'SurveyExportCSVPlugin' => ['SurveyExportCsvPlugin', 'filterModify'],
|
||||
];
|
||||
```
|
||||
If you have large surveys with large numbers of users answering them, you
|
||||
might want to ensure your c_survey_answer table is properly indexed. If not,
|
||||
use the following SQL statement to modify that:
|
||||
```sql
|
||||
alter table c_survey_answer add index idx_c_survey_answerucsq (user, c_id, survey_id, question_id);
|
||||
```
|
||||
95
plugin/surveyexportcsv/SurveyExportCsvPlugin.php
Normal file
95
plugin/surveyexportcsv/SurveyExportCsvPlugin.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Class SurveyExportCsvPlugin.
|
||||
*/
|
||||
class SurveyExportCsvPlugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* SurveyExportCsvPlugin constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$settings = [
|
||||
'enabled' => 'boolean',
|
||||
'export_incomplete' => 'boolean',
|
||||
];
|
||||
|
||||
parent::__construct('0.1', 'Angel Fernando Quiroz Campos', $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SurveyExportCsvPlugin|null
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
static $result = null;
|
||||
|
||||
return $result ? $result : $result = new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Installation process.
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstallation process.
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function filterModify($params)
|
||||
{
|
||||
$enabled = api_get_plugin_setting('surveyexportcsv', 'enabled');
|
||||
|
||||
if ($enabled !== 'true') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$surveyId = isset($params['survey_id']) ? (int) $params['survey_id'] : 0;
|
||||
$iconSize = isset($params['icon_size']) ? $params['icon_size'] : ICON_SIZE_SMALL;
|
||||
|
||||
if (empty($surveyId)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Display::url(
|
||||
Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], $iconSize),
|
||||
api_get_path(WEB_PLUGIN_PATH).'surveyexportcsv/export.php?survey='.$surveyId.'&'.api_get_cidreq()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create tools for all courses.
|
||||
*/
|
||||
private function createLinkToCourseTools()
|
||||
{
|
||||
$result = Database::getManager()
|
||||
->createQuery('SELECT c.id FROM ChamiloCoreBundle:Course c')
|
||||
->getResult();
|
||||
|
||||
foreach ($result as $item) {
|
||||
$this->createLinkToCourseTool($this->get_name().':teacher', $item['id'], 'survey.png');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all course tools created by plugin.
|
||||
*/
|
||||
private function removeLinkToCourseTools()
|
||||
{
|
||||
Database::getManager()
|
||||
->createQuery('DELETE FROM ChamiloCourseBundle:CTool t WHERE t.link LIKE :link AND t.category = :category')
|
||||
->execute(['link' => 'surveyexportcsv/start.php%', 'category' => 'plugin']);
|
||||
}
|
||||
}
|
||||
216
plugin/surveyexportcsv/export.php
Normal file
216
plugin/surveyexportcsv/export.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
use Chamilo\CourseBundle\Entity\CSurveyAnswer;
|
||||
use Chamilo\CourseBundle\Entity\CSurveyQuestionOption;
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
|
||||
api_protect_course_script(true);
|
||||
api_protect_teacher_script();
|
||||
|
||||
$surveyId = isset($_GET['survey']) ? (int) $_GET['survey'] : 0;
|
||||
$surveyData = SurveyManager::get_survey($surveyId);
|
||||
$courseId = api_get_course_int_id();
|
||||
|
||||
if (empty($surveyData)) {
|
||||
api_not_allowed(true);
|
||||
}
|
||||
|
||||
$plugin = SurveyExportCsvPlugin::create();
|
||||
$allowExportIncomplete = 'true' === $plugin->get('export_incomplete');
|
||||
|
||||
if ($plugin->get('enabled') !== 'true') {
|
||||
api_not_allowed(true);
|
||||
}
|
||||
|
||||
$questionsData = SurveyManager::get_questions($surveyId, $courseId);
|
||||
// Sort questions by their "sort" field
|
||||
$questionsData = array_filter(
|
||||
$questionsData,
|
||||
function ($questionData) {
|
||||
return in_array($questionData['type'], ['yesno', 'multiplechoice', 'open']);
|
||||
}
|
||||
);
|
||||
$numberOfQuestions = count($questionsData);
|
||||
|
||||
usort(
|
||||
$questionsData,
|
||||
function ($qL, $qR) {
|
||||
if ($qL['sort'] == $qR['sort']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $qL['sort'] < $qR['sort'] ? -1 : 1;
|
||||
}
|
||||
);
|
||||
|
||||
$content = [];
|
||||
$content[] = firstRow($questionsData);
|
||||
|
||||
$surveyAnswers = getSurveyAnswers($courseId, $surveyId);
|
||||
|
||||
// Process answers
|
||||
$i = 1;
|
||||
foreach ($surveyAnswers as $answer) {
|
||||
$row = otherRow($questionsData, $answer['user'], $courseId);
|
||||
|
||||
if (!$allowExportIncomplete && count($row) < $numberOfQuestions) {
|
||||
continue;
|
||||
}
|
||||
|
||||
array_unshift($row, $i);
|
||||
|
||||
$content[] = $row;
|
||||
$i++;
|
||||
}
|
||||
|
||||
// Generate file
|
||||
$fileName = md5($surveyId.time());
|
||||
|
||||
Export::arrayToCsv($content, $fileName, false, "'");
|
||||
|
||||
/**
|
||||
* Generate the first row for file.
|
||||
*
|
||||
* @param $questions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function firstRow($questions)
|
||||
{
|
||||
array_pop($questions);
|
||||
$positions = array_keys($questions);
|
||||
|
||||
$row = ['DATID'];
|
||||
|
||||
foreach ($positions as $position) {
|
||||
$row[] = sprintf("P%02d", $position + 1);
|
||||
}
|
||||
|
||||
$row[] = 'DATOBS';
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique answer for surveys by users.
|
||||
*
|
||||
* @param int $courseId
|
||||
* @param int $surveyId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getSurveyAnswers($courseId, $surveyId)
|
||||
{
|
||||
$surveyAnswers = Database::getManager()
|
||||
->createQuery(
|
||||
'SELECT sa.user, MIN(sa.iid) AS id FROM ChamiloCourseBundle:CSurveyAnswer sa
|
||||
WHERE sa.cId = :course AND sa.surveyId = :survey
|
||||
GROUP BY sa.user ORDER BY id ASC'
|
||||
)
|
||||
->setParameters(['course' => $courseId, 'survey' => $surveyId])
|
||||
->getResult();
|
||||
|
||||
return $surveyAnswers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $user
|
||||
* @param int $courseId
|
||||
* @param int $surveyId
|
||||
* @param int $questionId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getQuestionOptions($user, $courseId, $surveyId, $questionId)
|
||||
{
|
||||
$options = Database::getManager()
|
||||
->createQuery(
|
||||
'SELECT sqo FROM ChamiloCourseBundle:CSurveyQuestionOption sqo
|
||||
INNER JOIN ChamiloCourseBundle:CSurveyAnswer sa
|
||||
WITH
|
||||
sqo.cId = sa.cId
|
||||
AND sqo.questionId = sa.questionId
|
||||
AND sqo.surveyId = sa.surveyId
|
||||
AND sqo.iid = sa.optionId
|
||||
WHERE sa.user = :user AND sa.cId = :course AND sa.surveyId = :survey AND sa.questionId = :question'
|
||||
)
|
||||
->setMaxResults(1)
|
||||
->setParameters(
|
||||
[
|
||||
'user' => $user,
|
||||
'course' => $courseId,
|
||||
'survey' => $surveyId,
|
||||
'question' => $questionId,
|
||||
]
|
||||
)
|
||||
->getResult();
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $questionId
|
||||
* @param int $surveyId
|
||||
* @param int $courseId
|
||||
* @param string $user
|
||||
*
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*
|
||||
* @return CSurveyAnswer|null
|
||||
*/
|
||||
function getOpenAnswer($questionId, $surveyId, $courseId, $user)
|
||||
{
|
||||
$answer = Database::getManager()
|
||||
->createQuery(
|
||||
'SELECT sa FROM ChamiloCourseBundle:CSurveyAnswer sa
|
||||
WHERE sa.cId = :course AND sa.surveyId = :survey AND sa.questionId = :question AND sa.user = :user'
|
||||
)
|
||||
->setParameters(['course' => $courseId, 'survey' => $surveyId, 'question' => $questionId, 'user' => $user])
|
||||
->getOneOrNullResult();
|
||||
|
||||
return $answer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the content rows for file.
|
||||
*
|
||||
* @param array $questions
|
||||
* @param string $user
|
||||
* @param int $courseId
|
||||
*
|
||||
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function otherRow($questions, $user, $courseId)
|
||||
{
|
||||
$row = [];
|
||||
|
||||
foreach ($questions as $question) {
|
||||
if ('open' === $question['type']) {
|
||||
$answer = getOpenAnswer($question['question_id'], $question['survey_id'], $courseId, $user);
|
||||
|
||||
if ($answer) {
|
||||
$row[] = Security::remove_XSS($answer->getOptionId());
|
||||
}
|
||||
} else {
|
||||
$options = getQuestionOptions(
|
||||
$user,
|
||||
$courseId,
|
||||
$question['survey_id'],
|
||||
$question['question_id']
|
||||
);
|
||||
/** @var CSurveyQuestionOption|null $option */
|
||||
$option = end($options);
|
||||
|
||||
if ($option) {
|
||||
$value = $option->getSort();
|
||||
$row[] = '"'.$value.'"';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
4
plugin/surveyexportcsv/install.php
Normal file
4
plugin/surveyexportcsv/install.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
SurveyExportCsvPlugin::create()->install();
|
||||
9
plugin/surveyexportcsv/lang/english.php
Normal file
9
plugin/surveyexportcsv/lang/english.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Survey Export CSV";
|
||||
$strings['plugin_comment'] = "Export surveys results to CSV file";
|
||||
|
||||
$strings['enabled'] = 'Enabled';
|
||||
$strings['export_incomplete'] = 'Export incomplete';
|
||||
$strings['export_incomplete_help'] = 'Allow export incomplete surveys. Disabled by default.';
|
||||
9
plugin/surveyexportcsv/lang/spanish.php
Normal file
9
plugin/surveyexportcsv/lang/spanish.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Survey Export CSV";
|
||||
$strings['plugin_comment'] = "Exportar resultados de encuestas en archivo CSV.";
|
||||
|
||||
$strings['enabled'] = 'Habilitado';
|
||||
$strings['export_incomplete'] = 'Exportar incompletos';
|
||||
$strings['export_incomplete_help'] = 'Permitir exportar encuestas incompletas. Deshabilitado por defecto.';
|
||||
4
plugin/surveyexportcsv/plugin.php
Normal file
4
plugin/surveyexportcsv/plugin.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$plugin_info = SurveyExportCsvPlugin::create()->get_info();
|
||||
50
plugin/surveyexportcsv/start.php
Normal file
50
plugin/surveyexportcsv/start.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
|
||||
api_protect_course_script(true);
|
||||
api_protect_teacher_script();
|
||||
|
||||
$plugin = SurveyExportCsvPlugin::create();
|
||||
|
||||
$courseCode = api_get_course_id();
|
||||
|
||||
// Create a sortable table with survey-data
|
||||
$table = new SortableTable(
|
||||
'surveys',
|
||||
['SurveyUtil', 'get_number_of_surveys'],
|
||||
['SurveyUtil', 'get_survey_data'],
|
||||
2
|
||||
);
|
||||
$table->set_additional_parameters(['cidReq' => $courseCode]);
|
||||
$table->set_header(0, '', false);
|
||||
$table->setHideColumn(0);
|
||||
$table->set_header(1, get_lang('SurveyName'));
|
||||
$table->set_header(2, get_lang('SurveyCode'), true, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_header(3, get_lang('NumberOfQuestions'), true, ['class' => 'text-right'], ['class' => 'text-right']);
|
||||
$table->set_header(4, get_lang('Author'));
|
||||
$table->set_header(5, get_lang('AvailableFrom'), true, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_header(6, get_lang('AvailableUntil'), true, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_header(7, get_lang('Invite'), true, ['class' => 'text-right'], ['class' => 'text-right']);
|
||||
$table->set_header(8, get_lang('Anonymous'), true, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_column_filter(8, ['SurveyUtil', 'anonymous_filter']);
|
||||
|
||||
if (api_get_configuration_value('allow_mandatory_survey')) {
|
||||
$table->set_header(9, get_lang('IsMandatory'), true, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_header(10, get_lang('Export'), false, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_column_filter(10, ['SurveyExportCsvPlugin', 'filterModify']);
|
||||
} else {
|
||||
$table->set_header(9, get_lang('Export'), false, ['class' => 'text-center'], ['class' => 'text-center']);
|
||||
$table->set_column_filter(9, ['SurveyExportCsvPlugin', 'filterModify']);
|
||||
}
|
||||
|
||||
$pageTitle = $plugin->get_title();
|
||||
|
||||
$template = new Template($pageTitle);
|
||||
|
||||
$content = $table->return_table();
|
||||
|
||||
$template->assign('header', $pageTitle);
|
||||
$template->assign('content', $content);
|
||||
$template->display_one_col_template();
|
||||
4
plugin/surveyexportcsv/uninstall.php
Normal file
4
plugin/surveyexportcsv/uninstall.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
SurveyExportCsvPlugin::create()->uninstall();
|
||||
Reference in New Issue
Block a user