### Eclipse Workspace Patch 1.0
#P moodle
Index: mod/label/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/label/lib.php,v
retrieving revision 1.11
diff -u -r1.11 lib.php
--- mod/label/lib.php	11 Nov 2006 17:23:33 -0000	1.11
+++ mod/label/lib.php	1 Jan 2007 20:46:08 -0000
@@ -88,4 +88,15 @@
     return array();
 }
 
+function label_get_types() {
+    $types = array();
+
+    $type = new object();
+    $type->modclass = MOD_CLASS_RESOURCE;
+    $type->type = "label";
+    $type->typestr = get_string('resourcetypelabel', 'resource');
+    $types[] = $type;
+
+    return $types;
+}
 ?>
Index: course/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/course/lib.php,v
retrieving revision 1.461
diff -u -r1.461 lib.php
--- course/lib.php	21 Dec 2006 19:46:30 -0000	1.461
+++ course/lib.php	1 Jan 2007 20:45:59 -0000
@@ -22,6 +22,9 @@
 define('EXCELROWS', 65535);
 define('FIRSTUSEDEXCELROW', 3);
 
+define('MOD_CLASS_ACTIVITY', 0);
+define('MOD_CLASS_RESOURCE', 1);
+
 
 function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin",
                                     $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") {
@@ -1135,9 +1138,6 @@
             asort($modnamesused);
         }
     }
-
-    unset($modnames['resource']);
-    unset($modnames['label']);
 }
 
 
@@ -1342,33 +1342,49 @@
     }
 }
 
-
+/**
+ * Prints the menus to add activities and resources.
+ */
 function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
-// Prints the menus to add activities and resources
+    global $CFG;
 
-    global $CFG, $USER;
-    static $straddactivity, $stractivities, $straddresource, $resources;
+    static $resources = false;
+    static $activities = false;
 
