コード例 #1
0
 /**
  * Generates statistics
  *
  * @param int $surveyid The survey id
  * @param mixed $allfields
  * @param mixed $q2show
  * @param mixed $usegraph
  * @param string $outputType Optional - Can be xls, html or pdf - Defaults to pdf
  * @param string $pdfOutput Sets the target for the PDF output: DD=File download , F=Save file to local disk
  * @param string $statlangcode Lamguage for statistics
  * @param mixed $browse  Show browse buttons
  * @return buffer
  */
 public function generate_statistics($surveyid, $allfields, $q2show = 'all', $usegraph = 0, $outputType = 'pdf', $pdfOutput = 'I', $sLanguageCode = null, $browse = true)
 {
     $aStatisticsData = array();
     //astatdata generates data for the output page's javascript so it can rebuild graphs on the fly
     //load surveytranslator helper
     Yii::import('application.helpers.surveytranslator_helper', true);
     Yii::import('application.third_party.ar-php.Arabic', true);
     $sOutputHTML = "";
     //This string carries all the actual HTML code to print.
     $sTempDir = Yii::app()->getConfig("tempdir");
     $this->pdf = array();
     //Make sure $this->pdf exists - it will be replaced with an object if a $this->pdf is actually being created
     //pick the best font file if font setting is 'auto'
     if (is_null($sLanguageCode)) {
         $sLanguageCode = getBaseLanguageFromSurveyID($surveyid);
     }
     Yii::app()->setLanguage($sLanguageCode);
     /*
      * this variable is used in the function shortencode() which cuts off a question/answer title
      * after $maxchars and shows the rest as tooltip (in html mode)
      */
     $maxchars = 13;
     //we collect all the html-output within this variable
     $sOutputHTML = '';
     /**
      * $outputType: html || pdf ||
      */
     /**
      * get/set Survey Details
      */
     //no survey ID? -> come and get one
     if (!isset($surveyid)) {
         $surveyid = returnGlobal('sid');
     }
     //Get an array of codes of all available languages in this survey
     $surveylanguagecodes = Survey::model()->findByPk($surveyid)->additionalLanguages;
     $surveylanguagecodes[] = Survey::model()->findByPk($surveyid)->language;
     $fieldmap = createFieldMap($surveyid, "full", false, false, $sLanguageCode);
     // Set language for questions and answers to base language of this survey
     $language = $sLanguageCode;
     if ($q2show == 'all') {
         $summarySql = " SELECT gid, parent_qid, qid, type " . " FROM {{questions}} WHERE parent_qid=0" . " AND sid={$surveyid} ";
         $summaryRs = Yii::app()->db->createCommand($summarySql)->query()->readAll();
         foreach ($summaryRs as $field) {
             $myField = $surveyid . "X" . $field['gid'] . "X" . $field['qid'];
             // Multiple choice get special treatment
             if ($field['type'] == "M") {
                 $myField = "M{$myField}";
             }
             if ($field['type'] == "P") {
                 $myField = "P{$myField}";
             }
             //numerical input will get special treatment (arihtmetic mean, standard derivation, ...)
             if ($field['type'] == "N") {
                 $myField = "N{$myField}";
             }
             if ($field['type'] == "|") {
                 $myField = "|{$myField}";
             }
             if ($field['type'] == "Q") {
                 $myField = "Q{$myField}";
             }
             // textfields get special treatment
             if ($field['type'] == "S" || $field['type'] == "T" || $field['type'] == "U") {
                 $myField = "T{$myField}";
             }
             //statistics for Date questions are not implemented yet.
             if ($field['type'] == "D") {
                 $myField = "D{$myField}";
             }
             if ($field['type'] == "F" || $field['type'] == "H") {
                 //Get answers. We always use the answer code because the label might be too long elsewise
                 $query = "SELECT code, answer FROM {{answers}} WHERE qid='" . $field['qid'] . "' AND scale_id=0 AND language='{$language}' ORDER BY sortorder, answer";
                 $result = Yii::app()->db->createCommand($query)->query();
                 $counter2 = 0;
                 //check all the answers
                 foreach ($result->readAll() as $row) {
                     $row = array_values($row);
                     $myField = "{$myField}{$row[0]}";
                 }
                 //$myField = "{$surveyid}X{$flt[1]}X{$flt[0]}{$row[0]}[]";
             }
             if ($q2show == 'all') {
                 $summary[] = $myField;
             }
             //$allfields[]=$myField;
         }
     } else {
         // This gets all the 'to be shown questions' from the POST and puts these into an array
         if (!is_array($q2show)) {
             $summary = returnGlobal('summary');
         } else {
             $summary = $q2show;
         }
         //print_r($_POST);
         //if $summary isn't an array we create one
         if (isset($summary) && !is_array($summary)) {
             $summary = explode("+", $summary);
         }
     }
     /**
      * pdf Config
      */
     if ($outputType == 'pdf') {
         //require_once('classes/tcpdf/mypdf.php');
         Yii::import('application.libraries.admin.pdf', true);
         Yii::import('application.helpers.pdfHelper');
         $aPdfLanguageSettings = pdfHelper::getPdfLanguageSettings($language);
         // create new PDF document
         $this->pdf = new pdf();
         $surveyInfo = getSurveyInfo($surveyid, $language);
         // set document information
         $this->pdf->SetCreator(PDF_CREATOR);
         $this->pdf->SetAuthor('LimeSurvey');
         $this->pdf->SetTitle(sprintf(gT("Statistics survey %s"), $surveyid));
         $this->pdf->SetSubject($surveyInfo['surveyls_title']);
         $this->pdf->SetKeywords('LimeSurvey,' . gT("Statistics") . ', ' . sprintf(gT("Survey %s"), $surveyid));
         $this->pdf->SetDisplayMode('fullpage', 'two');
         $this->pdf->setLanguageArray($aPdfLanguageSettings['lg']);
         // set header and footer fonts
         $this->pdf->setHeaderFont(array($aPdfLanguageSettings['pdffont'], '', PDF_FONT_SIZE_MAIN));
         $this->pdf->setFooterFont(array($aPdfLanguageSettings['pdffont'], '', PDF_FONT_SIZE_DATA));
         // set default header data
         // Since png crashes some servers (and we can not try/catch that) we use .gif (or .jpg) instead
         $headerlogo = 'statistics.gif';
         $this->pdf->SetHeaderData($headerlogo, 10, gT("Quick statistics", 'unescaped'), gT("Survey") . " " . $surveyid . " '" . flattenText($surveyInfo['surveyls_title'], false, true, 'UTF-8') . "'");
         $this->pdf->SetFont($aPdfLanguageSettings['pdffont'], '', $aPdfLanguageSettings['pdffontsize']);
         // set default monospaced font
         $this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
     }
     if ($outputType == 'xls') {
         /**
          * Initiate the Spreadsheet_Excel_Writer
          */
         require_once APPPATH . '/third_party/pear/Spreadsheet/Excel/Xlswriter.php';
         if ($pdfOutput == 'F') {
             $sFileName = $sTempDir . '/statistic-survey' . $surveyid . '.xls';
             $this->workbook = new Xlswriter($sFileName);
         } else {
             $this->workbook = new Xlswriter();
         }
         $this->workbook->setVersion(8);
         // Inform the module that our data will arrive as UTF-8.
         // Set the temporary directory to avoid PHP error messages due to open_basedir restrictions and calls to tempnam("", ...)
         $this->workbook->setTempDir($sTempDir);
         // Inform the module that our data will arrive as UTF-8.
         // Set the temporary directory to avoid PHP error messages due to open_basedir restrictions and calls to tempnam("", ...)
         if (!empty($sTempDir)) {
             $this->workbook->setTempDir($sTempDir);
         }
         if ($pdfOutput != 'F') {
             $this->workbook->send('statistic-survey' . $surveyid . '.xls');
         }
         // Creating the first worksheet
         $this->sheet = $this->workbook->addWorksheet(utf8_decode('results-survey' . $surveyid));
         $this->xlsPercents =& $this->workbook->addFormat();
         $this->xlsPercents->setNumFormat('0.00%');
         $this->formatBold =& $this->workbook->addFormat(array('Bold' => 1));
         $this->sheet->setInputEncoding('utf-8');
         $this->sheet->setColumn(0, 20, 20);
         $separator = "~|";
         /**XXX*/
     }
     /**
      * Start generating
      */
     $selects = buildSelects($allfields, $surveyid, $language);
     //count number of answers
     $query = "SELECT count(*) FROM {{survey_{$surveyid}}}";
     //if incompleted answers should be filtert submitdate has to be not null
     if (incompleteAnsFilterState() == "incomplete") {
         $query .= " WHERE submitdate is null";
     } elseif (incompleteAnsFilterState() == "complete") {
         $query .= " WHERE submitdate is not null";
     }
     $total = Yii::app()->db->createCommand($query)->queryScalar();
     //are there any filters that have to be taken care of?
     if (isset($selects) && $selects) {
         //Save the filters to session for use in browsing text & other features (statistics.php function listcolumn())
         Yii::app()->session['statistics_selects_' . $surveyid] = $selects;
         //filter incomplete answers?
         if (incompleteAnsFilterState() == "complete" || incompleteAnsFilterState() == "incomplete") {
             $query .= " AND ";
         } else {
             $query .= " WHERE ";
         }
         //add filter criteria to SQL
         $query .= implode(" AND ", $selects);
     }
     //get me some data Scotty
     $results = Yii::app()->db->createCommand($query)->queryScalar();
     if ($total) {
         $percent = sprintf("%01.2f", $results / $total * 100);
     }
     switch ($outputType) {
         case "xls":
             $this->xlsRow = 0;
             $this->sheet->write($this->xlsRow, 0, gT("Number of records in this query:", 'unescaped'));
             $this->sheet->writeNumber($this->xlsRow, 1, $results);
             $this->xlsRow++;
             $this->sheet->write($this->xlsRow, 0, gT("Total records in survey:", 'unescaped'));
             $this->sheet->writeNumber($this->xlsRow, 1, $total);
             if ($total) {
                 $this->xlsRow++;
                 $this->sheet->write($this->xlsRow, 0, gT("Percentage of total:", 'unescaped'));
                 $this->sheet->writeNumber($this->xlsRow, 1, $results / $total, $this->xlsPercents);
             }
             break;
         case 'pdf':
             // add summary to pdf
             $array = array(array(gT("Number of records in this query:", 'unescaped'), $results), array(gT("Total records in survey:", 'unescaped'), $total));
             if ($total) {
                 $array[] = array(gT("Percentage of total:", 'unescaped'), $percent . "%");
             }
             $this->pdf->AddPage('P', ' A4');
             $this->pdf->Bookmark(gT("Results", 'unescaped'), 0, 0);
             $this->pdf->titleintopdf(gT("Results", 'unescaped'), gT("Survey", 'unescaped') . " " . $surveyid);
             $this->pdf->tableintopdf($array);
             break;
         case 'html':
             $sOutputHTML .= "<br />\n<table class='statisticssummary' >\n" . "\t<thead><tr><th colspan='2'>" . gT("Results") . "</th></tr></thead>\n" . "\t<tr><th >" . gT("Number of records in this query:") . '</th>' . "<td>{$results}</td></tr>\n" . "\t<tr><th>" . gT("Total records in survey:") . '</th>' . "<td>{$total}</td></tr>\n";
             //only calculate percentage if $total is set
             if ($total) {
                 $percent = sprintf("%01.2f", $results / $total * 100);
                 $sOutputHTML .= "\t<tr><th align='right'>" . gT("Percentage of total:") . '</th>' . "<td>{$percent}%</td></tr>\n";
             }
             $sOutputHTML .= "</table>\n";
             break;
         default:
             break;
     }
     //put everything from $selects array into a string connected by AND
     //This string ($sql) can then be passed on to other functions so you can
     //browse these results
     if (isset($selects) && $selects) {
         $sql = implode(" AND ", $selects);
     } elseif (!empty($newsql)) {
         $sql = $newsql;
     }
     if (!isset($sql) || !$sql) {
         $sql = null;
     }
     //only continue if we have something to output
     if ($results > 0) {
         if ($outputType == 'html' && $browse === true && Permission::model()->hasSurveyPermission($surveyid, 'responses', 'read')) {
             //add a buttons to browse results
             $sOutputHTML .= CHtml::form(array("admin/responses/sa/browse/surveyid/{$surveyid}"), 'post', array('target' => '_blank')) . "\n" . "\t\t<p>" . "\t\t\t<input type='submit' value='" . gT("Browse") . "'  />\n" . "\t\t\t<input type='hidden' name='sid' value='{$surveyid}' />\n" . "\t\t\t<input type='hidden' name='sql' value=\"{$sql}\" />\n" . "\t\t\t<input type='hidden' name='subaction' value='all' />\n" . "\t\t</p>" . "\t\t</form>\n";
         }
     }
     //end if (results > 0)
     /* Show Summary results
      * The $summary array contains each fieldname that we want to display statistics for
      *
      * */
     if (isset($summary) && $summary) {
         //let's run through the survey
         $runthrough = $summary;
         //START Chop up fieldname and find matching questions
         //loop through all selected questions
         foreach ($runthrough as $rt) {
             //Step 1: Get information about this response field (SGQA) for the summary
             $outputs = $this->buildOutputList($rt, $language, $surveyid, $outputType, $sql, $sLanguageCode);
             $sOutputHTML .= $outputs['statisticsoutput'];
             //2. Collect and Display results #######################################################################
             if (isset($outputs['alist']) && $outputs['alist']) {
                 $display = $this->displayResults($outputs, $results, $rt, $outputType, $surveyid, $sql, $usegraph, $browse, $sLanguageCode);
                 $sOutputHTML .= $display['statisticsoutput'];
                 $aStatisticsData = array_merge($aStatisticsData, $display['astatdata']);
             }
             //end if -> collect and display results
             //Delete Build Outputs data
             unset($outputs);
             unset($display);
         }
         // end foreach -> loop through all questions
         //output
         if ($outputType == 'html') {
             $sOutputHTML .= "<br />&nbsp;\n";
         }
     }
     //end if -> show summary results
     switch ($outputType) {
         case 'xls':
             $this->workbook->close();
             if ($pdfOutput == 'F') {
                 return $sFileName;
             } else {
                 return;
             }
             break;
         case 'pdf':
             $this->pdf->lastPage();
             if ($pdfOutput == 'F') {
                 // This is only used by lsrc to send an E-Mail attachment, so it gives back the filename to send and delete afterwards
                 $tempfilename = $sTempDir . "/Survey_" . $surveyid . ".pdf";
                 $this->pdf->Output($tempfilename, $pdfOutput);
                 return $tempfilename;
             } else {
                 return $this->pdf->Output(gT('Survey') . '_' . $surveyid . "_" . $surveyInfo['surveyls_title'] . '.pdf', $pdfOutput);
             }
             break;
         case 'html':
             $sGoogleMapsAPIKey = trim(Yii::app()->getConfig("googleMapsAPIKey"));
             if ($sGoogleMapsAPIKey != '') {
                 $sGoogleMapsAPIKey = '&key=' . $sGoogleMapsAPIKey;
             }
             $sSSL = '';
             if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") {
                 $sSSL = 's';
             }
             $sOutputHTML .= "<script type=\"text/javascript\" src=\"http{$sSSL}://maps.googleapis.com/maps/api/js?sensor=false{$sGoogleMapsAPIKey}\"></script>\n" . "<script type=\"text/javascript\">var site_url='" . Yii::app()->baseUrl . "';var temppath='" . Yii::app()->getConfig("tempurl") . "';var imgpath='" . Yii::app()->getConfig('adminimageurl') . "';var aStatData=" . ls_json_encode($aStatisticsData) . "</script>";
             return $sOutputHTML;
             break;
         default:
             return $sOutputHTML;
             break;
     }
 }
