<commit_before><?php

namespace Concrete\Controller\SinglePage\Dashboard\Reports;

use \Concrete\Core\Page\Controller\DashboardPageController;
use Loader,
    User,
    UserInfo,
    Page;
use \Concrete\Block\Form\MiniSurvey;
use \Concrete\Block\Form\Statistics as FormBlockStatistics;

class Forms extends DashboardPageController
{

    protected $pageSize = 10;

    public function view()
    {
        if ($_REQUEST['all']) {
            $this->pageSize = 100000;
            $_REQUEST['page'] = 1;
        }
        $this->loadSurveyResponses();
    }

    public function excel()
    {
        $dateHelper = Loader::helper('date');

        $this->pageSize = 0;
        $this->loadSurveyResponses();
        $textHelper = Loader::helper('text');

        $questionSet = $this->get('questionSet');
        $answerSets = $this->get('answerSets');
        $questions = $this->get('questions');
        $surveys = $this->get('surveys');

        $fileName = $textHelper->filterNonAlphaNum($surveys[$questionSet]['surveyName']);

        header("Content-Type: application/vnd.ms-excel");
        header("Cache-control: private");
        header("Pragma: public");
        $date = date('Ymd');
        header("Content-Disposition: inline; filename=" . $fileName . "_form_data_{$date}.xls");
        echo "<html>\r\n";
        echo "<head>\r\n";
        echo "<META http-equiv=Content-Type content=\"text/html; charset=" . APP_CHARSET . "\">\r\n";
        echo "<title>" . $textHelper->entities(t(/* i18n: %1$s is the survey name, %2$s is the date. */ '%1$s Form Data Output - Run on %2$s', $surveys[$questionSet]['surveyName'], $date)) . "</title>\r\n";
        echo "</head>\r\n";
        echo "<body>\r\n";
        echo "<table>\r\n";
        $hasCBRow = false;
        foreach ($questions as $questionId => $question) {
            if ($question['inputType'] == 'checkboxlist') {
                $hasCBRow = true;
            }
        }

        echo "<tr>";
        echo "\t\t<td ";
        if ($hasCBRow) {
            echo "rowspan=\"2\" valign='bottom'";
        }
        echo "><b>" . t('Submitted Date') . "</b></td>\r\n";
        echo "<td><b>" . t('User') . "</b></td>\r\n";

        foreach ($questions as $questionId => $question) {
            if ($question['inputType'] == 'checkboxlist') {
                $options = explode('%%', $question['options']);
                echo "\t\t" . '<td colspan="' . count($options) . '"><b>' . "\r\n";
            } else {
                echo "\t\t<td ";
                if ($hasCBRow) {
                    echo "rowspan=\"2\" valign='bottom'";
                }
                echo "><b>\r\n";
            }
            echo "\t\t\t" . $questions[$questionId]['question'] . "\r\n";
            echo "\t\t</b></td>\r\n";
        }
        echo "</tr>";

        // checkbox row
        if ($hasCBRow) {
            echo "<tr>";
            foreach ($questions as $questionId => $question) {
                if ($question['inputType'] == 'checkboxlist') {
                    $options = explode('%%', $question['options']);
                    foreach ($options as $opt) {
                        echo "<td><b>{$opt}</b></td>";
                    }
                }
            }
            echo "</tr>";
        }

        foreach ($answerSets as $answerSetId => $answerSet) {
            $questionNumber = 0;
            $numQuestionsToShow = 2;
            echo "\t<tr>\r\n";
            echo "\t\t<td>" . $dateHelper->getSystemDateTime($answerSet['created']) . "</td>\r\n";
            echo "\t\t<td>";
            if ($answerSet['uID'] > 0) {
                $ui = UserInfo::getByID($answerSet['uID']);
                if (is_object($ui)) {
                    echo $ui->getUserName();
                }
            }
            echo "</td>\r\n";
            foreach ($questions as $questionId => $question) {
                $questionNumber++;
                if ($question['inputType'] == 'checkboxlist') {
                    $options = explode('%%', $question['options']);
                    $subanswers = explode(',', $answerSet['answers'][$questionId]['answer']);
                    for ($i = 1; $i <= count($options); $i++) {
                        echo "\t\t<td align='center'>\r\n";
                        if (in_array(trim($options[$i - 1]), $subanswers)) {
                            // echo "\t\t\t".$options[$i-1]."\r\n";
                            echo "x";
                        } else {
                            echo "\t\t\t&nbsp;\r\n";
                        }
                        echo "\t\t</td>\r\n";
                    }
                } elseif ($question['inputType'] == 'fileupload') {
                    echo "\t\t<td>\r\n";
                    $fID = intval($answerSet['answers'][$questionId]['answer']);
                    $file = File::getByID($fID);
                    if ($fID && $file) {
                        $fileVersion = $file->getApprovedVersion();
                        echo "\t\t\t" . '<a href="' . $fileVersion->getDownloadURL() . '">' . $fileVersion->getFileName() . '</a>' . "\r\n";
                    } else {
                        echo "\t\t\t" . t('File not found') . "\r\n";
                    }
                    echo "\t\t</td>\r\n";
                } else {
                    echo "\t\t<td>\r\n";
                    echo "\t\t\t" . $answerSet['answers'][$questionId]['answer'] . $answerSet['answers'][$questionId]['answerLong'] . "\r\n";
                    echo "\t\t</td>\r\n";
                }
            }
            echo "\t</tr>\r\n";
        }
        echo "</table>\r\n";
        echo "</body>\r\n";
        echo "</html>\r\n";
        die;
    }

