public function validation($data, $files) { global $USER; $result = array(); // User record field to match against, has to be // one of three defined in the plugin's class if (!array_key_exists($data[local_userenrols_plugin::FORMID_USER_ID_FIELD], local_userenrols_plugin::get_user_id_field_options())) { $result[local_userenrols_plugin::FORMID_USER_ID_FIELD] = get_string('invaliduserfield', 'error', $data[local_userenrols_plugin::FORMID_USER_ID_FIELD]); } // File is not in the $files var, rather the itemid is in // $data, but we can get to it through file api. At this // stage, the file should be in the user's draft area $area_files = get_file_storage()->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $data[local_userenrols_plugin::FORMID_FILES], false, false); $import_file = array_shift($area_files); if (null == $import_file) { $result[local_userenrols_plugin::FORMID_FILES] = get_string('VAL_NO_FILES', local_userenrols_plugin::PLUGIN_NAME); } return $result; }
/** * Return list of valid options for user record field matching * * @access public * @static * @return array */ public static function get_user_id_field_options() { if (self::$user_id_field_options == null) { self::$user_id_field_options = array('username' => get_string('username'), 'email' => get_string('email'), 'idnumber' => get_string('idnumber')); } return self::$user_id_field_options; }
// pass validation, either case display the form echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_IMPORT', local_userenrols_plugin::PLUGIN_NAME); // Display the form with a filepicker echo $OUTPUT->container_start(); $mform->display(); echo $OUTPUT->container_end(); echo $OUTPUT->footer(); } else { // POST request, submit button clicked and formdata // passed validation, first check session spoofing require_sesskey(); // Collect the input $user_id_field = $formdata->{local_userenrols_plugin::FORMID_USER_ID_FIELD}; $role_id = $data->metacourse ? 0 : intval($formdata->{local_userenrols_plugin::FORMID_ROLE_ID}); $group_assign = intval($formdata->{local_userenrols_plugin::FORMID_GROUP}); $group_id = intval($formdata->{local_userenrols_plugin::FORMID_GROUP_ID}); $group_create = intval($formdata->{local_userenrols_plugin::FORMID_GROUP_CREATE}); // Leave the file in the user's draft area since we // will not plan to keep it after processing $area_files = get_file_storage()->get_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}, null, false); $result = local_userenrols_plugin::import_file($COURSE, $manual_enrol_instance, $user_id_field, $role_id, (bool) $group_assign, $group_id, (bool) $group_create, array_shift($area_files)); // Clean up the file area get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}); echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_IMPORT', local_userenrols_plugin::PLUGIN_NAME); // Output the processing result echo $OUTPUT->box(nl2br($result)); echo $OUTPUT->continue_button($groups_url); echo $OUTPUT->footer(); }
get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', file_get_submitted_draft_itemid(local_userenrols_plugin::FORMID_FILES)); redirect($course_url); } elseif (!$mform->is_submitted() || null == ($formdata = $mform->get_data())) { // GET request, or POST request where data did not // pass validation, either case display the form echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_UNENROLL_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_UNENROLL', local_userenrols_plugin::PLUGIN_NAME); // Display the form with a filepicker echo $OUTPUT->container_start(); $mform->display(); echo $OUTPUT->container_end(); echo $OUTPUT->footer(); } else { // POST request, submit button clicked and formdata // passed validation, first check session spoofing require_sesskey(); // Collect the input $user_id_field = $formdata->{local_userenrols_plugin::FORMID_USER_ID_FIELD}; // Leave the file in the user's draft area since we // will not plan to keep it after processing $area_files = get_file_storage()->get_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}, null, false); $result = local_userenrols_plugin::unenroll_file($COURSE, $user_id_field, array_shift($area_files)); // Clean up the file area get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}); echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_UNENROLL_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_UNENROLL', local_userenrols_plugin::PLUGIN_NAME); // Output the processing result echo $OUTPUT->box(nl2br($result)); echo $OUTPUT->continue_button($groups_url); echo $OUTPUT->footer(); }
public function validation($data, $files) { global $USER; $result = array(); // User record field to match against, has to be // one of three defined in the plugin's class if (!array_key_exists($data[local_userenrols_plugin::FORMID_USER_ID_FIELD], local_userenrols_plugin::get_user_id_field_options())) { $result[local_userenrols_plugin::FORMID_USER_ID_FIELD] = get_string('invaliduserfield', 'error', $data[local_userenrols_plugin::FORMID_USER_ID_FIELD]); } // Into which role to put the imported users, has // to be one valid for the current user. $role_id = intval($data[local_userenrols_plugin::FORMID_ROLE_ID]); if ($role_id > 0 && !array_key_exists($role_id, get_assignable_roles($this->_customdata['data']->context, ROLENAME_BOTH))) { $result[local_userenrols_plugin::FORMID_ROLE_ID] = get_string('invalidroleid', 'error'); } // For Yes/No select 0 and 1 only, anything else not valid $group_assign = intval($data[local_userenrols_plugin::FORMID_GROUP]); if ($group_assign < 0 or $group_assign > 1) { $result[local_userenrols_plugin::FORMID_GROUP] = get_string('VAL_INVALID_SELECTION', local_userenrols_plugin::PLUGIN_NAME); } $group_id = $group_assign ? intval($data[local_userenrols_plugin::FORMID_GROUP_ID]) : 0; if ($group_id > 0 && !array_key_exists($group_id, groups_get_all_groups($this->_customdata['data']->course->id))) { $group_id = 0; $result[local_userenrols_plugin::FORMID_GROUP_ID] = get_string('VAL_INVALID_SELECTION', local_userenrols_plugin::PLUGIN_NAME); } // For Yes/No select 0 and 1 only, anything else not valid $group_create = $group_assign && $group_id == 0 ? intval($data[local_userenrols_plugin::FORMID_GROUP_CREATE]) : 0; if ($group_create < 0 or $group_create > 1) { $result[local_userenrols_plugin::FORMID_GROUPING] = get_string('VAL_INVALID_SELECTION', local_userenrols_plugin::PLUGIN_NAME); } // File is not in the $files var, rather the itemid is in // $data, but we can get to it through file api. At this // stage, the file should be in the user's draft area $area_files = get_file_storage()->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $data[local_userenrols_plugin::FORMID_FILES], false, false); $import_file = array_shift($area_files); if (null == $import_file) { $result[local_userenrols_plugin::FORMID_FILES] = get_string('VAL_NO_FILES', local_userenrols_plugin::PLUGIN_NAME); } return $result; }
$data->context = $PAGE->context; $data->meta_enrols = $meta_enrols; $data->groups = groups_get_all_groups($course_id); $data->group_prefs = local_userenrols_plugin::get_group_prefs($COURSE->id); $formdata = null; $mform = new local_userenrols_assign_form(local_userenrols_plugin::get_plugin_url('assign', $COURSE->id)->out(), array('data' => $data)); if ($mform->is_cancelled()) { // POST request, but cancel button clicked redirect($course_url); } elseif (!$mform->is_submitted() || null == ($formdata = $mform->get_data())) { // GET request, or POST request where data did not // pass validation, either case display the form echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_ASSIGN_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_ASSIGN', local_userenrols_plugin::PLUGIN_NAME); $mform->display(); echo $OUTPUT->footer(); } else { // POST request, submit button clicked and formdata // passed validation, first check session spoofing require_sesskey(); // Collect the input $metagroup = $formdata->{local_userenrols_plugin::FORMID_METAGROUP}; $remove_current = intval($formdata->{local_userenrols_plugin::FORMID_REMOVE_CURRENT}); local_userenrols_plugin::metagroup_assign($COURSE, $metagroup, $remove_current); echo $OUTPUT->header(); echo $OUTPUT->heading_with_help(get_string('LBL_ASSIGN_TITLE', local_userenrols_plugin::PLUGIN_NAME), 'HELP_PAGE_ASSIGN', local_userenrols_plugin::PLUGIN_NAME); // Output the processing result echo $OUTPUT->notification(get_string('INF_IMPORT_SUCCESS', local_userenrols_plugin::PLUGIN_NAME), 'notifysuccess'); echo $OUTPUT->continue_button($groups_url); echo $OUTPUT->footer(); }