コード例 #2
0
 /**
  * printanswers::view()
  * View answers at the end of a survey in one place. To export as pdf, set 'usepdfexport' = 1 in lsconfig.php and $printableexport='pdf'.
  * @param mixed $surveyid
  * @param bool $printableexport
  * @return
  */
 function actionView($surveyid, $printableexport = FALSE)
 {
     Yii::app()->loadHelper("frontend");
     Yii::import('application.libraries.admin.pdf');
     $iSurveyID = (int) $surveyid;
     $sExportType = $printableexport;
     Yii::app()->loadHelper('database');
     if (isset($_SESSION['survey_' . $iSurveyID]['sid'])) {
         $iSurveyID = $_SESSION['survey_' . $iSurveyID]['sid'];
     } else {
         //die('Invalid survey/session');
     }
     // Get the survey inforamtion
     // Set the language for dispay
     if (isset($_SESSION['survey_' . $iSurveyID]['s_lang'])) {
         $sLanguage = $_SESSION['survey_' . $iSurveyID]['s_lang'];
     } elseif (Survey::model()->findByPk($iSurveyID)) {
         $sLanguage = Survey::model()->findByPk($iSurveyID)->language;
     } else {
         $iSurveyID = 0;
         $sLanguage = Yii::app()->getConfig("defaultlang");
     }
     $clang = SetSurveyLanguage($iSurveyID, $sLanguage);
     $aSurveyInfo = getSurveyInfo($iSurveyID, $sLanguage);
     //SET THE TEMPLATE DIRECTORY
     if (!isset($aSurveyInfo['templatedir']) || !$aSurveyInfo['templatedir']) {
         $aSurveyInfo['templatedir'] = Yii::app()->getConfig('defaulttemplate');
     }
     $sTemplate = validateTemplateDir($aSurveyInfo['templatedir']);
     //Survey is not finished or don't exist
     if (!isset($_SESSION['survey_' . $iSurveyID]['finished']) || !isset($_SESSION['survey_' . $iSurveyID]['srid'])) {
         sendCacheHeaders();
         doHeader();
         echo templatereplace(file_get_contents(getTemplatePath($sTemplate) . '/startpage.pstpl'), array());
         echo "<center><br />\n" . "\t<font color='RED'><strong>" . $clang->gT("Error") . "</strong></font><br />\n" . "\t" . $clang->gT("We are sorry but your session has expired.") . "<br />" . $clang->gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection.") . "<br />\n" . "\t" . sprintf($clang->gT("Please contact %s ( %s ) for further assistance."), Yii::app()->getConfig("siteadminname"), Yii::app()->getConfig("siteadminemail")) . "\n" . "</center><br />\n";
         echo templatereplace(file_get_contents(getTemplatePath($sTemplate) . '/endpage.pstpl'), array());
         doFooter();
         exit;
     }
     //Fin session time out
     $sSRID = $_SESSION['survey_' . $iSurveyID]['srid'];
     //I want to see the answers with this id
     //Ensure script is not run directly, avoid path disclosure
     //if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");}
     if ($aSurveyInfo['printanswers'] == 'N') {
         die;
         //Die quietly if print answers is not permitted
     }
     //CHECK IF SURVEY IS ACTIVATED AND EXISTS
     $sSurveyName = $aSurveyInfo['surveyls_title'];
     $sAnonymized = $aSurveyInfo['anonymized'];
     //OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK.
     //SHOW HEADER
     $sOutput = CHtml::form(array("printanswers/view/surveyid/{$iSurveyID}/printableexport/pdf"), 'post') . "<center><input type='submit' value='" . $clang->gT("PDF export") . "'id=\"exportbutton\"/><input type='hidden' name='printableexport' /></center></form>";
     if ($sExportType == 'pdf') {
         //require (Yii::app()->getConfig('rootdir').'/application/config/tcpdf.php');
         Yii::import('application.libraries.admin.pdf', true);
         Yii::import('application.helpers.pdfHelper');
         $aPdfLanguageSettings = pdfHelper::getPdfLanguageSettings($clang->langcode);
         $oPDF = new pdf();
         $oPDF->SetTitle($clang->gT("Survey name (ID)", 'unescaped') . ": {$sSurveyName} ({$iSurveyID})");
         $oPDF->SetSubject($sSurveyName);
         $oPDF->SetDisplayMode('fullpage', 'two');
         $oPDF->setLanguageArray($aPdfLanguageSettings['lg']);
         $oPDF->setHeaderFont(array($aPdfLanguageSettings['pdffont'], '', PDF_FONT_SIZE_MAIN));
         $oPDF->setFooterFont(array($aPdfLanguageSettings['pdffont'], '', PDF_FONT_SIZE_DATA));
         $oPDF->SetFont($aPdfLanguageSettings['pdffont'], '', $aPdfLanguageSettings['pdffontsize']);
         $oPDF->AddPage();
         $oPDF->titleintopdf($clang->gT("Survey name (ID)", 'unescaped') . ": {$sSurveyName} ({$iSurveyID})");
     }
     $sOutput .= "\t<div class='printouttitle'><strong>" . $clang->gT("Survey name (ID):") . "</strong> {$sSurveyName} ({$iSurveyID})</div><p>&nbsp;\n";
     LimeExpressionManager::StartProcessingPage(true);
     // means that all variables are on the same page
     // Since all data are loaded, and don't need JavaScript, pretend all from Group 1
     LimeExpressionManager::StartProcessingGroup(1, $aSurveyInfo['anonymized'] != "N", $iSurveyID);
     $printanswershonorsconditions = Yii::app()->getConfig('printanswershonorsconditions');
     $aFullResponseTable = getFullResponseTable($iSurveyID, $sSRID, $sLanguage, $printanswershonorsconditions);
     //Get the fieldmap @TODO: do we need to filter out some fields?
     if ($aSurveyInfo['datestamp'] != "Y" || $sAnonymized == 'Y') {
         unset($aFullResponseTable['submitdate']);
     } else {
         unset($aFullResponseTable['id']);
     }
     unset($aFullResponseTable['token']);
     unset($aFullResponseTable['lastpage']);
     unset($aFullResponseTable['startlanguage']);
     unset($aFullResponseTable['datestamp']);
     unset($aFullResponseTable['startdate']);
     $sOutput .= "<table class='printouttable' >\n";
     foreach ($aFullResponseTable as $sFieldname => $fname) {
         if (substr($sFieldname, 0, 4) == 'gid_') {
             $sOutput .= "\t<tr class='printanswersgroup'><td colspan='2'>{$fname[0]}</td></tr>\n";
         } elseif (substr($sFieldname, 0, 4) == 'qid_') {
             $sOutput .= "\t<tr class='printanswersquestionhead'><td colspan='2'>{$fname[0]}</td></tr>\n";
         } elseif ($sFieldname == 'submitdate') {
             if ($sAnonymized != 'Y') {
                 $sOutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]} {$sFieldname}</td><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
             }
         } else {
             $sOutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]}</td><td class='printanswersanswertext'>" . flattenText($fname[2]) . "</td></tr>";
         }
     }
     $sOutput .= "</table>\n";
     $sData['thissurvey'] = $aSurveyInfo;
     $sOutput = templatereplace($sOutput, array(), $sData, '', $aSurveyInfo['anonymized'] == "Y", NULL, array(), true);
     // Do a static replacement
     if ($sExportType == 'pdf') {
         $oPDF->writeHTML($sOutput);
         header("Pragma: public");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         $sExportFileName = sanitize_filename($sSurveyName);
         $oPDF->Output($sExportFileName . "-" . $iSurveyID . ".pdf", "D");
     } else {
         ob_start(function ($buffer, $phase) {
             App()->getClientScript()->render($buffer);
             App()->getClientScript()->reset();
             return $buffer;
         });
         ob_implicit_flush(false);
         sendCacheHeaders();
         doHeader();
         echo templatereplace(file_get_contents(getTemplatePath($sTemplate) . '/startpage.pstpl'), array(), $sData);
         echo templatereplace(file_get_contents(getTemplatePath($sTemplate) . '/printanswers.pstpl'), array('ANSWERTABLE' => $sOutput), $sData);
         echo templatereplace(file_get_contents(getTemplatePath($sTemplate) . '/endpage.pstpl'), array(), $sData);
         echo "</body></html>";
         ob_flush();
     }
     LimeExpressionManager::FinishProcessingGroup();
     LimeExpressionManager::FinishProcessingPage();
 }
