forked from jcharaoui/moodle-cegep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_cegep_enrol_admin_form.php
147 lines (119 loc) · 5.56 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
140
141
142
143
144
145
146
147
<?php
require_once($CFG->libdir.'/formslib.php');
class cegep_enrol_admin_form extends moodleform {
function definition() {
$mform =& $this->_form;
$current = cegep_local_current_term();
$year = substr($current, 0, 4);
$semester = substr($current, 4, 1);
$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="6", maxlength="6"');
$mform->addRule('coursegroup', get_string('specifycoursegroup','block_cegep'), 'required');
$mform->addRule('coursegroup', get_string('coursegroupsixnumbersonly','block_cegep'), 'numeric');
$mform->setType('coursegroup', PARAM_TEXT);
$mform->SetDefaults(array(
'year' => $year,
'semester' => $semester,
));
$this->add_action_buttons();
}
function validation($data, $files) {
global $COURSE;
$errors = parent::validation($data, $files);
$term = "$data[year]$data[semester]";
if (empty($coursecode) || !has_capability('moodle/site:doanything', $context)) {
$cc = explode('_', $COURSE->idnumber);
$coursecode = $cc[0];
}
else {
$coursecode = $data['coursecode'];
}
$coursegroup = $data['coursegroup'];
if (empty($data['semester']) || empty($data['year'])) {
$errors['semester'] = get_string('semesterinvalid','block_cegep');
}
elseif (empty($coursegroup) || !is_numeric($coursegroup)) {
$errors['coursegroup'] = get_string('coursegroupinvalid','block_cegep');
}
if (!empty($errors)) {
return $errors;
}
// Verify if the coursegroup is available in the system
elseif (!$coursegroup_id = cegep_local_get_coursegroup_id($coursecode, $data['coursegroup'], $term)) {
$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');
}
return $errors;
}
private function validate_coursegroup_exists($coursecode, $coursegroup, $term) {
global $CFG, $COURSE, $sisdb;
$context = get_context_instance(CONTEXT_SYSTEM);
if (empty($coursecode) || !has_capability('moodle/site:doanything', $context)) {
$cc = explode('_', $COURSE->idnumber);
$coursecode = $cc[0];
}
$select = "SELECT * FROM `$CFG->sisdb_name`.`coursegroup` WHERE `coursecode` = '$coursecode' AND `term` = '$term' AND `group` = '$coursegroup' 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 $result->fields['id'];
}
}
private function validate_coursegroup_enrolled($coursegroup_id) {
global $CFG, $COURSE, $enroldb;
$select = "SELECT COUNT(`coursegroup_id`) AS num FROM `$CFG->enrol_dbtable` 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;
}
}
}
?>