    private function loadSurveyResponses()
    {
        $c = Page::getCurrentPage();
        $db = Loader::db();
        $tempMiniSurvey = new MiniSurvey();
        $pageBase = DIR_REL . '/' . DISPATCHER_FILENAME . '?cID=' . $c->getCollectionID();<commit_msg>Form CSV export<commit_after><?php

namespace Concrete\Controller\SinglePage\Dashboard\Reports;

use Concrete\Core\File\File;
use \Concrete\Core\Page\Controller\DashboardPageController;
use Loader,
    UserInfo,
    Page;
use \Concrete\Block\Form\MiniSurvey;
use \Concrete\Block\Form\Statistics as FormBlockStatistics;

class Forms extends DashboardPageController
{

    protected $pageSize = 10;

    public function view()
    {
        if ($_REQUEST['all']) {
            $this->pageSize = 100000;
            $_REQUEST['page'] = 1;
        }
        $this->loadSurveyResponses();
    }

    public function excel()
    {
        $dateHelper = Loader::helper('date');

        $this->pageSize = 0;
        $this->loadSurveyResponses();
        $textHelper = Loader::helper('text');

        $questionSet = $this->get('questionSet');
        $answerSets = $this->get('answerSets');
        $questions = $this->get('questions');
        $surveys = $this->get('surveys');

        $fileName = $textHelper->filterNonAlphaNum($surveys[$questionSet]['surveyName']);

        header("Content-Type: Content-type: text/csv");
        header("Cache-control: private");
        header("Pragma: public");
        $date = date('Ymd');
        header("Content-Disposition: attachment; filename=" . $fileName . "_form_data_{$date}.csv");

        $fp = fopen('php://output', 'w');

        // write the columns
        $row = array(
            t('Submitted Date'),
            t('User'),
        );

        foreach ($questions as $questionId => $question) {
            if ($question['inputType'] == 'checkboxlist') {
                $options = explode('%%', $question['options']);
                foreach ($options as $opt) {
                    $row[] = $questions[$questionId]['question'] . ': ' . $opt;
                }
            } else {
                $row[] = $questions[$questionId]['question'];
            }
        }

        fputcsv($fp, $row);

        // write the data
        foreach ($answerSets as $answerSet) {
            $row = [];

            $row[] = $dateHelper->getSystemDateTime($answerSet['created']);

            if ($answerSet['uID'] > 0) {
                $ui = UserInfo::getByID($answerSet['uID']);
                if (is_object($ui)) {
                    $row[] = $ui->getUserName();
                }
            } else {
                $row[] = '';
            }

            foreach ($questions as $questionId => $question) {
                if ($question['inputType'] == 'checkboxlist') {
                    $options = explode('%%', $question['options']);
                    $subanswers = explode(',', $answerSet['answers'][$questionId]['answer']);
                    for ($i = 1; $i <= count($options); $i++) {
                        if (in_array(trim($options[$i - 1]), $subanswers)) {
                            // echo "\t\t\t".$options[$i-1]."\r\n";
                            $row[] = 'x';
                        } else {
                            $row[] = '';
                        }
                    }
                } else if ($question['inputType'] == 'fileupload') {
                    $fID = intval($answerSet['answers'][$questionId]['answer']);
                    $file = File::getByID($fID);
                    if ($fID && $file) {
                        $fileVersion = $file->getApprovedVersion();
                        $row[] = $fileVersion->getDownloadURL();
                    } else {
                        $row[] = t('File not found');
                    }
                } else {
                    $row[] = $answerSet['answers'][$questionId]['answer'] . $answerSet['answers'][$questionId]['answerLong'];
                }
            }

            fputcsv($fp, $row);
        }

        fclose($fp);
        die;
    }

    private function loadSurveyResponses()
    {
        $c = Page::getCurrentPage();
        $db = Loader::db();
        $tempMiniSurvey = new MiniSurvey();
        $pageBase = DIR_REL . '/' . DISPATCHER_FILENAME . '?cID=' . $c->getCollectionID();
{'fraction_non_alphanumeric': 0.18101038338658146, 'fraction_numerical': 0.0037939297124600637, 'mean_word_length': 1.5456162642947904, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3522359', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2839, 'n_words': 524}