-    if (!isset($straddactivity)) {
-        $straddactivity = get_string('addactivity');
-        $straddresource = get_string('addresource');
+    if ($resources === false) { 
+        $resources = array();
+        $activities = array();
 
-        /// Standard resource types
-        require_once("$CFG->dirroot/mod/resource/lib.php");
-        $resourceraw = resource_get_resource_types();
+        foreach($modnames as $modname=>$modnamestr) {
+            if (!course_allowed_module($course, $modname)) {
+                continue;
+            }
 
-        foreach ($resourceraw as $type => $name) {
-            $resources["resource&amp;type=$type"] = $name;
-        }
-        if (course_allowed_module($course,'label')) {
-            $resources['label'] = get_string('resourcetypelabel', 'resource');
+            require_once("$CFG->dirroot/mod/$modname/lib.php");
+            $gettypesfunc =  $modname.'_get_types';
+            if (function_exists($gettypesfunc)) {
+                $types = $gettypesfunc();
+                foreach($types as $type) {
+                    if ($type->modclass == MOD_CLASS_RESOURCE) {
+                        $resources[$type->type] = $type->typestr;
+                    } else {
+                        $activities[$type->type] = $type->typestr;
+                    }
+                }
+            } else {
+                // all mods without type are considered activity
+                $activities[$modname] = $modnamestr;
+            }
         }
     }
 
+    $straddactivity = get_string('addactivity');
+    $straddresource = get_string('addresource');
+
     $output  = '<div style="text-align: right">';
-    if (course_allowed_module($course,'resource')) {
-        $resourceallowed = true;
-        $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
+
+    if (!empty($resources)) {
+        $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
                               $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
     }
 
@@ -1376,21 +1392,12 @@
         $output .= '<div>';
     }
 
-    // we need to loop through the forms and check to see if we can add them.
-    foreach ($modnames as $key=>$value) {
-        if (!course_allowed_module($course,$key))
-            unset($modnames[$key]);
+    if (!empty($activities)) {
+        $output .= ' ';
+        $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
+                    $activities, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
     }
 
-    // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble.
-    if (course_allowed_module($course,'label') && empty($resourceallowed)) {
-        $modnames['label'] = get_string('modulename', 'label');
-    }
-
-    $output .= ' ';
-    $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
-                $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
-
     if ($vertical) {
         $output .= '</div>';
     }
Index: lib/weblib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/weblib.php,v
retrieving revision 1.722
diff -u -r1.722 weblib.php
--- lib/weblib.php	1 Jan 2007 13:26:23 -0000	1.722
+++ lib/weblib.php	1 Jan 2007 20:46:05 -0000
@@ -964,9 +964,20 @@
     }
 
     $inoptgroup = false;
+
     foreach ($options as $value => $label) {
 
-        if (substr($label,0,2) == '--') { /// we are starting a new optgroup
+        if ($label == '--') { /// we are ending previous optgroup
+            /// Check to see if we already have a valid open optgroup
+            /// XHTML demands that there be at least 1 option within an optgroup
+            if ($inoptgroup and (count($optgr) > 1) ) {
+                $output .= implode('', $optgr);
+                $output .= '   </optgroup>';
+            }
+            $optgr = array();
+            $inoptgroup = false;
+            continue;
+        } else if (substr($label,0,2) == '--') { /// we are starting a new optgroup
 
             /// Check to see if we already have a valid open optgroup
             /// XHTML demands that there be at least 1 option within an optgroup
Index: mod/resource/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/resource/lib.php,v
retrieving revision 1.57
diff -u -r1.57 lib.php
--- mod/resource/lib.php	29 Dec 2006 20:16:50 -0000	1.57
+++ mod/resource/lib.php	1 Jan 2007 20:46:08 -0000
@@ -629,16 +629,16 @@
     return false;
 }
 
-function resource_get_resource_types() {
-/// Returns a menu of current resource types, in standard order
-    global $resource_standard_order, $CFG;
+function resource_get_types() {
+    $types = array();
 
-    $resources = array();
-
-    /// Standard resource types
     $standardresources = array('text','html','file','directory');
     foreach ($standardresources as $resourcetype) {
-        $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
+        $type = new object();
+        $type->modclass = MOD_CLASS_RESOURCE;
+        $type->type = "resource&amp;type=$resourcetype";
+        $type->typestr = get_string("resourcetype$resourcetype", 'resource');
+        $types[] = $type;
     }
 
     /// Drop-in extra resource types
@@ -647,11 +647,16 @@
         if (!empty($CFG->{'resource_hide_'.$resourcetype})) {  // Not wanted
             continue;
         }
-        if (!in_array($resourcetype, $resources)) {
-            $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
+        if (!in_array($resourcetype, $standardresources)) {
+            $type = new object();
+            $type->modclass = MOD_CLASS_RESOURCE;
+            $type->type = "resource&amp;type=$resourcetype";
+            $type->typestr = get_string("resourcetype$resourcetype", 'resource');
+            $types[] = $type;
         }
     }
-    return $resources;
+
+    return $types;
 }
 
 function resource_get_view_actions() {
Index: mod/assignment/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/lib.php,v
retrieving revision 1.201
diff -u -r1.201 lib.php
--- mod/assignment/lib.php	11 Dec 2006 05:31:00 -0000	1.201
+++ mod/assignment/lib.php	1 Jan 2007 20:46:08 -0000
@@ -2476,4 +2476,46 @@
     return array('upload');
 }
 
+function assignment_get_types() {
+    $types = array();
+
+    $type = new object();
+    $type->modclass = MOD_CLASS_ACTIVITY;
+    $type->type = "assignment_group_start";
+    $type->typestr = '--'.get_string('modulenameplural', 'assignment');
+    $types[] = $type;
+
+    $standardassignments = array('upload','online','uploadsingle','offline');
+    foreach ($standardassignments as $assignmenttype) {
+        $type = new object();
+        $type->modclass = MOD_CLASS_ACTIVITY;
+        $type->type = "assignment&amp;type=$assignmenttype";
+        $type->typestr = get_string("type$assignmenttype", 'assignment');
+        $types[] = $type;
+    }
+
+    /// Drop-in extra assignment types
+    $assignmenttypes = get_list_of_plugins('mod/assignment/type');
+    foreach ($assignmenttypes as $assignmenttype) {
+        if (!empty($CFG->{'assignment_hide_'.$assignmenttype})) {  // Not wanted
+            continue;
+        }
+        if (!in_array($assignmenttype, $standardassignments)) {
+            $type = new object();
+            $type->modclass = MOD_CLASS_ACTIVITY;
+            $type->type = "assignment&amp;type=$assignmenttype";
+            $type->typestr = get_string("type$assignmenttype", 'assignment');
+            $types[] = $type;
+        }
+    }
+
+    $type = new object();
+    $type->modclass = MOD_CLASS_ACTIVITY;
+    $type->type = "assignment_group_end";
+    $type->typestr = '--';
+    $types[] = $type;
+
+    return $types;
+}
+
 ?>