コード例 #3
0
 /**
  * Create PDF object using parameters
  * 
  * @return PDF
  */
 protected function create_pdf_object()
 {
     //Default orientation is Landescape
     $orientation = 'L';
     if ($this->get_instance()->height > $this->get_instance()->width) {
         $orientation = 'P';
     }
     // Remove commas to avoid a bug in TCPDF where a string containing a commas will result in two strings.
     $keywords = get_string('keywords', 'simplecertificate') . ',' . format_string($this->get_instance()->coursename, true);
     $keywords = str_replace(",", " ", $keywords);
     // Replace commas with spaces.
     $keywords = str_replace("  ", " ", $keywords);
     // Replace two spaces with one.
     $pdf = new pdf($orientation, 'mm', array($this->get_instance()->width, $this->get_instance()->height), true, 'UTF-8');
     $pdf->SetTitle($this->get_instance()->name);
     $pdf->SetSubject($this->get_instance()->name . ' - ' . $this->get_instance()->coursename);
     $pdf->SetKeywords($keywords);
     $pdf->setPrintHeader(false);
     $pdf->setPrintFooter(false);
     $pdf->SetAutoPageBreak(false, 0);
     $pdf->setFontSubsetting(true);
     $pdf->SetMargins(0, 0, 0, true);
     return $pdf;
 }
