Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.15% |
100 / 104 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
ManageMentorsEditMentor | |
96.15% |
100 / 104 |
|
75.00% |
3 / 4 |
9 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
getFormFields | |
100.00% |
61 / 61 |
|
100.00% |
1 / 1 |
2 | |||
onSubmit | |
82.61% |
19 / 23 |
|
0.00% |
0 / 1 |
5.13 | |||
onSuccess | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Specials\Forms; |
4 | |
5 | use GrowthExperiments\MentorDashboard\MentorTools\IMentorWeights; |
6 | use GrowthExperiments\MentorDashboard\MentorTools\MentorStatusManager; |
7 | use GrowthExperiments\Mentorship\Provider\IMentorWriter; |
8 | use GrowthExperiments\Mentorship\Provider\MentorProvider; |
9 | use MediaWiki\Context\IContextSource; |
10 | use MediaWiki\Status\Status; |
11 | use MediaWiki\User\UserIdentity; |
12 | use MediaWiki\Utils\MWTimestamp; |
13 | |
14 | class ManageMentorsEditMentor extends ManageMentorsAbstractForm { |
15 | |
16 | private MentorProvider $mentorProvider; |
17 | private IMentorWriter $mentorWriter; |
18 | private MentorStatusManager $mentorStatusManager; |
19 | private UserIdentity $mentorUser; |
20 | |
21 | /** |
22 | * @param MentorProvider $mentorProvider |
23 | * @param IMentorWriter $mentorWriter |
24 | * @param MentorStatusManager $mentorStatusManager |
25 | * @param UserIdentity $mentorUser |
26 | * @param IContextSource $context |
27 | */ |
28 | public function __construct( |
29 | MentorProvider $mentorProvider, |
30 | IMentorWriter $mentorWriter, |
31 | MentorStatusManager $mentorStatusManager, |
32 | UserIdentity $mentorUser, |
33 | IContextSource $context |
34 | ) { |
35 | $this->mentorProvider = $mentorProvider; |
36 | $this->mentorWriter = $mentorWriter; |
37 | $this->mentorStatusManager = $mentorStatusManager; |
38 | $this->mentorUser = $mentorUser; |
39 | |
40 | parent::__construct( |
41 | $context, |
42 | 'growthexperiments-manage-mentors-' |
43 | ); |
44 | |
45 | $this->setPreHtml( $this->msg( |
46 | 'growthexperiments-manage-mentors-edit-pretext', |
47 | $mentorUser->getName() |
48 | )->parse() ); |
49 | } |
50 | |
51 | /** |
52 | * @inheritDoc |
53 | */ |
54 | protected function getFormFields(): array { |
55 | $mentor = $this->mentorProvider->newMentorFromUserIdentity( $this->mentorUser ); |
56 | $awayTimestamp = $this->mentorStatusManager->getMentorBackTimestamp( $this->mentorUser ); |
57 | $canChangeStatusBool = $this->mentorStatusManager->canChangeStatus( |
58 | $this->mentorUser |
59 | )->isOK(); |
60 | |
61 | return [ |
62 | 'message' => [ |
63 | 'type' => 'text', |
64 | 'label-message' => 'growthexperiments-manage-mentors-edit-intro-msg', |
65 | 'default' => $mentor->getIntroText(), |
66 | ], |
67 | 'weight' => [ |
68 | 'type' => 'radio', |
69 | 'label-message' => 'growthexperiments-manage-mentors-edit-weight', |
70 | 'options-messages' => [ |
71 | 'growthexperiments-mentor-dashboard-mentor-tools-mentor-weight-none' => |
72 | IMentorWeights::WEIGHT_NONE, |
73 | 'growthexperiments-mentor-dashboard-mentor-tools-mentor-weight-low' => |
74 | IMentorWeights::WEIGHT_LOW, |
75 | 'growthexperiments-mentor-dashboard-mentor-tools-mentor-weight-medium' => |
76 | IMentorWeights::WEIGHT_NORMAL, |
77 | 'growthexperiments-mentor-dashboard-mentor-tools-mentor-weight-high' => |
78 | IMentorWeights::WEIGHT_HIGH, |
79 | ], |
80 | 'default' => $mentor->getWeight(), |
81 | ], |
82 | 'isAway' => [ |
83 | 'type' => 'check', |
84 | 'label-message' => 'growthexperiments-manage-mentors-edit-is-away', |
85 | 'disabled' => !$canChangeStatusBool, |
86 | 'help' => !$canChangeStatusBool ? $this->msg( |
87 | 'growthexperiments-manage-mentors-edit-is-away-blocked' |
88 | ) |
89 | ->params( $this->mentorUser->getName() ) |
90 | ->escaped() : '', |
91 | 'default' => $this->mentorStatusManager->getMentorStatus( |
92 | $this->mentorUser |
93 | ) === MentorStatusManager::STATUS_AWAY, |
94 | ], |
95 | 'isAwayChangeable' => [ |
96 | 'type' => 'hidden', |
97 | 'default' => $canChangeStatusBool, |
98 | ], |
99 | 'awayTimestamp' => [ |
100 | 'type' => 'datetime', |
101 | 'label-message' => 'growthexperiments-manage-mentors-edit-away-until', |
102 | 'required' => true, |
103 | 'hide-if' => [ |
104 | 'OR', |
105 | [ '!==', 'isAway', '1' ], |
106 | [ '!==', 'isAwayChangeable', '1' ], |
107 | ], |
108 | 'min' => MWTimestamp::getInstance()->getTimestamp( TS_MW ), |
109 | 'default' => MWTimestamp::getInstance( (string)$awayTimestamp ) |
110 | ->format( 'Y-m-d\TH:m:s\Z' ), |
111 | ], |
112 | 'reason' => [ |
113 | 'type' => 'text', |
114 | 'label-message' => 'growthexperiments-manage-mentors-edit-reason', |
115 | ], |
116 | ]; |
117 | } |
118 | |
119 | /** |
120 | * @inheritDoc |
121 | */ |
122 | public function onSubmit( array $data ) { |
123 | if ( !self::canManageMentors( $this->getAuthority() ) ) { |
124 | return false; |
125 | } |
126 | |
127 | $mentor = $this->mentorProvider->newMentorFromUserIdentity( $this->mentorUser ); |
128 | $mentorStatus = $this->mentorStatusManager->getMentorStatus( $this->mentorUser ); |
129 | $awayTimestamp = $this->mentorStatusManager->getMentorBackTimestamp( $this->mentorUser ); |
130 | |
131 | $mentor->setIntroText( $data['message'] !== '' ? $data['message'] : null ); |
132 | $mentor->setWeight( (int)$data['weight'] ); |
133 | |
134 | $status = Status::newGood(); |
135 | if ( ( $mentorStatus === MentorStatusManager::STATUS_AWAY ) !== $data['isAway'] ) { |
136 | // isAway changed, implement the change |
137 | if ( $data['isAway'] ) { |
138 | $status->merge( $this->mentorStatusManager->markMentorAsAwayTimestamp( |
139 | $this->mentorUser, |
140 | $data['awayTimestamp'] |
141 | ) ); |
142 | } else { |
143 | $status->merge( $this->mentorStatusManager->markMentorAsActive( |
144 | $this->mentorUser |
145 | ) ); |
146 | } |
147 | } |
148 | |
149 | $status->merge( $this->mentorWriter->changeMentor( |
150 | $mentor, |
151 | $this->getUser(), |
152 | $data['reason'] |
153 | ) ); |
154 | return $status; |
155 | } |
156 | |
157 | /** |
158 | * @inheritDoc |
159 | */ |
160 | protected function onSuccess(): void { |
161 | $out = $this->getOutput(); |
162 | $out->addWikiMsg( |
163 | 'growthexperiments-manage-mentors-edit-success', |
164 | $this->mentorUser->getName() |
165 | ); |
166 | $out->addWikiMsg( |
167 | 'growthexperiments-manage-mentors-return-back' |
168 | ); |
169 | } |
170 | } |