-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_cegep_enrol_admin_form.php
139 lines (111 loc) · 5.27 KB
/
block_cegep_enrol_admin_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
<?php
require_once($CFG->libdir.'/formslib.php');
class cegep_enrol_admin_form extends moodleform {
function definition() {
global $COURSE;
$mform =& $this->_form;
$current = cegep_local_current_term();
$year = substr($current, 0, 4);
$semester = substr($current, 4, 1);
// If course isn't visible, offer the option
if (!$COURSE->visible) {
$mform->addElement('checkbox', 'makevisible', null, get_string('make_visible','block_cegep'));
}
$enrol = array();
$enrol[] =& $mform->createElement('select', 'semester', null, array('1' => get_string('winter','block_cegep'), '2' => get_string('summer','block_cegep'), '3' => get_string('autumn','block_cegep')));
$enrol[] =& $mform->createElement('select', 'year', null, array($year-1 => $year-1, $year => $year, $year+1 => $year+1));
$mform->addGroup($enrol, 'semester', get_string('semester','block_cegep').' :', ' ', false);
$mform->addRule('semester', get_string('specifysemester','block_cegep'), 'required');
$mform->addGroupRule('semester', array(
'trimestre' => array(
array(get_string('specifysemester','block_cegep'), 'required')
),
'year' => array(
array(get_string('specifyyear','block_cegep'), 'required')
)
), 'required', null, 2);
$mform->setType('semester', PARAM_INT);
$mform->setType('year', PARAM_INT);
$mform->addElement('text', 'coursecode', get_string('coursecode','block_cegep').' :', 'size="8", maxlength="8"');
$mform->setType('coursecode', PARAM_TEXT);
$mform->addElement('text', 'coursegroup', get_string('coursegroup','block_cegep').' :', 'size="8", maxlength="32"');
$mform->addRule('coursegroup', get_string('specifycoursegroup','block_cegep'), 'required');
$mform->setType('coursegroup', PARAM_TEXT);
$mform->SetDefaults(array(
'year' => $year,
'semester' => $semester,
));
$mform->addElement('checkbox', 'creategroups', null, get_string('creategroups','block_cegep'));
$this->add_action_buttons();
}
function validation($data, $files) {
global $COURSE, $USER;
$errors = parent::validation($data, $files);
$term = "$data[year]$data[semester]";
if (empty($data['coursecode'])) {
$cc = explode('_', $COURSE->idnumber);
$coursecode = $cc[0];
}
else {
$coursecode = $data['coursecode'];
}
if (empty($data['semester']) || empty($data['year'])) {
$errors['semester'] = get_string('semesterinvalid','block_cegep');
}
elseif (empty($data['coursegroup']) || preg_match('/[^0-9,]/', $data['coursegroup'])) {
$errors['coursegroup'] = get_string('coursegroupinvalid','block_cegep');
}
if (!empty($errors)) {
return $errors;
}
if (strstr($data['coursegroup'], ',')) {
$coursegroups = explode(',', $data['coursegroup']);
} else {
$coursegroups = array($data['coursegroup']);
}
foreach ($coursegroups as $coursegroup) {
$coursegroup = trim($coursegroup);
// Verify if the coursegroup is available in the system
if (!$coursegroup_id = cegep_local_get_coursegroup_id($coursecode, $coursegroup, $term)) {
$errors['coursegroup'] = get_string('coursegroupunavailable','block_cegep') . " ($coursegroup)";
}
// Verify if the coursegroup is already enrolled into this course
elseif (self::validate_coursegroup_enrolled($coursegroup_id)) {
$errors['coursegroup'] = get_string('coursegroupalreadyenrolled','block_cegep') . " ($coursegroup)";
}
// Verify if the coursegroup has students registered
elseif (!self::validate_coursegroup_students_registered($coursegroup_id)) {
$errors['coursegroup'] = get_string('coursegrouphasnostudents','block_cegep') . " ($coursegroup)";
}
}
return $errors;
}
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;
}
}
}
?>