Index: trunk/extensions/Survey/sql/Survey_indexSurveyName.sql |
— | — | @@ -0,0 +1 @@ |
| 2 | +CREATE UNIQUE INDEX /*i*/surveys_survey_name ON /*_*/surveys (survey_name); |
\ No newline at end of file |
Property changes on: trunk/extensions/Survey/sql/Survey_indexSurveyName.sql |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 3 | + native |
Index: trunk/extensions/Survey/Survey.hooks.php |
— | — | @@ -25,32 +25,37 @@ |
26 | 26 | public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) { |
27 | 27 | global $wgDBtype; |
28 | 28 | |
29 | | - if ( $wgDBtype == 'mysql' ) { |
30 | | - $updater->addExtensionUpdate( array( |
31 | | - 'addTable', |
32 | | - 'surveys', |
33 | | - dirname( __FILE__ ) . '/Survey.sql', |
34 | | - true |
35 | | - ) ); |
36 | | - $updater->addExtensionUpdate( array( |
37 | | - 'addTable', |
38 | | - 'survey_questions', |
39 | | - dirname( __FILE__ ) . '/Survey.sql', |
40 | | - true |
41 | | - ) ); |
42 | | - $updater->addExtensionUpdate( array( |
43 | | - 'addTable', |
44 | | - 'survey_submissions', |
45 | | - dirname( __FILE__ ) . '/Survey.sql', |
46 | | - true |
47 | | - ) ); |
48 | | - $updater->addExtensionUpdate( array( |
49 | | - 'addTable', |
50 | | - 'survey_answers', |
51 | | - dirname( __FILE__ ) . '/Survey.sql', |
52 | | - true |
53 | | - ) ); |
54 | | - } |
| 29 | + $updater->addExtensionUpdate( array( |
| 30 | + 'addTable', |
| 31 | + 'surveys', |
| 32 | + dirname( __FILE__ ) . '/Survey.sql', |
| 33 | + true |
| 34 | + ) ); |
| 35 | + $updater->addExtensionUpdate( array( |
| 36 | + 'addTable', |
| 37 | + 'survey_questions', |
| 38 | + dirname( __FILE__ ) . '/Survey.sql', |
| 39 | + true |
| 40 | + ) ); |
| 41 | + $updater->addExtensionUpdate( array( |
| 42 | + 'addTable', |
| 43 | + 'survey_submissions', |
| 44 | + dirname( __FILE__ ) . '/Survey.sql', |
| 45 | + true |
| 46 | + ) ); |
| 47 | + $updater->addExtensionUpdate( array( |
| 48 | + 'addTable', |
| 49 | + 'survey_answers', |
| 50 | + dirname( __FILE__ ) . '/Survey.sql', |
| 51 | + true |
| 52 | + ) ); |
| 53 | + $updater->addExtensionUpdate( array( |
| 54 | + 'addIndex', |
| 55 | + 'surveys', |
| 56 | + 'surveys_survey_name', |
| 57 | + dirname( __FILE__ ) . '/sql/Survey_indexSurveyName.sql', |
| 58 | + true |
| 59 | + ) ); |
55 | 60 | |
56 | 61 | return true; |
57 | 62 | } |
Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -13,10 +13,38 @@ |
14 | 14 | */ |
15 | 15 | class Survey { |
16 | 16 | |
| 17 | + /** |
| 18 | + * The ID of the surveys DB record, or null if not inserted yet. |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * @var integer|null |
| 22 | + */ |
17 | 23 | protected $id; |
| 24 | + |
| 25 | + /** |
| 26 | + * The name of the survey. |
| 27 | + * |
| 28 | + * @since 0.1 |
| 29 | + * @var string |
| 30 | + */ |
18 | 31 | protected $name; |
| 32 | + |
| 33 | + /** |
| 34 | + * If the survey is enabled or not. |
| 35 | + * |
| 36 | + * @since 0.1 |
| 37 | + * @var boolean |
| 38 | + */ |
19 | 39 | protected $enabled; |
20 | 40 | |
| 41 | + /** |
| 42 | + * The questions that go with this survey. |
| 43 | + * |
| 44 | + * @since 0.1 |
| 45 | + * @var array of SurveyQuestion |
| 46 | + */ |
| 47 | + protected $questions; |
| 48 | + |
21 | 49 | public static function newFromDBResult() { |
22 | 50 | |
23 | 51 | } |
— | — | @@ -29,9 +57,114 @@ |
30 | 58 | * @param integer|null $id |
31 | 59 | * @param string $name |
32 | 60 | * @param boolean $enabled |
| 61 | + * @param array $questions |
33 | 62 | */ |
34 | | - public function __construct( $id, $name, $enabled ) { |
| 63 | + public function __construct( $id, $name, $enabled, array $questions ) { |
| 64 | + $this->id = $id; |
| 65 | + $this->name = $name; |
| 66 | + $this->enabled = $enabled; |
| 67 | + $this->questions = $questions; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Writes the survey to the database, either updating it |
| 72 | + * when it already exists, or inserting it when it doesn't. |
| 73 | + * |
| 74 | + * @since 0.1 |
| 75 | + * |
| 76 | + * @return boolean Success indicator |
| 77 | + */ |
| 78 | + public function writeToDB() { |
| 79 | + if ( is_null( $this->id ) ) { |
| 80 | + return $this->insertIntoDB(); |
| 81 | + } |
| 82 | + else { |
| 83 | + return $this->updateInDB(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Updates the group in the database. |
| 89 | + * |
| 90 | + * @since 0.1 |
| 91 | + * |
| 92 | + * @return boolean Success indicator |
| 93 | + */ |
| 94 | + protected function updateInDB() { |
| 95 | + $dbr = wfGetDB( DB_MASTER ); |
35 | 96 | |
| 97 | + return $dbr->update( |
| 98 | + 'surveys', |
| 99 | + $this->getWriteValues(), |
| 100 | + array( 'survey_id' => $this->id ) |
| 101 | + ); |
36 | 102 | } |
37 | 103 | |
| 104 | + /** |
| 105 | + * Inserts the group into the database. |
| 106 | + * |
| 107 | + * @since 0.1 |
| 108 | + * |
| 109 | + * @return boolean Success indicator |
| 110 | + */ |
| 111 | + protected function insertIntoDB() { |
| 112 | + $dbr = wfGetDB( DB_MASTER ); |
| 113 | + |
| 114 | + $result = $dbr->insert( |
| 115 | + 'surveys', |
| 116 | + $this->getWriteValues() |
| 117 | + ); |
| 118 | + |
| 119 | + $this->id = $dbr->insertId(); |
| 120 | + |
| 121 | + return $result; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Gets the fields => values to write to the surveys table. |
| 126 | + * |
| 127 | + * @since 0.1 |
| 128 | + * |
| 129 | + * @return array |
| 130 | + */ |
| 131 | + protected function getWriteValues() { |
| 132 | + return array( |
| 133 | + 'survey_enabled' => $this->enabled, |
| 134 | + 'survey_name' => $this->name, |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns the survey database id. |
| 140 | + * |
| 141 | + * @since 0.1 |
| 142 | + * |
| 143 | + * @return integer|null |
| 144 | + */ |
| 145 | + public function getId() { |
| 146 | + return $this->id; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the survey name. |
| 151 | + * |
| 152 | + * @since 0.1 |
| 153 | + * |
| 154 | + * @return string |
| 155 | + */ |
| 156 | + public function getName() { |
| 157 | + return $this->name; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the surveys questions. |
| 162 | + * |
| 163 | + * @since 0.1 |
| 164 | + * |
| 165 | + * @return array of SurveyQuestion |
| 166 | + */ |
| 167 | + public function getQuestions() { |
| 168 | + return $this->questions; |
| 169 | + } |
| 170 | + |
38 | 171 | } |
\ No newline at end of file |
Index: trunk/extensions/Survey/api/ApiAddSurvey.php |
— | — | @@ -27,32 +27,74 @@ |
28 | 28 | |
29 | 29 | $params = $this->extractRequestParams(); |
30 | 30 | |
| 31 | + $survey = new Survey( |
| 32 | + null, |
| 33 | + $params['name'], |
| 34 | + $params['enabled'] == 1, |
| 35 | + $params['questions'] |
| 36 | + ); |
| 37 | + |
| 38 | + $this->getResult()->addValue( |
| 39 | + null, |
| 40 | + 'success', |
| 41 | + $survey->writeToDB() |
| 42 | + ); |
| 43 | + |
| 44 | + $this->getResult()->addValue( |
| 45 | + 'survey', |
| 46 | + 'id', |
| 47 | + $survey->getId() |
| 48 | + ); |
| 49 | + |
| 50 | + $this->getResult()->addValue( |
| 51 | + 'survey', |
| 52 | + 'name', |
| 53 | + $survey->getName() |
| 54 | + ); |
31 | 55 | } |
32 | 56 | |
33 | 57 | public function getAllowedParams() { |
34 | 58 | return array( |
| 59 | + 'name' => array( |
| 60 | + ApiBase::PARAM_TYPE => 'string', |
| 61 | + ApiBase::PARAM_REQUIRED => true, |
| 62 | + ), |
| 63 | + 'enabled' => array( |
| 64 | + ApiBase::PARAM_TYPE => 'integer', |
| 65 | + ApiBase::PARAM_REQUIRED => true, |
| 66 | + ), |
| 67 | + 'questions' => array( |
| 68 | + ApiBase::PARAM_TYPE => 'string', |
| 69 | + ApiBase::PARAM_ISMULTI => true, |
| 70 | + ApiBase::PARAM_DFLT => '', |
| 71 | + ), |
35 | 72 | ); |
36 | 73 | } |
37 | 74 | |
38 | 75 | public function getParamDescription() { |
39 | 76 | return array( |
| 77 | + 'name' => 'The name of the survey', |
| 78 | + 'enabled' => 'Enable the survey or not', |
| 79 | + 'questions' => 'The questions that make up the survey', |
40 | 80 | ); |
41 | 81 | } |
42 | 82 | |
43 | 83 | public function getDescription() { |
44 | 84 | return array( |
45 | | - '' |
| 85 | + 'API module for adding surveys.' |
46 | 86 | ); |
47 | 87 | } |
48 | 88 | |
49 | 89 | public function getPossibleErrors() { |
50 | 90 | return array_merge( parent::getPossibleErrors(), array( |
| 91 | + array( 'missingparam', 'name' ), |
| 92 | + array( 'missingparam', 'enabled' ), |
51 | 93 | ) ); |
52 | 94 | } |
53 | 95 | |
54 | 96 | protected function getExamples() { |
55 | 97 | return array( |
56 | | - 'api.php?action=addsurvey&', |
| 98 | + 'api.php?action=addsurvey&name=My awesome survey&enabled=1&questions=', |
57 | 99 | ); |
58 | 100 | } |
59 | 101 | |