Index: trunk/extensions/Survey/Survey.i18n.php |
— | — | @@ -25,4 +25,7 @@ |
26 | 26 | |
27 | 27 | 'special-surveys' => 'Surveys admin', |
28 | 28 | 'special-surveystats' => 'Survey statistics', |
| 29 | + |
| 30 | + 'survey-err-id-xor-name' => 'You need to provide either the id or the name of the survey to submit', |
| 31 | + 'survey-err-survey-name-unknown' => 'There is no survey with the name "$1"' |
29 | 32 | ); |
Index: trunk/extensions/Survey/Survey.php |
— | — | @@ -54,7 +54,9 @@ |
55 | 55 | $wgAutoloadClasses['ApiSubmitSurvey'] = dirname( __FILE__ ) . '/api/ApiSubmitSurvey.php'; |
56 | 56 | |
57 | 57 | $wgAutoloadClasses['Survey'] = dirname( __FILE__ ) . '/includes/Survey.class.php'; |
| 58 | +$wgAutoloadClasses['SurveyAnswer'] = dirname( __FILE__ ) . '/includes/SurveyAnswer.php'; |
58 | 59 | $wgAutoloadClasses['SurveyQuestion'] = dirname( __FILE__ ) . '/includes/SurveyQuestion.php'; |
| 60 | +$wgAutoloadClasses['SurveySubmission'] = dirname( __FILE__ ) . '/includes/SurveySubmission.php'; |
59 | 61 | |
60 | 62 | $wgAutoloadClasses['SpecialSurveys'] = dirname( __FILE__ ) . '/specials/SpecialSurveys.php'; |
61 | 63 | $wgAutoloadClasses['SpecialSurveyStats'] = dirname( __FILE__ ) . '/specials/SpecialSurveyStats.php'; |
Index: trunk/extensions/Survey/includes/SurveySubmission.php |
— | — | @@ -0,0 +1,157 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Simple survey submission object class. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file SurveySubmission.php |
| 10 | + * @ingroup Survey |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class SurveySubmission { |
| 16 | + |
| 17 | + /** |
| 18 | + * The ID of the question DB record, or null if not inserted yet. |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * @var integer|null |
| 22 | + */ |
| 23 | + protected $id; |
| 24 | + |
| 25 | + /** |
| 26 | + * The ID of the survey this submission is for. |
| 27 | + * |
| 28 | + * @since 0.1 |
| 29 | + * @var integer |
| 30 | + */ |
| 31 | + protected $surveyId; |
| 32 | + |
| 33 | + /** |
| 34 | + * The ID of the page this submission was made on. |
| 35 | + * |
| 36 | + * @since 0.1 |
| 37 | + * @var integer |
| 38 | + */ |
| 39 | + protected $pageId; |
| 40 | + |
| 41 | + /** |
| 42 | + * The name of the user that made the submission (username or ip). |
| 43 | + * |
| 44 | + * @since 0.1 |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + protected $userName; |
| 48 | + |
| 49 | + /** |
| 50 | + * Timestamp idnicating when the submission was made. |
| 51 | + * |
| 52 | + * @since 0.1 |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + protected $timeStamp; |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Constructor. |
| 60 | + * |
| 61 | + * @since 0.1 |
| 62 | + * |
| 63 | + * @param integer|null $id |
| 64 | + * @param integer $surveyId |
| 65 | + * @param string $userName |
| 66 | + * @param integer $pageId |
| 67 | + * @param string $timeStamp |
| 68 | + */ |
| 69 | + public function __construct( $id, $surveyId, $userName, $pageId, $timeStamp ) { |
| 70 | + $this->id = $id; |
| 71 | + $this->surveyId = $surveyId; |
| 72 | + $this->userName = $userName; |
| 73 | + $this->pageId = $pageId; |
| 74 | + $this->timeStamp = $timeStamp; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Writes the submission to the database, either updating it |
| 79 | + * when it already exists, or inserting it when it doesn't. |
| 80 | + * |
| 81 | + * @since 0.1 |
| 82 | + * |
| 83 | + * @return boolean Success indicator |
| 84 | + */ |
| 85 | + public function writeToDB() { |
| 86 | + if ( is_null( $this->id ) ) { |
| 87 | + return $this->insertIntoDB(); |
| 88 | + } |
| 89 | + else { |
| 90 | + return $this->updateInDB(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Updates the submission in the database. |
| 96 | + * |
| 97 | + * @since 0.1 |
| 98 | + * |
| 99 | + * @return boolean Success indicator |
| 100 | + */ |
| 101 | + protected function updateInDB() { |
| 102 | + $dbr = wfGetDB( DB_MASTER ); |
| 103 | + |
| 104 | + return $dbr->update( |
| 105 | + 'survey_sumissions', |
| 106 | + $this->getWriteValues(), |
| 107 | + array( 'submission_id' => $this->id ) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Inserts the survey into the database. |
| 113 | + * |
| 114 | + * @since 0.1 |
| 115 | + * |
| 116 | + * @return boolean Success indicator |
| 117 | + */ |
| 118 | + protected function insertIntoDB() { |
| 119 | + $dbr = wfGetDB( DB_MASTER ); |
| 120 | + |
| 121 | + $result = $dbr->insert( |
| 122 | + 'survey_sumissions', |
| 123 | + $this->getWriteValues() |
| 124 | + ); |
| 125 | + |
| 126 | + $this->id = $dbr->insertId(); |
| 127 | + |
| 128 | + return $result; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets the fields => values to write to the survey_sumissions table. |
| 133 | + * |
| 134 | + * @since 0.1 |
| 135 | + * |
| 136 | + * @return array |
| 137 | + */ |
| 138 | + protected function getWriteValues() { |
| 139 | + return array( |
| 140 | + 'submission_survey_id' => $this->surveyId, |
| 141 | + 'submission_user_name' => $this->userName, |
| 142 | + 'submission_page_id' => $this->pageId, |
| 143 | + 'submission_time' => $this->timeStamp, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the submission database id. |
| 149 | + * |
| 150 | + * @since 0.1 |
| 151 | + * |
| 152 | + * @return integer|null |
| 153 | + */ |
| 154 | + public function getId() { |
| 155 | + return $this->id; |
| 156 | + } |
| 157 | + |
| 158 | +} |
Property changes on: trunk/extensions/Survey/includes/SurveySubmission.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 159 | + native |
Index: trunk/extensions/Survey/includes/SurveyAnswer.php |
— | — | @@ -0,0 +1,145 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Simple survey submission object class. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file SurveySubmission.php |
| 10 | + * @ingroup Survey |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class SurveyAnswer { |
| 16 | + |
| 17 | + /** |
| 18 | + * The ID of the question DB record, or null if not inserted yet. |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * @var integer|null |
| 22 | + */ |
| 23 | + protected $id; |
| 24 | + |
| 25 | + /** |
| 26 | + * The answer text. |
| 27 | + * |
| 28 | + * @since 0.1 |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $text; |
| 32 | + |
| 33 | + /** |
| 34 | + * The ID of the submission this answer is part of. |
| 35 | + * |
| 36 | + * @since 0.1 |
| 37 | + * @var integer |
| 38 | + */ |
| 39 | + protected $submissionId; |
| 40 | + |
| 41 | + /** |
| 42 | + * The ID of the question this answer corresponds to. |
| 43 | + * |
| 44 | + * @since 0.1 |
| 45 | + * @var integer |
| 46 | + */ |
| 47 | + protected $questionId; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor. |
| 51 | + * |
| 52 | + * @since 0.1 |
| 53 | + * |
| 54 | + * @param integer|null $id |
| 55 | + * @param integer $submissionId |
| 56 | + * @param integer $questionId |
| 57 | + * @param string $text |
| 58 | + */ |
| 59 | + public function __construct( $id, $submissionId, $questionId, $text ) { |
| 60 | + $this->id = $id; |
| 61 | + $this->submissionId = $submissionId; |
| 62 | + $this->questionId = $questionId; |
| 63 | + $this->text = $text; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Writes the answer to the database, either updating it |
| 68 | + * when it already exists, or inserting it when it doesn't. |
| 69 | + * |
| 70 | + * @since 0.1 |
| 71 | + * |
| 72 | + * @return boolean Success indicator |
| 73 | + */ |
| 74 | + public function writeToDB() { |
| 75 | + if ( is_null( $this->id ) ) { |
| 76 | + return $this->insertIntoDB(); |
| 77 | + } |
| 78 | + else { |
| 79 | + return $this->updateInDB(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Updates the answer in the database. |
| 85 | + * |
| 86 | + * @since 0.1 |
| 87 | + * |
| 88 | + * @return boolean Success indicator |
| 89 | + */ |
| 90 | + protected function updateInDB() { |
| 91 | + $dbr = wfGetDB( DB_MASTER ); |
| 92 | + |
| 93 | + return $dbr->update( |
| 94 | + 'survey_answers', |
| 95 | + $this->getWriteValues(), |
| 96 | + array( 'answer_id' => $this->id ) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Inserts the answer into the database. |
| 102 | + * |
| 103 | + * @since 0.1 |
| 104 | + * |
| 105 | + * @return boolean Success indicator |
| 106 | + */ |
| 107 | + protected function insertIntoDB() { |
| 108 | + $dbr = wfGetDB( DB_MASTER ); |
| 109 | + |
| 110 | + $result = $dbr->insert( |
| 111 | + 'survey_answers', |
| 112 | + $this->getWriteValues() |
| 113 | + ); |
| 114 | + |
| 115 | + $this->id = $dbr->insertId(); |
| 116 | + |
| 117 | + return $result; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Gets the fields => values to write to the survey_answers table. |
| 122 | + * |
| 123 | + * @since 0.1 |
| 124 | + * |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + protected function getWriteValues() { |
| 128 | + return array( |
| 129 | + 'answer_submission_id' => $this->submissionId, |
| 130 | + 'answer_question_id' => $this->questionId, |
| 131 | + 'answer_text' => $this->text, |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns the answer database id. |
| 137 | + * |
| 138 | + * @since 0.1 |
| 139 | + * |
| 140 | + * @return integer|null |
| 141 | + */ |
| 142 | + public function getId() { |
| 143 | + return $this->id; |
| 144 | + } |
| 145 | + |
| 146 | +} |
Property changes on: trunk/extensions/Survey/includes/SurveyAnswer.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 147 | + native |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -106,6 +106,15 @@ |
107 | 107 | return self::newFromArray( $args ); |
108 | 108 | } |
109 | 109 | |
| 110 | + /** |
| 111 | + * Unserializes a survey question in the form of an associative array. |
| 112 | + * |
| 113 | + * @since 0.1 |
| 114 | + * |
| 115 | + * @param array $args |
| 116 | + * |
| 117 | + * @return SurveyQuestion |
| 118 | + */ |
110 | 119 | public static function newFromArray( array $args ) { |
111 | 120 | return new self( |
112 | 121 | array_key_exists( 'id', $args ) ? $args['id'] : null, |
— | — | @@ -129,6 +138,14 @@ |
130 | 139 | return str_replace( '=', '!', base64_encode( FormatJson::encode( $this->toArray() ) ) ); |
131 | 140 | } |
132 | 141 | |
| 142 | + /** |
| 143 | + * Serializes the survey question to an associative array which |
| 144 | + * can then easily be converted into JSON or similar. |
| 145 | + * |
| 146 | + * @since 0.1 |
| 147 | + * |
| 148 | + * @return array |
| 149 | + */ |
133 | 150 | public function toArray() { |
134 | 151 | $args = array( |
135 | 152 | 'text' => $this->text, |
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php |
— | — | @@ -27,10 +27,31 @@ |
28 | 28 | |
29 | 29 | $params = $this->extractRequestParams(); |
30 | 30 | |
| 31 | + if ( !( isset( $params['id'] ) XOR isset( $params['name'] ) ) ) { |
| 32 | + $this->dieUsage( wfMsgExt( 'survey-err-id-xor-name' ), 'id-xor-name' ); |
| 33 | + } |
| 34 | + |
| 35 | + if ( isset( $params['name'] ) ) { |
| 36 | + $survey = Survey::newFromName( $params['name'], false ); |
| 37 | + |
| 38 | + if ( $survey === false ) { |
| 39 | + $this->dieUsage( wfMsgExt( 'survey-err-survey-name-unknown', 'parsemag', $params['name'] ), 'survey-name-unknown' ); |
| 40 | + } |
| 41 | + |
| 42 | + $params['id'] = $survey->getId(); |
| 43 | + } |
| 44 | + |
| 45 | + $submission = new SurveySubmission(); |
31 | 46 | } |
32 | 47 | |
33 | 48 | public function getAllowedParams() { |
34 | 49 | return array( |
| 50 | + 'id' => array( |
| 51 | + ApiBase::PARAM_TYPE => 'integer', |
| 52 | + ), |
| 53 | + 'name' => array( |
| 54 | + ApiBase::PARAM_TYPE => 'string', |
| 55 | + ), |
35 | 56 | ); |
36 | 57 | } |
37 | 58 | |
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php |
— | — | @@ -35,6 +35,9 @@ |
36 | 36 | |
37 | 37 | foreach ( $params['ids'] as $surveyId ) { |
38 | 38 | $survey = Survey::newFromId( $surveyId, $params['incquestions'] == 1 )->toArray(); |
| 39 | + foreach ( $survey['questions'] as $nr => $question ) { |
| 40 | + $this->getResult()->setIndexedTagName( $survey['questions'][$nr], 'answer' ); |
| 41 | + } |
39 | 42 | $this->getResult()->setIndexedTagName( $survey['questions'], 'question' ); |
40 | 43 | $surveys[] = $survey; |
41 | 44 | } |