-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_cegep_enrol_form.php
142 lines (110 loc) · 5.26 KB
/
block_cegep_enrol_form.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
<?php
require_once($CFG->libdir.'/formslib.php');
class cegep_enrol_form extends moodleform {
function definition() {
global $COURSE, $USER;
$mform =& $this->_form;
// Extract enrolled coursegroups info
$coursegroups = self::get_available_coursegroups_list();
if ($coursegroups != FALSE && count($coursegroups) > 0) {
$mform->addElement('html', '<p>' . get_string('selectmultiplehelp','block_cegep') . '</p>');
$coursegroup_select = $mform->createElement('select', 'coursegroup', null, $coursegroups);
$coursegroup_select->setMultiple(true);
$mform->addElement($coursegroup_select);
$mform->addRule('coursegroup', get_string('specifycoursegroup','block_cegep'), 'required');
$mform->setType('coursegroup', PARAM_INT);
$mform->addElement('checkbox', 'creategroups', null, get_string('creategroups','block_cegep'));
$this->add_action_buttons();
}
else {
$mform->addElement('html', '<div class="alert alert-error">' . get_string('nocoursegroupsavailable','block_cegep') . '</div>');
}
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['coursegroup'])) {
$errors['coursegroup'] = get_string('nocoursegroupselected','block_cegep');
return $errors;
}
// Verify if the coursegroup is already enrolled into this course
foreach ($data['coursegroup'] as $coursegroup_id) {
if (!is_numeric($coursegroup_id)) {
$errors["coursegroup"] = get_string('coursegroupunavailable','block_cegep');
}
// Verify if the coursegroup is available in the system
elseif (!$coursegroup_id = self::validate_coursegroup_exists($coursegroup_id)) {
$errors["coursegroup"] = get_string('coursegroupunavailable','block_cegep');
}
// Verify if the coursegroup is already enrolled into this course
elseif (self::validate_coursegroup_enrolled($coursegroup_id)) {
$errors["coursegroup"] = get_string('coursegroupalreadyenrolled','block_cegep');
}
// Verify if the coursegroup has students registered
elseif (!self::validate_coursegroup_students_registered($coursegroup_id)) {
$errors["coursegroup"] = get_string('coursegrouphasnostudents','block_cegep');
}
array_push($coursegroup_ids, $coursegroup_id);
}
return $errors;
}
private function get_available_coursegroups_list() {
global $COURSE, $USER;
$coursegroups_list = array();
if (!$coursegroups = cegep_local_get_unenrolled_coursegroups($COURSE->idnumber, $USER->idnumber)) {
return false;
}
foreach ($coursegroups as $coursegroup) {
$coursegroups_list[$coursegroup['id']] = "$coursegroup[coursecode] #$coursegroup[group] - " . cegep_local_term_to_string($coursegroup['term']) . " ($coursegroup[numberofstudents] ".get_string('students', 'block_cegep').')';
}
return $coursegroups_list;
}
private function validate_term($term) {
global $CFG, $sisdb;
$select = "SELECT * FROM `$CFG->sisdb_name`.`coursegroup` WHERE `term` = '$term' LIMIT 1";
$result = $sisdb->Execute($select);
if (!$result) {
trigger_error($sisdb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
}
elseif ($result->RecordCount() < 1)
return false;
else
return true;
}
private function validate_coursegroup_exists($coursegroup_id) {
global $CFG, $COURSE, $sisdb;
$select = "SELECT * FROM `$CFG->sisdb_name`.`coursegroup` WHERE `id` = '$coursegroup_id';";
$result = $sisdb->Execute($select);
if (!$result) {
trigger_error($sisdb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
} elseif ($result->RecordCount() < 1)
return false;
else
return $result->fields['id'];
}
private function validate_coursegroup_enrolled($coursegroup_id) {
global $CFG, $COURSE, $enroldb;
$select = "SELECT COUNT(`coursegroup_id`) AS num FROM `$CFG->enrol_remoteenroltable` WHERE `$CFG->enrol_remotecoursefield` = '$COURSE->idnumber' AND `coursegroup_id` = '$coursegroup_id' LIMIT 1";
$result = $enroldb->Execute($select);
if (!$result) {
trigger_error($enroldb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
}
else
return $result->fields['num'];
}
private function validate_coursegroup_students_registered($coursegroup_id) {
global $CFG, $COURSE, $sisdb;
$select = "SELECT * FROM `$CFG->sisdb_name`.`student_enrolment` WHERE `coursegroup_id` = $coursegroup_id;";
$result = $sisdb->Execute($select);
if (!$result) {
trigger_error($sisdb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
} elseif ($result->RecordCount() < 1)
return false;
else
return true;
}
}
?>