r95362 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95361‎ | r95362 | r95363 >
Date:00:20, 24 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added add survey api module
Modified paths:
  • /trunk/extensions/Survey/Survey.hooks.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiAddSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/sql (added) (history)
  • /trunk/extensions/Survey/sql/Survey_indexSurveyName.sql (added) (history)

Diff [purge]

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
13 + native
Index: trunk/extensions/Survey/Survey.hooks.php
@@ -25,32 +25,37 @@
2626 public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) {
2727 global $wgDBtype;
2828
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+ ) );
5560
5661 return true;
5762 }
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -13,10 +13,38 @@
1414 */
1515 class Survey {
1616
 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+ */
1723 protected $id;
 24+
 25+ /**
 26+ * The name of the survey.
 27+ *
 28+ * @since 0.1
 29+ * @var string
 30+ */
1831 protected $name;
 32+
 33+ /**
 34+ * If the survey is enabled or not.
 35+ *
 36+ * @since 0.1
 37+ * @var boolean
 38+ */
1939 protected $enabled;
2040
 41+ /**
 42+ * The questions that go with this survey.
 43+ *
 44+ * @since 0.1
 45+ * @var array of SurveyQuestion
 46+ */
 47+ protected $questions;
 48+
2149 public static function newFromDBResult() {
2250
2351 }
@@ -29,9 +57,114 @@
3058 * @param integer|null $id
3159 * @param string $name
3260 * @param boolean $enabled
 61+ * @param array $questions
3362 */
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 );
3596
 97+ return $dbr->update(
 98+ 'surveys',
 99+ $this->getWriteValues(),
 100+ array( 'survey_id' => $this->id )
 101+ );
36102 }
37103
 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+
38171 }
\ No newline at end of file
Index: trunk/extensions/Survey/api/ApiAddSurvey.php
@@ -27,32 +27,74 @@
2828
2929 $params = $this->extractRequestParams();
3030
 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+ );
3155 }
3256
3357 public function getAllowedParams() {
3458 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+ ),
3572 );
3673 }
3774
3875 public function getParamDescription() {
3976 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',
4080 );
4181 }
4282
4383 public function getDescription() {
4484 return array(
45 - ''
 85+ 'API module for adding surveys.'
4686 );
4787 }
4888
4989 public function getPossibleErrors() {
5090 return array_merge( parent::getPossibleErrors(), array(
 91+ array( 'missingparam', 'name' ),
 92+ array( 'missingparam', 'enabled' ),
5193 ) );
5294 }
5395
5496 protected function getExamples() {
5597 return array(
56 - 'api.php?action=addsurvey&',
 98+ 'api.php?action=addsurvey&name=My awesome survey&enabled=1&questions=',
5799 );
58100 }
59101

Sign-offs

UserFlagDate
Nikerabbitinspected05:55, 24 August 2011

Status & tagging log