From bde49a2b72dcc26dfe7122d9fb3a99a23f9d9050 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 13 Feb 2019 13:46:18 -0500 Subject: [PATCH] Plugin: Add ApaExportSurvey - refs BT#15281 --- plugin/apaexportsurvey/ApaExportSurvey.php | 96 ++++++++++++ plugin/apaexportsurvey/README.md | 10 ++ plugin/apaexportsurvey/export.php | 174 +++++++++++++++++++++ plugin/apaexportsurvey/install.php | 4 + plugin/apaexportsurvey/lang/english.php | 7 + plugin/apaexportsurvey/lang/spanish.php | 7 + plugin/apaexportsurvey/plugin.php | 4 + plugin/apaexportsurvey/start.php | 50 ++++++ plugin/apaexportsurvey/uninstall.php | 4 + 9 files changed, 356 insertions(+) create mode 100644 plugin/apaexportsurvey/ApaExportSurvey.php create mode 100644 plugin/apaexportsurvey/README.md create mode 100644 plugin/apaexportsurvey/export.php create mode 100644 plugin/apaexportsurvey/install.php create mode 100644 plugin/apaexportsurvey/lang/english.php create mode 100644 plugin/apaexportsurvey/lang/spanish.php create mode 100644 plugin/apaexportsurvey/plugin.php create mode 100644 plugin/apaexportsurvey/start.php create mode 100644 plugin/apaexportsurvey/uninstall.php diff --git a/plugin/apaexportsurvey/ApaExportSurvey.php b/plugin/apaexportsurvey/ApaExportSurvey.php new file mode 100644 index 0000000000..87920d510d --- /dev/null +++ b/plugin/apaexportsurvey/ApaExportSurvey.php @@ -0,0 +1,96 @@ + 'boolean', + ]; + + parent::__construct('0.1', 'Angel Fernado Quiroz Campos', $settings); + } + + /** + * @return ApaExportSurvey|null + */ + public static function create() + { + static $result = null; + + return $result ? $result : $result = new self(); + } + + /** + * Installation process. + */ + public function install() + { + } + + /** + * 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' => 'apaexportsurvey/start.php%', 'category' => 'plugin']); + } + + /** + * Uninstallation process. + */ + public function uninstall() + { + } + + /** + * @param $id + * + * @return string + */ + public static function filterModify($params) + { + $enabled = api_get_plugin_setting('apaexportsurvey', '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_evaluation.png', get_lang('Export'), [], $iconSize), + api_get_path(WEB_PLUGIN_PATH).'apaexportsurvey/export.php?survey='.$surveyId.'&'.api_get_cidreq() + ); + } +} diff --git a/plugin/apaexportsurvey/README.md b/plugin/apaexportsurvey/README.md new file mode 100644 index 0000000000..093f04604f --- /dev/null +++ b/plugin/apaexportsurvey/README.md @@ -0,0 +1,10 @@ +# APA Export Survey + +Export surveys for APA. + +This plugin will add a new action button in survey list allowing export the survey. + +**Instructions** + +- Install plugin +- Set enabled in configuration diff --git a/plugin/apaexportsurvey/export.php b/plugin/apaexportsurvey/export.php new file mode 100644 index 0000000000..544b645e9b --- /dev/null +++ b/plugin/apaexportsurvey/export.php @@ -0,0 +1,174 @@ +get('enabled') !== 'true') { + api_not_allowed(true); +} + +$questionsData = SurveyManager::get_questions($surveyId, $courseId); + +// Sort questions by their "sort" field +usort( + $questionsData, + function ($qL, $qR) { + if ($qL['sort'] == $qR['sort']) { + return 0; + } + + return $qL['sort'] < $qR['sort'] ? -1 : 1; + } +); + +$content = []; + +$parts = []; +$indexPart = 0; + +// Separate questions in introduction and main blocks +foreach ($questionsData as $questionData) { + if (!in_array($questionData['type'], ['yesno', 'pagebreak', 'multiplechoice'])) { + continue; + } + + if ('pagebreak' === $questionData['type']) { + $indexPart++; + + continue; + } + + if (0 === $indexPart) { + $parts[0][] = $questionData; + + continue; + } + + $parts[$indexPart][] = $questionData; +} + +// Process introduction questions to show +foreach ($parts[0] as $key => $introQuestion) { + $content[] = chr($key + 97).'. '.strip_tags($introQuestion['question']); + + foreach ($introQuestion['answers'] as $answer) { + $content[] = '>'.strip_tags($answer); + } +} + +$content[] = str_repeat('*', 40); + +// Get surveys by users +$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(); + +// Process answers +foreach ($surveyAnswers as $i => $answer) { + $surveyLine = '"'.($i + 1).'","'; + + // Show answers for introduction questions + foreach ($parts[0] as $introQuestion) { + $options = getQuestionOptions( + $answer['user'], + $courseId, + $introQuestion['survey_id'], + $introQuestion['question_id'] + ); + + /** @var CSurveyQuestionOption $option */ + foreach ($options as $option) { + $surveyLine .= $option->getSort(); + } + } + + $surveyLine .= '","'; + + // Show answers for main questions + foreach ($parts[1] as $mainQuestion) { + $options = getQuestionOptions( + $answer['user'], + $courseId, + $mainQuestion['survey_id'], + $mainQuestion['question_id'] + ); + + /** @var CSurveyQuestionOption $option */ + foreach ($options as $option) { + $surveyLine .= $option->getSort(); + } + } + + $surveyLine .= '"'; + + $content[] = $surveyLine; +} + +// Add EOL to lines +$fileContent = array_map( + function ($line) { + return html_entity_decode($line).PHP_EOL.PHP_EOL; + }, + $content +); + +// Generate file +$fileName = api_get_path(SYS_ARCHIVE_PATH).md5($surveyId.time()).'.txt'; + +file_put_contents($fileName, $fileContent); + +DocumentManager::file_send_for_download($fileName, true); + +/** + * @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' + ) + ->setParameters( + [ + 'user' => $user, + 'course' => $courseId, + 'survey' => $surveyId, + 'question' => $questionId, + ] + ) + ->getResult(); + + return $options; +} diff --git a/plugin/apaexportsurvey/install.php b/plugin/apaexportsurvey/install.php new file mode 100644 index 0000000000..04091a8266 --- /dev/null +++ b/plugin/apaexportsurvey/install.php @@ -0,0 +1,4 @@ +install(); diff --git a/plugin/apaexportsurvey/lang/english.php b/plugin/apaexportsurvey/lang/english.php new file mode 100644 index 0000000000..fd56d68c7b --- /dev/null +++ b/plugin/apaexportsurvey/lang/english.php @@ -0,0 +1,7 @@ +get_info(); diff --git a/plugin/apaexportsurvey/start.php b/plugin/apaexportsurvey/start.php new file mode 100644 index 0000000000..684cbc0960 --- /dev/null +++ b/plugin/apaexportsurvey/start.php @@ -0,0 +1,50 @@ +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, ['ApaExportSurvey', 'filterModify']); +} else { + $table->set_header(9, get_lang('Export'), false, ['class' => 'text-center'], ['class' => 'text-center']); + $table->set_column_filter(9, ['ApaExportSurvey', '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(); diff --git a/plugin/apaexportsurvey/uninstall.php b/plugin/apaexportsurvey/uninstall.php new file mode 100644 index 0000000000..2aa9696833 --- /dev/null +++ b/plugin/apaexportsurvey/uninstall.php @@ -0,0 +1,4 @@ +uninstall();