-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixformula.php
1548 lines (1333 loc) · 63.1 KB
/
matrixformula.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
res hours: assume repeat each year
hours: ok
travel: ok
years: adds to year start
year start: calculate what timeline years are committed
brainless: alters tally
items: separate brainless, update to timeline[] on fly, in final calc delimit brainless, push to result[]
visions: final calc, delimit brainless, push to result[]
summary: final calc, separate brainless, push to result[]
*/
// would having a single array built from the loops, then all calculations done at the end, be easier to maintain code than the current cross-cutting structure?
// start query time test
$starttime = microtime(true);
// declare time zone
date_default_timezone_set('Africa/Lagos');
require_once('headerDB.inc.php');
if (isset($_POST["qLimit"])) { $qLimit = $_POST["qLimit"]; } else { $qLimit = 'a'; }
if (isset($_POST["vLimit"]) && $_POST["vLimit"] != '') { $vLimit = $_POST["vLimit"]; } else { $vLimit = false; }
// initialise array used to collate calculation results
$result = array();
/* example result
$result[] = array(
'type' => 'a', // ie goal, summary
'visId' => '0',
'id' => '0', // item id
'attrId' => '0',
'tline' => 'a', // timeline pair a or b for brainless or not
'form' => 'a', // summary pair a or b for pos or neg
'value' => '0');
*/
// initialise temporary array to collate value sums
$valuessum = array();
// initialise temporary array to collate context sums
$contextssum = array();
// initialise temporary array to collate scores for optimising
$scores = array();
// initialise temporary array to collate years for value scores
$yrsval = array();
// initialise temporary array to collate timeline results
$timeline = array();
// initialise temporary array for null item timeline values
$tlineNull = array();
// initialise temporary array to collate probability results
$certainties = array();
// call all visions
$values = array();
$values['filterquery'] = ' WHERE ia.type = "v" ';
if ($vLimit) $values['filterquery'] .= ' AND ia.itemId IN (' . $vLimit . ') ';
$visions = query("getitems",$config,$values,$sort);
// call values parent id
$values = array();
$values['qQuery'] = "`format`";
$values['qValue'] = 'unqvalues';
$qualities = query("getqualities",$config,$values,$sort);
foreach ((array) $qualities as $qual) {
if ($qual['format'] == 'unqvalues') $valParId = $qual['qId'];
}
// call context parent id
$values = array();
$values['qQuery'] = "`format`";
$values['qValue'] = 'unqcontext';
$qualities = query("getqualities",$config,$values,$sort);
foreach ((array) $qualities as $qual) {
if ($qual['format'] == 'unqcontext') $cntxtParId = $qual['qId'];
}
// call all attributes, exclude angles and qualities
$values = array();
$values['qQuery'] = "`qType`";
$values['qValue'] = 'attribute';
$values['qSearch'] = "`disp`";
$values['qNeedle'] = $qLimit;
$attributes = query("getqualities",$config,$values,$sort);
// if using unqhoursyear = 2500 unqhoursyearbrainless = 750
// for instance to show timeline where over or under committed
// need to recall above query with qValue = 'variable'
// set configuration variables and arrays relating to time
// holds ids for all variables that count hours
$unqhoursIds = array ();
// holds ids for all variables of years on timeline
$unqtimelineIds = array ();
// holds ids for all variables with scores
$scoreqIds = array();
// holds ids for any variables not to directly calculate
$skipqIds = array();
// optimisation array to capture item scores
$scoreweights = array();
// default not to calculate optimisation
$optim = false;
foreach ((array) $attributes as $attr) {
// variable to flag when travel hours are being counted, and treat as brainless
// attr: Travel / Year (int)
if ($attr['format'] == 'unqhourstravel') { $travelId = $attr['qId']; $unqhoursIds[] = $travelId; }
// attr: Effort / Year (int)
if ($attr['format'] == 'unqhours') { $hoursId = $attr['qId']; $unqhoursIds[] = $attr['qId']; }
// attr: Hours research (int)
if ($attr['format'] == 'unqhoursresearch') { $reseId = $attr['qId']; $unqhoursIds[] = $attr['qId']; }
// attr: Years (int)
if ($attr['format'] == 'unqyears') $yearqId = $attr['qId'];
// attr: Years probability (int)
if ($attr['format'] == 'unqyearsprob') $yearsprobId = $attr['qId'];
// attr: Brainless hours (boolean)
if ($attr['format'] == 'unqbrainless') $brainlessId = $attr['qId'];
// attr: [one of the n years duration of timeline] (int)
if ($attr['format'] == 'unqtimeline') { $unqtimelineIds[$attr['title']] = $attr['qId']; $skipqIds[] = $attr['qId']; }
// attr: Year Start (date)
if ($attr['format'] == 'unqyearstart') $yearstqId = $attr['qId'];
// attr: Year End (date)
if ($attr['format'] == 'unqyearend') $yearenqId = $attr['qId'];
// variable to flag optimise preferences attribute id
if ($attr['format'] == 'unqoptimisepref') { $unqoptimiseprefId = $attr['qId']; $skipqIds[] = $attr['qId']; $optim = true; }
// variable to flag optimise balance attribute id
if ($attr['format'] == 'unqoptimisebala') { $unqoptimisebalaId = $attr['qId']; $skipqIds[] = $attr['qId']; $optim = true; }
// variable to flag values sum attribute id
if ($attr['format'] == 'unqvaluessum') { $unqvaluessumId = $attr['qId']; $skipqIds[] = $attr['qId']; }
// attr: Value Sum / Hours (int)
if ($attr['format'] == 'unqvaluessumhrs') { $unqtimelineIds[0] = $attr['qId']; $skipqIds[] = $attr['qId']; } // not clear why unqvaluessumhrs needs to be in $unqtimelineIds array
// variable to flag contexts sum attribute id
if ($attr['format'] == 'unqcontextssum') { $unqcontextssumId = $attr['qId']; $skipqIds[] = $attr['qId']; }
// attr: Context Sum / Hours (int)
if ($attr['format'] == 'unqcontextssumhrs') { $unqcontextssumhrs = $attr['qId']; $skipqIds[] = $attr['qId']; }
// array to flag when scores are being used to check their probability, and to optimise weighting after calculations
if ($attr['format'] == 'score') { $scoreqIds[$attr['qId']] = $attr['weight']; $scoreweights[$attr['qId']] = NULL; }
// array to skip attributes not to calculate
if ($attr['formulaSum1'] == null &&
$attr['formulaSum2'] == null &&
$attr['formulaVis1'] == null &&
$attr['formulaSum2'] == null
) $skipqIds[] = $attr['qId'];
// attr: Certainty of completion (int)
if ($attr['format'] == 'unqprobability') { $skipqIds[] = $attr['qId']; $unqprobabilityId = $attr['qId']; }
// skip all probability items as they are currently called from the attribute itself
if ($attr['format'] == 'probability') $skipqIds[] = $attr['qId'];
// attr: Career (boolean)
if ($attr['format'] == 'unqcareer') $careerId = $attr['qId'];
}
// score optimisation
ksort($scoreqIds);
ksort($scoreweights);
if ($optim) {
$scoreopt = array();
$scoreopt[] = array('type' => 'x', 'form' => 'a', 'scores' => $scoreweights);
$scoreopt[] = array('type' => 'x', 'form' => 'b', 'scores' => $scoreweights);
foreach ((array) $visions as $visn) $scoreopt[] = array('type' => 'v', 'visId' => $visn['itemId'], 'scores' => $scoreweights);
}
// call itemMeta types
$values = array();
$values['qQuery'] = "`qType`";
$values['qValue'] = 'itemMeta';
$qualMeta = query("getqualities",$config,$values,$sort);
foreach ((array) $qualMeta as $qualMet) {
// attr: Checklist assigned to vision is someday
if ($qualMet['format'] == 'someday' && $qualMet['typeReq'] == 'cl') $listSomedayId = $qualMet['qId'];
}
// forces json return for these vision attributes even if calculation finds no positive matches (ie if all items set to someday then recalc otherwise doesn't refresh vision)
// to do: move function to client side script
$resForce = array ('unqhoursresearch', 'score', 'integer', 'unqhours', 'unqhourstravel'/*, 'unqyears'*/);
// special case for years
$visYrs = array();
// loop visions
foreach ((array) $visions as $visn) {
if ($visn['isSomeday'] == 'n' && $visn['dateCompleted'] == '') { $active = 1; } else { $active = 0; }
$visYrs[$visn['itemId']] = array('yrs' => 0, 'yearst' => 0, 'active' => $active);
}
// loop attributes
foreach ((array) $attributes as $attr) {
// skip attribute if not to calculate
if (in_array($attr['qId'], $skipqIds, true)) continue;
// variables to store grand totals, #1 is for row A, #2 for row B
#$summary1 = '';
#$summary2 = '';
$summary1 = (float) 0;
$summary2 = (float) 0;
// special case effort/year
$summaryBless1 = (float) 0;
$summaryBless2 = (float) 0;
// set variable for score weighting for attribute
if (!is_null($attr['weight']) && isset($attr['weight'])) {
$weight = (float) $attr['weight'];
} else {
$weight = (float) 10;
}
//$weight = $weight / 10; // original weighting, range 0 to 1.0
//$weight = ($weight / 20) + 0.5; // moderated to not disfavour low weighting too much, set range 0.5 to 1.0
$weight = (float) 1;
// loop visions
foreach ((array) $visions as $visn) {
// initialise sql parameters
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $attr['qId'];
// call the vision's child records from main table for current attribute
$items = query("lookupqualities",$config,$values,$sort);
// special cases of value for calculation held in other table
// case: checklist hours from checklist table
if ($attr['qId'] == $hoursId) {
// query the list lookup table
$values = array();
$values['parentId'] = $visn['itemId'];
$values['type'] = 'c';
$itemVars = query("getchildlists",$config,$values,$sort);
if (!empty($itemVars) && is_array($itemVars) && count($itemVars) > 0) {
foreach ((array) $itemVars as $item) {
$values['listId'] = $item['listId'];
$res = query("selectchecklist",$config,$values,$sort);
if (count($res) > 0 && is_array($res)) {
if (!is_array($items)) $items = array(); // error handling where no child match in main table, but match from other table, ie checklist hours
$items[] = array('visId' => $visn['itemId'], 'itemId' => $item['listId'], 'qId' => $hoursId, 'itemType' => 'c', 'value' => $res[0]['effort']);
}
}
}
}
// initialise variables to store totals, #0 for combining text results from #1 and #2 after calculation
// #1 for actual result, and #2 as supplementary paired result for #1
#$output = '';
#$output1 = '';
#$output2 = '';
#$pseudo2 = '';
$output = (float) 0;
$output1 = (float) 0;
$output2 = (float) 0;
$pseudo2 = (float) 0;
// special case effort/year
$bless1 = (float) 0;
$bless2 = (float) 0;
// special case for years
$outputYrs = (float) 0;
#$outputYear = false;
$outputYear = (float) 0;
// confirm the vision has child record(s) in main table for current attribute
if (!is_array($items) || empty($items) || count($items) == 0) continue;
// loop vision's child records
foreach ((array) $items as $item) {
// skip zero value items
// except effort, as triggers year records
// all probability items are currently called from the attribute itself
if ($item['value'] == 0 && $attr['qId'] != $hoursId) {
if ($optim && array_key_exists($attr['qId'], $scoreqIds)) $scores[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'attrId' => $attr['qId'], 'value' => $item['value']);
continue;
}
// control for the case that it was once a child of the vision
// however artifact records remain in the main table and must not be counted
// to do: maybe update pages where lists are unassigned from items so that main table records are deleted
// for lists
if ($item['itemType'] == 'c' || $item['itemType'] == 'l') {
// default to counted in case no record in lookup
$someday = false;
$complete = false;
// query the list lookup table to check if vision is a parent
$values = array();
$values['parentId'] = $visn['itemId'];
$values['type'] = $item['itemType'];
$values['listId'] = $item['itemId'];
$itemVars = query("getchildlists",$config,$values,$sort);
if (!empty($itemVars) && is_array($itemVars) && count($itemVars) > 0) {
// if so look up qualities if someday
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $listSomedayId;
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort);
if (!empty($itemVars) && is_array($itemVars) && count($itemVars) > 0) {
if ($itemVars[0]['value'] == 'y') {
$someday = true;
$complete = true;
} else {
$someday = false;
$complete = false;
}
}
} else {
// list is not a child
continue;
}
// else item is not a list
} else {
// query items parents
// to do: see if this can be queried from the looping vision array
$values = array();
$values['itemId'] = $item['itemId'];
$itemPars = query("lookupparentshort",$config,$values,$sort);
// for false positives, prepare to escape
$child = false;
// confirms that item is child to vision parent
if (!empty($itemPars) && is_array($itemPars) && count($itemPars) > 0) {
foreach ((array) $itemPars as $pars) if ($pars['parentId'] == $visn['itemId']) $child = true;
}
// item is not a child
if (!$child) continue;
// query item status to count or not
$itemVars = query("selectitemstatus",$config,$values,$sort);
($itemVars[0]['isSomeday'] == 'y' ? $someday = true : $someday = false);
($itemVars[0]['dateCompleted'] == '' ? $complete = false : $complete = true);
}
// query years duration recorded against item, ie project, goal etc
// for use in calculation of scores, ie project yielding x value and taking n years provides annual value of x/n
// ignore if record is not a score or count of hours
// estimate years if probability assigned
// to do: put years/probs into array rather than requery to opmitise
if (array_key_exists($attr['qId'], $scoreqIds) ||
in_array($attr['qId'], $unqhoursIds, true)) {
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $yearqId;
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort);
if (empty($itemVars) || !is_array($itemVars) || $itemVars[0]['value'] == '' || $itemVars[0]['value'] == 0) {
$years = (float) 1;
} else {
$years = (float) $itemVars[0]['value'];
}
$values['qId'] = $yearsprobId;
$itemVars = query("lookupqualities",$config,$values,$sort);
if (empty($itemVars) || !is_array($itemVars)) {
$yearsprob = (float) 1;
} else {
if ($itemVars[0]['value'] == '') {
$yearsprob = (float) 1;
} else {
$yearsprob = (float) $itemVars[0]['value'];
$yearsprob /= 10;
}
}
// estimate years
$years = (float) ceil($years * ($yearsprob > 0 ? 1/$yearsprob : 0));
}
// query if project, goal etc can be done during brain down time (brainless)
// for counting time separately
if ($item['qId'] == $travelId) { // override travel
$brainless = 'y';
} elseif ($item['qId'] == $reseId) { // override research
$brainless = 'n';
} else {
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $brainlessId;
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort);
if (empty($itemVars) || !is_array($itemVars)) {
$brainless = 'n';
} else {
$brainless = $itemVars[0]['value'];
// default not brainless
if ($brainless == '') $brainless = 'n';
}
}
// default probability
$prob = 1;
// check if attribute has probabilities
if (!is_null($attr['probId'])) {
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $attr['probId'];
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort); // this could just be checked in $items array
if (!empty($itemVars) && is_numeric($itemVars[0]['value'])) {
$prob = (float) $itemVars[0]['value'];
$prob /= 10;
// capture p for calculating Certainty
// record active status
if (!$someday && !$complete) { $active = 1; } else { $active = 0; }
// update value sums array value
$certainties[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'value' => $prob, 'active' => $active);
} else {
$prob = (float) 1;
}
}
// finish setting the variables for calculation
// $value, $prob, $weight, $years, $someday, $complete, $brainless, $output1, $output2, $summary1, $summary2
$value = (float) $item['value'];
// eval to execute as script value for attribute's formula for vision
// updates the $output1 and $output2 variables, formulae defined in gtdfuncs.php
if (!$someday && !$complete) {
eval(formula($attr['formulaVis1']));
/* file_put_contents ('a.txt',
formula($attr['formulaVis1']) . "\n" .
print_r($attr,true) . "\n" .
gettype($output1) . "\n" .
'$output1 ' . $output1 ."\n" ."\n" .
gettype($output1) . "\n" .
'$pseudo2 ' . $pseudo2 ."\n" ."\n" .
gettype($value) . "\n" .
'$value ' . $value . "\n" ."\n" .
gettype($prob) . "\n" .
'$prob ' . $prob . "\n" ."\n" .
gettype($weight) . "\n" .
'$weight ' . $weight . "\n" ."\n" .
gettype($years) . "\n" .
'$years ' . $years . "\n"
);*/
eval(formula($attr['formulaVis2']));
}
// calculate value
$sum = 0;
eval(formula('cubeYrItem'));
// update scores array
if (array_key_exists($attr['qId'], $scoreqIds)) $scores[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'attrId' => $attr['qId'], 'value' => $sum);
// special case for attributes scoring values
if ($attr['parId'] == $valParId) {
// search if sum captured already from another value attribute, if so reuse sum
$i = 0;
foreach ((array) $valuessum as $valuesum) {
if ($valuesum['type'] == $item['itemType'] &&
$valuesum['visId'] == $visn['itemId'] &&
$valuesum['id'] == $item['itemId']
) {
$sum += $valuesum['value'];
// remove record
unset($valuessum[$i]);
// reset array keys
$valuessum = array_values($valuessum);
// stop searching
break;
}
$i++;
}
// record active status
if (!$someday && !$complete) { $active = 1; } else { $active = 0; }
// update value sums array value
$valuessum[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'value' => $sum, 'active' => $active);
}
// special case for attributes scoring context
if ($attr['parId'] == $cntxtParId) {
// if ($attr['qId'] == 41) {
// search if sum captured already from another context attribute, if so reuse sum
$i = 0;
foreach ((array) $contextssum as $contextsum) {
if ($contextsum['type'] == $item['itemType'] &&
$contextsum['visId'] == $visn['itemId'] &&
$contextsum['id'] == $item['itemId']
) {
$sum += $contextsum['value'];
// remove record
unset($contextssum[$i]);
// reset array keys
$contextssum = array_values($contextssum);
// stop searching
break;
}
$i++;
}
// record active status
if (!$someday && !$complete) { $active = 1; } else { $active = 0; }
// update context sums array value
$contextssum[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'value' => $sum, 'active' => $active);
}
// special case for attributes counting hours
// to do: incorporate formulae, and prob formula, into eval
if (in_array($item['qId'], $unqhoursIds, true)) {
// query year start
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $yearstqId;
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort);
if (!empty($itemVars) && is_numeric($itemVars[0]['value'])) {
$yearst = (float) $itemVars[0]['value'];
$tlineNull[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'itemId' => $item['itemId']);
} else {
// default this year
$yearst = (float) date("Y");
}
// query year end
$values = array();
$values['visId'] = $visn['itemId'];
$values['qId'] = $yearenqId;
$values['itemId'] = $item['itemId'];
$values['itemType'] = $item['itemType'];
$itemVars = query("lookupqualities",$config,$values,$sort);
// default item years
$in = (float) 0; // inclusive of year itself
if (!empty($itemVars) && is_numeric($itemVars[0]['value'])) {
$yearen = (float) $itemVars[0]['value'];
$in++;
$tlineNull[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'itemId' => $item['itemId']);
} else {
$yearen = $yearst + $years;
}
// set years duration
$yearsdur = $yearen + $in - $yearst;
// calculate number of years diff: current year - year start
$d1 = DateTime::createFromFormat("Y",$yearst);
$d2 = new DateTime("Y");
// add 1 minute difference
$d2->add(new DateInterval('PT1M'));
$diff = $d2->diff($d1);
$yrdiff = $diff->y;
/* cases:
item year start in future, starts within timeline and ends before timeline end
2020 2 yr
item year start in future, starts within timeline and ends after timeline end
2020 4 yr
item year start in future, starts after timeline end
2023 1 yr
item year start this year, and ends before timeline end
'' 4 yr
item year start this year, and ends after timeline end
'' 6 yr
item year start in past, and ends before timeline start
2010 9 yr
item year start in past, and ends before timeline end
2010 12 yr
item year start in past, and ends after timeline end
2010 14 yr
brainless
not brainless
probability applied to research
probability applied to hours
probability applied to travel
p applied to years
*/
// item year start in future
if ($d1 > $d2) {
// year to begin counting, diff incremented to allow for 1 minute difference
$yrbegin = $yrdiff + 2;
// year to end counting
$yrend = $yrbegin + $yearsdur - 1;
// else item year start this year or in past
} else {
// year to begin counting
$yrbegin = 1;
// year to end counting
$yrend = $yearsdur - $yrdiff;
}
// update max years remaining
if (!$someday && !$complete) eval(formula('maxYrs'));
// limit to number of timeline years
if ($yrend > count($unqtimelineIds) - 1) $yrend = count($unqtimelineIds) - 1; // - 1 to ignore $unqtimelineIds[0]
// years for value product
$yrsval[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'active' => $active, 'yrs' => $yrend - $yrbegin + 1);
// process value for each year item on timeline
// 0 is base value used for sum of weighted value results per hour per year
$yr = 0;
while ($yr <= $yrend || $yr == 0) {
// skip years if start is in future
// except for first loop of yr = 0 always proceed
if ($yr < $yrbegin && $yr !== 0) { $yr++; continue; }
// initialise timeline item values
$tlinevala = 0;
$tlinevalb = 0;
// search if value captured already from another time attribute, if so reuse value
$i = 0;
foreach ((array) $timeline as $res) {
if ($res['type'] == $item['itemType'] &&
$res['visId'] == $visn['itemId'] &&
$res['id'] == $item['itemId'] &&
$res['attrId'] == $unqtimelineIds[$yr]
) {
$tlinevala = $res['valuea'];
$tlinevalb = $res['valueb'];
// remove record
unset($timeline[$i]);
// reset array keys
$timeline = array_values($timeline);
// stop searching
break;
}
$i++;
}
// increment hours value
if ($brainless == 'n') {
$tlinevala += ceil($value * ($prob > 0 ? 1/$prob : 0));
} else {
$tlinevalb += ceil($value * ($prob > 0 ? 1/$prob : 0));
}
// record active status
if (!$someday && !$complete) { $active = 1; } else { $active = 0; }
// update timeline array value, ignoring nil values
if ($tlinevala !== 0 || $tlinevalb !== 0) $timeline[] = array('type' => $item['itemType'], 'visId' => $visn['itemId'], 'id' => $item['itemId'], 'attrId' => $unqtimelineIds[$yr], 'valuea' => $tlinevala, 'valueb' => $tlinevalb, 'active' => $active);
// loop to next year on timeline
$yr++;
}
}
}
// if paired output A obtained, store in text variable $output
if ($output1 !== '') $output = $output1;
// if paired output B obtained, assum A was obtained, separate A and B with character, and store in text variable $output
if ($output2 !== '') { if ($output !== '') $output .= ", "; $output .= $output2; }
// if output obtained, or the current attribute must force a response to the matrix, push the record onto the final result
if ($output !== '' || in_array($attr['format'],$resForce)) {
if ($output == '') $output = 0;
//$result[] = array('type' => 'v', 'visId' => $visn['itemId'], 'attrId' => $attr['qId'], 'value' => $output);
// if score item and optimising, place in array
if ($optim && array_key_exists($attr['qId'], $scoreqIds)) {
foreach ((array) $scoreopt as $key => $opt) {
if ($opt['type'] == 'v' && $opt['visId'] == $visn['itemId']) {
$scoreopt[$key]['scores'][$attr['qId']] = $output;
break;
}
}
}
}
// special case $brainless for effort/year
if ($attr['qId'] == $hoursId) {
// check calculation variable
$result[] = array('type' => 'v', 'visId' => $visn['itemId'], 'attrId' => $brainlessId, 'value' => $bless1 + $bless2);
if ($visn['isSomeday'] !== 'y' && $visn['dateCompleted'] == '') {
$summaryBless1 += $bless1;
$summaryBless2 += $bless2;
}
}
// special case $outputYrs for years
if ($outputYrs > 0) {
foreach ((array) $visYrs as $key=>$visYr) {
if ($visn['itemId'] == $key) {
if ($outputYrs > $visYr['yrs']) $visYrs[$key]['yrs'] = $outputYrs;
break;
}
}
}
if ($outputYear) {
foreach ((array) $visYrs as $key=>$visYr) {
if ($visn['itemId'] == $key) {
if (
$outputYear < $visYr['yearst']
|| !$visYr['yearst']
) $visYrs[$key]['yearst'] = $outputYear;
break;
}
}
}
// set variables for vision active or not to affect following summary calculation
if ($visn['isSomeday'] == 'y') { $someday = true; } else { $someday = false; }
if ($visn['dateCompleted'] == '') { $complete = false; } else { $complete = true; }
// eval to execute as script value for attribute's formula for summary
// updates the $summary1 and $summary2 variables, formulae defined in gtdfuncs.php
if (!$someday && !$complete) {
eval(formula($attr['formulaSum1']));
eval(formula($attr['formulaSum2']));
}
}
// if output obtained, or the current attribute must force a response to the matrix, push the record onto the final result
// if ($summary1 !== '' || in_array($attr['format'],$resForce)) $result[] = array('type' => 'x', 'attrId' => $attr['qId'], 'form' => 'a', 'value' => $summary1);
// if ($summary2 !== '' || in_array($attr['format'],$resForce)) $result[] = array('type' => 'x', 'attrId' => $attr['qId'], 'form' => 'b', 'value' => $summary2);
// if score item and optimising, place in array
if ($optim && array_key_exists($attr['qId'], $scoreqIds)) {
$i = 0;
foreach ((array) $scoreopt as $key => $opt) {
if ($opt['type'] == 'x' && $opt['form'] == 'a') {
$scoreopt[$key]['scores'][$attr['qId']] = $summary1;
$i++;
}
if ($opt['type'] == 'x' && $opt['form'] == 'b') {
$scoreopt[$key]['scores'][$attr['qId']] = $summary2;
$i++;
}
if ($i == 2) break;
}
}
// special case $brainless for effort/year
if ($attr['qId'] == $hoursId) {
$result[] = array('type' => 'x', 'attrId' => $brainlessId, 'form' => 'a', 'value' => $summaryBless1);
$result[] = array('type' => 'x', 'attrId' => $brainlessId, 'form' => 'b', 'value' => $summaryBless2);
}
}
// process ouputYrs
$summaryYrs = 0;
$summaryYear = false;
foreach ((array) $visYrs as $key=>$visYr) {
if (isset($yearqId)) $result[] = array('type' => 'v', 'visId' => $key, 'attrId' => $yearqId, 'value' => date("Y") - $visYr['yearst'] + $visYr['yrs']);
if (isset($yearstqId) && $visYr['active']) $result[] = array('type' => 'v', 'visId' => $key, 'attrId' => $yearstqId, 'value' => $visYr['yearst']);
if (isset($yearenqId) && $visYr['active']) $result[] = array('type' => 'v', 'visId' => $key, 'attrId' => $yearenqId, 'value' => date("Y") + $visYr['yrs'] - 1);
if (!$visYr['active']) continue;
if (!$summaryYear) $summaryYear = $visYr['yearst'];
if ($summaryYrs < $visYr['yrs']) $summaryYrs = $visYr['yrs'];
if ($summaryYear > $visYr['yearst'] && $visYr['active']) $summaryYear = $visYr['yearst'];
}
if (isset($yearqId)) $result[] = array('type' => 'x', 'attrId' => $yearqId, 'form' => 'a', 'value' => $summaryYrs);
if (isset($yearqId)) $result[] = array('type' => 'x', 'attrId' => $yearqId, 'form' => 'b', 'value' => '');
if (isset($yearstqId)) $result[] = array('type' => 'x', 'attrId' => $yearstqId, 'form' => 'a', 'value' => $summaryYear);
if (isset($yearstqId)) $result[] = array('type' => 'x', 'attrId' => $yearstqId, 'form' => 'b', 'value' => '');
if (isset($yearenqId)) $result[] = array('type' => 'x', 'attrId' => $yearenqId, 'form' => 'a', 'value' => $summaryYrs + date("Y") - 1);
if (isset($yearenqId)) $result[] = array('type' => 'x', 'attrId' => $yearenqId, 'form' => 'b', 'value' => '');
// process value year scores
$yrsval = array_values(array_unique($yrsval, SORT_REGULAR));
// initialise temp vision array
$valuessumvisn = array();
foreach ((array) $visions as $visn) {
if ($visn['isSomeday'] !== 'y' && $visn['dateCompleted'] == '') { $active = 1; } else { $active = 0; }
$valuessumvisn[$visn['itemId']] = array('active' => $active);
}
// process items
foreach ((array) $scores as $score) {
if (!isset($valuessumvisn[$score['visId']][$score['attrId']])) $valuessumvisn[$score['visId']][$score['attrId']] = 0;
foreach ((array) $yrsval as $item) {
if (
$item['type'] == $score['type'] &&
$item['visId'] == $score['visId'] &&
$item['id'] == $score['id']
) {
$valuessumvisn[$score['visId']][$score['attrId']] += $item['yrs'] * $score['value'] * $item['active'];
break;
}
}
}
// push vision value sums onto the final result
$formsummary = array('a' => 0, 'b' => 0);
$valuessummaryn = array();
foreach ((array) $valuessumvisn as $key => $visn) {
foreach ((array) $visn as $valkey => $value) {
if ($valkey == 'active') { $active = $value; continue; }
if (!isset($valuessummaryn[$valkey])) $valuessummaryn[$valkey] = $formsummary;
if ($value >= 0) { $value = ceil($value); } else { $value = floor($value); }
$result[] = array('type' => 'v', 'visId' => $key, 'attrId' => $valkey, 'value' => $value);
// if vision is live then increment summary value sums
if ($active) {
if ($value >= 0) {
$valuessummaryn[$valkey]['a'] += $value;
} else {
$valuessummaryn[$valkey]['b'] += $value;
}
}
}
}
// push summary value sums onto final result
foreach ((array) $valuessummaryn as $key => $summary) {
$result[] = array('type' => 'x', 'attrId' => $key, 'form' => 'a', 'value' => $summary['a']);
$result[] = array('type' => 'x', 'attrId' => $key, 'form' => 'b', 'value' => $summary['b']);
}
// process optimsing scores
if ($optim) {
// process items
foreach ((array) $scores as $score) {
$exist = false;
foreach ((array) $scoreopt as $item) {
if (
$item['type'] == $score['type'] &&
$item['visId'] == $score['visId'] &&
$item['id'] == $score['id']
) {
$exist = true;
break;
}
}
if (!$exist) $scoreopt[] = array('type' => $score['type'], 'visId' => $score['visId'], 'id' => $score['id'], 'scores' => $scoreweights);
foreach ((array) $scoreopt as $key => $item) {
if (
$item['type'] == $score['type'] &&
$item['visId'] == $score['visId'] &&
$item['id'] == $score['id']
) {
$scoreopt[$key]['scores'][$score['attrId']] = $score['value'];
break;
}
}
}
// calculate items, values, and summary
foreach ((array) $scoreopt as $opt) {
// valid array
$cont = true;
$zeros = true;
foreach ((array) $opt['scores'] as $key => $score) {
if (strlen($score) == 0) $cont = false;
if ($score !== '0') $zeros = false;
}
if (!$cont || $zeros) continue;
// correlations
if (stDev($opt['scores']) !== 0 && stDev($scoreqIds) !== 0) {
$pearson = corr($scoreqIds, $opt['scores']);
$pearson = round($pearson, 1);
if ($pearson == '0') $pearson = '0.0';
} else {
$pearson = '';
}
$uniformity = uniformity($opt['scores']);
$uniformity = round($uniformity, 1);
if ($uniformity < 0 || $uniformity == '0') $uniformity = '0.0';
// push to result
if ($opt['type'] == 'x') {
if (isset($unqoptimiseprefId)) $result[] = array('type' => 'x', 'attrId' => $unqoptimiseprefId, 'form' => $opt['form'], 'value' => $pearson);
if (isset($unqoptimisebalaId)) $result[] = array('type' => 'x', 'attrId' => $unqoptimisebalaId, 'form' => $opt['form'], 'value' => $uniformity);
} elseif ($opt['type'] == 'v') {
if (isset($unqoptimiseprefId)) $result[] = array('type' => 'v', 'visId' => $opt['visId'], 'attrId' => $unqoptimiseprefId, 'value' => $pearson);
if (isset($unqoptimisebalaId)) $result[] = array('type' => 'v', 'visId' => $opt['visId'], 'attrId' => $unqoptimisebalaId, 'value' => $uniformity);
} else {
if (isset($unqoptimiseprefId)) $result[] = array('type' => $opt['type'], 'visId' => $opt['visId'], 'itemId' => $opt['id'], 'attrId' => $unqoptimiseprefId, 'value' => $pearson);
if (isset($unqoptimisebalaId)) $result[] = array('type' => $opt['type'], 'visId' => $opt['visId'], 'itemId' => $opt['id'], 'attrId' => $unqoptimisebalaId, 'value' => $uniformity);
}
}
}
// process value sums
// initialise temp vision array
$valuessumvis = array();
foreach ((array) $visions as $visn) {
$valuessumvis[$visn['itemId']] = 0;
}
foreach ((array) $valuessum as $valuesum) {
// push item value sums onto the final result
$result[] = array('type' => $valuesum['type'], 'visId' => $valuesum['visId'], 'itemId' => $valuesum['id'], 'attrId' => $unqvaluessumId, 'value' => round($valuesum['value']));
// increment vision value sums
if ($valuesum['active']) $valuessumvis[$valuesum['visId']] += $valuesum['value'];
}
// push vision value sums onto the final result
$valuessummary = array('a' => 0, 'b' => 0);
foreach ((array) $visions as $visn) {
// crude error check here
if (!isset($unqvaluessumId)) continue;
if ($valuessumvis[$visn['itemId']] >= 0) {
$result[] = array('type' => 'v', 'visId' => $visn['itemId'], 'attrId' => $unqvaluessumId, 'value' => ceil($valuessumvis[$visn['itemId']]));
} else {
$result[] = array('type' => 'v', 'visId' => $visn['itemId'], 'attrId' => $unqvaluessumId, 'value' => floor($valuessumvis[$visn['itemId']]));
}
// if vision is live then increment summary value sums
if ($visn['isSomeday'] !== 'y' && $visn['dateCompleted'] == '') {
if ($valuessumvis[$visn['itemId']] >= 0) {
$valuessummary['a'] += ceil($valuessumvis[$visn['itemId']]);
} else {
$valuessummary['b'] += floor($valuessumvis[$visn['itemId']]);
}
}
}
// push summary value sums onto final result
if (isset($unqvaluessumId)) {
$result[] = array('type' => 'x', 'attrId' => $unqvaluessumId, 'form' => 'a', 'value' => $valuessummary['a']);
$result[] = array('type' => 'x', 'attrId' => $unqvaluessumId, 'form' => 'b', 'value' => $valuessummary['b']);
}
// calculate and push item value sum per hours
// initialise temp vision array
$valuessumvishours = array();
foreach ((array) $visions as $visn) {
$valuessumvishours[$visn['itemId']] = array('value' => 0, 'hours' => 0);
}
foreach ((array) $timeline as $tline) {
// ignore non-base timeline
if ($tline['attrId'] == $unqtimelineIds[0]) {
foreach ((array) $valuessum as $valuesum) {
if (
$valuesum['type'] == $tline['type'] &&
$valuesum['visId'] == $tline['visId'] &&
$valuesum['id'] == $tline['id']
) {
// push item onto the final result
$denominator = $tline['valuea'] + $tline['valueb'];
// attribute conflict handling if hours for item = 0 or negative, assume no comparable value
if ($denominator > 1) {
$value = number_format($valuesum['value'] / $denominator, 1);
$result[] = array('type' => $valuesum['type'], 'visId' => $valuesum['visId'], 'itemId' => $valuesum['id'], 'attrId' => $unqtimelineIds[0], 'value' => $value);
// increment vision value sums
if ($valuesum['active']) {
$valuessumvishours[$valuesum['visId']]['value'] += $valuesum['value'];
$valuessumvishours[$valuesum['visId']]['hours'] += $denominator;
}
}
// stop searching