-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStudyImages.php
373 lines (372 loc) · 13 KB
/
StudyImages.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/*
* GNU General Public License v3.0
* Contributors: ADD YOUR NAME HERE, Mike P. Sinn
*/
namespace App\Studies;
use App\Buttons\States\PredictorSearchStateButton;
use App\Computers\ThisComputer;
use App\VariableRelationships\QMVariableRelationship;
use App\Models\GlobalVariableRelationship;
use App\Models\UserVariableRelationship;
use App\Repos\ImagesRepo;
use App\Traits\HasCauseAndEffect;
use App\Types\QMStr;
use App\UI\CssHelper;
use App\UI\HtmlHelper;
use App\UI\ImageHelper;
use App\Utils\AppMode;
use App\Variables\QMVariableCategory;
use Illuminate\Support\Collection;
class StudyImages {
public const ROBOT_PUZZLED_PNG = 'https://static.quantimo.do/img/robots/quantimodo-robot-puzzled-213-300.png'; // Need PNG instead of SVG so it works in emails
private $hasCorrelationCoefficient;
private $hasCauseAndEffect;
public $causeVariableImageUrl;
public $causeVariableSvgUrl;
public $causeVariablePngUrl;
public $causeVariableIonIcon;
public $effectVariableImageUrl;
public $effectVariableSvgUrl;
public $effectVariablePngUrl;
public $effectVariableIonIcon;
public $gaugeImage;
public $gaugeImageSquare;
public $gaugeSharingImageUrl;
public $imageUrl;
public $robotSharingImageUrl;
public $avatar;
/**
* StudyImages constructor.
* @param GlobalVariableRelationship|UserVariableRelationship|QMVariableRelationship|null $hasCorrelationCoefficient
* @param HasCauseAndEffect|QMStudy|null $hasCauseAndEffect
*/
public function __construct($hasCorrelationCoefficient = null, $hasCauseAndEffect = null){
if($hasCorrelationCoefficient){
$this->hasCorrelationCoefficient = $hasCorrelationCoefficient;
}
if($hasCauseAndEffect){
$this->hasCauseAndEffect = $hasCauseAndEffect;
}
if(!$hasCauseAndEffect && !$hasCorrelationCoefficient){
return;
}
$this->setVariableIconsAndImages();
$this->gaugeSharingImageUrl = $this->setRobotSharingImageUrl();
$this->gaugeImage = $this->gaugeImageSquare = ImageHelper::getRobotPuzzledUrl();
if($hasCorrelationCoefficient && $hasCorrelationCoefficient->getCorrelationCoefficient()){
$this->setGaugeImage();
$this->setGaugeImageSquare();
$this->setGaugeSharingImageUrl();
}
$this->getImage();
}
/**
* @return QMStudy|HasCauseAndEffect
*/
public function getHasCauseAndEffect(){
return $this->hasCauseAndEffect ?? $this->hasCorrelationCoefficient;
}
/**
* @return QMVariableCategory
*/
private function getCauseVariableCategory(): QMVariableCategory{
if($this->hasCorrelationCoefficient && $this->getCorrelation()->getCauseQMVariableCategory()){
return $this->getCorrelation()->getCauseQMVariableCategory();
}
$study = $this->getHasCauseAndEffect();
if(!$study){
le("No Study!");
}
return $this->getHasCauseAndEffect()->getCauseQMVariableCategory();
//return $study->getOrSetCauseQMVariable()->getQMVariableCategory();
}
/**
* @return QMVariableCategory
*/
private function getEffectVariableCategory(): QMVariableCategory{
if($this->hasCorrelationCoefficient && $this->getCorrelation()->getEffectQMVariableCategory()){
return $this->getCorrelation()->getEffectQMVariableCategory();
}
return $this->getHasCauseAndEffect()->getOrSetEffectQMVariable()->getQMVariableCategory();
}
private function setVariableIconsAndImages(){
if(!isset($this->causeVariableImageUrl) || !$this->causeVariableImageUrl){
$this->causeVariableImageUrl = $this->causeVariableSvgUrl = $this->getCauseVariableCategory()->getSvgUrl();
$this->causeVariablePngUrl = $this->getCauseVariableCategory()->getPngUrl();
}
if(!isset($this->causeVariableIonIcon) || !$this->causeVariableIonIcon){
$this->causeVariableIonIcon = $this->getCauseVariableCategory()->getIonIcon();
}
if(!isset($this->effectVariableImageUrl) || !$this->effectVariableImageUrl){
$this->effectVariableImageUrl = $this->effectVariableSvgUrl = $this->getEffectVariableCategory()->getSvgUrl();
$this->effectVariablePngUrl = $this->getEffectVariableCategory()->getPngUrl();
}
if(!isset($this->effectVariableIonIcon) || !$this->effectVariableIonIcon){
$this->effectVariableIonIcon = $this->getEffectVariableCategory()->getIonIcon();
}
}
/**
* @return string
*/
private function setGaugeImage(): string {
return $this->gaugeImage = self::generateGaugeUrl($this->getCorrelation()->getEffectSize());
}
/**
* @return void
*/
private function setGaugeImageSquare(): void{
$this->gaugeImageSquare = ImageHelper::getImageUrl('gauges/200-200/' .
$this->getGaugeFilename($this->getCorrelation()->getEffectSize()) . '-200-200.png');
}
/**
* @param string $effectSize
* @return string
*/
public static function getGaugeFilename(string $effectSize): string{
$fileName = 'gauge-'.str_replace(" ", "-", $effectSize).'-relationship';
$fileName = str_replace('very-', '', $fileName); // TODO: Make more gauge images
return $fileName;
}
/**
* @param string $effectSize
* @return string
*/
public static function generateGaugeUrl(string $effectSize): string{
return ImageHelper::getImageUrl('gauges/246-120/'.self::getGaugeFilename($effectSize).'.png');
}
/**
* @return string
*/
private function setGaugeSharingImageUrl(): string {
return $this->gaugeSharingImageUrl = self::generateGaugeSharingImageUrl($this->getCorrelation()->getEffectSize(),
$this->getCauseVariableCategory(), $this->getEffectVariableCategory());
}
/**
* @param string $effectSize
* @param string|int|QMVariableCategory $causeCategory
* @param string|int|QMVariableCategory $effectCategory
* @return string
*/
public static function generateGaugeSharingImageUrl(string $effectSize, $causeCategory, $effectCategory): string{
return QMVariableRelationship::S3_IMAGE_PATH.QMVariableRelationship::GAUGE_IMAGE_PATH . self::getGaugeFilename($effectSize) .
'_' . self::getCauseEffectFileName($causeCategory, $effectCategory) . '_background.png';
}
/**
* @param QMVariableCategory|int|string $causeCategory
* @param QMVariableCategory|int|string $effectCategory
* @return string
*/
private static function getCauseEffectFileName($causeCategory, $effectCategory): string{
if(!$causeCategory instanceof QMVariableCategory){
$causeCategory = QMVariableCategory::find($causeCategory);
}
if(!$effectCategory instanceof QMVariableCategory){
$effectCategory = QMVariableCategory::find($effectCategory);
}
return $causeCategory->getStudyImageFileName().'_'.$effectCategory->getStudyImageFileName();
}
/**
* @return string
*/
private function setRobotSharingImageUrl(): string{
return $this->robotSharingImageUrl = self::generateVariableCategoriesRobotSharingImageWithBackgroundUrl($this->getCauseVariableCategory(), $this->getEffectVariableCategory());
}
/**
* @param QMVariableCategory|int|string $causeCategory
* @param QMVariableCategory|int|string $effectCategory
* @return string
*/
public static function generateVariableCategoriesRobotSharingImageWithBackgroundUrl($causeCategory, $effectCategory): string{
return ImageHelper::STATIC_BASE_URL.QMVariableRelationship::VARIABLE_CATEGORIES_COMBINED_ROBOT_BACKGROUND .
self::getCauseEffectFileName($causeCategory, $effectCategory) . '_robot_background.png';
}
/**
* @param QMVariableCategory|int|string $causeCategory
* @param QMVariableCategory|int|string $effectCategory
* @return string
*/
public static function generateCategoriesWithoutBackgroundRobotImageUrl($causeCategory, $effectCategory): string{
return QMVariableRelationship::S3_IMAGE_PATH.QMVariableRelationship::VARIABLE_CATEGORIES_COMBINED_ROBOT .
self::getCauseEffectFileName($causeCategory, $effectCategory) . '_robot.png';
}
/**
* @param string $gauge
* @param string $tagLine
* @return string
*/
public static function generateGaugeImagesTagLine(string $gauge, string $tagLine): string{
$html = "
<div style=\"text-align: center; max-width: 95%; margin: auto\">
<div> $gauge </div>
<div> $tagLine </div>
</div>
";
if(AppMode::isTestingOrStaging()){
$html = HtmlHelper::checkForMissingHtmlClosingTags($html, __FUNCTION__);
}
if(str_contains($html, "/public/img/")){
le("getGaugeAndImagesWithTagLine should not be $html");
}
QMStr::errorIfLengthGreaterThan($html, __FUNCTION__, 100);
return $html;
}
/**
* @param QMVariableCategory|int|string $causeCategory
* @param QMVariableCategory|int|string $effectCategory
* @return string
*/
public static function generateSmallCategoriesRobotImageUrl($causeCategory, $effectCategory): string{
return QMVariableRelationship::S3_IMAGE_PATH.QMVariableRelationship::VARIABLE_CATEGORIES_COMBINED_ROBOT_SMALL .
self::getCauseEffectFileName($causeCategory, $effectCategory) . '_robot-600-315.png';
}
/**
* @return QMVariableRelationship|GlobalVariableRelationship|UserVariableRelationship
*/
public function getCorrelation() {
return $this->hasCorrelationCoefficient;
}
/**
* @param GlobalVariableRelationship[]|Collection $correlations
* @param string|null $title
* @param string|null $description
* @param bool $addSharingButtons
* @return string
*/
public static function getStudiesListWithGauges($correlations, ?string $title, ?string $description,
bool $addSharingButtons = false): string{
if(!$correlations){
le("No user_variable_relationships provided to " . __FUNCTION__);
}
$html = '';
if($title){
$html .= CssHelper::addTitleCss($title);
}
if($description){
$html .= CssHelper::addBodyCss($description);
}
$c = QMVariableRelationship::getFirst($correlations);
$html .= '
<div style="display: inline-block;">
<img style="max-width: 100%;"
src="' . $c->getStudyImages()->getRobotSharingImageUrl() . '"
alt="robot image">
</div>
';
foreach($correlations as $c){
$html .= $c->getTitleGaugesTagLineHeader(true, true);
if($addSharingButtons){ // TODO: Make sharing buttons safe for email so we can always add them
$html = $c->getOrAddSocialSharingButtons($html);
}
$html .= $c->getButton()->getRoundOutlineWithIcon();
}
$html .= PredictorSearchStateButton::getMoreDiscoveriesButtonHtml();
//$html .= HtmlHelper::getStartTrackingHtml();
//$html .= HtmlHelper::getBrownSocialLinksHtml();
//$html .= DownloadButtonsQMCard::getDownloadButtonsHtml();
$html = "
<div class=\"page-wrapper\" style=\"font-family: sans-serif; text-align: center;\">
$html
</div>
";
QMStr::errorIfLengthGreaterThan($html, __FUNCTION__, 30);
return $html;
}
/**
* @return string
*/
public function getGaugeImage(): string{
return $this->gaugeImage ?: $this->setGaugeImage();
}
/**
* @return string
*/
public function getRobotSharingImageUrl(): string{
return $this->robotSharingImageUrl ?: $this->setRobotSharingImageUrl();
}
/**
* @return string
*/
public function getCauseVariableImageUrl(): string{
return $this->causeVariableImageUrl;
}
/**
* @return string
*/
public function getEffectVariableImageUrl(): string{
return $this->effectVariableImageUrl;
}
/**
* @return string
*/
public function getCauseVariablePngUrl(): string {
$url = $this->causeVariablePngUrl;
if(strpos($url, "/public/img/") !== false){
le("causeVariablePngUrl should not be $url");
}
return $url;
}
/**
* @return string
*/
public function getCauseVariableSvgUrl(): string{
$url = $this->causeVariableSvgUrl;
if(strpos($url, "/public/img/") !== false){
le("causeVariableSvgUrl should not be $url");
}
return $this->causeVariableSvgUrl;
}
/**
* @return string
*/
public function getEffectVariablePngUrl(): string{
return $this->effectVariablePngUrl;
}
/**
* @return string
*/
public function getEffectVariableSvgUrl(): string{
return $this->effectVariableSvgUrl;
}
/**
* @return string
*/
public function getAvatar(): string {
if($this->hasCauseAndEffect && $this->getHasCauseAndEffect()->principalInvestigator && $this->getHasCauseAndEffect()->getPrincipalInvestigator()
->hasNonGravatarOrNonDefaultAvatar()){
$this->avatar = $this->getHasCauseAndEffect()->getPrincipalInvestigator()->getAvatar();
}else if($this->getGaugeImage()){
$this->avatar = $this->getGaugeImage();
}
return $this->avatar;
}
/**
* @return string
*/
public function getImage(): string {
$c = $this->getCorrelation();
if($c && $c->getCorrelationCoefficient()){
$this->imageUrl = $this->setGaugeSharingImageUrl();
}
if (!$this->imageUrl) {
$this->imageUrl = $this->setRobotSharingImageUrl();
}
return $this->imageUrl;
}
/**
* @return string
*/
public static function getRobotPuzzledHtml(): string {
return ImageHelper::getRobotPuzzledHtml();
}
public static function generateCategoryImages(){
ThisComputer::exec("bash scripts/generate_study_images.sh");
}
/**
* @noinspection PhpUnused
*/
public static function uploadStudyImages(){
ImagesRepo::convertLargePngsToJpgs();
ImagesRepo::uploadToS3Public();
}
}