Index: trunk/extensions/UsabilityInitiative/PrefSwitch/PrefSwitch.classes.php |
— | — | @@ -16,6 +16,13 @@ |
17 | 17 | |
18 | 18 | /* Static Functions */ |
19 | 19 | |
| 20 | + /** |
| 21 | + * Render the HTML for a survey. |
| 22 | + * @param $name string Survey name |
| 23 | + * @param $questions array Array containing question data |
| 24 | + * @param $loadFromDB bool Load previous survey data from the database |
| 25 | + * @return string HTML |
| 26 | + */ |
20 | 27 | public static function render( $name, $questions, $loadFromDB = false ) { |
21 | 28 | global $wgUser, $wgOut; |
22 | 29 | // Load existing data from the database - allowing the user to change their answers |
— | — | @@ -36,17 +43,23 @@ |
37 | 44 | foreach ( $questions as $field => $config ) { |
38 | 45 | $answer = isset( $loaded[$field] ) ? $loaded[$field][0] : null; |
39 | 46 | $answerData = isset( $loaded[$field] ) ? $loaded[$field][1] : null; |
40 | | - $html .= call_user_func( |
41 | | - array( self::$fieldTypes[$config['type']], 'render' ), $field, $config, $answer, $answerData |
| 47 | + $html .= call_user_func( array( self::$fieldTypes[$config['type']], 'render' ), |
| 48 | + $field, $config, $answer, $answerData |
42 | 49 | ); |
43 | 50 | } |
44 | 51 | $html .= Xml::closeElement( 'dl' ); |
45 | 52 | return $html; |
46 | 53 | } |
| 54 | + |
| 55 | + /** |
| 56 | + * Save a survey to the database |
| 57 | + * @param $name string Survey name |
| 58 | + * @param $survey array Survey configuration data |
| 59 | + */ |
47 | 60 | public static function save( $name, $survey ) { |
48 | 61 | global $wgRequest, $wgUser; |
49 | 62 | $dbw = wfGetDb( DB_MASTER ); |
50 | | - $now = $dbw->timestamp( wfTimestamp() ); |
| 63 | + $now = $dbw->timestamp(); |
51 | 64 | foreach ( $survey['questions'] as $question => $config ) { |
52 | 65 | $dbw->insert( |
53 | 66 | 'prefswitch_survey', |
— | — | @@ -68,7 +81,22 @@ |
69 | 82 | |
70 | 83 | /* Static Functions */ |
71 | 84 | |
| 85 | + /** |
| 86 | + * Render the HTML for a question |
| 87 | + * @param $question string Question ID |
| 88 | + * @param $config array Configuration array, including answers. See $wgPrefSwitchSurveys for format |
| 89 | + * @param $answer string ID of currently selected answer |
| 90 | + * @param $answerData string Data for currently selected answer |
| 91 | + * @return string HTML |
| 92 | + */ |
72 | 93 | public static function render( $question, $config, $answer, $answerData ); |
| 94 | + |
| 95 | + /** |
| 96 | + * Get values to insert into the database for a question |
| 97 | + * @param $question Question ID |
| 98 | + * @param $request WebRequest object containing answer data |
| 99 | + * @return array( field => value ) |
| 100 | + */ |
73 | 101 | public static function save( $question, $request ); |
74 | 102 | } |
75 | 103 | class PrefSwitchSurveyFieldSelect implements PrefSwitchSurveyField { |
— | — | @@ -106,6 +134,7 @@ |
107 | 135 | $html .= Xml::closeElement( 'dd' ); |
108 | 136 | return $html; |
109 | 137 | } |
| 138 | + |
110 | 139 | public static function save( $question, $request ) { |
111 | 140 | $answer = $request->getVal( "prefswitch-survey-{$question}", '' ); |
112 | 141 | switch ( $answer ) { |
— | — | @@ -262,10 +291,11 @@ |
263 | 292 | return $html; |
264 | 293 | } |
265 | 294 | public static function save( $question, $request ) { |
| 295 | + $answer = $request->getVal( "prefswitch-survey-{$question}", null ); |
266 | 296 | return array( |
267 | | - 'pss_answer' => $request->getVal( "prefswitch-survey-{$question}", null ), |
268 | | - 'pss_answer_data' => $insert['pss_answer'] == 'true' || $insert['pss_answer'] == 'false' ? |
269 | | - $request->getVal( "prefswitch-survey-{$question}-if{$insert['pss_answer']}", null ) : null, |
| 297 | + 'pss_answer' => $answer |
| 298 | + 'pss_answer_data' => $answer == 'true' || $answer == 'false' ? |
| 299 | + $request->getVal( "prefswitch-survey-{$question}-if{$answer}", null ) : null, |
270 | 300 | ); |
271 | 301 | } |
272 | 302 | } |
— | — | @@ -290,17 +320,10 @@ |
291 | 321 | public static function save( $question, $request ) { |
292 | 322 | $x = $request->getVal( "prefswitch-survey-{$question}-x" ); |
293 | 323 | $y = $request->getVal( "prefswitch-survey-{$question}-y" ); |
294 | | - if ( $x === '' && $y === '' ) { |
295 | | - return array( |
296 | | - 'pss_answer' => null, |
297 | | - 'pss_answer_data' => null, |
298 | | - ); |
299 | | - } else { |
300 | | - return array( |
301 | | - 'pss_answer' => null, |
302 | | - 'pss_answer_data' => $x . 'x' . $y, |
303 | | - ); |
304 | | - } |
| 324 | + return array( |
| 325 | + 'pss_answer' => null, |
| 326 | + 'pss_answer_data' => ( $x != '' || $y != '' ) ? "{$x}x{$y}" : null, |
| 327 | + ); |
305 | 328 | } |
306 | 329 | } |
307 | 330 | class PrefSwitchSurveyFieldText implements PrefSwitchSurveyField { |
— | — | @@ -316,16 +339,9 @@ |
317 | 340 | } |
318 | 341 | public static function save( $question, $request ) { |
319 | 342 | $answer = $request->getVal( "prefswitch-survey-{$question}" ); |
320 | | - if ( $answer === '' ) { |
321 | | - return array( |
322 | | - 'pss_answer' => null, |
323 | | - 'pss_answer_data' => null, |
324 | | - ); |
325 | | - } else { |
326 | | - return array( |
327 | | - 'pss_answer' => null, |
328 | | - 'pss_answer_data' => $answer, |
329 | | - ); |
330 | | - } |
| 343 | + return array( |
| 344 | + 'pss_answer' => null, |
| 345 | + 'pss_answer_data' => $answer !== '' ? $answer : null, |
| 346 | + ); |
331 | 347 | } |
332 | 348 | } |
Index: trunk/extensions/UsabilityInitiative/PrefSwitch/PrefSwitch.php |
— | — | @@ -20,10 +20,10 @@ |
21 | 21 | |
22 | 22 | $wgPrefSwitchStyleVersion = 1; |
23 | 23 | |
24 | | -// Preferences to set when users swtich prefs |
| 24 | +// Preferences to set when users switch prefs |
25 | 25 | // array( |
26 | | -// in => array( pref => value ), |
27 | | -// out => array( pref => value ), |
| 26 | +// 'off' => array( pref => value ), |
| 27 | +// 'on' => array( pref => value ), |
28 | 28 | // ) |
29 | 29 | $wgPrefSwitchPrefs = array( |
30 | 30 | 'off' => array( |
— | — | @@ -41,18 +41,22 @@ |
42 | 42 | // Survey questions to ask when users switch prefs |
43 | 43 | // array( |
44 | 44 | // survey-id => array( |
45 | | -// field-id => array( |
46 | | -// question => msg-id, |
47 | | -// type => msg-id, |
48 | | -// answers => array( |
49 | | -// answer => msg-id, |
50 | | -// ... |
| 45 | +// 'submit-msg' => message key for submit button caption |
| 46 | +// 'updateable' => boolean, |
| 47 | +// 'questions' => array( |
| 48 | +// field-id => array( |
| 49 | +// 'question' => msg-id, |
| 50 | +// 'type' => msg-id, |
| 51 | +// 'answers' => array( |
| 52 | +// answer => msg-id, |
| 53 | +// ... |
| 54 | +// ), |
| 55 | +// 'other' => msg-id, |
| 56 | +// 'ifyes' => msg-id |
51 | 57 | // ), |
52 | | -// other => msg-id, |
53 | | -// ifyes => msg-id |
| 58 | +// ... |
54 | 59 | // ), |
55 | | -// ... |
56 | | -// ), |
| 60 | +// ) |
57 | 61 | // ) |
58 | 62 | $wgPrefSwitchSurveys = array(); |
59 | 63 | $wgPrefSwitchSurveys['feedback'] = array( |
— | — | @@ -131,8 +135,8 @@ |
132 | 136 | // Credits |
133 | 137 | $wgExtensionCredits['other'][] = array( |
134 | 138 | 'path' => __FILE__, |
135 | | - 'name' => 'PrefStat', |
136 | | - 'author' => 'Roan Kattouw', |
| 139 | + 'name' => 'PrefSwitch', |
| 140 | + 'author' => array( 'Trevor Parscal', 'Roan Kattouw' ), |
137 | 141 | 'version' => '0.1.2', |
138 | 142 | 'url' => 'http://www.mediawiki.org/wiki/Extension:UsabilityInitiative', |
139 | 143 | 'descriptionmsg' => 'prefswitch-desc', |
Index: trunk/extensions/UsabilityInitiative/PrefSwitch/PrefSwitch.sql |
— | — | @@ -22,4 +22,4 @@ |
23 | 23 | pss_answer_data text NULL |
24 | 24 | ) /*$wgDBTableOptions*/; |
25 | 25 | |
26 | | -CREATE UNIQUE INDEX /*i*/pss_user_timestamp_question ON /*_*/prefswitch_survey (pss_user, pss_timestamp, pss_question, pss_name); |
| 26 | +CREATE UNIQUE INDEX /*i*/pss_user_timestamp_name_question ON /*_*/prefswitch_survey (pss_user, pss_timestamp, pss_name, pss_question); |