コード例 #4
0
 /**
  * printanswers::view()
  * View answers at the end of a survey in one place. To export as pdf, set 'usepdfexport' = 1 in lsconfig.php and $printableexport='pdf'.
  * @param mixed $surveyid
  * @param bool $printableexport
  * @return
  */
 function actionView($surveyid, $printableexport = FALSE)
 {
     global $siteadminname, $siteadminemail;
     Yii::app()->loadHelper("frontend");
     Yii::import('application.libraries.admin.pdf');
     $surveyid = (int) $surveyid;
     Yii::app()->loadHelper('database');
     if (isset($_SESSION['survey_' . $surveyid]['sid'])) {
         $surveyid = $_SESSION['survey_' . $surveyid]['sid'];
     } else {
         die('Invalid survey/session');
     }
     //Debut session time out
     if (!isset($_SESSION['survey_' . $surveyid]['finished']) || !isset($_SESSION['survey_' . $surveyid]['srid'])) {
         //require_once($rootdir.'/classes/core/language.php');
         $baselang = Survey::model()->findByPk($surveyid)->language;
         Yii::import('application.libraries.Limesurvey_lang', true);
         $clang = new Limesurvey_lang($baselang);
         //A nice exit
         sendCacheHeaders();
         doHeader();
         echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default")) . "/startpage.pstpl"), array(), array());
         echo "<center><br />\n" . "\t<font color='RED'><strong>" . $clang->gT("Error") . "</strong></font><br />\n" . "\t" . $clang->gT("We are sorry but your session has expired.") . "<br />" . $clang->gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection.") . "<br />\n" . "\t" . sprintf($clang->gT("Please contact %s ( %s ) for further assistance."), $siteadminname, $siteadminemail) . "\n" . "</center><br />\n";
         echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default")) . "/endpage.pstpl"), array(), array());
         doFooter();
         exit;
     }
     //Fin session time out
     $id = $_SESSION['survey_' . $surveyid]['srid'];
     //I want to see the answers with this id
     $clang = $_SESSION['survey_' . $surveyid]['s_lang'];
     //Ensure script is not run directly, avoid path disclosure
     //if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");}
     // Set the language for dispay
     //require_once($rootdir.'/classes/core/language.php');  // has been secured
     if (isset($_SESSION['survey_' . $surveyid]['s_lang'])) {
         $clang = SetSurveyLanguage($surveyid, $_SESSION['survey_' . $surveyid]['s_lang']);
         $language = $_SESSION['survey_' . $surveyid]['s_lang'];
     } else {
         $language = Survey::model()->findByPk($surveyid)->language;
         $clang = SetSurveyLanguage($surveyid, $language);
     }
     // Get the survey inforamtion
     $thissurvey = getSurveyInfo($surveyid, $language);
     //SET THE TEMPLATE DIRECTORY
     if (!isset($thissurvey['templatedir']) || !$thissurvey['templatedir']) {
         $thistpl = validateTemplateDir("default");
     } else {
         $thistpl = validateTemplateDir($thissurvey['templatedir']);
     }
     if ($thissurvey['printanswers'] == 'N') {
         die;
         //Die quietly if print answers is not permitted
     }
     //CHECK IF SURVEY IS ACTIVATED AND EXISTS
     $surveytable = "{{survey_{$surveyid}}}";
     $surveyname = $thissurvey['surveyls_title'];
     $anonymized = $thissurvey['anonymized'];
     //OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK.
     //SHOW HEADER
     $printoutput = '';
     $printoutput .= "<form action='" . Yii::app()->getController()->createUrl('printanswers/view/surveyid/' . $surveyid . '/printableexport/pdf') . "' method='post'>\n<center><input type='submit' value='" . $clang->gT("PDF export") . "'id=\"exportbutton\"/><input type='hidden' name='printableexport' /></center></form>";
     if ($printableexport == 'pdf') {
         require Yii::app()->getConfig('rootdir') . '/application/config/tcpdf.php';
         Yii::import('application.libraries.admin.pdf', true);
         $pdf = new pdf();
         $pdf->setConfig($tcpdf);
         //$pdf->SetFont($pdfdefaultfont,'',$pdffontsize);
         $pdf->AddPage();
         //$pdf->titleintopdf($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})");
         $pdf->SetTitle($clang->gT("Survey name (ID)", 'unescaped') . ": {$surveyname} ({$surveyid})");
     }
     $printoutput .= "\t<div class='printouttitle'><strong>" . $clang->gT("Survey name (ID):") . "</strong> {$surveyname} ({$surveyid})</div><p>&nbsp;\n";
     LimeExpressionManager::StartProcessingPage(true);
     // means that all variables are on the same page
     // Since all data are loaded, and don't need JavaScript, pretend all from Group 1
     LimeExpressionManager::StartProcessingGroup(1, $thissurvey['anonymized'] != "N", $surveyid);
     $aFullResponseTable = getFullResponseTable($surveyid, $id, $language, true);
     //Get the fieldmap @TODO: do we need to filter out some fields?
     unset($aFullResponseTable['id']);
     unset($aFullResponseTable['token']);
     unset($aFullResponseTable['lastpage']);
     unset($aFullResponseTable['startlanguage']);
     unset($aFullResponseTable['datestamp']);
     unset($aFullResponseTable['startdate']);
     $printoutput .= "<table class='printouttable' >\n";
     if ($printableexport == 'pdf') {
         $pdf->intopdf($clang->gT("Question", 'unescaped') . ": " . $clang->gT("Your answer", 'unescaped'));
     }
     $oldgid = 0;
     $oldqid = 0;
     foreach ($aFullResponseTable as $sFieldname => $fname) {
         if (substr($sFieldname, 0, 4) == 'gid_') {
             if ($printableexport) {
                 $pdf->intopdf(flattenText($fname[0], false, true));
                 $pdf->ln(2);
             } else {
                 $printoutput .= "\t<tr class='printanswersgroup'><td colspan='2'>{$fname[0]}</td></tr>\n";
             }
         } elseif (substr($sFieldname, 0, 4) == 'qid_') {
             if ($printableexport == 'pdf') {
                 $pdf->intopdf(flattenText($fname[0] . $fname[1], false, true) . ": " . $fname[2]);
                 $pdf->ln(2);
             } else {
                 $printoutput .= "\t<tr class='printanswersquestionhead'><td  colspan='2'>{$fname[0]}</td></tr>\n";
             }
         } elseif ($sFieldname == 'submitdate') {
             if ($anonymized != 'Y') {
                 if ($printableexport == 'pdf') {
                     $pdf->intopdf(flattenText($fname[0] . $fname[1], false, true) . ": " . $fname[2]);
                     $pdf->ln(2);
                 } else {
                     $printoutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]} {$sFieldname}</td><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
                 }
             }
         } else {
             if ($printableexport == 'pdf') {
                 $pdf->intopdf(flattenText($fname[0] . $fname[1], false, true) . ": " . $fname[2]);
                 $pdf->ln(2);
             } else {
                 $printoutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]}</td><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
             }
         }
     }
     $printoutput .= "</table>\n";
     if ($printableexport == 'pdf') {
         header("Pragma: public");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         $sExportFileName = sanitize_filename($surveyname);
         $pdf->Output($sExportFileName . "-" . $surveyid . ".pdf", "D");
     }
     //Display the page with user answers
     if (!$printableexport) {
         sendCacheHeaders();
         doHeader();
         echo templatereplace(file_get_contents(getTemplatePath($thistpl) . '/startpage.pstpl'));
         echo templatereplace(file_get_contents(getTemplatePath($thistpl) . '/printanswers.pstpl'), array('ANSWERTABLE' => $printoutput));
         echo templatereplace(file_get_contents(getTemplatePath($thistpl) . '/endpage.pstpl'));
         echo "</body></html>";
     }
     LimeExpressionManager::FinishProcessingGroup();
     LimeExpressionManager::FinishProcessingPage();
 }
