$usersuptodate = 0; //not printed yet anywhere $userserrors = 0; $deletes = 0; $deleteerrors = 0; $renames = 0; $renameerrors = 0; $usersskipped = 0; $weakpasswords = 0; // caches $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway! $cohorts = array(); $rolecache = uu_allowed_roles_cache(); // Course roles lookup cache. $sysrolecache = uu_allowed_sysroles_cache(); // System roles lookup cache. $manualcache = array(); // cache of used manual enrol plugins in each course $supportedauths = uu_supported_auths(); // officially supported plugins that are enabled // we use only manual enrol plugin here, if it is disabled no enrol is done if (enrol_is_enabled('manual')) { $manual = enrol_get_plugin('manual'); } else { $manual = NULL; } // clear bulk selection if ($bulk) { $SESSION->bulk_users = array(); }
/** * Enrol the user, add him to groups and assign him roles. * * @return bool false if an error occured */ protected function add_to_egr() { global $DB; foreach ($this->rawdata as $field => $value) { if (preg_match('/^sysrole\\d+$/', $field)) { $removing = false; if (!empty($value)) { $sysrolename = $value; // Removing sysrole. if ($sysrolename[0] == '-') { $removing = true; $sysrolename = substr($sysrolename, 1); } // System roles lookup. $sysrolecache = uu_allowed_sysroles_cache(); if (array_key_exists($sysrolename, $sysrolecache)) { $sysroleid = $sysrolecache[$sysrolename]->id; } else { $this->set_status('unknownrole', new lang_string('unknownrole', 'error', s($sysrolename))); continue; } $isassigned = user_has_role_assignment($this->finaldata->id, $sysroleid, SYSCONTEXTID); if ($removing) { if ($isassigned) { role_unassign($sysroleid, $this->finaldata->id, SYSCONTEXTID); } } else { if (!$isassigned) { role_assign($sysroleid, $this->finaldata->id, SYSCONTEXTID); } } } } else { if (preg_match('/^course\\d+$/', $field)) { // Course number. $i = substr($field, 6); $shortname = $value; $course = $DB->get_record('course', array('shortname' => $shortname)); $course->groups = NULL; if (!$course) { $this->set_status('unknowncourse', new lang_string('unknowncourse', 'error', s($shortname))); continue; } $courseid = $course->id; $coursecontext = context_course::instance($courseid); $roles = uu_allowed_roles_cache(); // TODO: manualcache if ($instances = enrol_get_instances($courseid, false)) { foreach ($instances as $instance) { if ($instance->enrol === 'manual') { $coursecache = $instance; break; } } } // Checking if manual enrol is enabled. If it's not, no // enrolment is done. if (enrol_is_enabled('manual')) { $manual = enrol_get_plugin('manual'); } else { $manual = NULL; } if ($courseid == SITEID) { if (!empty($this->rawdata['role' . $i])) { $rolename = $this->rawdata['role' . $i]; if (array_key_exists($rolename, $roles)) { $roleid = $roles[$rolename]->id; } else { $this->set_status('unknownrole', new lang_string('unknownrole', 'error', s($rolename))); continue; } role_assign($roleid, $this->finaldata->id, context_course::instance($courseid)); } } else { if ($manual) { $roleid = false; if (!empty($this->rawdata['role' . $i])) { $rolename = $this->rawdata['role' . $i]; if (array_key_exists($rolename, $roles)) { $roleid = $roles[$rolename]->id; } else { $this->set_status('unknownrole', new lang_string('unknownrole', 'error', s($rolename))); continue; } } else { if (!empty($this->rawdata['type' . $i])) { // If no role, find "old" enrolment type. $addtype = $this->rawdata['type' . $i]; if ($addtype < 1 or $addtype > 3) { $this->set_status('typeerror', new lang_string('typeerror', 'tool_uploadusercli')); continue; } else { $roleid = $this->rawdata['type' . $i]; } } else { // No role specified, use default course role. $roleid = $coursecache->roleid; } } if ($roleid) { // Find duration and/or enrol status. $timeend = 0; $status = NULL; if (isset($this->rawdata['enrolstatus' . $i])) { $enrolstatus = $this->rawdata['enrolstatus' . $i]; if ($enrolstatus == '') { // Do nothing } else { if ($enrolstatus === (string) ENROL_USER_ACTIVE) { $status = ENROL_USER_ACTIVE; } else { if ($enrolstatus === (string) ENROL_USER_SUSPENDED) { $status = ENROL_USER_SUSPENDED; } else { $this->set_status('unknownenrolstatus', new lang_string('unknownenrolstatus', 'tool_uploadusercli')); } } } } if ($this->do === self::DO_UPDATE) { $now = $this->finaldata->timemodified; } else { $now = $this->finaldata->timecreated; } if (!empty($this->rawdata['enrolperiod' . $i])) { // Duration, in seconds. $duration = (int) $this->rawdata['enrolperiod' . $i] * 60 * 60 * 24; if ($duration > 0) { $timeend = $now + $duration; } } else { if ($coursecache->enrolperiod > 0) { $timeend = $now + $coursecache->enrolperiod; } } try { $manual->enrol_user($coursecache, $this->finaldata->id, $roleid, $now, $timeend, $status); } catch (Exception $e) { $this->error('usernotenrollederror', new lang_string('usernotenrollederror', 'tool_uploadusercli')); return false; } } } } // Add to group. if (isset($this->rawdata['group' . $i])) { $groupname = isset($this->rawdata['group' . $i]); } if (!empty($groupname)) { // Enrol into course before adding to group. if (!is_enrolled($coursecontext, $this->finaldata->id)) { $this->error('usernotenrollederror', new lang_string('usernotenrollederror', '', $groupname, 'error')); return false; } $course->groups = array(); $groups = groups_get_all_groups($courseid); if ($groups) { foreach ($groups as $gid => $group) { $course->groups[$gid] = new stdClass(); $course->groups[$gid]->id = $gid; $course->groups[$gid]->name = $group->name; if (!is_numeric($group->name)) { $course->groups[$group->name] = new stdClass(); $course->groups[$group->name]->id = $gid; $course->groups[$group->name]->name = $group->name; } } } // Does the group exist? if (!array_key_exists($groupname, $course->groups)) { // Create it. $newgroupdata = new stdClass(); $newgroupdata->name = $groupname; $newgroupdata->courseid = $course->id; $newgroupdata->description = ''; $gid = groups_create_group($newgroupdata); if ($gid) { $course->groups[$groupname] = new stdClass(); $course->groups[$groupname]->id = $gid; $course->groups[$groupname]->name = $newgroupname->name; } else { $this->error('unknowngroup', new lang_string('unknowngroup', 'error', s($groupname))); return false; } } $gid = $course->groups[$groupname]->id; $gname = $course->groups[$groupname]->name; try { groups_add_member($gid, $this->finaldata->id); } catch (moodle_exception $e) { $this - error('addedtogroupnot', new lang_string('addedtogroupnot', '', s($gname), 'error')); return false; } } } } } return true; }