r95364 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95363‎ | r95364 | r95365 >
Date:00:50, 24 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey questions
Modified paths:
  • /trunk/extensions/Survey/api/ApiAddSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -45,10 +45,75 @@
4646 */
4747 protected $questions;
4848
49 - public static function newFromDBResult() {
 49+ /**
 50+ * Returns the Survey with specified name, or false if there is no such survey.
 51+ *
 52+ * @since 0.1
 53+ *
 54+ * @param string $surveyName
 55+ * @param boolean $loadQuestions
 56+ *
 57+ * @return Survey or false
 58+ */
 59+ public static function newFromName( $surveyName, $loadQuestions = true ) {
 60+ return self::newFromDB( array( 'survey_name' => $surveyName ), $loadQuestions );
 61+ }
 62+
 63+ /**
 64+ * Returns the Survey with specified ID, or false if there is no such survey.
 65+ *
 66+ * @since 0.1
 67+ *
 68+ * @param integer surveyId
 69+ * @param boolean $loadQuestions
 70+ *
 71+ * @return Survey or false
 72+ */
 73+ public static function newFromId( $surveyId, $loadQuestions = true ) {
 74+ return self::newFromDB( array( 'survey_id' => surveyId ), $loadQuestions );
 75+ }
 76+
 77+ /**
 78+ * Returns a new instance of Survey build from a database result
 79+ * obtained by doing a select with the porvided conditions on the surveys table.
 80+ * If no survey matches the conditions, false will be returned.
 81+ *
 82+ * @since 0.1
 83+ *
 84+ * @param array $conditions
 85+ * @param boolean $loadQuestions
 86+ *
 87+ * @return Survey or false
 88+ */
 89+ protected static function newFromDB( array $conditions, $loadQuestions = true ) {
 90+ $dbr = wfGetDB( DB_SLAVE );
5091
 92+ $survey = $dbr->selectRow(
 93+ 'surveys',
 94+ array(
 95+ 'survey_id',
 96+ 'survey_name',
 97+ 'survey_enabled',
 98+ ),
 99+ $conditions
 100+ );
 101+
 102+ if ( !$survey ) {
 103+ return false;
 104+ }
 105+
 106+ $questions = $loadQuestions ? self::getQuestionsFromDB( $dbr, $survey->survey_id ) : array();
 107+
 108+ return new self(
 109+ $survey->survey_id,
 110+ $survey->survey_name,
 111+ $survey->survey_enabled,
 112+ $questions
 113+ );
51114 }
52115
 116+
 117+
53118 /**
54119 * Constructor.
55120 *
@@ -63,6 +128,7 @@
64129 $this->id = $id;
65130 $this->name = $name;
66131 $this->enabled = $enabled;
 132+
67133 $this->questions = $questions;
68134 }
69135
@@ -84,7 +150,7 @@
85151 }
86152
87153 /**
88 - * Updates the group in the database.
 154+ * Updates the survey in the database.
89155 *
90156 * @since 0.1
91157 *
@@ -101,7 +167,7 @@
102168 }
103169
104170 /**
105 - * Inserts the group into the database.
 171+ * Inserts the survey into the database.
106172 *
107173 * @since 0.1
108174 *
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -13,10 +13,46 @@
1414 */
1515 class SurveyQuestion {
1616
 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+ */
1723 protected $id;
 24+
 25+ /**
 26+ * The question text.
 27+ *
 28+ * @since 0.1
 29+ * @var string
 30+ */
1831 protected $text;
 32+
 33+ /**
 34+ * The question type.
 35+ *
 36+ * @since 0.1
 37+ * @var integer
 38+ */
1939 protected $type;
 40+
 41+ /**
 42+ * Indicated if the question is required,
 43+ * ie if the user can not submit the survey without answering it.
 44+ *
 45+ * @since 0.1
 46+ * @var boolean
 47+ */
2048 protected $required;
 49+
 50+ /**
 51+ * List of allowed values for the question.
 52+ * Empty list for no restrictions.
 53+ *
 54+ * @since 0.1
 55+ * @var array of string|number
 56+ */
2157 protected $answers;
2258
2359 /**
@@ -31,7 +67,146 @@
3268 * @param array $answers
3369 */
3470 public function __construct( $id, $text, $type, $required, array $answers = array() ) {
 71+ $this->id = $id;
 72+ $this->text = $text;
 73+ $this->type = $type;
 74+ $this->required = $required;
 75+ $this->answers = $answers;
 76+ }
 77+
 78+ /**
 79+ * Returns the questions for the specified survey.
 80+ *
 81+ * @since 0.1
 82+ *
 83+ * @param integer $surveyId
 84+ *
 85+ * @return array of SurveyQuestion
 86+ */
 87+ protected static function getQuestionsForSurvey( $surveyId ) {
 88+ return self::getQuestionsFromDB( array( 'question_survey_id' => $surveyId ) );
 89+ }
 90+
 91+ /**
 92+ * Returns the questions matching the specified conditions.
 93+ *
 94+ * @since 0.1
 95+ *
 96+ * @param array $conditions
 97+ *
 98+ * @return array of SurveyQuestion
 99+ */
 100+ protected static function getQuestionsFromDB( array $conditions ) {
 101+ $dbr = wfgetDB( DB_SLAVE );
35102
 103+ $questions = $dbr->select(
 104+ 'survey_questions',
 105+ array(
 106+ 'question_id',
 107+ 'question_text',
 108+ 'question_type',
 109+ 'question_required',
 110+ 'question_answers',
 111+ ),
 112+ $conditions
 113+ );
 114+
 115+ if ( !$questions ) {
 116+ return array();
 117+ }
 118+
 119+ foreach ( $questions as &$question ) {
 120+ $question = new SurveyQuestion(
 121+ $question->question_id,
 122+ $question->question_text,
 123+ $question->question_type,
 124+ $question->question_required,
 125+ unserialize( $question->question_answers )
 126+ );
 127+ }
 128+
 129+ return $questions;
36130 }
37131
38 -}
\ No newline at end of file
 132+ /**
 133+ * Writes the question to the database, either updating it
 134+ * when it already exists, or inserting it when it doesn't.
 135+ *
 136+ * @since 0.1
 137+ *
 138+ * @return boolean Success indicator
 139+ */
 140+ public function writeToDB() {
 141+ if ( is_null( $this->id ) ) {
 142+ return $this->insertIntoDB();
 143+ }
 144+ else {
 145+ return $this->updateInDB();
 146+ }
 147+ }
 148+
 149+ /**
 150+ * Updates the question in the database.
 151+ *
 152+ * @since 0.1
 153+ *
 154+ * @return boolean Success indicator
 155+ */
 156+ protected function updateInDB() {
 157+ $dbr = wfGetDB( DB_MASTER );
 158+
 159+ return $dbr->update(
 160+ 'survey_questions',
 161+ $this->getWriteValues(),
 162+ array( 'question_id' => $this->id )
 163+ );
 164+ }
 165+
 166+ /**
 167+ * Inserts the question into the database.
 168+ *
 169+ * @since 0.1
 170+ *
 171+ * @return boolean Success indicator
 172+ */
 173+ protected function insertIntoDB() {
 174+ $dbr = wfGetDB( DB_MASTER );
 175+
 176+ $result = $dbr->insert(
 177+ 'survey_questions',
 178+ $this->getWriteValues()
 179+ );
 180+
 181+ $this->id = $dbr->insertId();
 182+
 183+ return $result;
 184+ }
 185+
 186+ /**
 187+ * Gets the fields => values to write to the survey_questions table.
 188+ *
 189+ * @since 0.1
 190+ *
 191+ * @return array
 192+ */
 193+ protected function getWriteValues() {
 194+ return array(
 195+ 'question_text' => $this->text,
 196+ 'question_type' => $this->type,
 197+ 'question_required' => $this->required,
 198+ 'question_answers' => serialize( $this->answers ),
 199+ );
 200+ }
 201+
 202+ /**
 203+ * Returns the question database id.
 204+ *
 205+ * @since 0.1
 206+ *
 207+ * @return integer|null
 208+ */
 209+ public function getId() {
 210+ return $this->id;
 211+ }
 212+
 213+}
Index: trunk/extensions/Survey/api/ApiAddSurvey.php
@@ -31,7 +31,7 @@
3232 null,
3333 $params['name'],
3434 $params['enabled'] == 1,
35 - $params['questions']
 35+ $params['questions'] // TODO
3636 );
3737
3838 $this->getResult()->addValue(

Status & tagging log