Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
WelcomeSurveyReminder | |
0.00% |
0 / 63 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
canRender | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getHeaderText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHeaderIconName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMobileSummaryHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getBodyContent | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
2 | |||
getBody | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMobileSummaryBody | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getState | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\HomepageModules; |
4 | |
5 | use GrowthExperiments\ExperimentUserManager; |
6 | use GrowthExperiments\WelcomeSurveyFactory; |
7 | use MediaWiki\Config\Config; |
8 | use MediaWiki\Context\IContextSource; |
9 | use MediaWiki\Html\Html; |
10 | use MediaWiki\SpecialPage\SpecialPageFactory; |
11 | use OOUI\ButtonInputWidget; |
12 | use OOUI\IconWidget; |
13 | |
14 | /** |
15 | * A module for displaying a dismissable reminder to users who have not filled out the welcome survey. |
16 | */ |
17 | class WelcomeSurveyReminder extends BaseModule { |
18 | |
19 | /** @inheritDoc */ |
20 | protected static $supportedModes = [ |
21 | self::RENDER_DESKTOP, |
22 | self::RENDER_MOBILE_SUMMARY, |
23 | // RENDER_MOBILE_DETAILS is not supported |
24 | ]; |
25 | |
26 | /** @var SpecialPageFactory */ |
27 | private $specialPageFactory; |
28 | |
29 | /** @var WelcomeSurveyFactory */ |
30 | private $welcomeSurveyFactory; |
31 | |
32 | /** |
33 | * @inheritDoc |
34 | * @param SpecialPageFactory $specialPageFactory |
35 | * @param WelcomeSurveyFactory $welcomeSurveyFactory |
36 | */ |
37 | public function __construct( |
38 | IContextSource $context, |
39 | Config $wikiConfig, |
40 | ExperimentUserManager $experimentUserManager, |
41 | SpecialPageFactory $specialPageFactory, |
42 | WelcomeSurveyFactory $welcomeSurveyFactory |
43 | ) { |
44 | parent::__construct( 'welcomesurveyreminder', $context, $wikiConfig, $experimentUserManager ); |
45 | $this->specialPageFactory = $specialPageFactory; |
46 | $this->welcomeSurveyFactory = $welcomeSurveyFactory; |
47 | } |
48 | |
49 | /** |
50 | * The module is enabled if the user has not filled out the welcome survey, or they have |
51 | * registered more than 60 days ago. |
52 | * @inheritDoc |
53 | */ |
54 | protected function canRender() { |
55 | return $this->getContext()->getConfig()->get( 'WelcomeSurveyEnabled' ) |
56 | && $this->welcomeSurveyFactory->newWelcomeSurvey( $this->getContext() )->isUnfinished(); |
57 | } |
58 | |
59 | /** @inheritDoc */ |
60 | protected function getHeaderText() { |
61 | return ''; |
62 | } |
63 | |
64 | /** @inheritDoc */ |
65 | protected function getHeaderIconName() { |
66 | return ''; |
67 | } |
68 | |
69 | /** @inheritDoc */ |
70 | protected function getHeader() { |
71 | return ''; |
72 | } |
73 | |
74 | /** @inheritDoc */ |
75 | protected function getMobileSummaryHeader() { |
76 | return ''; |
77 | } |
78 | |
79 | private function getBodyContent(): string { |
80 | $welcomeSurvey = $this->welcomeSurveyFactory->newWelcomeSurvey( $this->getContext() ); |
81 | $group = $welcomeSurvey->getGroup( true ); |
82 | $surveyTitle = $this->specialPageFactory->getTitleForAlias( 'WelcomeSurvey' ); |
83 | $returnTo = $this->specialPageFactory->getTitleForAlias( 'Homepage' )->getPrefixedText(); |
84 | $returnToQuery = wfArrayToCgi( [ 'source' => 'welcomesurvey-originalcontext' ] ); |
85 | $surveyUrl = $surveyTitle->getLocalURL( |
86 | $welcomeSurvey->getRedirectUrlQuery( $group, $returnTo, $returnToQuery ) |
87 | ); |
88 | $surveyLink = Html::element( |
89 | 'a', |
90 | [ |
91 | 'href' => $surveyUrl, |
92 | 'class' => 'welcomesurvey-reminder-link', |
93 | 'data-link-id' => 'welcomesurvey-reminder', |
94 | ], |
95 | $this->getContext()->msg( 'welcomesurvey-reminder-link' )->text() |
96 | ); |
97 | $reminderHtml = $this->getContext()->msg( 'welcomesurvey-reminder' ) |
98 | ->rawParams( $surveyLink ) |
99 | ->parse(); |
100 | // wrap for flexbox friendliness |
101 | $reminderBlock = Html::rawElement( 'p', [ |
102 | 'class' => 'welcomesurvey-reminder-message-block', |
103 | ], $reminderHtml ); |
104 | |
105 | $messageIcon = new IconWidget( [ |
106 | 'icon' => 'feedback', |
107 | 'classes' => [ 'welcomesurvey-reminder-feedback-icon' ], |
108 | ] ); |
109 | |
110 | $ajaxSkipUrl = wfScript( 'rest' ) . '/growthexperiments/v0/welcomesurvey/skip'; |
111 | $noJsSkipUrl = $this->specialPageFactory->getTitleForAlias( 'WelcomeSurvey' ) |
112 | ->getSubpage( 'skip' ) |
113 | ->getLocalURL(); |
114 | $disableButton = new ButtonInputWidget( [ |
115 | 'type' => 'submit', |
116 | 'icon' => 'close', |
117 | 'framed' => false, |
118 | 'classes' => [ 'welcomesurvey-reminder-dismiss' ], |
119 | 'label' => $this->getContext()->msg( 'welcomesurvey-reminder-dismiss' )->text(), |
120 | 'invisibleLabel' => true, |
121 | ] ); |
122 | $disableButton->setAttributes( [ 'data-ajax' => $ajaxSkipUrl ] ); |
123 | $disableButton->setAttributes( [ 'data-link-id' => 'welcomesurvey-skip' ] ); |
124 | $disableToken = Html::element( 'input', [ |
125 | 'type' => 'hidden', |
126 | 'name' => 'token', |
127 | 'value' => $this->getContext()->getCsrfTokenSet()->getToken( 'welcomesurvey' ), |
128 | ] ); |
129 | $disableForm = HTML::rawElement( 'form', [ |
130 | 'action' => $noJsSkipUrl, |
131 | 'method' => 'POST', |
132 | ], $disableButton . $disableToken ); |
133 | |
134 | return $messageIcon . $reminderBlock . $disableForm; |
135 | } |
136 | |
137 | /** @inheritDoc */ |
138 | protected function getBody() { |
139 | return $this->getBodyContent(); |
140 | } |
141 | |
142 | /** @inheritDoc */ |
143 | protected function getMobileSummaryBody() { |
144 | return $this->getBodyContent(); |
145 | } |
146 | |
147 | /** @inheritDoc */ |
148 | public function getState() { |
149 | return $this->canRender() ? self::MODULE_STATE_ACTIVATED : self::MODULE_STATE_UNACTIVATED; |
150 | } |
151 | |
152 | } |