Ejemplo n.º 1
0
 /**
  * Returns arrays of all cm ids and all section ids
  * used in the subpage
  * @param mod_subpage $subpage
  * @return array activities,sections
  */
 public function get_includes($subpage)
 {
     $cm = $subpage->get_course_module();
     $cmids = array($cm->id);
     $sids = array();
     $modinfo = get_fast_modinfo($subpage->get_course(), -1);
     $sections = $modinfo->get_sections();
     $spsections = $subpage->get_sections();
     foreach ($spsections as $sid => $info) {
         $sids[] = $sid;
         if (!empty($sections[$info->section])) {
             foreach ($sections[$info->section] as $cmid) {
                 $cmids[] = $cmid;
                 $cminfo = $modinfo->get_cm($cmid);
                 if (strtolower($cminfo->get_module_type_name()) == 'subpage') {
                     // Support subpage in subpage.
                     $asubpage = \mod_subpage::get_from_cmid($cmid);
                     list($acmids, $asids) = $this->get_includes($asubpage);
                     $cmids = array_merge($cmids, $acmids);
                     $sids = array_merge($sids, $asids);
                 }
             }
         }
     }
     return array($cmids, $sids);
 }
Ejemplo n.º 2
0
 /**
  * Return an array of modules that can be moved in this situation
  *
  * The Array is keyed first with sections (subpage or main course)
  * and then the modules within each section by cmid.
  *
  * @param mod_subpage $subpage Current subpage
  * @param array $allsubpages Array of other subpage objects
  * @param array $coursesections Array of course sections
  * @param course_modinfo $modinfo Modinfo object
  * @param string $move Whether to move 'to' or 'from' the current subpage
  * @return array An array of items organised by section
  */
 public static function moveable_modules(mod_subpage $subpage, array $allsubpages, $coursesections, course_modinfo $modinfo, $move)
 {
     $modinfo = get_fast_modinfo($subpage->get_course()->id);
     $allmods = $modinfo->get_cms();
     $modnames = get_module_types_names();
     $modnamesplural = get_module_types_names(true);
     $mods = array();
     if ($move === 'to') {
         $parentcmids = array();
         // Get the subpage cm that owns each section.
         $subpagesectioncm = array();
         foreach ($modinfo->get_instances_of('subpage') as $subpageid => $cm) {
             // Get sectionsids array stored in the customdata.
             $cmdata = $cm->customdata;
             if ($cmdata) {
                 foreach ($cmdata->sectionids as $sectionid) {
                     $subpagesectioncm[$sectionid] = $cm;
                 }
             }
         }
         // Loop through ancestor subpages.
         $cm = $modinfo->get_cm($subpage->get_course_module()->id);
         while (true) {
             if (array_key_exists($cm->section, $subpagesectioncm)) {
                 $cm = $subpagesectioncm[$cm->section];
                 // In case of a subpage within itself, prevent endless loop.
                 if (array_key_exists($cm->id, $parentcmids)) {
                     break;
                 }
                 $parentcmids[$cm->id] = true;
             } else {
                 break;
             }
         }
     }
     $subsections = array();
     if (!empty($allsubpages) && $move === 'to') {
         foreach ($allsubpages as $sub) {
             $subsections += $sub->get_sections();
         }
         $sections = $coursesections;
     } else {
         $subsections = $subpage->get_sections();
         $sections = $subsections;
     }
     if ($sections) {
         foreach ($sections as $section) {
             if (!empty($section->sequence)) {
                 if ($move === 'to' && array_key_exists($section->id, $subsections)) {
                     continue;
                 }
                 $sectionalt = isset($section->pageorder) ? $section->pageorder : $section->section;
                 if ($move === 'to') {
                     // Include the required course/format library.
                     global $CFG;
                     require_once "{$CFG->dirroot}/course/format/" . $subpage->get_course()->format . "/lib.php";
                     $callbackfunction = 'callback_' . $subpage->get_course()->format . '_get_section_name';
                     if (function_exists($callbackfunction)) {
                         $name = $callbackfunction($subpage->get_course(), $section);
                     } else {
                         $name = $section->name ? $section->name : get_string('section') . ' ' . $sectionalt;
                     }
                 } else {
                     $name = $section->name ? $section->name : get_string('section') . ' ' . $sectionalt;
                 }
                 $sectionmods = explode(',', $section->sequence);
                 foreach ($sectionmods as $modnumber) {
                     if (empty($allmods[$modnumber]) || $modnumber === $subpage->get_course_module()->id) {
                         continue;
                     }
                     if ($move === 'to') {
                         // Prevent moving a parent subpage to its child.
                         if (!empty($parentcmids[$modnumber])) {
                             continue;
                         }
                     }
                     $instancename = format_string($modinfo->cms[$modnumber]->name, true, $subpage->get_course()->id);
                     $icon = $modinfo->get_cm($modnumber)->get_icon_url();
                     $mod = $allmods[$modnumber];
                     $mods[$section->section]['section'] = $name;
                     $mods[$section->section]['pageorder'] = $sectionalt;
                     $mods[$section->section]['mods'][$modnumber] = "<span><img src='{$icon}' /> " . $instancename . "</span>";
                 }
             }
         }
     }
     return $mods;
 }