コード例 #5
0
$getpdf = optional_param('getpdf', 0, PARAM_INT);
$fontfamily = optional_param('fontfamily', PDF_FONT_NAME_MAIN, PARAM_ALPHA);
// to be configurable
if (!$fontfamily) {
    $fontfamily = PDF_FONT_NAME_MAIN;
}
/**
 * Extend the standard PDF class to get access to some protected values we want to display
 * at the test page.
 *
 * @copyright 2009 David Mudrak <*****@*****.**>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
if ($getpdf) {
    $doc = new pdf();
    $doc->SetTitle('Moodle PDF library test');
    $doc->SetAuthor('Moodle ' . $CFG->release);
    $doc->SetCreator('lib/tests/other/pdflibtestpage.php');
    $doc->SetKeywords('Moodle, PDF');
    $doc->SetSubject('This has been generated by Moodle as its PDF library test page');
    $doc->SetMargins(15, 30);
    $doc->setPrintHeader(true);
    $doc->setHeaderMargin(10);
    $doc->setHeaderFont(array($fontfamily, 'b', 10));
    $doc->setHeaderData('pix/moodlelogo-med-white.gif', 40, $SITE->fullname, $CFG->wwwroot);
    $doc->setPrintFooter(true);
    $doc->setFooterMargin(10);
    $doc->setFooterFont(array($fontfamily, '', 8));
    $doc->AddPage();
    $doc->SetTextColor(255, 255, 255);
    $doc->SetFillColor(255, 203, 68);
コード例 #6
0
/**
 * Generate the PDF for the specified customcert and user.
 *
 * @param stdClass $customcert
 * @param bool $preview true if it is a preview, false otherwise
 */
