mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 09:26:35 +02:00
MDL-30616 Move module metadata retrieval to new function
This commit is contained in:
parent
c4a12afaf9
commit
59155a3fb3
1 changed files with 121 additions and 47 deletions
168
course/lib.php
168
course/lib.php
|
@ -1806,65 +1806,49 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$urlbase = "/course/mod.php?id=$course->id§ion=$section&sesskey=".sesskey().'&add=';
|
// Retrieve all modules with associated metadata
|
||||||
|
$modules = get_module_metadata($course, $modnames);
|
||||||
|
|
||||||
|
// We'll sort resources and activities into two lists
|
||||||
$resources = array();
|
$resources = array();
|
||||||
$activities = array();
|
$activities = array();
|
||||||
|
|
||||||
foreach($modnames as $modname=>$modnamestr) {
|
// We need to add the section section to the link for each module
|
||||||
if (!course_allowed_module($course, $modname)) {
|
$sectionlink = '§ion=' . $section;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$libfile = "$CFG->dirroot/mod/$modname/lib.php";
|
foreach ($modules as $module) {
|
||||||
if (!file_exists($libfile)) {
|
if (isset($module->types)) {
|
||||||
continue;
|
// This module has a subtype
|
||||||
}
|
|
||||||
include_once($libfile);
|
|
||||||
$gettypesfunc = $modname.'_get_types';
|
|
||||||
if (function_exists($gettypesfunc)) {
|
|
||||||
// NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
|
// NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
|
||||||
if ($types = $gettypesfunc()) {
|
$subtypes = array();
|
||||||
$menu = array();
|
foreach ($module->types as $subtype) {
|
||||||
$atype = null;
|
$subtypes[$subtype->link . $sectionlink] = $subtype->title;
|
||||||
$groupname = null;
|
}
|
||||||
foreach($types as $type) {
|
|
||||||
if ($type->typestr === '--') {
|
// Sort module subtypes into the list
|
||||||
continue;
|
if (!empty($module->title)) {
|
||||||
}
|
// This grouping has a name
|
||||||
if (strpos($type->typestr, '--') === 0) {
|
if ($module->archetype == MOD_CLASS_RESOURCE) {
|
||||||
$groupname = str_replace('--', '', $type->typestr);
|
$resources[] = array($module->title=>$subtypes);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$type->type = str_replace('&', '&', $type->type);
|
|
||||||
if ($type->modclass == MOD_CLASS_RESOURCE) {
|
|
||||||
$atype = MOD_CLASS_RESOURCE;
|
|
||||||
}
|
|
||||||
$menu[$urlbase.$type->type] = $type->typestr;
|
|
||||||
}
|
|
||||||
if (!is_null($groupname)) {
|
|
||||||
if ($atype == MOD_CLASS_RESOURCE) {
|
|
||||||
$resources[] = array($groupname=>$menu);
|
|
||||||
} else {
|
|
||||||
$activities[] = array($groupname=>$menu);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if ($atype == MOD_CLASS_RESOURCE) {
|
$activities[] = array($module->title=>$subtypes);
|
||||||
$resources = array_merge($resources, $menu);
|
}
|
||||||
} else {
|
} else {
|
||||||
$activities = array_merge($activities, $menu);
|
// This grouping does not have a name
|
||||||
}
|
if ($module->archetype == MOD_CLASS_RESOURCE) {
|
||||||
|
$resources = array_merge($resources, $subtypes);
|
||||||
|
} else {
|
||||||
|
$activities = array_merge($activities, $subtypes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
|
// This module has no subtypes
|
||||||
if ($archetype == MOD_ARCHETYPE_RESOURCE) {
|
if ($module->archetype == MOD_ARCHETYPE_RESOURCE) {
|
||||||
$resources[$urlbase.$modname] = $modnamestr;
|
$resources[$module->link . $sectionlink] = $module->title;
|
||||||
} else if ($archetype === MOD_ARCHETYPE_SYSTEM) {
|
} else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) {
|
||||||
// System modules cannot be added by user, do not add to dropdown
|
// System modules cannot be added by user, do not add to dropdown
|
||||||
} else {
|
} else {
|
||||||
// all other archetypes are considered activity
|
$activities[$module->link . $sectionlink] = $module->title;
|
||||||
$activities[$urlbase.$modname] = $modnamestr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1903,6 +1887,96 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all metadata for the requested modules
|
||||||
|
*
|
||||||
|
* @param object $course The Course
|
||||||
|
* @param array $modnames An array containing the list of modules and their
|
||||||
|
* names
|
||||||
|
* @return array A list of stdClass objects containing metadata about each
|
||||||
|
* module
|
||||||
|
*/
|
||||||
|
function get_module_metadata($course, $modnames) {
|
||||||
|
global $CFG, $OUTPUT;
|
||||||
|
|
||||||
|
// get_module_metadata will be called once per section on the page and courses may show
|
||||||
|
// different modules to one another
|
||||||
|
static $modlist = array();
|
||||||
|
if (!isset($modlist[$course->id])) {
|
||||||
|
$modlist[$course->id] = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$return = array();
|
||||||
|
$urlbase = "/course/mod.php?id=$course->id&sesskey=".sesskey().'&add=';
|
||||||
|
foreach($modnames as $modname => $modnamestr) {
|
||||||
|
if (!course_allowed_module($course, $modname)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isset($modlist[$modname])) {
|
||||||
|
// This module is already cached
|
||||||
|
$return[$modname] = $modlist[$course->id][$modname];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include the module lib
|
||||||
|
$libfile = "$CFG->dirroot/mod/$modname/lib.php";
|
||||||
|
if (!file_exists($libfile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
include_once($libfile);
|
||||||
|
|
||||||
|
// NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
|
||||||
|
$gettypesfunc = $modname.'_get_types';
|
||||||
|
if (function_exists($gettypesfunc)) {
|
||||||
|
if ($types = $gettypesfunc()) {
|
||||||
|
$group = new stdClass();
|
||||||
|
$group->name = $modname;
|
||||||
|
$group->icon = $OUTPUT->pix_icon('icon', '', $modname, array('class' => 'icon'));
|
||||||
|
foreach($types as $type) {
|
||||||
|
if ($type->typestr === '--') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (strpos($type->typestr, '--') === 0) {
|
||||||
|
$group->title = str_replace('--', '', $type->typestr);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Set the Sub Type metadata
|
||||||
|
$subtype = new stdClass();
|
||||||
|
$subtype->title = $type->typestr;
|
||||||
|
$subtype->type = str_replace('&', '&', $type->type);
|
||||||
|
$subtype->name = preg_replace('/.*type=/', '', $subtype->type);
|
||||||
|
$subtype->archetype = $type->modclass;
|
||||||
|
|
||||||
|
// The group archetype should match the subtype archetypes and all subtypes
|
||||||
|
// should have the same archetype
|
||||||
|
$group->archetype = $subtype->archetype;
|
||||||
|
|
||||||
|
if (get_string_manager()->string_exists('help' . $subtype->name, $modname)) {
|
||||||
|
$subtype->help = get_string('help' . $subtype->name, $modname);
|
||||||
|
}
|
||||||
|
$subtype->link = $urlbase . $type->type;
|
||||||
|
$group->types[] = $subtype;
|
||||||
|
}
|
||||||
|
$modlist[$course->id][$modname] = $group;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$module = new stdClass();
|
||||||
|
$module->title = get_string('modulename', $modname);
|
||||||
|
$module->name = $modname;
|
||||||
|
$module->link = $urlbase . $modname;
|
||||||
|
$module->icon = $OUTPUT->pix_icon('icon', '', $module->name, array('class' => 'icon'));
|
||||||
|
if (get_string_manager()->string_exists('modulename_help', $modname)) {
|
||||||
|
$module->help = get_string('modulename_help', $modname);
|
||||||
|
}
|
||||||
|
$module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
|
||||||
|
$modlist[$course->id][$modname] = $module;
|
||||||
|
}
|
||||||
|
$return[$modname] = $modlist[$course->id][$modname];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the course category context for the category with id $categoryid, except
|
* Return the course category context for the category with id $categoryid, except
|
||||||
* that if $categoryid is 0, return the system context.
|
* that if $categoryid is 0, return the system context.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue