PageRenderTime 21ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/survey/classes/ezsurveyentry.php

https://bitbucket.org/netgen/ezsurvey
PHP | 181 lines | 127 code | 20 blank | 34 comment | 18 complexity | 5b86039456345a277e1a3f748c3ff829 MD5 | raw file
  1. <?php
  2. //
  3. // Created on: <02-Apr-2004 00:00:00 Jan Kudlicka>
  4. //
  5. // Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
  6. //
  7. // This source file is part of the eZ Publish (tm) Open Source Content
  8. // Management System.
  9. //
  10. // This file may be distributed and/or modified under the terms of the
  11. // "GNU General Public License" version 2 as published by the Free
  12. // Software Foundation and appearing in the file LICENSE.GPL included in
  13. // the packaging of this file.
  14. //
  15. // Licencees holding valid "eZ Publish professional licences" may use this
  16. // file in accordance with the "eZ Publish professional licence" Agreement
  17. // provided with the Software.
  18. //
  19. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
  20. // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. // PURPOSE.
  22. //
  23. // The "eZ Publish professional licence" is available at
  24. // http://ez.no/products/licences/professional/. For pricing of this licence
  25. // please contact us via e-mail to licence@ez.no. Further contact
  26. // information is available at http://ez.no/home/contact/.
  27. //
  28. // The "GNU General Public License" (GPL) is available at
  29. // http://www.gnu.org/copyleft/gpl.html.
  30. //
  31. // Contact licence@ez.no if any conditions of this licencing isn't clear to
  32. // you.
  33. //
  34. /*! \file ezsurveyentry.php
  35. */
  36. class eZSurveyEntry extends eZSurveyQuestion
  37. {
  38. function eZSurveyEntry( $row = false )
  39. {
  40. $this->eZSurveyQuestion( $row );
  41. }
  42. function processViewActions( &$validation, $params )
  43. {
  44. $http = eZHTTPTool::instance();
  45. $variableArray = array();
  46. $prefix = eZSurveyType::PREFIX_ATTRIBUTE;
  47. $attributeID = $params['contentobjectattribute_id'];
  48. $postSurveyAnswer = $prefix . '_ezsurvey_answer_' . $this->ID . '_' . $attributeID;
  49. if ( $this->attribute( 'mandatory' ) == 1 and strlen( trim ( $http->postVariable( $postSurveyAnswer ) ) ) == 0 )
  50. {
  51. $validation['error'] = true;
  52. $validation['errors'][] = array( 'message' => ezpI18n::tr( 'survey', 'Please answer the question %number as well!', null,
  53. array( '%number' => $this->questionNumber() ) ),
  54. 'question_number' => $this->questionNumber(),
  55. 'code' => 'general_answer_number_as_well',
  56. 'question' => $this );
  57. }
  58. $this->setAnswer( trim ( $http->postVariable( $postSurveyAnswer ) ) );
  59. $variableArray['answer'] = trim ( $http->postVariable( $postSurveyAnswer ) );
  60. return $variableArray;
  61. }
  62. function result()
  63. {
  64. $surveyID = $this->attribute( 'survey_id' );
  65. $survey = eZSurvey::fetch( $surveyID );
  66. $contentObjectID = $survey->attribute( 'contentobject_id' );
  67. $contentClassAttributeID = $survey->attribute( 'contentclassattribute_id' );
  68. $languageCode = $survey->attribute( 'language_code' );
  69. $result = eZSurveyEntry::fetchResult( $this, $contentObjectID, $contentClassAttributeID, $languageCode, false, 5 );
  70. return $result['result'];
  71. }
  72. function fetchResult( $question, $contentObjectID, $contentClassAttributeID, $languageCode, $metadata = false, $limit = false, $sortBy = false )
  73. {
  74. $db = eZDB::instance();
  75. $resultArray = array();
  76. $questionOriginalID = $question->attribute( 'original_id' );
  77. $query = 'SELECT text as value FROM ezsurveyquestionresult';
  78. if ( $metadata != false )
  79. {
  80. for( $index = 1; $index <= count( $metadata ); $index++ )
  81. {
  82. $query .= ', ezsurveymetadata as m';
  83. $query .= $index;
  84. }
  85. }
  86. $query .= ' where questionoriginal_id=\'';
  87. $query .= $question->attribute( 'original_id' );
  88. $query .= '\' and length(text)>0';
  89. $index = 0;
  90. if ( $metadata != false )
  91. {
  92. foreach ( array_keys( $metadata ) as $key )
  93. {
  94. $index++;
  95. if ( $index == 1 )
  96. $query .= ' and ezsurveyquestionresult.result_id=m1.result_id';
  97. else
  98. {
  99. $query .= ' and m';
  100. $query .= ( $index - 1 );
  101. $query .= '.result_id=m';
  102. $query .= $index;
  103. $query .= '.result_id';
  104. }
  105. $query .= ' and m';
  106. $query .= $index;
  107. $query .= '.attr_name=\'';
  108. $query .= $key;
  109. $query .= '\' and m';
  110. $query .= $index;
  111. $query .= '.attr_value=\'';
  112. $query .= $metadata[$key];
  113. $query .= '\'';
  114. }
  115. }
  116. if ( is_array( $sortBy ) )
  117. {
  118. $sortField = 'text';
  119. if ( isSet( $sortBy[0] ) )
  120. $sortField = $sortBy[0];
  121. switch ( $sortField )
  122. {
  123. case 'text':
  124. {
  125. $sortingFields = 'ezsurveyquestionresult.text';
  126. } break;
  127. default:
  128. {
  129. eZDebug::writeWarning( 'Unknown sort field: ' . $sortField, 'eZSurveyEntry::fetchResult' );
  130. continue;
  131. };
  132. }
  133. $sortOrder = true;
  134. if ( isSet( $sortBy[1] ) )
  135. $sortOrder = $sortBy[1];
  136. $sortingFields .= $sortOrder ? " ASC" : " DESC";
  137. $query .= " ORDER BY $sortingFields";
  138. }
  139. else
  140. {
  141. $query .= ' ORDER BY ezsurveyquestionresult.id DESC';
  142. }
  143. $queryParams = array();
  144. if ( $limit != false && $limit != 0 )
  145. $queryParams['limit'] = $limit;
  146. $rows = $db->arrayQuery( $query, $queryParams );
  147. $result = array( 'result' => $rows );
  148. return $result;
  149. }
  150. function &fetchResultItem( $question, $result_id, $metadata = false )
  151. {
  152. $result = eZPersistentObject::fetchObject( eZSurveyQuestionResult::definition(),
  153. 'text',
  154. array( 'question_id' => $question->attribute( 'id' ),
  155. 'result_id' => $result_id ),
  156. false );
  157. $returnArray = array( 'result' => $result['text'] );
  158. return $returnArray;
  159. }
  160. }
  161. ?>