function customcert_generate_pdf($customcert, $preview = false)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/pdflib.php';
    // Get the pages for the customcert, there should always be at least one page for each customcert.
    if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $customcert->id), 'pagenumber ASC')) {
        // Create the pdf object.
        $pdf = new pdf();
        if (!empty($customcert->protection)) {
            $protection = explode(', ', $customcert->protection);
            $pdf->SetProtection($protection);
        }
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $pdf->SetTitle($customcert->name);
        $pdf->SetAutoPageBreak(true, 0);
        // Remove full-stop at the end, if it exists, to avoid "..pdf" being created and being filtered by clean_filename.
        $filename = rtrim($customcert->name, '.');
        $filename = clean_filename($filename . '.pdf');
        // Loop through the pages and display their content.
        foreach ($pages as $page) {
            // Add the page to the PDF.
            if ($page->width > $page->height) {
                $orientation = 'L';
            } else {
                $orientation = 'P';
            }
            $pdf->AddPage($orientation, array($page->width, $page->height));
            $pdf->SetMargins(0, 0, $page->margin);
            // Get the elements for the page.
            if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
                // Loop through and display.
                foreach ($elements as $element) {
                    // Get an instance of the element class.
                    if ($e = customcert_get_element_instance($element)) {
                        $e->render($pdf, $preview);
                    }
                }
            }
        }
        $pdf->Output($filename, 'D');
    }
}
コード例 #7
0
<?php

