From 56c0878b4500f4d062c5b8e227814e2fe9ebd71c Mon Sep 17 00:00:00 2001
From: Sam Chaffee <sam@moodlerooms.com>
Date: Thu, 29 Nov 2012 09:27:47 +0000
Subject: [PATCH] MDL-36881 Handle poorly behaved modname_get_types functions
 better

The modname_get_types function is not always well implemented by
third-party plugins and some return poor data. This in turn leads to
incorrect module definitions, and can lead to problems in both the
'Add an activity...' dropdowns and the Activity chooser.

This will also prevent display of plugins which legitimately can have
subtypes but where no subtypes were found. Since such plugins cannot be
used in this fashion in any case, this is also beneficial.

Signed-off-by: Andrew Robert Nicols <andrew.nicols@lancaster.ac.uk>
---
 course/lib.php | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/course/lib.php b/course/lib.php
index 0673aded462..804d33cd78e 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -1144,7 +1144,8 @@ function get_module_metadata($course, $modnames, $sectionreturn = null) {
         // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
         $gettypesfunc =  $modname.'_get_types';
         if (function_exists($gettypesfunc)) {
-            if ($types = $gettypesfunc()) {
+            $types = $gettypesfunc();
+            if (is_array($types) && count($types) > 0) {
                 $group = new stdClass();
                 $group->name = $modname;
                 $group->icon = $OUTPUT->pix_icon('icon', '', $modname, array('class' => 'icon'));
@@ -1193,7 +1194,11 @@ function get_module_metadata($course, $modnames, $sectionreturn = null) {
             $module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
             $modlist[$course->id][$modname] = $module;
         }
-        $return[$modname] = $modlist[$course->id][$modname];
+        if (isset($modlist[$course->id][$modname])) {
+            $return[$modname] = $modlist[$course->id][$modname];
+        } else {
+            debugging("Invalid module metadata configuration for {$modname}");
+        }
     }
 
     return $return;