forked from charyorde/acm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuni_school.module
executable file
·2477 lines (2222 loc) · 81.9 KB
/
uni_school.module
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
/*
* Implements hook_menu
*/
function uni_school_menu() {
$items = array();
// menu is only available if this module is enabled
$items['admission/apply/course'] = array(
'title' => t('Application to [School name]'),
'description' => t('Student application for admission to a school'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_admission_course_form'),
'access arguments' => array(''), // edit admission form
'access callback' => TRUE
);
$items['admission/apply/sponsor'] = array(
'title' => t('Application to [School name]'),
'description' => t('Student application for admission to a school'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_admission_sponsors_form'), // function callback
'access arguments' => array(''), // edit admission form
'access callback' => TRUE // remove if access arguments is set
);
$items['admission/apply/nextofkin'] = array(
'title' => t('Application to [School name]'),
'description' => t('Student application for admission to a school'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_admission_nextofkin_form'), // function callback
'access arguments' => array(''), // edit admission form
'access callback' => TRUE // remove if access arguments is set
);
$items['admission/apply/health'] = array(
'title' => t('Application to [School name]'),
'description' => t('Student application for admission to a school'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_admission_health_form'), // function callback
'access arguments' => array(''), // edit admission form
'access callback' => TRUE // remove if access arguments is set
);
$items['admission/apply/examresults'] = array(
'title' => t('Application to [School name]'),
'description' => t('Student application for admission to a school'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_admission_examresults_form'), // function callback
'access arguments' => array(''), // edit admission form
'access callback' => TRUE // remove if access arguments is set
);
// edit a programme
$items['admin/settings/school/programme/%acm_school_programme/edit'] = array(
'title' => t('Configure Programme'),
'description' => t('School programme settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_programme_form', 4),
'access arguments' => array('administer programme settings'),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
// edit a department
$items['admin/settings/school/department/%acm_school_department/edit'] = array(
'title' => t('Configure Department'),
'description' => t('School department settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_faculty_dept_form', 4),
'access arguments' => array('administer department settings'),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
// edit a course
$items['admin/settings/school/course/%acm_school_course/edit'] = array(
'title' => t('Configure Course'),
'description' => t('School course settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_addcourse_form', 4),
'access arguments' => array('administer course settings'),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
// edit a faculty
$items['admin/settings/school/faculty/%acm_school_faculty/edit'] = array(
'title' => t('Configure Faculty'),
'description' => t('School faculty settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_faculty_form', 4),
'access arguments' => array('administer faculty settings'),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
// edit a student grade
$items['admin/settings/school/grade/%acm_school_grade/edit'] = array(
'title' => t('Edit Grade'),
'description' => t('Edit student grade'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_grade_form', 4),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
// edit a student score
$items['admin/settings/school/score/%acm_school_score/edit'] = array(
'title' => t('Edit Score'),
'description' => t('Edit a student score'),
'page callback' => 'drupal_get_form',
'page arguments' => array('uni_school_score_form', 4),
'access arguments' => array('score a student'),
'type' => MENU_CALLBACK,
'load arguments' => array(4)
);
return $items;
}
/**
* Implements hook_acm_roles
*
* Defines default roles needed in a university for school module
*/
function uni_school_acm_roles() {
return array('Registrar', 'Lecturer', 'HOD', 'Dean', 'Senate', 'Vice-chancellor', 'Rector');
}
/**
* Implements hook_acm_roles_perm
*
* Assigns permission(s) to role(s)
*/
function uni_school_acm_roles_perm() {
}
/**
* Implements hook_school_settings
*/
function uni_school_school_settings() {
$settings['type'] = 'configure';
// General configuration
/*$settings['configure']['term'] = array(
//'term_name' => '',
//'preferred_semester_name' => ''
'title' => t('Configure term'),
'callback url' => t('admin/settings/school/admission/term')
);*/
$settings['configure']['admission'] = array(
'title' => t('Set admission status'),
'callback url' => t('admin/settings/school/admission/status')
);
$settings['configure']['users'] = array(
'title' => 'Configure users',
'callback url' => 'admin/settings/school/users'
);
$settings['configure']['fees'] = array(
'title' => 'Setup Fees',
'callback url' => 'admin/settings/school/fees'
);
$settings['configure']['dorm'] = array(
'title' => 'Configure dormitory allocation',
'callback url' => 'admin/settings/school/dormitory'
);
$settings['configure']['faculty'] = array(
'title' => 'Add faculty',
'callback url' => 'admin/settings/school/faculty/add'
);
$settings['configure']['department'] = array(
'title' => 'Add department',
'callback url' => 'admin/settings/school/department/add'
);
$settings['configure']['grade'] = array(
'title' => 'Configure grading system',
'callback url' => 'admin/settings/school/grade/gradingsystem'
);
$settings['configure']['school_settings'] = drupal_get_form('uni_school_school_settings_form');
return $settings;
}
/**
* Implements hook_admission_settings
*/
function uni_school_admission_settings($type) {
$settings['type'] = 'admission';
$settings['admission']['configure'] = array(
'title' => 'Configure',
'callback url' => 'admin/settings/school/'
);
$settings['admission']['view applications'] = array(
'title' => 'View applications',
'callback url' => 'admission/applications'
);
if($type == 'status') {
$settings['admission']['admission_status_form'] = drupal_get_form('uni_school_admission_settings_form');
}
return $settings;
}
// menu callback
// @Todo change to hook
function uni_school_admission_settings_form(&$form_state) {
$form['#attributes'] = array('class' => 'form-horizontal');
$form['admission_status'] = array(
'#type' => 'select',
'#title' => t('Admission status'),
'#default_value' => variable_get('admission_status', ''),
'#options' => array('Open', 'Closed'),
'#description' => t('Admission status'),
);
$form['academic_session'] = array(
'#type' => 'textfield',
'#title' => t('Academic session'),
'#default_value' => variable_get('academic_session', ''),
'#description' => t('Set the current academic session')
);
$semester = array(
variable_get('first_semester_name', ''),
variable_get('second_semester_name', '')
);
$form['admission_semester'] = array(
'#type' => 'select',
'#title' => t('Semester'),
'#default_value' => variable_get('admission_semester', ''),
//'#options' => array('Fall', 'Summer'), //module_invoke('school_terms') or variable_get('semesters');
'#options' => $semester,
'#description' => t('Admission current semester'),
);
$form['#submit'][] = 'uni_school_admission_settings_form_submit';
return system_settings_form($form);
}
function uni_school_admission_settings_form_submit($form, &$form_state) {
$session = $form_state['values']['academic_session'];
AcademicSession::addAcademicSession($session);
// insert or update Admission object
}
function uni_school_school_settings_form() {
$form['#attributes'] = array('class' => 'form-horizontal');
$form['first_semester_name'] = array(
'#type' => 'textfield',
'#title' => t('First Semester name'),
'#default_value' => variable_get('first_semester_name', ''),
'#required' => TRUE
);
$form['second_semester_name'] = array(
'#type' => 'textfield',
'#title' => t('Second Semester name'),
'#default_value' => variable_get('second_semester_name', ''),
'#required' => TRUE
);
$form['#submit'][] = 'uni_school_semester_settings_form_submit';
return system_settings_form($form);
}
function uni_school_semester_settings_form_submit($form, &$form_state) {
$first_semester_name = $form_state['values']['first_semester_name'];
$second_semester_name = $form_state['values']['second_semester_name'];
/*if($form_state['values']['op']) {
if(isset($first_semester_name)) {
db_query("INSERT INTO {academic_semester} (session) VALUES ('%s')", $first_semester_name);
}
if(isset($second_semester_name)) {
db_query("INSERT INTO {academic_semester} (session) VALUES ('%s')", $second_semester_name);
}
}*/
/*$items = array($first_semester_name, $second_semester_name);
$result = db_query("SELECT id, session FROM {academic_semester}");
$semesters = array();
while($semester = db_fetch_object($result)) {
if(isset($semester)) {
$semesters[$semester->id] = $semester->session;
}
}
if(!empty($semesters)) {
foreach($semesters as $key => $value) {
db_query("UPDATE {academic_semester} SET session = '%s' WHERE id = %d, $value, $key");
}
}else {
$success = db_query("INSERT INTO {academic_semester} (session) VALUES ('%s')", $item);
}*/
//AcademicSemester::addAcademicSemester();
}
/**
* implements hook_term_type
*/
function uni_school_term_type() {
//return array('Semester');
return variable_set('term_type', 'Semester');
}
/**
* Implements hook_faculty_settings
*/
function uni_school_faculty_settings($type) {
$settings['type'] = 'faculty';
$settings['faculty']['add'] = array(
'title' => t('Add'),
'callback url' => 'admin/settings/school/faculty/add'
);
$settings['faculty']['add dept'] = array(
'title' => 'Add department',
'callback url' => 'admin/settings/school/department/add'
);
$settings['faculty']['view'] = array(
'title' => 'View faculty',
'callback url' => 'faculty'
);
if($type == 'add') {
$settings['faculty']['add_faculty_form'] = drupal_get_form('uni_school_faculty_form');
}
if($type == 'add_dept') {
$settings['faculty']['add_dept_form'] = drupal_get_form('uni_school_faculty_dept_form');
}
return $settings;
}
/**
* Faculty add and edit form
*
* @param type $form_state
* @param type $edit
* @return boolean
*/
function uni_school_faculty_form(&$form_state, $faculty = NULL, $edit = NULL) {
$edit = (empty($form_state['values'])) ? (array)$faculty : $form_state['values'];
if($edit) {
drupal_set_title('Edit faculty');
}else {
drupal_set_title('Add faculty');
}
$form['#attributes'] = array('class' => 'form-horizontal');
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $edit['name'],
'#required' => TRUE,
'#attributes' => array('class' => 'faculty-name'),
);
$form['machine_name'] = array(
'#type' => 'textfield',
'#title' => t('Machine-readable name'),
'#description' => t('Example: social_science'). '<br/>' .t('May only contain lowercase letters, numbers and underscores. <strong>Try to avoid conflicts with the names of existing modules.</strong>'),
'#required' => TRUE,
'#default_value' => $edit['machine_name'],
'#attributes' => array('class' => 'faculty-name-machine'),
'#element_validate' => array('acm_school_settings_validate_field'),
);
// If recreating this feature, disable machine name field and blank out
// js-attachment classes to ensure the machine name cannot be changed.
if (isset($edit)) {
$form['machine_name']['#value'] = $edit['name'];
$form['machine_name']['#disabled'] = TRUE;
$form['machine_name']['#attributes'] = array();
}
$form['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $edit['description']
);
$form['facultycode'] = array(
'#type' => 'textfield',
'#title' => t('Faculty code'),
'#default_value' => $edit['facultycode'],
'#required' => TRUE
);
$form['emailsubdomain'] = array(
'#type' => 'textfield',
'#title' => t('Email subdomain'),
'#default_value' => $edit['emailsubdomain']
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($faculty) ? 'Update' : 'Save',
'#attributes' => array('class' => 'btn'),
);
$form['_faculty'] = array('#type' => 'value', '#value' => $edit);
if(!isset($faculty)) {
$form['created'] = array('#type' => 'value', '#value' => time());
}else {
$form['updated'] = array('#type' => 'value', '#value' => time());
$form['_loaded'] = array('#type' => 'value', '#value' => $faculty);
}
return $form;
}
function uni_school_faculty_form_validate($form, &$form_state) {
$faculty = $form_state['values']['_faculty'];
$faculty_name = $form_state['values']['name'];
$faculty_code = $form_state['values']['facultycode'];
$fields = drupal_schema_fields_sql('faculty');
$op = $form_state['values']['op'];
if($op != 'Update') {
// does faculty name exist?
if(in_array($faculty_name, Faculty::listFaculty($fields))) {
form_error($form['name'], t('The faculty, @name already exist', array('@name' => $faculty_name)));
}
// does faculty_code exist?
if(in_array($faculty_code, Faculty::listFaculty($fields))) {
form_error($form['code'], t('The faculty code, @code already exist', array('@code' => $faculty_code)));
}
// @todo validate emailsubdomain field
}
}
function uni_school_faculty_form_submit($form, &$form_state) {
$faculty = $form_state['values']['_faculty'];
$loaded = $form_state['values']['_loaded'];
/*$created = time();
$form_state['values']['created'] = $created;*/
$faculty_table = 'faculty';
$success = FALSE;
if(!empty($faculty)) {
// it's an edit
//$success = drupal_write_record($faculty_table, $faculty, 'id');
db_query("UPDATE {faculty} SET name = '%s', facultycode ='%s', description = '%s', emailsubdomain = '%s', updated = %d WHERE id = %d", $form_state['values']['name'], $form_state['values']['facultycode'], $form_state['values']['description'], $form_state['values']['emailsubdomain'], $form_state['values']['updated'], $loaded['id']);
$success = TRUE;
if($success) {
drupal_set_message('Update completed successfully');
return;
}
}
drupal_write_record($faculty_table, $form_state['values']);
$success = TRUE;
if($success) {
drupal_set_message('Faculty created successfully');
}
}
/**
* Faculty department add and edit form
*/
function uni_school_faculty_dept_form(&$form_state, $dept = NULL, $edit = NULL) {
$edit = (empty($form_state['values'])) ? (array)$dept : $form_state['values'];
if($edit) {
drupal_set_title('Edit department');
}else {
drupal_set_title('Add department');
}
$form['#attributes'] = array('class' => 'form-horizontal');
$form['facultyid'] = array(
'#type' => 'select',
'#title' => t('Faculty'),
'#options' => Faculty::listFaculty('name', 'id'),
'#default_value' => $edit['facultyid'],
'#required' => TRUE
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $edit['name'],
'#required' => TRUE,
'#attributes' => array('class' => 'dept-name'),
);
/*$form['dept_name_machine'] = array(
'#type' => 'textfield',
'#title' => t('Machine-readable name'),
'#description' => t('Example: computer_science'). '<br/>' .t('May only contain lowercase letters, numbers and underscores. <strong>Try to avoid conflicts with the names of existing modules.</strong>'),
'#required' => TRUE,
'#default_value' => $edit['dept_name_machine'],
'#attributes' => array('class' => 'dept-name-machine'),
'#element_validate' => array('acm_school_settings_validate_field'),
);*/
// If recreating this feature, disable machine name field and blank out
// js-attachment classes to ensure the machine name cannot be changed.
/*if (isset($edit)) {
$form['dept_name_machine']['#value'] = $edit['dept_name'];
$form['dept_name_machine']['#disabled'] = TRUE;
$form['dept_name']['#attributes'] = array();
}*/
/* $form['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#required' => TRUE
);*/
$form['code'] = array(
'#type' => 'textfield',
'#title' => t('Department code'),
'#default_value' => $edit['code'],
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($dept) ? 'Update' : 'Save',
'#attributes' => array('class' => 'btn'),
);
$form['_department'] = array('#type' => 'value', '#value' => $edit);
if(!isset($dept)) {
$form['created'] = array('#type' => 'value', '#value' => time());
}else {
$form['updated'] = array('#type' => 'value', '#value' => time());
$form['_loaded'] = array('#type' => 'value', '#value' => $dept);
}
return $form;
}
/**
* Validate Add department form
*/
function uni_school_faculty_dept_form_validate($form, &$form_state) {
$department = $form_state['values']['_department'];
$department_name = $form_state['values']['name'];
$department_code = $form_state['values']['code'];
$fields = drupal_schema_fields_sql('academic_department');
$op = $form_state['values']['op'];
if($op != 'Update') {
// does department name exist?
if(in_array($department_name, Department::listDepartments($fields))) {
form_error($form['name'], t('The department, @name already exist', array('@name' => $department_name)));
}
// does department_code exist?
if(in_array($department_code, Department::listDepartments($fields))) {
form_error($form['code'], t('The department code, @code already exist', array('@code' => $department_code)));
}
}
}
function uni_school_faculty_dept_form_submit($form, &$form_state) {
$department = $form_state['values']['_department'];
$loaded = $form_state['values']['_loaded'];
$department_table = 'academic_department';
$success = FALSE;
if(!empty($department)) {
// it's an edit
// drupal_write_record($department_table, $form_state['values'], 'id');
db_query("UPDATE {academic_department} SET name = '%s', code ='%s', facultyid = %d, updated = %d WHERE id = %d", $form_state['values']['name'], $form_state['values']['code'], $form_state['values']['facultyid'], $form_state['values']['updated'], $loaded['id']);
$success = TRUE;
if($success) {
drupal_set_message('Update completed successfully');
return;
}
}
drupal_write_record($department_table, $form_state['values']);
$success = TRUE;
if($success) {
drupal_set_message('Department created successfully');
}
}
/**
* Implements hook_department_settings
*/
function uni_school_department_settings($type) {
$settings['type'] = 'department';
$settings['dept']['add'] = array(
'title' => t('Add'),
'callback url' => 'admin/settings/school/department/add'
);
// add course to a department
$settings['dept']['add course'] = array(
'title' => 'Add Course',
'callback url' => 'admin/settings/school/department/addcourse'
);
// configure settings for a course
/*$settings['dept']['course setting'] = array(
'title' => 'Configure course',
'callback url' => 'admin/settings/school/department/course'
);*/
$settings['dept']['grade point'] = array(
'title' => 'Set grade point',
'callback url' => 'admin/settings/school/grade/dept_gradepoint'
);
if($type == 'add') {
$settings['dept']['add_dept_form'] = drupal_get_form('uni_school_faculty_dept_form'); //uni_school_add_dept_form()
}
if($type == 'addcourse') {
$settings['dept']['addcourse_form'] = drupal_get_form('uni_school_addcourse_form');
}
return $settings;
}
function uni_school_programme_settings($type) {
$settings['type'] = 'programme';
$settings['prog']['add'] = array(
'title' => t('Add'),
'callback url' => 'admin/settings/school/programme/add'
);
// add course to a department
# $settings['dept']['add course'] = array(
# 'title' => 'Add Course',
# 'callback url' => 'admin/settings/school/department/addcourse'
# );
#
if($type == 'add') {
$settings['prog']['add_prog_form'] = drupal_get_form('uni_school_programme_form');
}
/*if($type == 'addcourse') {
$settings['dept']['addcourse_form'] = drupal_get_form('uni_school_addcourse_form');
}*/
return $settings;
}
function uni_school_programme_form(&$form_state, $programme = NULL, $edit = NULL) {
$edit = (empty($form_state['values'])) ? (array)$programme : $form_state['values'];
if($edit) {
drupal_set_title('Edit Programme');
}else {
drupal_set_title('Add Programme');
}
$form['#attributes'] = array('class' => 'form-horizontal');
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Programme'),
'#description' => t('The programme name'),
'#default_value' => $edit['name'],
'#required' => TRUE
);
/*$form['programme_name_machine'] = array(
'#type' => 'textfield',
'#title' => t('Machine-readable name'),
'#description' => t('Example: human_computer_interaction'). '<br/>' .t('May only contain lowercase letters, numbers and underscores. <strong>Try to avoid conflicts with the names of existing modules.</strong>'),
'#required' => TRUE,
'#default_value' => $edit['programme_name_machine'],
'#attributes' => array('class' => 'programme-name-machine'),
'#element_validate' => array('acm_school_settings_validate_field'),
);*/
// If recreating this feature, disable machine name field and blank out
// js-attachment classes to ensure the machine name cannot be changed.
/*if (isset($edit)) {
$form['programme_name_machine']['#value'] = $edit['programme_name'];
$form['programme_name_machine']['#disabled'] = TRUE;
$form['programme_name']['#attributes'] = array();
}*/
$form['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $edit['description'],
);
$form['abbr'] = array(
'#type' => 'textfield',
'#title' => t('Programme code'),
'#description' => t('Programme code or abbreviation'),
'#default_value' => $edit['abbr'],
'#required' => TRUE
);
$form['academic_departmentid'] = array(
'#type' => 'select',
'#title' => t('Department'),
'#description' => t('(Optional) The department this programme belong to. For example, Economics programme belongs to department of Economics'),
'#options' => Department::listDepartmentsByIds(),
'#default_value' => $edit['academic_departmentid'],
'#required' => TRUE
);
$form['duration'] = array(
'#type' => 'textfield',
'#title' => t('Duration'),
'#description' => t('Duration of the programme'),
'#default_value' => $edit['duration'],
'#required' => TRUE
);
$form['maximum_duration'] = array(
'#type' => 'textfield',
'#title' => t('Maximum duration'),
'#description' => t('The maximum duration of a student can use for this course'),
'#default_value' => $edit['maximum_duration'],
'#required' => TRUE
);
$form['type'] = array(
'#type' => 'select',
'#title' => t('Programme type'),
'#description' => t('Programme study type'),
'#options' => StudyType::listStudyType(),
'#default_value' => $edit['type'],
'#required' => TRUE
);
$form['minimum_credit_load'] = array(
'#type' => 'textfield',
'#title' => t('Minimum credit load'),
'#description' => t('Minimum credit load to graduate'),
'#default_value' => $edit['minimum_credit_load'],
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($programme) ? 'Update' : 'Save',
'#attributes' => array('class' => 'btn'),
);
$form['_programme'] = array('#type' => 'value', '#value' => $edit);
$form['created'] = array('#type' => 'value', '#value' => time());
if(isset($programme)) {
$form['state'] = array('#type' => 'value', '#value' => 'update');
$form['updated'] = array('#type' => 'value', '#value' => time());
$form['_loaded'] = array('#type' => 'value', '#value' => $programme);
}
return $form;
}
function uni_school_programme_form_validate($form, &$form_state) {
$programme = $form_state['values']['_programme'];
$programme_name = $form_state['values']['name'];
$programme_abbr = $form_state['values']['abbr'];
$fields = drupal_schema_fields_sql('academic_programme');
$op = $form_state['values']['op'];
if($op != 'Update') {
// does programme name exist?
if(in_array($programme_name, Programme::listProgrammes($fields))) {
form_error($form['name'], t('The programme @name already exist', array('@name' => $programme_name)));
}
// does programme code exist?
if(in_array($programme_abbr, Programme::listProgrammes($fields))) {
form_error($form['abbr'], t('The programme code @name already exist.', array('@name' => $programme_abbr)));
}
}
}
function uni_school_programme_form_submit($form, &$form_state) {
$programme = $form_state['values']['_programme'];
$programme_table = 'academic_programme';
$loaded = $form_state['values']['_loaded'];
$op = $form_state['values']['op'];
$success = FALSE;
//if(!empty($programme)) {
if($op == 'Update') {
// it's an edit
//drupal_write_record($programme_table, $form_state['values'], 'id');
db_query("UPDATE {academic_programme} SET name = '%s', academic_departmentid = %d, description ='%s', abbr = '%s', duration = %d, maximum_duration = %d, type = '%s', minimum_credit_load = %d, updated = %d WHERE id = %d", $form_state['values']['name'], $form_state['values']['academic_departmentid'], $form_state['values']['description'], $form_state['values']['abbr'], $form_state['values']['duration'],
$form_state['values']['maximum_duration'], $form_state['values']['type'], $form_state['values']['minimum_credit_load'], $form_state['values']['updated'], $loaded['id']);
$success = TRUE;
if($success) {
drupal_set_message('Update completed successfully');
return;
}
drupal_set_message('An error occurred while updating Programme');
return FALSE;
}
drupal_write_record($programme_table, $form_state['values']);
$success = TRUE;
if($success) {
drupal_set_message('Programme created successfully');
}
}
function uni_school_student_application_table_info() {
$table['course_info'] = array(
'primary key' => 'id'
);
return $table;
}
/**
* Course form to save or edit a course for a department and programme
*
* A course can be under a programme and also taught in a specific
* department
*
* @param type $form_state
* @param type $edit
* @param type $dept
*/
function uni_school_addcourse_form(&$form_state, $course = NULL, $edit = NULL) {
$edit = (empty($form_state['values'])) ? (array)$course : $form_state['values'];
if($edit) {
drupal_set_title('Edit course');
}else {
drupal_set_title('Add course');
}
// @todo $fields = module_invoke($module, 'course_field_info');
// snippet: if(isset($fields['title'])
$form['#attributes'] = array('class' => 'form-horizontal');
// course title
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Course title'),
'#description' => t('The course title'),
'#default_value' => $edit['title'],
'#required' => TRUE
);
$form['programme'] = array(
'#type' => 'select',
'#title' => t('Programme'),
'#description' => t('The programme this course belongs to'),
'#options' => Programme::listProgrammes(array('id','name'), 'id'),
'#default_value' => $edit['programme'],
'#required' => TRUE
);
// level
$level = module_invoke('uni_school', 'academic_level');
$form['level'] = array(
'#type' => 'select',
'#title' => t('Academic level'),
//'#options' => AcademicLevel::listAcademicLevels(),
//'#options' => drupal_map_assoc(array(100, 200, 300, 400, 500)),
'#options' => $level,
'#default_value' => $edit['level'],
'#required' => TRUE
);
$semester = drupal_map_assoc(array(
variable_get('first_semester_name', ''),
variable_get('second_semester_name', '')
));
// semester
$form['semester'] = array(
'#type' => 'select',
'#title' => t('Academic Term'),
'#description' => t('Course term or semester'),
//'#options' => AcademicSemester::listAcademicSemesters(),
'#options' => $semester,
'#default_value' => $edit['semester'],
'#required' => TRUE
);
// course code
$form['code'] = array(
'#type' => 'textfield',
'#title' => t('Course code'),
'#description' => t('The course code'),
'#default_value' => $edit['code'],
'#required' => TRUE
);
// course type
$form['type'] = array(
'#type' => 'select',
'#title' => t('Course type'),
'#description' => t('Core, required or elective'),
'#options' => drupal_map_assoc(array('Core', 'Required', 'Elective')), // core, required or elective
'#default_value' => $edit['type'],
'#required' => TRUE
);
// credit point
$form['creditpoint'] = array(
'#type' => 'select',
'#title' => t('Credit point'),
'#description' => t('The applicable credit for the course. 3 points, 5 points etc'),
'#default_value' => $edit['creditpoint'],
'#options' => drupal_map_assoc(array(1,2,3,4,5)),
'#required' => TRUE
);
// academic session
$form['session'] = array(
'#type' => 'select',
'#title' => t('Academic session'),
'#description' => t('The academic session this course is available for'),
'#options' => AcademicSession::listAcademicSession(),
'#default_value' => $edit['session'],
'#required' => TRUE
);
// lecturer
$lecturers = acm_school_get_users_in_a_role(37);
$form['lecturerincharge'] = array(
'#type' => 'select',// separate by a comma
'#title' => t('Course lecturer'),
'#description' => t('Course assigned to a lecturer'),
'#options' => $lecturers, // a list of lecturers in the school
'#default_value' => $edit['lecturerincharge'],
'#required' => TRUE
);
// department
$form['academic_departmentid'] = array(
'#type' => 'select',
'#title' => t('Department'),
'#description' => t('The department the course belong to'),
'#options' => Department::listDepartmentsByIds(),
'#default_value' => $edit['academic_departmentid'],
'#required' => TRUE
);
// course prerequisites
$form['prerequisites'] = array(
'#type' => 'textfield', // seperat with a comma
'#title' => t('Course prerequisites'),
'#description' => t('Courses this course depend on to have been taken. Please separate each item with a comma. E.g Maths for Economics, Finance'),
'#default_value' => $edit['prerequisites']
);
$form['prerequisitecodes'] = array(
'#type' => 'textfield',
'#title' => t('Course prerequisite codes'),
'#description' => t('Course codes this course depend on to have been taken. Please separate each item with a comma. E.g ECO 101, ECO 203'),
'#default_value' => $edit['prerequisitecodes']
);
$form['numberofcas'] = array(
'#type' => 'texfield',
'#title' => t('Course title'),
'#description' => t('A setting to configure total number of CA(continuous assessment) for this course'),
'#default_value' => $edit['numberofcas']
);
$form['caapprovalmethod'] = array(
'#type' => 'select',
'#title' => t('CA approval method'),
'#description' => t('A setting to configure continuous assessment approval method.'),
'#default_value' => $edit['caapprovalmethod'],
'#options' => drupal_map_assoc(array('Approved along with exams', 'Approved individually'))
);
$form['test_score_id'] = array(
'#type' => 'textfield',
'#title' => t('Test score'),
'#description' => t('This course test score'),
'#default_value' => $edit['test_score_id']
);
$form['maxtestmark'] = array(
'#type' => 'textfield',
'#title' => t('Maximum test mark'),
'#description' => t('A test score maximum mark. For example, 10 is the maximum mark for a test score of 5/10'),
'#default_value' => $edit['maxtestmark']
);
$form['exam_score_id'] = array(
'#type' => 'textfield',
'#title' => t('Exam score'),
'#description' => t('This course exam score'),
'#default_value' => $edit['exam_score_id']
);
$form['maxexammark'] = array(
'#type' => 'textfield',
'#title' => t('Maximum exam mark'),
'#description' => t('An exam score maximum mark. For example, 100 is the maximum mark for an exam score of 50/100'),
'#default_value' => $edit['maxexammark']
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($course) ? 'Update' : 'Save',
'#attributes' => array('class' => 'btn'),
);
$form['_course'] = array('#type' => 'value', '#value' => $edit);
if(!isset($course)) {
$form['created'] = array('#type' => 'value', '#value' => time());
} else {
$form['updated'] = array('#type' => 'value', '#value' => time());
$form['_loaded'] = array('#type' => 'value', '#value' => $course);
}
return $form;
}
function uni_school_addcourse_form_submit($form, &$form_state) {
$course = $form_state['values']['_course'];
$loaded = $form_state['values']['_loaded'];
$course_table = 'academic_course';
$success = FALSE;
if(!empty($course)) {
// it's an edit
//drupal_write_record($course_table, $course, 'id');
//dsm($form_state['values']);
db_query("UPDATE {academic_course} SET title = '%s', programme = %d, academic_departmentid = %d, code = '%s', description = %d, creditpoint = %d, type = '%s', prerequisitecodes = '%s', prerequisites = '%s', level = '%s', semester = '%s', session = '%s', lecturerincharge = '%s', numberofcas = %d, caapprovalmethod = '%s', test_score_id = %d, maxtestmark = %d, exam_score_id = %d, maxexammark = %d, updated = %d WHERE id = %d", $form_state['values']['title'], $form_state['values']['programme'],
$form_state['values']['academic_departmentid'], $form_state['values']['code'], $form_state['values']['description'], $form_state['values']['creditpoint'], $form_state['values']['type'], $form_state['values']['prerequisitecodes'], $form_state['values']['prerequisites'], $form_state['values']['level'], $form_state['values']['semester'],
$form_state['values']['session'], $form_state['values']['lecturerincharge'],
$form_state['values']['numberofcas'], $form_state['values']['caapprovalmethod'], $form_state['values']['test_score_id'], $form_state['values']['maxtestmark'], $form_state['values']['exam_score_id'], $form_state['values']['maxexammark'], $form_state['values']['updated'], $loaded['id']);
// Also update student_course
db_query("UPDATE {student_course} SET totalcreditload = '%s' WHERE academic_courseid = %d", $form_state['values']['creditpoint'], $loaded['id']);
$success = TRUE;
if($success) {
drupal_set_message('Update completed successfully');
return;
}
}
$success = drupal_write_record($course_table, $form_state['values']);
//$course_id = db_last_insert_id($course_table, 'id');
$course_id = db_fetch_object(db_query("SELECT MAX(id) as id FROM {$course_table}"));
// insert into student_course
db_query("INSERT INTO {student_course} (academic_courseid, registered, totalcreditload, uid) VALUES (%d, %d, %d, %d)", $course_id, 0, $form_state['values']['creditpoint'], 0);
if($success) {
// TODO: Add new course_content node(s). Contents to add are Course Introduction, Course materials, course references
// Invoke acm_school_create_batch_course_content here
//acm_school_create_batch_course_content($course_id);