PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/controllers/single_pages/dashboard/reports/forms.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 254 lines | 210 code | 31 blank | 13 comment | 33 complexity | 9a078f6572b8db3beaf6eb01e465fe6f MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. Loader::block('form');
  3. class Concrete5_Controller_Dashboard_Reports_Forms extends DashboardBaseController {
  4. protected $pageSize=10;
  5. public function view(){
  6. if($_REQUEST['all']){
  7. $this->pageSize=100000;
  8. $_REQUEST['page']=1;
  9. }
  10. $this->loadSurveyResponses();
  11. }
  12. public function excel(){
  13. $dateHelper = Loader::helper('date');
  14. $this->pageSize=0;
  15. $this->loadSurveyResponses();
  16. $textHelper = Loader::helper('text');
  17. $questionSet=$this->get('questionSet');
  18. $answerSets=$this->get('answerSets');
  19. $questions=$this->get('questions');
  20. $surveys=$this->get('surveys');
  21. $fileName=$textHelper->filterNonAlphaNum($surveys[$questionSet]['surveyName']);
  22. header("Content-Type: application/vnd.ms-excel");
  23. header("Cache-control: private");
  24. header("Pragma: public");
  25. $date = date('Ymd');
  26. header("Content-Disposition: inline; filename=".$fileName."_form_data_{$date}.xls");
  27. header("Content-Title: ".$surveys[$questionSet]['surveyName']." Form Data Output - Run on {$date}"); echo "<html>\r\n";
  28. echo "<head><META http-equiv=Content-Type content=\"text/html; charset=".APP_CHARSET."\"></head>\r\n";
  29. echo "<body>\r\n";
  30. echo "<table>\r\n";
  31. $hasCBRow = false;
  32. foreach($questions as $questionId=>$question){
  33. if ($question['inputType'] == 'checkboxlist') {
  34. $hasCBRow = true;
  35. }
  36. }
  37. echo "<tr>";
  38. echo "\t\t<td ";
  39. if ($hasCBRow) {
  40. echo "rowspan=\"2\" valign='bottom'";
  41. }
  42. echo "><b>Submitted Date</b></td>\r\n";
  43. foreach($questions as $questionId=>$question){
  44. if ($question['inputType'] == 'checkboxlist')
  45. {
  46. $options = explode('%%', $question['options']);
  47. echo "\t\t".'<td colspan="'.count($options).'"><b>'."\r\n";
  48. }
  49. else
  50. {
  51. echo "\t\t<td ";
  52. if ($hasCBRow) {
  53. echo "rowspan=\"2\" valign='bottom'>";
  54. }
  55. echo "<b>\r\n";
  56. }
  57. echo "\t\t\t".$questions[$questionId]['question']."\r\n";
  58. echo "\t\t</b></td>\r\n";
  59. }
  60. echo "</tr>";
  61. // checkbox row
  62. if ($hasCBRow) {
  63. echo "<tr>";
  64. foreach($questions as $questionId=>$question){
  65. if ($question['inputType'] == 'checkboxlist')
  66. {
  67. $options = explode('%%', $question['options']);
  68. foreach($options as $opt) {
  69. echo "<td><b>{$opt}</b></td>";
  70. }
  71. }
  72. }
  73. echo "</tr>";
  74. }
  75. foreach($answerSets as $answerSetId=>$answerSet){
  76. $questionNumber=0;
  77. $numQuestionsToShow=2;
  78. echo "\t<tr>\r\n";
  79. echo "\t\t<td>". $dateHelper->getSystemDateTime($answerSet['created'])."</td>\r\n";
  80. foreach($questions as $questionId=>$question){
  81. $questionNumber++;
  82. if ($question['inputType'] == 'checkboxlist'){
  83. $options = explode('%%', $question['options']);
  84. $subanswers = explode(',', $answerSet['answers'][$questionId]['answer']);
  85. for ($i = 1; $i <= count($options); $i++)
  86. {
  87. echo "\t\t<td align='center'>\r\n";
  88. if (in_array(trim($options[$i-1]), $subanswers)) {
  89. // echo "\t\t\t".$options[$i-1]."\r\n";
  90. echo "x";
  91. } else {
  92. echo "\t\t\t&nbsp;\r\n";
  93. }
  94. echo "\t\t</td>\r\n";
  95. }
  96. }elseif($question['inputType']=='fileupload'){
  97. echo "\t\t<td>\r\n";
  98. $fID=intval($answerSet['answers'][$questionId]['answer']);
  99. $file=File::getByID($fID);
  100. if($fID && $file){
  101. $fileVersion=$file->getApprovedVersion();
  102. echo "\t\t\t".'<a href="'. $fileVersion->getDownloadURL() .'">'.$fileVersion->getFileName().'</a>'."\r\n";
  103. }else{
  104. echo "\t\t\t".t('File not found')."\r\n";
  105. }
  106. echo "\t\t</td>\r\n";
  107. }else{
  108. echo "\t\t<td>\r\n";
  109. echo "\t\t\t".$answerSet['answers'][$questionId]['answer'].$answerSet['answers'][$questionId]['answerLong']."\r\n";
  110. echo "\t\t</td>\r\n";
  111. }
  112. }
  113. echo "\t</tr>\r\n";
  114. }
  115. echo "</table>\r\n";
  116. echo "</body>\r\n";
  117. echo "</html>\r\n";
  118. die;
  119. }
  120. private function loadSurveyResponses(){
  121. $c=$this->getCollectionObject();
  122. $db = Loader::db();
  123. $tempMiniSurvey = new MiniSurvey();
  124. $pageBase = DIR_REL . '/' . DISPATCHER_FILENAME . '?cID=' . $c->getCollectionID();
  125. if( $_REQUEST['action'] == 'deleteForm' ){
  126. if (!Loader::helper('validation/token')->validate('deleteForm')) {
  127. $this->error->add(t('Invalid Token.'));
  128. } else {
  129. $this->deleteForm($_REQUEST['bID'], $_REQUEST['qsID']);
  130. }
  131. }
  132. if( $_REQUEST['action'] == 'deleteFormAnswers' ){
  133. if (!Loader::helper('validation/token')->validate('deleteFormAnswers')) {
  134. $this->error->add(t('Invalid Token.'));
  135. } else {
  136. $this->deleteFormAnswers($_REQUEST['qsID']);
  137. $this->redirect('/dashboard/reports/forms');
  138. }
  139. }
  140. if( $_REQUEST['action'] == 'deleteResponse' ){
  141. if (!Loader::helper('validation/token')->validate('deleteResponse')) {
  142. $this->error->add(t('Invalid Token.'));
  143. } else {
  144. $this->deleteAnswers($_REQUEST['asid']);
  145. }
  146. }
  147. //load surveys
  148. $surveysRS=FormBlockStatistics::loadSurveys($tempMiniSurvey);
  149. //index surveys by question set id
  150. $surveys=array();
  151. while($survey=$surveysRS->fetchRow()){
  152. //get Survey Answers
  153. $survey['answerSetCount'] = MiniSurvey::getAnswerCount( $survey['questionSetId'] );
  154. $surveys[ $survey['questionSetId'] ] = $survey;
  155. }
  156. //load requested survey response
  157. if (!empty($_REQUEST['qsid'])) {
  158. $questionSet = intval(preg_replace('/[^[:alnum:]]/','',$_REQUEST['qsid']));
  159. //get Survey Questions
  160. $questionsRS = MiniSurvey::loadQuestions($questionSet);
  161. $questions = array();
  162. while( $question = $questionsRS->fetchRow() ){
  163. $questions[$question['msqID']]=$question;
  164. }
  165. //get Survey Answers
  166. $answerSetCount = MiniSurvey::getAnswerCount($questionSet);
  167. //pagination
  168. $pageBaseSurvey = $pageBase.'&qsid='.$questionSet;
  169. $paginator = Loader::helper('pagination');
  170. $sortBy = $_REQUEST['sortBy'];
  171. $paginator->init(
  172. (int) $_REQUEST['page'],
  173. $answerSetCount,
  174. $pageBaseSurvey.'&page=%pageNum%&sortBy='.$sortBy,
  175. $this->pageSize
  176. );
  177. if ($this->pageSize>0) {
  178. $limit = $paginator->getLIMIT();
  179. } else {
  180. $limit = '';
  181. }
  182. $answerSets = FormBlockStatistics::buildAnswerSetsArray( $questionSet, $sortBy, $limit );
  183. }
  184. $this->set('questions',$questions);
  185. $this->set('answerSets',$answerSets);
  186. $this->set('paginator',$paginator);
  187. $this->set('questionSet',$questionSet);
  188. $this->set('surveys',$surveys);
  189. }
  190. // SET UP DELETE FUNCTIONS HERE
  191. // DELETE SUBMISSIONS
  192. private function deleteAnswers($asID){
  193. $db = Loader::db();
  194. $v = array(intval($asID));
  195. $q = 'DELETE FROM btFormAnswers WHERE asID = ?';
  196. $r = $db->query($q, $v);
  197. $q = 'DELETE FROM btFormAnswerSet WHERE asID = ?';
  198. $r = $db->query($q, $v);
  199. }
  200. //DELETE A FORM ANSWERS
  201. private function deleteFormAnswers($qsID){
  202. $db = Loader::db();
  203. $v = array(intval($qsID));
  204. $q = 'SELECT asID FROM btFormAnswerSet WHERE questionSetId = ?';
  205. $r = $db->query($q, $v);
  206. while ($row = $r->fetchRow()) {
  207. $asID = $row['asID'];
  208. $this->deleteAnswers($asID);
  209. }
  210. }
  211. //DELETE FORMS AND ALL SUBMISSIONS
  212. private function deleteForm($bID, $qsID){
  213. $db = Loader::db();
  214. $this->deleteFormAnswers($qsID);
  215. $v = array(intval($bID));
  216. $q = 'DELETE FROM btFormQuestions WHERE bID = ?';
  217. $r = $db->query($q, $v);
  218. $q = 'DELETE FROM btForm WHERE bID = ?';
  219. $r = $db->query($q, $v);
  220. $q = 'DELETE FROM Blocks WHERE bID = ?';
  221. $r = $db->query($q, $v);
  222. }
  223. }