function get_authoriser($author_id, $modular, $role, $fields) { global $DB; $authoriser_id = 0; if ($role == 1) { // CSA $authoriser = get_complete_user_data('username', 'csa'); $authoriser_id = $authoriser->id; } else { if ($role == 2) { // Module Leader $modules = get_current_modules(); $module_id = array_search(strtoupper($fields['module']), $modules, true); $authoriser_id = get_module_leader($module_id); } else { if ($role == 3) { // Subject Coordinator if ($fields['course']) { // Might not be present (or might not be mandatory) $courses = get_current_courses(); $course_id = array_search(strtoupper($fields['course']), $courses, true); } else { // Get the author's current course (programme) $courses = get_current_courses($author_id, $modular); $course_id = key($courses); } if ($course_id) { $context = context_course::instance($course_id); $sc_role = $DB->get_record('role', array('shortname' => 'subject_coordinator'), 'id', MUST_EXIST); $subject_coordinators = get_role_users($sc_role->id, $context, false, 'u.id'); // Exclude inherited roles foreach ($subject_coordinators as $subject_coordinator) { $authoriser_id = $subject_coordinator->id; } } } else { if ($role == 4 && $fields['supervisor']) { // Supervisor (field must be present) $start_pos = strpos($fields['supervisor'], '(') + 1; $end_pos = strpos($fields['supervisor'], ')', $start_pos); $supervisor = $DB->get_record('user', array('username' => substr($fields['supervisor'], $start_pos, $end_pos - $start_pos)), 'id', MUST_EXIST); $authoriser_id = $supervisor->id; } else { if ($role == 5) { // Academic Adviser $context = context_user::instance($author_id); $aa_role = $DB->get_record('role', array('shortname' => 'academic_adviser'), 'id', MUST_EXIST); $academic_advisers = get_role_users($aa_role->id, $context, false, 'u.id'); // Exclude inherited roles foreach ($academic_advisers as $academic_adviser) { $authoriser_id = $academic_adviser->id; } } else { if ($role == 6) { // Programme Lead $authoriser_id = get_programme_lead($author_id, $modular, 0); } else { if ($role == 7) { // Programme Lead (2) - only present for joint honours students (will skip step otherwise) $authoriser_id = get_programme_lead($author_id, $modular, 1); } } } } } } } if ($authoriser_id == 0 && $role != 7) { // Don't leave them hanging... $authoriser = get_complete_user_data('username', 'csa-tbd'); // Default ('TO BE DETERMINED') $authoriser_id = $authoriser->id; } return $authoriser_id; }
$supervisor = get_supervisors($USER->id); } break; case 'course': if (empty($course)) { $course = get_current_courses(0, $form->modular); } break; case 'not_enroled': if (empty($not_enroled)) { $not_enroled = get_current_modules(0, null, $USER->id, false); } break; case 'enroled': if (empty($enroled)) { $enroled = get_current_modules(0, null, $USER->id, true); } break; case 'study_mode': if (empty($study_mode)) { $study_mode = get_study_modes(); } break; case 'reason': if (empty($reason)) { $reason = get_reasons(); } break; case 'addition_reason': if (empty($addition_reason)) { $addition_reason = get_addition_reasons();
function validation($data, $files) { $errors = parent::validation($data, $files); // Ensure we don't miss errors from any higher-level validation // Get valid dates for +- 5 years (MMMYY format) $start_dates = get_dates(date('m'), date('y'), 60, 60); // Check if at least one field in a required group has an entry if (empty($this->required_group)) { $group_entry = true; } else { $group_entry = false; foreach ($this->required_group as $key) { if ($data[$key] != '') { $group_entry = true; } } } // Do our own validation and add errors to array $required_value = false; foreach ($data as $key => $value) { if ($value == '' && in_array($key, $this->required_field, true)) { $required_value = true; // Leave the field error display to Moodle } else { if (!$group_entry && in_array($key, $this->required_group, true)) { // One of a required group with no entries $errors[$key] = get_string('group_required', 'local_obu_forms'); } else { if ($value == '' && in_array($key, $this->set_group, true) && (array_key_exists($this->check_id, $data) && $data[$this->check_id] == '1')) { // Controlled by a check box $errors[$key] = get_string('value_required', 'local_obu_forms'); } else { if ($value == '' && in_array($key, $this->unset_group, true) && (array_key_exists($this->check_id, $data) && $data[$this->check_id] == '0')) { // Controlled by a check box $errors[$key] = get_string('value_required', 'local_obu_forms'); } else { if ($key == 'adviser') { // They must have selected one if ($value == '0') { // Oh No! They haven't! $errors[$key] = get_string('value_required', 'local_obu_forms'); } } else { if ($key == 'supervisor') { // They must have selected one if ($value == '0') { // Oh No! They haven't! $errors[$key] = get_string('value_required', 'local_obu_forms'); } } else { if ($key == 'course') { // Exact match - should be a current course (programme) code if ($value != '') { // Might not be mandatory $current_courses = get_current_courses(0, $this->_customdata['modular']); if (!in_array(strtoupper($value), $current_courses, true)) { $errors[$key] = get_string('course_not_found', 'local_obu_forms'); } } } else { if (strpos($key, 'module') !== false) { // Validate module code format etcetera if ($value != '') { // Only validate if the field was completed $prefix = strtoupper(substr($value, 0, 1)); $suffix = substr($value, 1); if (strlen($value) != 6 || $prefix != 'C' && $prefix != 'F' && $prefix != 'P' && $prefix != 'U' || !is_numeric($suffix)) { $errors[$key] = get_string('invalid_module_code', 'local_obu_forms'); } else { if ($this->_customdata['modular'] && $prefix != 'U') { $errors[$key] = get_string('invalid_module_code', 'local_obu_forms'); } else { if ($key == 'module') { // Exact match - should be a current module $current_modules = get_current_modules(); if (!in_array($prefix . $suffix, $current_modules, true)) { $errors[$key] = get_string('module_not_found', 'local_obu_forms'); } } } } // Check that any associated module fields have also been completed $pos = strpos($key, 'module'); $prefix = substr($key, 0, $pos); $suffix = substr($key, $pos + 6); $key = $prefix . 'title' . $suffix; if (array_key_exists($key, $data) && $data[$key] == '') { $errors[$key] = get_string('value_required', 'local_obu_forms'); } $key = $prefix . 'start' . $suffix; if (array_key_exists($key, $data) && $data[$key] == '') { $errors[$key] = get_string('value_required', 'local_obu_forms'); } $key = $prefix . 'mark' . $suffix; if (array_key_exists($key, $data) && $data[$key] == '') { $errors[$key] = get_string('value_required', 'local_obu_forms'); } $key = $prefix . 'credit' . $suffix; if (array_key_exists($key, $data) && $data[$key] == '') { $errors[$key] = get_string('value_required', 'local_obu_forms'); } } } else { if (strpos($key, 'start') !== false) { // Validate start date format if ($value != '') { // Only validate if the field was completed $month = strtoupper(substr($value, 0, 3)); $year = substr($value, 3); if (strlen($value) != 5 || is_numeric($month) || !is_numeric($year)) { $errors[$key] = get_string('invalid_date_format', 'local_obu_forms'); } else { if (!in_array($month . $year, $start_dates, true)) { $errors[$key] = get_string('invalid_start_date', 'local_obu_forms'); } } } } } } } } } } } } } if ($required_value || !empty($errors)) { $errors['form_error'] = get_string('form_error', 'local_obu_forms'); } return $errors; }