Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MentorDashboardDiscoveryHooks | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
isDiscoveryEnabled | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
onSkinTemplateNavigation__Universal | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
onBeforePageDisplay | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\MentorDashboard; |
4 | |
5 | use GrowthExperiments\Mentorship\Provider\MentorProvider; |
6 | use MediaWiki\Config\Config; |
7 | use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook; |
8 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
9 | use MediaWiki\SpecialPage\SpecialPage; |
10 | use MediaWiki\User\Options\UserOptionsLookup; |
11 | use MediaWiki\User\User; |
12 | |
13 | class MentorDashboardDiscoveryHooks implements SkinTemplateNavigation__UniversalHook, BeforePageDisplayHook { |
14 | |
15 | public const MENTOR_DASHBOARD_SEEN_PREF = 'growthexperiments-mentor-dashboard-seen'; |
16 | |
17 | private Config $config; |
18 | private MentorProvider $mentorProvider; |
19 | private UserOptionsLookup $userOptionsLookup; |
20 | |
21 | /** |
22 | * @param Config $config |
23 | * @param MentorProvider $mentorProvider |
24 | * @param UserOptionsLookup $userOptionsLookup |
25 | */ |
26 | public function __construct( |
27 | Config $config, |
28 | MentorProvider $mentorProvider, |
29 | UserOptionsLookup $userOptionsLookup |
30 | ) { |
31 | $this->config = $config; |
32 | $this->mentorProvider = $mentorProvider; |
33 | $this->userOptionsLookup = $userOptionsLookup; |
34 | } |
35 | |
36 | /** |
37 | * Are mentor dashboard discovery features enabled? |
38 | * |
39 | * @param User $user |
40 | * @return bool |
41 | */ |
42 | private function isDiscoveryEnabled( User $user ): bool { |
43 | return $this->config->get( 'GEMentorDashboardEnabled' ) && |
44 | $user->isNamed() && |
45 | $this->mentorProvider->isMentor( $user ); |
46 | } |
47 | |
48 | /** |
49 | * @inheritDoc |
50 | */ |
51 | public function onSkinTemplateNavigation__Universal( $skin, &$links ): void { |
52 | if ( !$this->isDiscoveryEnabled( $skin->getUser() ) ) { |
53 | return; |
54 | } |
55 | $personalUrls = $links['user-menu'] ?? []; |
56 | |
57 | $newPersonalUrls = []; |
58 | foreach ( $personalUrls as $key => $link ) { |
59 | if ( $key == 'logout' ) { |
60 | $newPersonalUrls['mentordashboard'] = [ |
61 | 'id' => 'pt-mentordashboard', |
62 | 'text' => $skin->msg( 'growthexperiments-mentor-dashboard-pt-link' )->text(), |
63 | 'href' => SpecialPage::getTitleFor( 'MentorDashboard' )->getLocalURL(), |
64 | 'icon' => 'userGroup', |
65 | ]; |
66 | } |
67 | $newPersonalUrls[$key] = $link; |
68 | } |
69 | |
70 | $links['user-menu'] = $newPersonalUrls; |
71 | } |
72 | |
73 | /** |
74 | * @inheritDoc |
75 | */ |
76 | public function onBeforePageDisplay( $out, $skin ): void { |
77 | $user = $skin->getUser(); |
78 | |
79 | if ( |
80 | !$this->isDiscoveryEnabled( $user ) || |
81 | // do not show the blue dot if the user ever visited their mentor dashboard |
82 | $this->userOptionsLookup->getBoolOption( $user, self::MENTOR_DASHBOARD_SEEN_PREF ) || |
83 | // do not show the blue dot if the user is currently at their dashboard |
84 | $skin->getTitle()->equals( SpecialPage::getTitleFor( 'MentorDashboard' ) ) |
85 | ) { |
86 | return; |
87 | } |
88 | |
89 | $out->addModules( 'ext.growthExperiments.MentorDashboard.Discovery' ); |
90 | } |
91 | } |