Undefined index: numsections in index.php on line 115

Undefined index: numsections in index.php on line 115

by Perry Way -
Number of replies: 8

I tested upgrading one of our Moodle sites from version 3.8.1 to 3.9.6 latest release and after all the upgrading was performed experienced a strange behavior on the 3.9.6 site.

On top of the main section (section 0) of the front page, there was an error outputted:

Undefined index: numsections in {website file path}/index.php on line 115.

The same error does not occur in Moodle 3.8.1 yet the code is consistent between the two sites. I have enclosed two screenshots, one that does not have the error is Moodle 3.8.1:


and the one with the error is Moodle 3.9.6 latest release: 


I put some debug output (var_dump()) to show the contents of the array being referenced and those outputs are showing on both screenshots. As you can see, the contents of the array are identical and lack a "numsections" array item.

I decided to get around this issue to modify the code in index.php and test for isset() before referencing that array member, which is proper. However, what befuddles me is why did the behavior change with 3.9.6 latest release when the code is identical?

Here is the original code:

} else if ($siteformatoptions['numsections'] > 0) {

Here is my modified code:

} else if (isset($siteformatoptions['numsections']) && ($siteformatoptions['numsections'] > 0)) {

By doing this I was able to get past this issue, however what befuddles me and which I have not yet identified is that in the 3.8.1 site that section of code is not hit. The conditions prefacing that bit of code did not also get hit when technically speaking therefore code execution should have fallen to the else if condition test and then therefore failed just like the 3.9.6 latest release did but doesn't. Code snippet:

// Print Section or custom info.
if (!empty($CFG->customfrontpageinclude)) {
    // Pre-fill some variables that custom front page might use.
    $modnames = get_module_types_names();
    $modnamesplural = get_module_types_names(true);
    $mods = $modinfo->get_cms();

    include($CFG->customfrontpageinclude);

} else if (isset($siteformatoptions['numsections']) && ($siteformatoptions['numsections'] > 0)) {
    echo $courserenderer->frontpage_section1();
}

I don't have the time presently to look into this further at the moment since today is a day for new term course preparations and I'm swamped with bad course builds and massive questions from instructors, but thought I would mention this bit in this forum in case anyone else has had a similar problem, and wants to solve the error from showing on their site, or in case someone knows what the underlying issue is and can provide some helpful feedback.

Average of ratings: -
In reply to Perry Way

Re: Undefined index: numsections in index.php on line 115

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

What theme is in use? And does this problem happen if you switch to the default Boost theme?

format_site->course_format_options() should include a 'numsections' item in the options (in course/format/lib.php):

        if ($courseformatoptions === false) {
            $courseformatoptions = array(
                'numsections' => array(
                    'default' => 1,
                    'type' => PARAM_INT,
                ),
            );
        }
        return $courseformatoptions;

Maybe some customisation in the theme is overriding this behaviour?

In reply to Leon Stringer

Re: Undefined index: numsections in index.php on line 115

by Perry Way -

Hi Leon,

The theme we are using is a custom one I made inheriting from Boost to allow customized front page content to be more easily managed by our web designer using .html templates.

I switched the theme back to Boost and the problem is still there as you can see.


I checked that code you mentioned and it does exist:

    /**
     * Definitions of the additional options that site uses
     *
     * @param bool $foreditform
     * @return array of options
     */
    public function course_format_options($foreditform = false) {
        static $courseformatoptions = false;
        if ($courseformatoptions === false) {
            $courseformatoptions = array(
                'numsections' => array(
                    'default' => 1,
                    'type' => PARAM_INT,
                ),
            );
        }
        return $courseformatoptions;
    }

This is an interesting predicament. Later today or Monday afternoon I can devote a lot more time to this issue and I'll dig a lot deeper, look for where numsections or $courseformatoptions are touched. The thing I find most interesting is that the array element does not exist on our live 3.8.1 site and it does not throw an exception even though that section of the code is executed. That could be an extra clue to resolving this issue. Meanwhile I'm leaving my isset() addition in place so school administrators don't see that and give them caution for upgrading as I've been attempting to get an upgrade approved for over a year.

In reply to Perry Way

Re: Undefined index: numsections in index.php on line 115

by Matt T -
Is it possible debugging mode is disabled on 3.8.1 site, but enabled on 3.9.6 site?
In reply to Matt T

Re: Undefined index: numsections in index.php on line 115

by Perry Way -

Hi Matt, yes debugging is on for the purpose of finding anything wrong while testing. But turned off on the live 3.8.1 site.

In reply to Perry Way

Re: Undefined index: numsections in index.php on line 115

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Matt's explanation for the additional "Undefined index" message would explain why you see this on the 3.9 site only.

As to why it happens, what do you get for:

SELECT format FROM mdl_course WHERE category = 0;

This should be 'site' but maybe you get 'topics'? If you do the presumably either the database has been manually changed or something has gone wrong in the database because as far as I can see this isn't a setting you can change within Moodle.

To explain: courses in Moodle can have a different format. The front page is a special kind of course normally with ID 1 in the database. It should only have format 'site' which has the option 'numsections' only. But if the front page is somehow using format 'topics' then it would have options 'hiddensections' and 'coursedisplay' instead, thus the error.

Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Undefined index: numsections in index.php on line 115

by Perry Way -
Hi Leon, thanks for your response. Yes, 'topics' returns from that query. Since you say that it's a setting that you can't change within Moodle, it makes me wonder how it got that way. Should I "fix" it by updating the value to to 'site' or could this be a sign of a deeper problem that I should chase down?

By the way I executed the same query without the where clause and every course in our system has 'topics' as their value if that helps with understanding.
In reply to Perry Way

Re: Undefined index: numsections in index.php on line 115

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

To the best of my knowledge the site course –  the mdl_course row with category = 0 – should always have format = 'site'. I'm not able to state that canonically but the fact index.php is assuming the 'numsections' option will be present backs that up.

All other courses may have different formats: topics format ('topics') is the default, there's also single activity format ('singleactivity'), social format ('social') and weekly format ('weeks'). And other formats can be added using plugins.

Could it be a sign of a deeper problem? I don't think so, there was a problem with this 10 years ago: MDL-36840. Is your site that old? Maybe the issue has been there for a long time.

I would just change this in the database (take a backup of the database first):

UPDATE mdl_course SET format = 'site' WHERE category = 0;
Average of ratings: Useful (2)
In reply to Leon Stringer

Re: Undefined index: numsections in index.php on line 115

by Perry Way -

Once I updated it to site the issue went away and then I decided to also do the same to our live 3.8.1 site because we will be upgrading that one this next weekend.  Thanks for the illumination on this Leon. No, our site is only like 8 years old. I have a vague memory from about 8 years ago of doing a mass update on that field to set everything to topic, since our instructors had each experimented with different formats for each of their courses until Administration defined a global perspective for all courses taught here at the college. So I did the mass update at that time and likely overwrote the setting then not knowing that course id 1 should remain as 'site'.