-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStudyLinks.php
312 lines (307 loc) · 10.7 KB
/
StudyLinks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/*
* GNU General Public License v3.0
* Contributors: ADD YOUR NAME HERE, Mike P. Sinn
*/
namespace App\Studies;
use App\Buttons\Sharing\EmailSharingButton;
use App\Buttons\Sharing\FacebookSharingButton;
use App\Buttons\Sharing\RedditSharingButton;
use App\Buttons\Sharing\TwitterSharingButton;
use App\Buttons\States\StudyJoinStateButton;
use App\Buttons\States\StudyStateButton;
use App\VariableRelationships\QMVariableRelationship;
use App\VariableRelationships\QMUserVariableRelationship;
use App\Models\Vote;
use App\Slim\Model\StaticModel;
use App\Slim\View\Request\QMRequest;
use App\Traits\HasCauseAndEffect;
use App\Utils\IonicHelper;
use App\Utils\UrlHelper;
use InvalidArgumentException;
class StudyLinks extends StaticModel {
private $hasCauseAndEffect;
public $studyJoinLink;
public $studyLinkDynamic;
public $studyLinkEmail;
public $studyLinkFacebook;
public $studyLinkReddit;
public $studyLinkStatic;
public $studyLinkTwitter;
public $downVoteUrl;
public $upVoteUrl;
/**
* StudyImages constructor.
* @param HasCauseAndEffect|QMStudy $hasCauseAndEffect
*/
public function __construct($hasCauseAndEffect = null){
if(!$hasCauseAndEffect){return;}
$this->setHasCauseAndEffect($hasCauseAndEffect);
$this->getStudyLinkStatic();
$this->getStudyUrlDynamic();
$this->getStudyLinkFacebook();
$this->getStudyLinkReddit();
$this->getStudyLinkTwitter();
$this->getStudyLinkEmail();
$this->getStudyJoinUrl();
$this->getDownVoteUrl();
$this->getUpVoteUrl();
if(!$this->studyLinkDynamic){le("no studyLinkDynamic");}
}
/**
* @return string
*/
private function getStudyId(): string{
return $this->getHasCauseAndEffect()->getStudyId();
}
/**
* @return string
*/
private function getStudyTitle(): string{
if($this->hasCauseAndEffect && $this->getHasCauseAndEffect()->studyText){
return $this->getHasCauseAndEffect()->getStudyTitle();
}
return "Help us determine the effects of ".$this->getCauseVariableName()." on ".$this->getEffectVariableName()."!";
}
/**
* @return string
*/
private function getCauseVariableName(): string{
if(!$this->hasCauseAndEffect && $this->hasCauseAndEffect){
return $this->getHasCauseAndEffect()->getCauseVariableName();
}
$study = $this->getHasCauseAndEffect();
if(!$study){
le("No study!");
}
return $study->getCauseVariableName();
}
/**
* @return string
*/
private function getEffectVariableName(): string{
if(!$this->hasCauseAndEffect && $this->hasCauseAndEffect){
return $this->getHasCauseAndEffect()->getEffectVariableName();
}
return $this->getHasCauseAndEffect()->getEffectVariableName();
}
/**
* @return int
*/
private function getUserId(): ?int {
$correlation = $this->getHasCauseAndEffect();
if($correlation && isset($correlation->userId)){
return $correlation->userId;
}
$study = $this->getHasCauseAndEffect();
if($study && $study->getUserId()){
return $study->getUserId();
}
return null;
}
/**
* @return string
*/
public function getStudyLinkEmail(): string{
return $this->studyLinkEmail =
EmailSharingButton::getEmailShareLink($this->getStudyLinkStatic(), $this->getStudyTitle(),
"Check out my study at ");
}
/**
* @return string
*/
public function getStudyLinkFacebook(): string{
return $this->studyLinkFacebook = FacebookSharingButton::getFacebookShareLink($this->getStudyLinkStatic());
}
/**
* @return string
*/
public function getStudyLinkReddit(): string{
return $this->studyLinkReddit =
RedditSharingButton::getRedditTextLink($this->getStudyTitle(), $this->getStudyInvitationText());
}
public function getStudyInvitationTextShort(): string{
return "Please donate 30 seconds/day to help us discover the short and long-term effects of {$this->getCauseVariableName()} on {$this->getEffectVariableName()}! Thank you!";
}
public function getStudyInvitationText(): string{
$create = IonicHelper::getStudyCreationUrl();
return "
Hi!
I'm doing an observational study based on donated {$this->getCauseVariableName()} and {$this->getEffectVariableName()} data.
I'd be very grateful if you'd join to help us discover the short and long-term effects and what factors make it more or less effective.
It only requires about 30 seconds to click 2 buttons a day to record your {$this->getCauseVariableName()} and {$this->getEffectVariableName()}.
{$this->getStudyLinkStatic()}
";
//You can:
//Join this Study: {$this->getStudyJoinUrl()}
//or
//See Current Results: {$this->getStudyLinkStatic()}
//or
//Create a Different Study: {$create}
//
//Please let me know if you have any issues.
//Thank you!
// ";
}
/**
* @return string $studyLinkStatic
*/
public function getStudyLinkStatic(array $params = []): string{
$link = self::generateStudyLinkStatic($this->getStudyId(), $params);
return $this->studyLinkStatic = $link;
}
/**
* @param string|null $studyId
* @return string
*/
public static function generateStudyLinkStatic(string $studyId, array $params = []): string{
// For some reason study links keep having utopia.quantimo.do and local.quantimo.do in them
return QMRequest::getAppHostUrl("/study/$studyId", $params);
}
/**
* @return string $studyLinkTwitter
*/
public function getStudyLinkTwitter(): string{
return $this->studyLinkTwitter = TwitterSharingButton::getTwitterShareLink($this->getStudyLinkStatic(), $this->getStudyInvitationTextShort());
}
/**
* @param array $additionalParams
* @return string
*/
public function getStudyUrlDynamic(array $additionalParams = []): string{
$url = self::generateStudyUrlDynamic($this->getCauseVariableId(),
$this->getEffectVariableId(), $this->getUserId(), $this->getStudyId());
if($additionalParams){$url = UrlHelper::addParams($url, $additionalParams);}
return $this->studyLinkDynamic = $url;
}
/**
* @return string
*/
public function getRecalculateStudyUrl(): string{
$url = self::generateStudyUrlDynamic($this->getCauseVariableId(), $this->getEffectVariableId(), $this->getUserId(), $this->getStudyId());
return $url."&recalculate=true";
}
/**
* @return string
*/
public function getStudyJoinUrl(): string{
return $this->studyJoinLink = $this->getButton()->getUrl();
}
/**
* @return StudyJoinStateButton
*/
public function getButton(): StudyJoinStateButton {
return new StudyJoinStateButton($this->hasCauseAndEffect);
}
/**
* @return array
*/
private function getStudyJoinUrlParams(): array{
$params = [];
if($id = $this->getCauseVariableId()){$params['causeVariableId'] = $id;}
if($name = $this->getCauseVariableName()){ // Need name for populating text
$params['causeVariableName'] = rawurlencode($name);
}
if($id = $this->getEffectVariableId()){$params['effectVariableId'] = $id;}
if($name = $this->getEffectVariableName()){ // Need name for populating text
$params['effectVariableName'] = rawurlencode($name);
}
if($id = $this->getStudyId()){$params['studyId'] = rawurlencode($id);}
return $params;
}
/**
* @param HasCauseAndEffect|\App\Studies\QMStudy $study
*/
public function setHasCauseAndEffect($study){
$this->hasCauseAndEffect = $study;
}
/**
* @return string
*/
public function getDownVoteUrl(): string{
$url = Vote::generateVoteUrl($this->getStudyUrlParams());
$url = UrlHelper::addParam($url, 'vote', 'down');
return $this->downVoteUrl = $url;
}
/**
* @return string
*/
public function getUpVoteUrl(): string {
$url = Vote::generateVoteUrl($this->getStudyUrlParams());
$url = UrlHelper::addParam($url, 'vote', 'up');
return $this->upVoteUrl = $url;
}
/**
* @return array
*/
public function getStudyUrlParams(): array{
$params = $this->getStudyJoinUrlParams();
// Study ID should contain user id if necessary and adding this param could cause confusion
//if($this->getUserId()){$params['userId'] = $this->getUserId();}
return $params;
}
/**
* @param string|int $causeNameOrId
* @param string|int $effectNameOrId
* @param int|null $userId
* @param string|null $studyId
* @return string
*/
public static function generateStudyUrlDynamic($causeNameOrId, $effectNameOrId, int $userId = null,
string $studyId = null): string{
$params = [];
if(is_string($causeNameOrId)){
$params['causeVariableName'] = rawurlencode($causeNameOrId);
}else{
$params['causeVariableId'] = $causeNameOrId;
}
if(is_string($effectNameOrId)){
$params['effectVariableName'] = rawurlencode($effectNameOrId);
}else{
$params['effectVariableId'] = $effectNameOrId;
}
if($userId){
$params['userId'] = rawurlencode($userId);
}
if($studyId){
$params['studyId'] = rawurlencode($studyId);
}
if($studyId && is_numeric($studyId)){
throw new InvalidArgumentException("study id should not be numeric!");
}
return StudyStateButton::getStudyUrl($params);
}
/**
* @return QMStudy|HasCauseAndEffect
*/
public function getHasCauseAndEffect() {
if (!$this->hasCauseAndEffect) {
$this->hasCauseAndEffect = $this->getHasCauseAndEffect()->findInMemoryOrNewQMStudy();
}
return $this->hasCauseAndEffect;
}
/**
* @return int
*/
private function getCauseVariableId(): int{
if($this->hasCauseAndEffect){
return $this->getHasCauseAndEffect()->getCauseVariableId();
}
return $this->getHasCauseAndEffect()->getCauseVariableId();
}
/**
* @return int
*/
private function getEffectVariableId(): int{
if($this->hasCauseAndEffect){
return $this->getHasCauseAndEffect()->getEffectVariableId();
}
return $this->getHasCauseAndEffect()->getEffectVariableId();
}
public function getTitleAttribute(): string{
return "Study Links for ".$this->getStudyTitle();
}
public function getUrl(array $params = []): string{
return $this->getStudyLinkStatic();
}
}