MDL-30775 get_course_contents: remove warnings when the section is empty
authorJerome Mouneyrac <jerome@mouneyrac.com>
Tue, 30 Apr 2013 06:13:53 +0000 (14:13 +0800)
committerJerome Mouneyrac <jerome@mouneyrac.com>
Tue, 30 Apr 2013 06:13:53 +0000 (14:13 +0800)
course/externallib.php

index 88c0693..0eeb13e 100644 (file)
@@ -108,6 +108,7 @@ class core_course_external extends external_api {
             $sections = get_all_sections($course->id);
 
             //for each sections (first displayed to last displayed)
             $sections = get_all_sections($course->id);
 
             //for each sections (first displayed to last displayed)
+            $modinfosections = $modinfo->get_sections();
             foreach ($sections as $key => $section) {
 
                 $showsection = (has_capability('moodle/course:viewhiddensections', $context) or $section->visible or !$course->hiddensections);
             foreach ($sections as $key => $section) {
 
                 $showsection = (has_capability('moodle/course:viewhiddensections', $context) or $section->visible or !$course->hiddensections);
@@ -126,62 +127,64 @@ class core_course_external extends external_api {
                 $sectioncontents = array();
 
                 //for each module of the section
                 $sectioncontents = array();
 
                 //for each module of the section
-                foreach ($modinfo->sections[$section->section] as $cmid) { //matching /course/lib.php:print_section() logic
-                    $cm = $modinfo->cms[$cmid];
+                if (!empty($modinfosections[$section->section])) {
+                    foreach ($modinfosections[$section->section] as $cmid) {
+                        $cm = $modinfo->cms[$cmid];
 
 
-                    // stop here if the module is not visible to the user
-                    if (!$cm->uservisible) {
-                        continue;
-                    }
+                        // stop here if the module is not visible to the user
+                        if (!$cm->uservisible) {
+                            continue;
+                        }
 
 
-                    $module = array();
+                        $module = array();
 
 
-                    //common info (for people being able to see the module or availability dates)
-                    $module['id'] = $cm->id;
-                    $module['name'] = format_string($cm->name, true);
-                    $module['modname'] = $cm->modname;
-                    $module['modplural'] = $cm->modplural;
-                    $module['modicon'] = $cm->get_icon_url()->out(false);
-                    $module['indent'] = $cm->indent;
+                        //common info (for people being able to see the module or availability dates)
+                        $module['id'] = $cm->id;
+                        $module['name'] = format_string($cm->name, true);
+                        $module['modname'] = $cm->modname;
+                        $module['modplural'] = $cm->modplural;
+                        $module['modicon'] = $cm->get_icon_url()->out(false);
+                        $module['indent'] = $cm->indent;
 
 
-                    $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
+                        $modcontext = context_module::instance($cm->id);
 
 
-                    if (!empty($cm->showdescription)) {
-                        $module['description'] = $cm->get_content();
-                    }
+                        if (!empty($cm->showdescription)) {
+                            $module['description'] = $cm->get_content();
+                        }
 
 
-                    //url of the module
-                    $url = $cm->get_url();
-                    if ($url) { //labels don't have url
-                        $module['url'] = $cm->get_url()->out();
-                    }
+                        //url of the module
+                        $url = $cm->get_url();
+                        if ($url) { //labels don't have url
+                            $module['url'] = $cm->get_url()->out();
+                        }
 
 
-                    $canviewhidden = has_capability('moodle/course:viewhiddenactivities',
-                                        get_context_instance(CONTEXT_MODULE, $cm->id));
-                    //user that can view hidden module should know about the visibility
-                    $module['visible'] = $cm->visible;
+                        $canviewhidden = has_capability('moodle/course:viewhiddenactivities',
+                                            context_module::instance($cm->id));
+                        //user that can view hidden module should know about the visibility
+                        $module['visible'] = $cm->visible;
 
 
-                    //availability date (also send to user who can see hidden module when the showavailabilyt is ON)
-                    if ($canupdatecourse or ($CFG->enableavailability && $canviewhidden && $cm->showavailability)) {
-                        $module['availablefrom'] = $cm->availablefrom;
-                        $module['availableuntil'] = $cm->availableuntil;
-                    }
+                        //availability date (also send to user who can see hidden module when the showavailabilyt is ON)
+                        if ($canupdatecourse or ($CFG->enableavailability && $canviewhidden && $cm->showavailability)) {
+                            $module['availablefrom'] = $cm->availablefrom;
+                            $module['availableuntil'] = $cm->availableuntil;
+                        }
 
 
-                    $baseurl = 'webservice/pluginfile.php';
+                        $baseurl = 'webservice/pluginfile.php';
 
 
-                    //call $modulename_export_contents
-                    //(each module callback take care about checking the capabilities)
-                    require_once($CFG->dirroot . '/mod/' . $cm->modname . '/lib.php');
-                    $getcontentfunction = $cm->modname.'_export_contents';
-                    if (function_exists($getcontentfunction)) {
-                        if ($contents = $getcontentfunction($cm, $baseurl)) {
-                            $module['contents'] = $contents;
+                        //call $modulename_export_contents
+                        //(each module callback take care about checking the capabilities)
+                        require_once($CFG->dirroot . '/mod/' . $cm->modname . '/lib.php');
+                        $getcontentfunction = $cm->modname.'_export_contents';
+                        if (function_exists($getcontentfunction)) {
+                            if ($contents = $getcontentfunction($cm, $baseurl)) {
+                                $module['contents'] = $contents;
+                            }
                         }
                         }
-                    }
 
 
-                    //assign result to $sectioncontents
-                    $sectioncontents[] = $module;
+                        //assign result to $sectioncontents
+                        $sectioncontents[] = $module;
 
 
+                    }
                 }
                 $sectionvalues['modules'] = $sectioncontents;
 
                 }
                 $sectionvalues['modules'] = $sectioncontents;