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})";
         } elseif (self::validate_coursegroup_enrolled($coursegroup_id)) {
             $errors['coursegroup'] = get_string('coursegroupalreadyenrolled', 'block_cegep') . " ({$coursegroup})";
         } elseif (!self::validate_coursegroup_students_registered($coursegroup_id)) {
             $errors['coursegroup'] = get_string('coursegrouphasnostudents', 'block_cegep') . " ({$coursegroup})";
         }
     }
     return $errors;
 }
Ejemplo n.º 2
0
/**
 * Enrol a coursegroup/section into a Moodle course.
 * Accepts either the coursegroup id OR coursecode, coursegroup
 * and term for the coursegroup/section to enrol.
 */
function cegep_local_enrol_coursegroup()
{
    global $CFG, $COURSE, $DB, $enroldb, $sisdb;
    $args = func_get_args();
    if (count($args) == 1) {
        $coursegroup_id = $args[0];
    } elseif (count($args) == 3) {
        $coursegroup_id = cegep_local_get_coursegroup_id($args[0], $args[1], $args[2]);
        $coursegroup = $args[1];
    } else {
        return FALSE;
    }
    // Fetch records of students enrolled into this course from SIS
    $select_students = "SELECT * FROM `{$CFG->sisdb_name}`.`student_enrolment` WHERE `coursegroup_id` = {$coursegroup_id};";
    $students_rs = $sisdb->Execute($select_students);
    // Fail if can't find coursegroup id or student list
    if (!$coursegroup_id || !$students_rs) {
        return FALSE;
    }
    $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
    $student_role = $DB->get_record('role', array($CFG->enrol_localrolefield => $CFG->block_cegep_studentrole));
    // Go through each student and insert Moodle external enrolment database record
    $students_enrolled = array();
    while ($students_rs && !$students_rs->EOF) {
        $student = $students_rs->fields;
        if (!cegep_local_enrol_user($COURSE->idnumber, $student['username'], $CFG->block_cegep_studentrole, $coursegroup_id)) {
            return FALSE;
        } else {
            array_push($students_enrolled, $student['username']);
        }
        $students_rs->MoveNext();
    }
    return $students_enrolled;
}