require_once 'config.php';
define('ID_LANG', 1);
if (isset($_GET['id'])) {
    $booking = Booking::byId($_GET['id']);
    $user = User::byId($booking->id_user);
    $room = Room::byId($booking->id_room);
    // HEADER
    // create a PDF object
    $pdf = new pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // set document (meta) information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Hotel Plaza Nueva');
    $pdf->SetTitle('Hotel Plaza Nueva');
    $pdf->SetSubject('Hotel Plaza Nueva');
    $pdf->SetKeywords('hotel, plaza, nueva, granada');
    // add a page
    $pdf->AddPage();
    // create address box
    $pdf->CreateTextBox($user->name, 0, 55, 80, 10, 10, 'B');
    $pdf->CreateTextBox($user->email, 0, 60, 80, 10, 10);
    $pdf->CreateTextBox($user->phone, 0, 65, 80, 10, 10);
    //$pdf->CreateTextBox('Zip, city name', 0, 70, 80, 10, 10);
    // invoice title / number
    $pdf->CreateTextBox('Factura #' . $booking->id, 0, 90, 120, 20, 16);
    // date, order ref
    $date = date_create($booking->date_booking);
    $pdf->CreateTextBox('Fecha: ' . date_format($date, "d/m/Y H:i:s"), 0, 100, 0, 10, 10, '', 'R');
    $pdf->CreateTextBox('Order ref.: #' . $booking->id, 0, 105, 0, 10, 10, '', 'R');
    // ROWS