Merge branch 'MDL-63583-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 15 Oct 2018 22:29:02 +0000 (00:29 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 15 Oct 2018 22:29:02 +0000 (00:29 +0200)
1  2 
course/externallib.php

diff --combined course/externallib.php
@@@ -58,8 -58,6 +58,8 @@@ class core_course_external extends exte
                                                  'The expected keys (value format) are:
                                                  excludemodules (bool) Do not return modules, return only the sections structure
                                                  excludecontents (bool) Do not return module contents (i.e: files inside a resource)
 +                                                includestealthmodules (bool) Return stealth modules for students in a special
 +                                                    section (with id -1)
                                                  sectionid (int) Return only this section
                                                  sectionnumber (int) Return only this section with number (order)
                                                  cmid (int) Return only this module information (among the whole sections structure)
                      switch ($name) {
                          case 'excludemodules':
                          case 'excludecontents':
 +                        case 'includestealthmodules':
                              $value = clean_param($option['value'], PARAM_BOOL);
                              $filters[$name] = $value;
                              break;
              $modinfo = get_fast_modinfo($course);
              $sections = $modinfo->get_section_info_all();
              $coursenumsections = course_get_format($course)->get_last_section_number();
 +            $stealthmodules = array();   // Array to keep all the modules available but not visible in a course section/topic.
  
              //for each sections (first displayed to last displayed)
              $modinfosections = $modinfo->get_sections();
              foreach ($sections as $key => $section) {
  
 -                // Show the section if the user is permitted to access it, OR if it's not available
 -                // but there is some available info text which explains the reason & should display.
 -                $showsection = $section->uservisible ||
 -                    ($section->visible && !$section->available &&
 -                    !empty($section->availableinfo));
 -
 -                if (!$showsection) {
 -                    continue;
 -                }
 -
                  // This becomes true when we are filtering and we found the value to filter with.
                  $sectionfound = false;
  
  
                  $sectioncontents = array();
  
 -                // For each module of the section (if it is visible).
 -                if ($section->uservisible and empty($filters['excludemodules']) and !empty($modinfosections[$section->section])) {
 +                // For each module of the section.
 +                if (empty($filters['excludemodules']) and !empty($modinfosections[$section->section])) {
                      foreach ($modinfosections[$section->section] as $cmid) {
                          $cm = $modinfo->cms[$cmid];
  
                              }
                          }
  
 -                        //assign result to $sectioncontents
 -                        $sectioncontents[] = $module;
 +                        // Assign result to $sectioncontents, there is an exception,
 +                        // stealth activities in non-visible sections for students go to a special section.
 +                        if (!empty($filters['includestealthmodules']) && !$section->uservisible && $cm->is_stealth()) {
 +                            $stealthmodules[] = $module;
 +                        } else {
 +                            $sectioncontents[] = $module;
 +                        }
  
                          // If we just did a filtering, break the loop.
                          if ($modfound) {
                  $sectionvalues['modules'] = $sectioncontents;
  
                  // assign result to $coursecontents
 -                $coursecontents[] = $sectionvalues;
 +                $coursecontents[$key] = $sectionvalues;
  
                  // Break the loop if we are filtering.
                  if ($sectionfound) {
                      break;
                  }
              }
 +
 +            // Now that we have iterated over all the sections and activities, check the visibility.
 +            // We didn't this before to be able to retrieve stealth activities.
 +            foreach ($coursecontents as $sectionnumber => $sectioncontents) {
 +                $section = $sections[$sectionnumber];
 +                // Show the section if the user is permitted to access it, OR if it's not available
 +                // but there is some available info text which explains the reason & should display.
 +                $showsection = $section->uservisible ||
 +                    ($section->visible && !$section->available &&
 +                    !empty($section->availableinfo));
 +
 +                if (!$showsection) {
 +                    unset($coursecontents[$sectionnumber]);
 +                    continue;
 +                }
 +
 +                // Remove modules information if the section is not visible for the user.
 +                if (!$section->uservisible) {
 +                    $coursecontents[$sectionnumber]['modules'] = array();
 +                }
 +            }
 +
 +            // Include stealth modules in special section (without any info).
 +            if (!empty($stealthmodules)) {
 +                $coursecontents[] = array(
 +                    'id' => -1,
 +                    'name' => '',
 +                    'summary' => '',
 +                    'summaryformat' => FORMAT_MOODLE,
 +                    'modules' => $stealthmodules
 +                );
 +            }
 +
          }
          return $coursecontents;
      }
          foreach ($course->get_course_contacts() as $contact) {
               $coursecontacts[] = array(
                  'id' => $contact['user']->id,
 -                'fullname' => $contact['username']
 -            );
 +                'fullname' => $contact['username'],
 +                'roles' => array_map(function($role){
 +                    return array('id' => $role->id, 'name' => $role->displayname);
 +                }, $contact['roles']),
 +                'role' => array('id' => $contact['role']->id, 'name' => $contact['role']->displayname),
 +                'rolename' => $contact['rolename']
 +             );
          }
  
          // Allowed enrolment methods (maybe we can self-enrol).
       */
      public static function get_courses_by_field($field = '', $value = '') {
          global $DB, $CFG;
+         require_once($CFG->dirroot . '/course/lib.php');
          require_once($CFG->libdir . '/filterlib.php');
  
          $params = self::validate_parameters(self::get_courses_by_field_parameters(),
          $sort = $params['sort'];
  
          switch($classification) {
 +            case COURSE_TIMELINE_ALL:
 +                break;
              case COURSE_TIMELINE_PAST:
                  break;
              case COURSE_TIMELINE_INPROGRESS: