Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
QuestionFormatter | |
0.00% |
0 / 54 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
formatUnarchived | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
formatArchived | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
getPostedOnHtml | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getRelativeTime | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\HelpPanel; |
4 | |
5 | use GrowthExperiments\Util; |
6 | use IContextSource; |
7 | use MediaWiki\Html\Html; |
8 | |
9 | class QuestionFormatter { |
10 | |
11 | /** |
12 | * @var IContextSource |
13 | */ |
14 | private $contextSource; |
15 | private $dataLinkId; |
16 | private $postedOnMsgKey; |
17 | private $archivedMsgKey; |
18 | private $archivedTooltipMsgKey; |
19 | /** |
20 | * @var QuestionRecord |
21 | */ |
22 | private $questionRecord; |
23 | |
24 | /** |
25 | * @param IContextSource $contextSource |
26 | * @param QuestionRecord $questionRecord |
27 | * @param string $dataLinkId |
28 | * @param string $postedOnMsgKey |
29 | * @param string $archivedMsgKey |
30 | * @param string $archivedTooltipMsgKey |
31 | */ |
32 | public function __construct( |
33 | IContextSource $contextSource, |
34 | QuestionRecord $questionRecord, |
35 | $dataLinkId, |
36 | $postedOnMsgKey, |
37 | $archivedMsgKey, |
38 | $archivedTooltipMsgKey |
39 | ) { |
40 | $this->contextSource = $contextSource; |
41 | $this->dataLinkId = $dataLinkId; |
42 | $this->postedOnMsgKey = $postedOnMsgKey; |
43 | $this->archivedMsgKey = $archivedMsgKey; |
44 | $this->questionRecord = $questionRecord; |
45 | $this->archivedTooltipMsgKey = $archivedTooltipMsgKey; |
46 | } |
47 | |
48 | /** |
49 | * @return string |
50 | */ |
51 | public function format() { |
52 | return $this->questionRecord->isArchived() ? $this->formatArchived() : $this->formatUnarchived(); |
53 | } |
54 | |
55 | private function formatUnarchived() { |
56 | return Html::rawElement( |
57 | 'div', |
58 | [ 'class' => 'question-link-wrapper' ], |
59 | Html::element( |
60 | 'a', |
61 | [ |
62 | 'class' => 'question-text', |
63 | 'href' => $this->questionRecord->getResultUrl(), |
64 | 'data-link-id' => $this->dataLinkId |
65 | ], |
66 | $this->questionRecord->getQuestionText() |
67 | ) |
68 | ) . $this->getPostedOnHtml(); |
69 | } |
70 | |
71 | private function formatArchived() { |
72 | return Html::rawElement( |
73 | 'div', |
74 | [ 'class' => 'question-link-wrapper question-archived' ], |
75 | Html::element( |
76 | 'span', |
77 | [ 'class' => 'question-text' ], |
78 | $this->questionRecord->getQuestionText() |
79 | ) . |
80 | ' (' . |
81 | Html::element( |
82 | 'a', |
83 | [ |
84 | 'href' => $this->questionRecord->getArchiveUrl(), |
85 | 'data-link-id' => $this->dataLinkId, |
86 | 'title' => $this->contextSource->msg( $this->archivedTooltipMsgKey )->text() |
87 | ], |
88 | $this->contextSource->msg( $this->archivedMsgKey )->text() |
89 | ) . |
90 | ')' |
91 | ) . |
92 | $this->getPostedOnHtml(); |
93 | } |
94 | |
95 | private function getPostedOnHtml() { |
96 | return Html::element( |
97 | 'span', |
98 | [ 'class' => 'question-posted-on' ], |
99 | $this->contextSource |
100 | ->msg( $this->postedOnMsgKey ) |
101 | ->params( $this->getRelativeTime() ) |
102 | ->text() |
103 | ); |
104 | } |
105 | |
106 | private function getRelativeTime() { |
107 | $elapsedTime = (int)wfTimestamp() - (int)wfTimestamp( |
108 | TS_UNIX, |
109 | $this->questionRecord->getTimestamp() |
110 | ); |
111 | return Util::getRelativeTime( $this->contextSource, $elapsedTime ); |
112 | } |
113 | } |