Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskExplanationWidget
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 7
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 getInfoRow
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getInfo
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getIcon
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getDescriptionRow
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDifficultyAndTime
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\HomepageModules\SuggestedEditsComponents;
4
5use GrowthExperiments\NewcomerTasks\Task\TaskSet;
6use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
7use OOUI\ButtonWidget;
8use OOUI\IconWidget;
9use OOUI\Tag;
10use OOUI\Widget;
11
12/**
13 * Server-side rendering of the TaskExplanationWidget in Suggested Edits.
14 *
15 * Corresponds roughly to TaskExplanationWidget.js
16 */
17class TaskExplanationWidget extends Widget {
18
19    /** @var \MessageLocalizer */
20    private $localizer;
21
22    /** @var TaskType */
23    private $taskType;
24
25    /** @inheritDoc */
26    public function __construct( array $config = [] ) {
27        parent::__construct( $config );
28        /** @var TaskSet|\StatusValue $taskSet */
29        $taskSet = $config['taskSet'];
30        if ( !$taskSet instanceof TaskSet || !$taskSet->count() ) {
31            return;
32        }
33        $this->taskType = $taskSet[0]->getTaskType();
34        /** @var \MessageLocalizer $localizer */
35        $this->localizer = $config['localizer'];
36
37        $this->appendContent(
38            ( new Tag( 'div' ) )
39                ->addClasses( [ 'suggested-edits-task-explanation-wrapper' ] )
40                ->appendContent(
41                    $this->getInfoRow(),
42                    $this->getDifficultyAndTime(),
43                    $this->getDescriptionRow()
44                )
45        );
46    }
47
48    /**
49     * @return Tag
50     */
51    private function getInfoRow(): Tag {
52        return ( new Tag( 'div' ) )
53            ->addClasses( [ 'suggested-edits-taskexplanation-additional-info' ] )
54            ->appendContent(
55                $this->getName(),
56                $this->getInfo()
57            );
58    }
59
60    /**
61     * @return Tag
62     */
63    private function getName(): Tag {
64        return ( new Tag( 'span' ) )
65            ->addClasses( [ 'suggested-edits-task-explanation-heading' ] )
66            ->appendContent( $this->taskType->getName( $this->localizer )->text() );
67    }
68
69    /**
70     * @return ButtonWidget
71     */
72    private function getInfo(): ButtonWidget {
73        return new ButtonWidget( [
74            'icon' => 'info-unpadded',
75            'framed' => false,
76            'label' => $this->taskType->getShortDescription( $this->localizer )->text(),
77            'invisibleLabel' => true,
78            'classes' => [ 'suggested-edits-task-explanation-info-button' ],
79        ] );
80    }
81
82    /**
83     * @return IconWidget|null
84     */
85    private function getIcon(): ?IconWidget {
86        $iconData = $this->taskType->getIconData();
87        if ( array_key_exists( 'icon', $iconData ) ) {
88            return new IconWidget(
89                [
90                    'icon' => $iconData['icon'],
91                    'classes' => [ 'suggested-edits-task-explanation-icon' ]
92                ]
93            );
94        }
95        return null;
96    }
97
98    /**
99     * @return Tag
100     */
101    private function getDescriptionRow(): Tag {
102        return ( new Tag( 'p' ) )
103            ->addClasses( [ 'suggested-edits-short-description' ] )
104            ->appendContent( $this->taskType->getShortDescription( $this->localizer )->text() );
105    }
106
107    /**
108     * @return Tag
109     */
110    private function getDifficultyAndTime(): Tag {
111        $difficulty = $this->taskType->getDifficulty();
112        return ( new Tag( 'div' ) )
113            ->addClasses( [ 'suggested-edits-taskexplanation-difficulty-and-time' ] )
114            ->appendContent(
115                ( new Tag( 'div' ) )
116                    ->addClasses( [ 'suggested-edits-difficulty-time-estimate' ] )
117                    ->appendContent(
118                        $this->getIcon() ?? '',
119                        ( new Tag( 'div' ) )->addClasses( [
120                            // The following classes are generated here:
121                            // * suggested-edits-difficulty-indicator-easy
122                            // * suggested-edits-difficulty-indicator-medium
123                            // * suggested-edits-difficulty-indicator-hard
124                            'suggested-edits-difficulty-indicator suggested-edits-difficulty-indicator-' . $difficulty
125                        ] )->appendContent(
126                            $this->localizer->msg(
127                                'growthexperiments-homepage-suggestededits-difficulty-indicator-label-' .
128                                $difficulty
129                            )->text()
130                        ),
131                        ( new Tag( 'div' ) )->addClasses( [
132                            'suggested-edits-difficulty-level'
133                        ] )->appendContent( $this->taskType->getTimeEstimate( $this->localizer )->text() )
134                    )
135            );
136    }
137}