Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Forum posts' in My Courses Error #271

Open
djarran opened this issue Dec 12, 2023 · 3 comments
Open

'Forum posts' in My Courses Error #271

djarran opened this issue Dec 12, 2023 · 3 comments

Comments

@djarran
Copy link

djarran commented Dec 12, 2023

Description

Users who have recent forum posts are receiving an error in their "My courses" overview section with dmlreadexception. The full error message (below) is attempting to find the forum discussions table for the hsuforum plugin despite it not being installed:

dmlreadexception 
ERROR:  relation "mdl_hsuforum_discussions" does not exist
LINE 2:                               FROM mdl_hsuforum_discussions
                                           ^
SELECT id, groupid
                              FROM mdl_hsuforum_discussions
                             WHERE id IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
[array (
  ....
)]
Error code: dmlreadexception 
* line 494 of /lib/dml/moodle_database.php: dml_read_exception thrown
* line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end()
* line 341 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->read_slave_query_end()
* line 1027 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->query_end()
* line 1987 of /theme/snap/classes/local.php: call to pgsql_native_moodle_database->get_records_sql()
* line 2008 of /theme/snap/classes/local.php: call to theme_snap\local::get_groups_ids()
* line 2443 of /theme/snap/classes/local.php: call to theme_snap\local::recent_forum_activity_data()
* line 98 of /theme/snap/classes/webservice/ws_feed.php: call to theme_snap\local::get_feed()
* line ? of unknownfile: call to theme_snap\webservice\ws_feed::service()
* line 262 of /lib/externallib.php: call to call_user_func_array()
* line 81 of /lib/ajax/service.php: call to external_api::call_external_function()

Steps to Reproduce

  1. Setup Moodle 4.1 environment (git clone -b MOODLE_401_STABLE [email protected]:moodle/moodle.git forum-error)
  2. Create small test course at admin/tool/generator/maketestcourse.php. This should also create a forum.
  3. Install theme_snap, upgrade Moodle
  4. Click My Course button
  5. Observe error
    image

Proposed Solution

The error is occuring in the get_groups_ids function in classes/local.php as described in the error in the following conditional starting in line 1970

        if (!get_config('hsuforum')) {
            $groupsid['hsuforum'] = [];
        } else {
            // SQL for hsuforums.
            $sqlhsuforum = "SELECT id, groupid
                              FROM {hsuforum_discussions}
                             WHERE id $insql";

            $groupsid['hsuforum'] = $DB->get_records_sql($sqlhsuforum, $params);
        }
        return $groupsid;
  

get_config('hsuforum') returns an empty object (even though it should return false) if the plugin specified doesn't exist, and since an empty object is considered true, this conditional evaluates to false and the SQL is executed. Changing hsuforum to a random string shows the same behaviour.

Checking if what is returned is an empty object (or false, as that is what get_config() should return) as below solves this issue.

if (get_config('hsuforum') == new stdClass() || !get_config('hsuforum')) {

image

@djarran
Copy link
Author

djarran commented Dec 12, 2023

I have done more investigation and it seems that get_config() will only return false if two arguments are passed. If only one is passed, it will return an object. See the PHPDoc:

/**
 * Get configuration values from the global config table
 * or the config_plugins table.
 *
 * If called with one parameter, it will load all the config
 * variables for one plugin, and return them as an object.
 *
 * If called with 2 parameters it will return a string single
 * value or false if the value is not found.
 *
 * NOTE: this function is called from lib/db/upgrade.php
 *
 * @param string $plugin full component name
 * @param string $name default null
 * @return mixed hash-like object or single value, return false no config found
 * @throws dml_exception
 */
function get_config($plugin, $name = null) {
    ....
}

The second commit in Pull Request #272 adds this second argument, checking if entry version exists for hsuforum plugin.

@DaniCastel
Copy link
Contributor

Hi @djarran ,

Thank you for submitting this pull request! We appreciate your contribution.

We'll review the changes and provide feedback or merge them. If there are any changes required, I'll add review comments to guide you through them.

Thanks again for your contribution.

@alexmorrisnz
Copy link

Would be great to see this fixed, it's been an issue for years
https://github.com/open-lms-open-source/moodle-theme_snap/pull/161/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants