コード例 #1
0
 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;
 }
コード例 #2
0
// Want this for subsequent print_error() calls
$course_url = new moodle_url("{$CFG->wwwroot}/course/view.php", array('id' => $COURSE->id));
$groups_url = new moodle_url("{$CFG->wwwroot}/group/index.php", array('id' => $COURSE->id));
$page_head_title = get_string('LBL_IMPORT_TITLE', local_userenrols_plugin::PLUGIN_NAME) . ' : ' . $COURSE->shortname;
$PAGE->set_title($page_head_title);
$PAGE->set_heading($page_head_title);
$PAGE->set_pagelayout('incourse');
$PAGE->set_url(local_userenrols_plugin::get_plugin_url('import', $COURSE->id));
$PAGE->set_cacheable(false);
// Fix up the form. Have not determined yet whether this is a
// GET or POST, but the form will be used in either case.
// Fix up our customdata object to pass to the form constructor
$data = new stdClass();
$data->course = $COURSE;
$data->context = $PAGE->context;
$data->user_id_field_options = local_userenrols_plugin::get_user_id_field_options();
$data->metacourse = false;
$data->default_role_id = 0;
// Iterate the list of active enrol plugins looking for
// the manual course plugin, deal breaker if not found
$manual_enrol_instance = null;
$enrols_enabled = enrol_get_instances($COURSE->id, true);
foreach ($enrols_enabled as $enrol) {
    if ($enrol->enrol == 'manual') {
        $manual_enrol_instance = $enrol;
        $data->default_role_id = $enrol->roleid;
        break;
    }
}
// Deal breaker
if (null == $manual_enrol_instance) {
コード例 #3
0
 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;
 }