-
Notifications
You must be signed in to change notification settings - Fork 341
/
Update.fnc.php
1056 lines (837 loc) · 29.9 KB
/
Update.fnc.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
/**
* Update functions
*
* Incremental updates
*
* Update() function called if ROSARIO_VERSION != version in DB
*
* @package RosarioSIS
* @subpackage ProgramFunctions
*/
/**
* Update manager function
*
* Call the specific versions functions
*
* @since 2.9
*
* @return boolean false if wrong version or update failed, else true
*/
function Update()
{
$from_version = Config( 'VERSION' );
$to_version = ROSARIO_VERSION;
/**
* Check if Update() version < ROSARIO_VERSION.
*
* Prevent DB version update if new Update.fnc.php file has NOT been uploaded YET.
* Update must be run once both new Warehouse.php & Update.fnc.php files are uploaded.
*/
if ( version_compare( '11.4.2', ROSARIO_VERSION, '<' ) )
{
return false;
}
// Check if version in DB >= ROSARIO_VERSION.
if ( version_compare( $from_version, $to_version, '>=' ) )
{
return false;
}
require_once 'ProgramFunctions/UpdateV2_3.fnc.php';
require_once 'ProgramFunctions/UpdateV4_5.fnc.php';
require_once 'ProgramFunctions/UpdateV6_8_9.fnc.php';
$return = true;
switch ( true )
{
case version_compare( $from_version, '2.9-alpha', '<' ) :
$return = _update29alpha();
case version_compare( $from_version, '2.9.2', '<' ) :
$return = _update292();
case version_compare( $from_version, '2.9.5', '<' ) :
$return = _update295();
case version_compare( $from_version, '2.9.12', '<' ) :
$return = _update2912();
case version_compare( $from_version, '2.9.13', '<' ) :
$return = _update2913();
case version_compare( $from_version, '2.9.14', '<' ) :
$return = _update2914();
case version_compare( $from_version, '3.0', '<' ) :
$return = _update30();
case version_compare( $from_version, '3.1', '<' ) :
$return = _update31();
case version_compare( $from_version, '3.5', '<' ) :
$return = _update35();
case version_compare( $from_version, '3.7-beta', '<' ) :
$return = _update37beta();
case version_compare( $from_version, '3.9', '<' ) :
$return = _update39();
case version_compare( $from_version, '4.0-beta', '<' ) :
$return = _update40beta();
case version_compare( $from_version, '4.2-beta', '<' ) :
$return = _update42beta();
case version_compare( $from_version, '4.3-beta', '<' ) :
$return = _update43beta();
case version_compare( $from_version, '4.4-beta', '<' ) :
$return = _update44beta();
case version_compare( $from_version, '4.4-beta2', '<' ) :
$return = _update44beta2();
case version_compare( $from_version, '4.5-beta2', '<' ) :
$return = _update45beta2();
case version_compare( $from_version, '4.6-beta', '<' ) :
$return = _update46beta();
case version_compare( $from_version, '4.7-beta', '<' ) :
$return = _update47beta();
case version_compare( $from_version, '4.7-beta2', '<' ) :
$return = _update47beta2();
case version_compare( $from_version, '4.9-beta', '<' ) :
$return = _update49beta();
case version_compare( $from_version, '5.0-beta', '<' ) :
$return = _update50beta();
case version_compare( $from_version, '5.0.1', '<' ) :
$return = _update501();
case version_compare( $from_version, '5.2-beta', '<' ) :
$return = _update52beta();
case version_compare( $from_version, '5.3-beta', '<' ) :
$return = _update53beta();
case version_compare( $from_version, '5.4.1', '<' ) :
$return = _update541();
case version_compare( $from_version, '5.4.2', '<' ) :
$return = _update542();
case version_compare( $from_version, '5.5-beta3', '<' ) :
$return = _update55beta3();
case version_compare( $from_version, '5.7', '<' ) :
$return = _update57();
case version_compare( $from_version, '5.8-beta5', '<' ) :
$return = _update58beta5();
case version_compare( $from_version, '5.9-beta', '<' ) :
$return = _update59beta();
case version_compare( $from_version, '5.9-beta2', '<' ) :
$return = _update59beta2();
case version_compare( $from_version, '5.9', '<' ) :
$return = _update59();
case version_compare( $from_version, '5.9.1', '<' ) :
$return = _update591();
case version_compare( $from_version, '6.3', '<' ) :
$return = _update63();
case version_compare( $from_version, '6.6', '<' ) :
$return = _update66();
case version_compare( $from_version, '6.9-beta', '<' ) :
$return = _update69beta();
case version_compare( $from_version, '8.1', '<' ) :
$return = _update81();
case version_compare( $from_version, '8.3', '<' ) :
$return = _update83();
case version_compare( $from_version, '8.4', '<' ) :
$return = _update84();
case version_compare( $from_version, '8.5', '<' ) :
$return = _update85();
case version_compare( $from_version, '8.7', '<' ) :
$return = _update87();
case version_compare( $from_version, '9.2', '<' ) :
$return = _update92();
case version_compare( $from_version, '9.2.1', '<' ) :
$return = _update921();
case version_compare( $from_version, '9.3', '<' ) :
$return = _update93();
case version_compare( $from_version, '10.1', '<' ) :
$return = _update101();
case version_compare( $from_version, '10.6.1', '<' ) :
$return = _update1061();
case version_compare( $from_version, '10.8', '<' ) :
$return = _update108();
case version_compare( $from_version, '10.9', '<' ) :
$return = _update109();
case version_compare( $from_version, '11.0', '<' ) :
$return = _update110();
case version_compare( $from_version, '11.1', '<' ) :
$return = _update111();
case version_compare( $from_version, '11.2', '<' ) :
$return = _update112();
case version_compare( $from_version, '11.2.1', '<' ) :
$return = _update1121();
case version_compare( $from_version, '11.3.1', '<' ) :
$return = _update1131();
}
// Update version in DB config table.
Config( 'VERSION', ROSARIO_VERSION );
return $return;
}
/**
* Is function called by Update()?
*
* Local function
*
* @example _isCallerUpdate( debug_backtrace() );
*
* @since 2.9.13
*
* @param array $callers debug_backtrace().
*
* @return boolean Exit with error message if not called by Update().
*/
function _isCallerUpdate( $callers )
{
if ( ! isset( $callers[1]['function'] )
|| $callers[1]['function'] !== 'Update' )
{
exit( 'Error: the update functions must be called by Update() only!' );
}
return true;
}
/**
* Update to version 10.1
*
* 1. Add dual VIEW for compatibility with MySQL 5.6 to avoid syntax error when WHERE without FROM clause
*
* Local function
*
* @since 10.1
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update101()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. Add dual VIEW for compatibility with MySQL 5.6 to avoid syntax error when WHERE without FROM clause
* PostgreSQL only
*/
if ( $DatabaseType !== 'mysql' )
{
DBQuery( "CREATE OR REPLACE VIEW dual AS SELECT 'X' AS dummy;" );
}
return $return;
}
/**
* Update to version 10.6.1
* Fix Class Rank float comparison issue: do NOT use double precision type (inexact), use numeric (exact)
* @link https://www.rosariosis.org/forum/d/665-le-classement-diff-rent-mais-m-me-moyenne/
* Fix regression since 10.0, change sum/cum factors & credit_attempted/earned columns type from double precision to numeric
*
* PostgreSQL
* 1. Drop transcript_grades view, so we can alter student_report_card_grades table
* 2. SQL student_report_card_grades table: convert CREDIT_ATTEMPTED & CREDIT_EARNED columns to numeric
* 3. SQL student_mp_stats table: convert sum/cum factors & credit_attempted/earned columns to numeric
* 4. Recreate transcript_grades view
*
* MySQL
* 1. SQL student_report_card_grades table: convert CREDIT_ATTEMPTED & CREDIT_EARNED columns to numeric(22,16)
* 2. SQL student_mp_stats table: convert sum/cum factors & credit_attempted/earned columns to numeric(22,16)
*
* Local function
*
* @since 10.6.1
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update1061()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
if ( $DatabaseType === 'postgresql' )
{
// Check if data type is numeric already.
$data_type_is_numeric = DBGetOne( "SELECT 1
FROM student_report_card_grades
WHERE CAST(PG_TYPEOF(credit_attempted) AS varchar(7))='numeric'
LIMIT 1;" );
}
else
{
// Check if data type is numeric already.
$data_type_is_numeric = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=DATABASE()
AND table_name='student_report_card_grades'
AND COLUMN_NAME='credit_attempted'
AND DATA_TYPE='decimal'
LIMIT 1;" );
}
if ( $data_type_is_numeric )
{
return $return;
}
if ( $DatabaseType === 'postgresql' )
{
// 1. Drop transcript_grades view, so we can alter student_report_card_grades table
DBQuery( "DROP VIEW transcript_grades;" );
// 2. SQL student_report_card_grades table: convert CREDIT_ATTEMPTED & CREDIT_EARNED columns to numeric
DBQuery( "ALTER TABLE student_report_card_grades
ALTER credit_attempted TYPE numeric,
ALTER credit_earned TYPE numeric;" );
// 3. SQL student_mp_stats table: convert sum/cum factors & credit_attempted/earned columns to numeric
DBQuery( "ALTER TABLE student_mp_stats
ALTER cum_weighted_factor TYPE numeric,
ALTER cum_unweighted_factor TYPE numeric,
ALTER sum_weighted_factors TYPE numeric,
ALTER sum_unweighted_factors TYPE numeric,
ALTER cr_weighted_factors TYPE numeric,
ALTER cr_unweighted_factors TYPE numeric,
ALTER cum_cr_weighted_factor TYPE numeric,
ALTER cum_cr_unweighted_factor TYPE numeric,
ALTER credit_attempted TYPE numeric,
ALTER credit_earned TYPE numeric,
ALTER gp_credits TYPE numeric,
ALTER cr_credits TYPE numeric" );
// 4. Recreate transcript_grades view
DBQuery( "CREATE VIEW transcript_grades AS
SELECT mp.syear,mp.school_id,mp.marking_period_id,mp.mp_type,
mp.short_name,mp.parent_id,mp.grandparent_id,
(SELECT mp2.end_date
FROM student_report_card_grades
JOIN marking_periods mp2
ON mp2.marking_period_id = student_report_card_grades.marking_period_id
WHERE student_report_card_grades.student_id = sms.student_id
AND (student_report_card_grades.marking_period_id = mp.parent_id
OR student_report_card_grades.marking_period_id = mp.grandparent_id)
AND student_report_card_grades.course_title = srcg.course_title
ORDER BY mp2.end_date LIMIT 1) AS parent_end_date,
mp.end_date,sms.student_id,
(sms.cum_weighted_factor * COALESCE(schools.reporting_gp_scale, (SELECT reporting_gp_scale FROM schools WHERE mp.school_id = id ORDER BY syear LIMIT 1))) AS cum_weighted_gpa,
(sms.cum_unweighted_factor * schools.reporting_gp_scale) AS cum_unweighted_gpa,
sms.cum_rank,sms.mp_rank,sms.class_size,
((sms.sum_weighted_factors / sms.count_weighted_factors) * schools.reporting_gp_scale) AS weighted_gpa,
((sms.sum_unweighted_factors / sms.count_unweighted_factors) * schools.reporting_gp_scale) AS unweighted_gpa,
sms.grade_level_short,srcg.comment,srcg.grade_percent,srcg.grade_letter,
srcg.weighted_gp,srcg.unweighted_gp,srcg.gp_scale,srcg.credit_attempted,
srcg.credit_earned,srcg.course_title,srcg.school AS school_name,
schools.reporting_gp_scale AS school_scale,
((sms.cr_weighted_factors / sms.count_cr_factors::numeric) * schools.reporting_gp_scale) AS cr_weighted_gpa,
((sms.cr_unweighted_factors / sms.count_cr_factors::numeric) * schools.reporting_gp_scale) AS cr_unweighted_gpa,
(sms.cum_cr_weighted_factor * schools.reporting_gp_scale) AS cum_cr_weighted_gpa,
(sms.cum_cr_unweighted_factor * schools.reporting_gp_scale) AS cum_cr_unweighted_gpa,
srcg.class_rank,sms.comments,
srcg.credit_hours
FROM marking_periods mp
JOIN student_report_card_grades srcg
ON mp.marking_period_id = srcg.marking_period_id
JOIN student_mp_stats sms
ON sms.marking_period_id = mp.marking_period_id
AND sms.student_id = srcg.student_id
LEFT OUTER JOIN schools
ON mp.school_id = schools.id
AND (mp.mp_source<>'History' AND mp.syear = schools.syear)
OR (mp.mp_source='History' AND mp.syear=(SELECT syear FROM schools WHERE mp.school_id = id ORDER BY syear LIMIT 1))
ORDER BY srcg.course_period_id;" );
}
else
{
// MySQL.
// 1. SQL student_report_card_grades table: convert CREDIT_ATTEMPTED & CREDIT_EARNED columns to numeric(22,16)
DBQuery( "ALTER TABLE student_report_card_grades
CHANGE credit_attempted credit_attempted numeric(22,16),
CHANGE credit_earned credit_earned numeric(22,16);" );
// 2. SQL student_mp_stats table: convert sum/cum factors & credit_attempted/earned columns to numeric(22,16)
DBQuery( "ALTER TABLE student_mp_stats
CHANGE cum_weighted_factor cum_weighted_factor numeric(22,16),
CHANGE cum_unweighted_factor cum_unweighted_factor numeric(22,16),
CHANGE sum_weighted_factors sum_weighted_factors numeric(22,16),
CHANGE sum_unweighted_factors sum_unweighted_factors numeric(22,16),
CHANGE cr_weighted_factors cr_weighted_factors numeric(22,16),
CHANGE cr_unweighted_factors cr_unweighted_factors numeric(22,16),
CHANGE cum_cr_weighted_factor cum_cr_weighted_factor numeric(22,16),
CHANGE cum_cr_unweighted_factor cum_cr_unweighted_factor numeric(22,16),
CHANGE credit_attempted credit_attempted numeric(22,16),
CHANGE credit_earned credit_earned numeric(22,16),
CHANGE gp_credits gp_credits numeric(22,16),
CHANGE cr_credits cr_credits numeric(22,16);" );
}
return $return;
}
/**
* Update to version 10.8
*
* 1. resources table: Add PUBLISHED_PROFILES & PUBLISHED_GRADE_LEVELS columns.
*
* Local function
*
* @since 10.8
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update108()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. resources table: Add PUBLISHED_PROFILES & PUBLISHED_GRADE_LEVELS columns.
*/
$published_profiles_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='resources'
AND column_name='published_profiles';" );
if ( ! $published_profiles_column_exists )
{
DBQuery( "ALTER TABLE resources
ADD published_profiles text,
ADD published_grade_levels text;" );
}
return $return;
}
/**
* Update to version 10.9
*
* 1. gradebook_assignments table: Add WEIGHT column.
*
* Local function
*
* @since 10.9
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update109()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. gradebook_assignments table: Add WEIGHT column.
*/
$weight_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='gradebook_assignments'
AND column_name='weight';" );
if ( ! $weight_column_exists )
{
DBQuery( "ALTER TABLE gradebook_assignments
ADD weight integer;" );
}
return $return;
}
/**
* Update to version 11.0
*
* 1. Move "Progress Reports" from Teacher Programs to Grades menu (admin)
* 2. Add accounting_categories table
* 3. Add TITLE column to accounting_payments table
* 4. Add CATEGORY_ID column to accounting_incomes & accounting_payments tables
* 5. Give admin profile access to Accounting > Categories program
*
* Local function
*
* @since 11.0
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update110()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. Move "Progress Reports" from Teacher Programs to Grades menu (admin)
*/
$profile_exception_exists = DBGetOne( "SELECT 1
FROM profile_exceptions
WHERE MODNAME='Grades/ProgressReports.php'
AND PROFILE_ID='1'" );
if ( ! $profile_exception_exists )
{
DBQuery( "UPDATE profile_exceptions
SET MODNAME='Grades/ProgressReports.php'
WHERE MODNAME='Users/TeacherPrograms.php&include=Grades/ProgressReports.php'
AND PROFILE_ID='1'" );
}
$staff_exceptions_user_ids = DBGetOne( "SELECT " . DBSQLCommaSeparatedResult( 'USER_ID' ) . " AS USER_IDS
FROM staff_exceptions
WHERE MODNAME='Grades/ProgressReports.php'" );
$where_not_user_id_sql = '';
if ( $staff_exceptions_user_ids )
{
$where_not_user_id_sql = " AND USER_ID NOT IN(" . $staff_exceptions_user_ids . ")";
}
DBQuery( "UPDATE staff_exceptions
SET MODNAME='Grades/ProgressReports.php'
WHERE MODNAME='Users/TeacherPrograms.php&include=Grades/ProgressReports.php'" . $where_not_user_id_sql );
/**
* 2. Add accounting_categories table
*/
$accounting_categories_table_exists = DBGetOne( "SELECT 1
FROM information_schema.tables
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='accounting_categories';" );
if ( ! $accounting_categories_table_exists )
{
if ( $DatabaseType === 'mysql' )
{
DBQuery( "CREATE TABLE accounting_categories (
id integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
school_id integer NOT NULL,
title text NOT NULL,
short_name varchar(10),
type varchar(100),
sort_order numeric,
created_at timestamp DEFAULT current_timestamp,
updated_at timestamp NULL ON UPDATE current_timestamp
);" );
}
else
{
DBQuery( "CREATE TABLE accounting_categories (
id serial PRIMARY KEY,
school_id integer NOT NULL,
title text NOT NULL,
short_name varchar(10),
type varchar(100),
sort_order numeric,
created_at timestamp DEFAULT current_timestamp,
updated_at timestamp
);" );
}
}
/**
* 3. Add TITLE column to accounting_payments table
*/
$title_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='accounting_payments'
AND column_name='title';" );
if ( ! $title_column_exists )
{
DBQuery( "ALTER TABLE accounting_payments ADD title text" );
}
/**
* 4. Add CATEGORY_ID column to accounting_incomes & accounting_payments tables
*/
$category_id_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='accounting_incomes'
AND column_name='category_id';" );
if ( ! $category_id_column_exists )
{
DBQuery( "ALTER TABLE accounting_incomes ADD category_id integer,
ADD FOREIGN KEY (category_id) REFERENCES accounting_categories(id);
ALTER TABLE accounting_payments ADD category_id integer,
ADD FOREIGN KEY (category_id) REFERENCES accounting_categories(id);" );
}
/**
* 5. Give admin profile access to Accounting > Categories program
*/
DBQuery( "INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 1, 'Accounting/Categories.php', 'Y', 'Y'
FROM DUAL
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Accounting/Categories.php'
AND profile_id=1);" );
return $return;
}
/**
* Update to version 11.1
*
* 0. Drop calc_cum_cr_gpa & calc_cum_gpa procedures (MySQL only)
* 0. Create plpgsql language in case it does not exist (PostgreSQL only)
* 1. SQL set min Credits to 0 & fix division by zero error
*
* Local function
*
* @since 11.1
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update111()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
if ( $DatabaseType === 'mysql' )
{
/**
* MySQL
*
* 0. Drop calc_cum_cr_gpa & calc_cum_gpa procedures
* 1. SQL set min Credits to 0 & fix division by zero error
*/
$mysql_no_delimiter = MySQLRemoveDelimiter( "DROP PROCEDURE IF EXISTS calc_cum_cr_gpa;
DROP PROCEDURE IF EXISTS calc_cum_gpa;
--
-- Name: calc_cum_cr_gpa(mp_id integer, s_id integer); Type: FUNCTION;
-- @since 11.1 SQL set min Credits to 0 & fix division by zero error
--
DELIMITER $$
CREATE PROCEDURE calc_cum_cr_gpa(mp_id integer, s_id integer)
BEGIN
UPDATE student_mp_stats
SET cum_cr_weighted_factor = (case when cr_credits = '0' THEN '0' ELSE cr_weighted_factors/cr_credits END),
cum_cr_unweighted_factor = (case when cr_credits = '0' THEN '0' ELSE cr_unweighted_factors/cr_credits END)
WHERE student_mp_stats.student_id = s_id and student_mp_stats.marking_period_id = mp_id;
END$$
DELIMITER ;
--
-- Name: calc_cum_gpa(mp_id integer, s_id integer); Type: FUNCTION;
-- @since 11.1 SQL set min Credits to 0 & fix division by zero error
--
DELIMITER $$
CREATE PROCEDURE calc_cum_gpa(mp_id integer, s_id integer)
BEGIN
UPDATE student_mp_stats
SET cum_weighted_factor = (case when gp_credits = '0' THEN '0' ELSE sum_weighted_factors/gp_credits END),
cum_unweighted_factor = (case when gp_credits = '0' THEN '0' ELSE sum_unweighted_factors/gp_credits END)
WHERE student_mp_stats.student_id = s_id and student_mp_stats.marking_period_id = mp_id;
END$$
DELIMITER ;" );
DBQuery( $mysql_no_delimiter );
return $return;
}
/**
* PostgreSQL
*
* 0. Create plpgsql language in case it does not exist
* 1. SQL set min Credits to 0 & fix division by zero error
*/
DBQuery( "CREATE OR REPLACE FUNCTION create_language_plpgsql() RETURNS boolean AS $$
CREATE LANGUAGE plpgsql;
SELECT TRUE;
$$ LANGUAGE SQL;
SELECT CASE WHEN NOT
(
SELECT TRUE AS exists
FROM pg_language
WHERE lanname = 'plpgsql'
UNION
SELECT FALSE AS exists
ORDER BY exists DESC
LIMIT 1
)
THEN
create_language_plpgsql()
ELSE
FALSE
END AS plpgsql_created;
DROP FUNCTION create_language_plpgsql();
--
-- Name: calc_cum_cr_gpa(mp_id integer, s_id integer); Type: FUNCTION; Schema: public; Owner: postgres
-- @since 11.1 SQL set min Credits to 0 & fix division by zero error
--
CREATE OR REPLACE FUNCTION calc_cum_cr_gpa(mp_id integer, s_id integer) RETURNS integer AS $$
BEGIN
UPDATE student_mp_stats
SET cum_cr_weighted_factor = (case when cr_credits = '0' THEN '0' ELSE cr_weighted_factors/cr_credits END),
cum_cr_unweighted_factor = (case when cr_credits = '0' THEN '0' ELSE cr_unweighted_factors/cr_credits END)
WHERE student_mp_stats.student_id = s_id and student_mp_stats.marking_period_id = mp_id;
RETURN 1;
END;
$$ LANGUAGE plpgsql;
--
-- Name: calc_cum_gpa(mp_id integer, s_id integer); Type: FUNCTION; Schema: public; Owner: postgres
-- @since 11.1 SQL set min Credits to 0 & fix division by zero error
--
CREATE OR REPLACE FUNCTION calc_cum_gpa(mp_id integer, s_id integer) RETURNS integer AS $$
BEGIN
UPDATE student_mp_stats
SET cum_weighted_factor = (case when gp_credits = '0' THEN '0' ELSE sum_weighted_factors/gp_credits END),
cum_unweighted_factor = (case when gp_credits = '0' THEN '0' ELSE sum_unweighted_factors/gp_credits END)
WHERE student_mp_stats.student_id = s_id and student_mp_stats.marking_period_id = mp_id;
RETURN 1;
END;
$$ LANGUAGE plpgsql;" );
return $return;
}
/**
* Update to version 11.2
*
* 1. Add CREATED_BY column to billing_fees & billing_payments tables
*
* Local function
*
* @since 11.2
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update112()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. Add CREATED_BY column to billing_fees & billing_payments tables
*/
$created_by_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='billing_fees'
AND column_name='created_by';" );
if ( ! $created_by_column_exists )
{
DBQuery( "ALTER TABLE billing_fees ADD created_by text;
ALTER TABLE billing_payments ADD created_by text;" );
}
return $return;
}
/**
* Update to version 11.2.1
*
* 1. Add MENU_ITEM_ID column to food_service_transaction_items & food_service_staff_transaction_items tables
* Fix regression since 11.2 PostgreSQL error duplicate key value violates unique constraint
* Fix regression since 11.2 MySQL error duplicate entry '2-3' for key 'PRIMARY'
*
* Local function
*
* @since 11.2.1
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update1121()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. Add MENU_ITEM_ID column to food_service_transaction_items & food_service_staff_transaction_items tables
*/
$menu_item_id_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='food_service_transaction_items'
AND column_name='menu_item_id';" );
if ( ! $menu_item_id_column_exists )
{
DBQuery( "ALTER TABLE food_service_transaction_items
ADD menu_item_id integer;" );
}
$menu_item_id_column_exists = DBGetOne( "SELECT 1
FROM information_schema.columns
WHERE table_schema=" . ( $DatabaseType === 'mysql' ? 'DATABASE()' : 'CURRENT_SCHEMA()' ) . "
AND table_name='food_service_staff_transaction_items'
AND column_name='menu_item_id';" );
if ( ! $menu_item_id_column_exists )
{
DBQuery( "ALTER TABLE food_service_staff_transaction_items
ADD menu_item_id integer;" );
}
return $return;
}
/**
* Update to version 11.3.1
*
* 1. Update set_class_rank_mp() function
* Fix regression since 4.7 SQL set_class_rank_mp() Class Rank calculation when various school years
*
* Local function
*
* @since 11.3.1
*
* @return boolean false if update failed or if not called by Update(), else true
*/
function _update1131()
{
global $DatabaseType;
_isCallerUpdate( debug_backtrace() );
$return = true;
/**
* 1. Update set_class_rank_mp() function
*/
if ( $DatabaseType === 'mysql' )
{
$mysql_no_delimiter = MySQLRemoveDelimiter( "DROP FUNCTION IF EXISTS set_class_rank_mp;
DELIMITER $$
CREATE FUNCTION set_class_rank_mp(mp_id integer) RETURNS integer
BEGIN
update student_mp_stats sms
JOIN (
select mp.marking_period_id, sgm.student_id,
(select count(*)+1
from student_mp_stats sgm3
where sgm3.cum_cr_weighted_factor > sgm.cum_cr_weighted_factor
and sgm3.marking_period_id = mp.marking_period_id
and sgm3.student_id in (select distinct sgm2.student_id
from student_mp_stats sgm2, student_enrollment se2
where sgm2.student_id = se2.student_id
and sgm2.marking_period_id = mp.marking_period_id
and se2.grade_id = se.grade_id
and se2.syear = se.syear)) as class_rank,
(select count(*)
from student_mp_stats sgm4
where sgm4.marking_period_id = mp.marking_period_id
and sgm4.student_id in (select distinct sgm5.student_id
from student_mp_stats sgm5, student_enrollment se3
where sgm5.student_id = se3.student_id
and sgm5.marking_period_id = mp.marking_period_id
and se3.grade_id = se.grade_id
and se3.syear = se.syear)) as class_size
from student_enrollment se, student_mp_stats sgm, marking_periods mp
where se.student_id = sgm.student_id
and sgm.marking_period_id = mp.marking_period_id
and mp.marking_period_id = mp_id
and se.syear = mp.syear
and not sgm.cum_cr_weighted_factor is null
) as class_rank
ON sms.marking_period_id = class_rank.marking_period_id and sms.student_id = class_rank.student_id
set sms.cum_rank = class_rank.class_rank, sms.class_size = class_rank.class_size;
RETURN 1;
END$$
DELIMITER ;" );
DBQuery( $mysql_no_delimiter );
return $return;
}
// PostgreSQL.
DBQuery( "CREATE FUNCTION create_language_plpgsql()
RETURNS BOOLEAN AS $$
CREATE LANGUAGE plpgsql;
SELECT TRUE;
$$ LANGUAGE SQL;
SELECT CASE WHEN NOT
(
SELECT TRUE AS exists
FROM pg_language