MDL-70143 tool_uploaduser: fix wrong cache reference

This commit is contained in:
Marina Glancy 2020-11-06 18:07:24 +01:00
parent 2444c9d9be
commit c2aa5a29f1

View file

@ -1119,10 +1119,10 @@ class process {
$this->upt->track('enrolments', get_string('unknowncourse', 'error', s($shortname)), 'error'); $this->upt->track('enrolments', get_string('unknowncourse', 'error', s($shortname)), 'error');
continue; continue;
} }
$ccache[$shortname] = $course; $this->ccache[$shortname] = $course;
$ccache[$shortname]->groups = null; $this->ccache[$shortname]->groups = null;
} }
$courseid = $ccache[$shortname]->id; $courseid = $this->ccache[$shortname]->id;
$coursecontext = \context_course::instance($courseid); $coursecontext = \context_course::instance($courseid);
if (!isset($this->manualcache[$courseid])) { if (!isset($this->manualcache[$courseid])) {
$this->manualcache[$courseid] = false; $this->manualcache[$courseid] = false;
@ -1241,41 +1241,41 @@ class process {
continue; continue;
} }
// Build group cache. // Build group cache.
if (is_null($ccache[$shortname]->groups)) { if (is_null($this->ccache[$shortname]->groups)) {
$ccache[$shortname]->groups = array(); $this->ccache[$shortname]->groups = array();
if ($groups = groups_get_all_groups($courseid)) { if ($groups = groups_get_all_groups($courseid)) {
foreach ($groups as $gid => $group) { foreach ($groups as $gid => $group) {
$ccache[$shortname]->groups[$gid] = new \stdClass(); $this->ccache[$shortname]->groups[$gid] = new \stdClass();
$ccache[$shortname]->groups[$gid]->id = $gid; $this->ccache[$shortname]->groups[$gid]->id = $gid;
$ccache[$shortname]->groups[$gid]->name = $group->name; $this->ccache[$shortname]->groups[$gid]->name = $group->name;
if (!is_numeric($group->name)) { // Only non-numeric names are supported!!! if (!is_numeric($group->name)) { // Only non-numeric names are supported!!!
$ccache[$shortname]->groups[$group->name] = new \stdClass(); $this->ccache[$shortname]->groups[$group->name] = new \stdClass();
$ccache[$shortname]->groups[$group->name]->id = $gid; $this->ccache[$shortname]->groups[$group->name]->id = $gid;
$ccache[$shortname]->groups[$group->name]->name = $group->name; $this->ccache[$shortname]->groups[$group->name]->name = $group->name;
} }
} }
} }
} }
// Group exists? // Group exists?
$addgroup = $user->{'group'.$i}; $addgroup = $user->{'group'.$i};
if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) { if (!array_key_exists($addgroup, $this->ccache[$shortname]->groups)) {
// If group doesn't exist, create it. // If group doesn't exist, create it.
$newgroupdata = new \stdClass(); $newgroupdata = new \stdClass();
$newgroupdata->name = $addgroup; $newgroupdata->name = $addgroup;
$newgroupdata->courseid = $ccache[$shortname]->id; $newgroupdata->courseid = $this->ccache[$shortname]->id;
$newgroupdata->description = ''; $newgroupdata->description = '';
$gid = groups_create_group($newgroupdata); $gid = groups_create_group($newgroupdata);
if ($gid) { if ($gid) {
$ccache[$shortname]->groups[$addgroup] = new \stdClass(); $this->ccache[$shortname]->groups[$addgroup] = new \stdClass();
$ccache[$shortname]->groups[$addgroup]->id = $gid; $this->ccache[$shortname]->groups[$addgroup]->id = $gid;
$ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name; $this->ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
} else { } else {
$this->upt->track('enrolments', get_string('unknowngroup', 'error', s($addgroup)), 'error'); $this->upt->track('enrolments', get_string('unknowngroup', 'error', s($addgroup)), 'error');
continue; continue;
} }
} }
$gid = $ccache[$shortname]->groups[$addgroup]->id; $gid = $this->ccache[$shortname]->groups[$addgroup]->id;
$gname = $ccache[$shortname]->groups[$addgroup]->name; $gname = $this->ccache[$shortname]->groups[$addgroup]->name;
try { try {
if (groups_add_member($gid, $user->id)) { if (groups_add_member($gid, $user->id)) {