-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_cegep_unenrolprogram_form.php
93 lines (65 loc) · 3.29 KB
/
block_cegep_unenrolprogram_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
<?php
require_once($CFG->libdir.'/formslib.php');
class cegep_unenrolprogram_form extends moodleform {
function definition() {
$mform =& $this->_form;
// Extract enrolled coursegroups info
$programs = self::get_enrolled_programs();
$programs_select = $mform->createElement('select', 'program', null, $programs);
$programs_select->setMultiple(true);
$mform->addElement($programs_select);
$mform->addRule('program', get_string('specifyprogram','block_cegep'), 'required');
$mform->setType('program', PARAM_TEXT);
$this->add_action_buttons(true, get_string('unenrolbutton', 'block_cegep'));
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['program'])) {
$errors['program'] = get_string('programinvalid','block_cegep');
return $errors;
}
// Verify if the program is enrolled into this course
foreach ($data['program'] as $program_idyear) {
if (!self::validate_program_enrolled($program_idyear))
$errors['program'] = get_string('programnotenrolled','block_cegep');
}
return $errors;
}
private function get_enrolled_programs() {
global $CFG, $COURSE, $enroldb, $sisdb;
$select = "SELECT DISTINCT `program_idyear`, COUNT(`program_idyear`) AS num FROM `$CFG->enrol_remoteenroltable` WHERE `$CFG->enrol_remotecoursefield` = '$COURSE->idnumber' AND `$CFG->enrol_remoterolefield` = '$CFG->block_cegep_studentrole' AND `program_idyear` IS NOT NULL GROUP BY `program_idyear` ORDER BY `program_idyear`";
$programs_rs = $enroldb->Execute($select);
if (!$programs_rs) {
trigger_error($enroldb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
}
$program_id = '';
$program_year = '';
$programs = array();
while (!$programs_rs->EOF) {
$program_idyear = $programs_rs->fields['program_idyear'];
$program_idyear = explode('_', $program_idyear);
$program_id = $program_idyear[0];
$program_year = $program_idyear[1];
$num_students = $programs_rs->fields['num'];
$select = "SELECT * FROM `$CFG->sisdb_name`.`program` WHERE id = '$program_id'";
$program = $sisdb->Execute($select)->fields;
$programs[$program_id.'_'.$program_year] = "$program_id - $program[title] - ".get_string('programyear'.$program_year,'block_cegep')." ($num_students ".get_string('students', 'block_cegep').')';
$programs_rs->MoveNext();
}
return $programs;
}
private function validate_program_enrolled($program_idyear) {
global $CFG, $COURSE, $enroldb;
$select = "SELECT COUNT(`program_idyear`) AS num FROM `$CFG->enrol_remoteenroltable` WHERE `$CFG->enrol_remotecoursefield` = '$COURSE->idnumber' AND `program_idyear` = '$program_idyear' LIMIT 1";
$result = $enroldb->Execute($select);
if (!$result) {
trigger_error($enroldb->ErrorMsg() .' STATEMENT: '. $select, E_USER_ERROR);
return false;
}
else {
return $result->fields['num'];
}
}
}
?>