gjbarnard / moodle-format_topcoll

Collapsed Topics course format for Moodle.
http://docs.moodle.org/en/Collapsed_Topics_course_format
GNU General Public License v3.0
35 stars 60 forks source link

Undefined property: stdClass::$groupmember in moodle/course/format/topcoll/classes/activity.php on line 650 #93

Closed frederikmillingpytlick closed 3 years ago

frederikmillingpytlick commented 3 years ago

PHP message:

PHP message: PHP Notice: Undefined property: stdClass::$groupmember in /var/www/example.com/moodle/course/format/topcoll/classes/activity.php on line 650 PHP message: PHP Warning: Invalid argument supplied for foreach() in /var/www/example.com/moodle/course/format/topcoll/classes/activity.php on line 650

Line: https://github.com/gjb2048/moodle-format_topcoll/blob/18c95d48d65e563f775d2e98e12fe6c0625a6dfa/classes/activity.php#L650

You should be able to fix the issue by using: groups_get_user_groups($course_id, $user_id); //It defaults to use $USER's id if nothing is specified

gjb2048 commented 3 years ago

@frederikmillingpytlick Thanks for the report, I'll look into it.

gjb2048 commented 3 years ago

Ok, for me 'groupmember' is an empty array. What version of Moodle are you using please?

frederikmillingpytlick commented 3 years ago

Moodle v3.9.3 (2020061503)

gjb2048 commented 3 years ago

Thanks. Ok, have you disabled the calendar / not showing the calendar / upcoming events on that course page?

frederikmillingpytlick commented 3 years ago

The calender block isn't added to the course page, neither is the upcoming events block

gjb2048 commented 3 years ago

Thanks. Ok 'groupmember' is populated by 'get_complete_user_data()' which has 'This is a special hack to speedup calendar display.' and the SQL it uses is different to 'groups_get_user_groups()' need to think.

gjb2048 commented 3 years ago

Ok, given that 'get_complete_user_data()' has:

    $sql = "SELECT g.id, g.courseid
              FROM {groups} g, {groups_members} gm
             WHERE gm.groupid=g.id AND gm.userid=?";

    // This is a special hack to speedup calendar display.
    $user->groupmember = array();
    if (!isguestuser($user)) {
        if ($groups = $DB->get_records_sql($sql, array($user->id))) {
            foreach ($groups as $group) {
                if (!array_key_exists($group->courseid, $user->groupmember)) {
                    $user->groupmember[$group->courseid] = array();
                }
                $user->groupmember[$group->courseid][$group->id] = $group->id;
            }
        }
    }

then I think this:

            foreach ($USER->groupmember as $grouparray) {
                foreach ($grouparray as $group) {
                    $usergroups[] = $group;
                }
            }

            $theresults = array();
            foreach ($results as $r) {
                if (!empty($r->userid)) { // User id of 0 means that there should be a groupid.
                    if ($r->userid == $USER->id) { // This record is for us.
                        $theresults[$r->assignment] = $r;
                    }
                } else if (!empty($r->groupid)) {
                    if (in_array($r->groupid, $usergroups)) { // This record is in one of our groups.
                        $theresults[$r->assignment] = $r;
                    }
                }
            }

is wrong as the activity.php code is looking at $usergroups as a single array and not the array of arrays with groups that it is.

gjb2048 commented 3 years ago

@frederikmillingpytlick Please test https://github.com/gjb2048/moodle-format_topcoll/commit/21f3dc0144eb38075c758f26cc33014e990f049c - easiest way is to simply take a copy of the entire activity.php as a drop in replacement.

frederikmillingpytlick commented 3 years ago

@gjb2048 Tested OK, thanks ;)