-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_cegep_unenrol_form.php
79 lines (58 loc) · 2.88 KB
/
block_cegep_unenrol_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
<?php
require_once($CFG->libdir.'/formslib.php');
class cegep_unenrol_form extends moodleform {
function definition() {
$mform =& $this->_form;
// Extract enrolled coursegroups info
$coursegroups = self::get_enrolled_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);
$this->add_action_buttons(true, get_string('unenrolbutton', 'block_cegep'));
}
else {
$mform->addElement('html', '<div class="alert alert-error">' . get_string('nocoursegroupsenrolled','block_cegep') . '</div>');
}
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['coursegroup'])) {
$errors['coursegroup'] = get_string('coursegroupinvalid','block_cegep');
return $errors;
}
// Verify if the coursegroup is already enrolled into this course
foreach ($data['coursegroup'] as $coursegroup_id) {
if (!self::validate_coursegroup_enrolled($coursegroup_id))
$errors['coursegroup'] = get_string('coursegroupnotenrolled','block_cegep');
}
return $errors;
}
private function get_enrolled_coursegroups_list() {
global $CFG, $COURSE, $enroldb, $sisdb;
$coursegroups_list = array();
if (!$coursegroups = cegep_local_get_enrolled_coursegroups($COURSE->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_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'];
}
}
}
?>