diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php
index 38c2db3d9d5..37731a4dfa1 100644
--- a/blocks/site_main_menu/block_site_main_menu.php
+++ b/blocks/site_main_menu/block_site_main_menu.php
@@ -63,7 +63,6 @@ class block_site_main_menu extends block_list {
$ismoving = ismoving($course->id);
$section = get_course_section(0, $course->id);
$modinfo = get_fast_modinfo($course);
- get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
$groupbuttons = $course->groupmode;
$groupbuttonslink = (!$course->groupmodeforce);
@@ -134,11 +133,7 @@ class block_site_main_menu extends block_list {
$this->content->icons[] = '';
}
- if (!empty($modnames)) {
- $this->content->footer = print_section_add_menus($course, 0, $modnames, true, true);
- } else {
- $this->content->footer = '';
- }
+ $this->content->footer = print_section_add_menus($course, 0, null, true, true);
return $this->content;
}
diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php
index f87ae7fb5e0..b98f44f9788 100644
--- a/blocks/social_activities/block_social_activities.php
+++ b/blocks/social_activities/block_social_activities.php
@@ -67,10 +67,6 @@ class block_social_activities extends block_list {
$modinfo = get_fast_modinfo($course);
$section = $modinfo->get_section_info(0);
- if (!empty($section)) {
- get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
- }
-
$groupbuttons = $course->groupmode;
$groupbuttonslink = (!$course->groupmodeforce);
@@ -141,11 +137,7 @@ class block_social_activities extends block_list {
$this->content->icons[] = '';
}
- if ($modnames) {
- $this->content->footer = print_section_add_menus($course, 0, $modnames, true, true);
- } else {
- $this->content->footer = '';
- }
+ $this->content->footer = print_section_add_menus($course, 0, null, true, true);
return $this->content;
}
diff --git a/course/format/renderer.php b/course/format/renderer.php
index fc8ac72f0f6..74baa080b44 100644
--- a/course/format/renderer.php
+++ b/course/format/renderer.php
@@ -271,7 +271,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course The course entry from DB
- * @param array $mods course modules indexed by id (from get_all_mods)
+ * @param array $mods course modules indexed by id (from get_fast_modinfo()->get_cms())
* @return string HTML to output.
*/
protected function section_summary($section, $course, $mods) {
@@ -318,7 +318,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course the course record from DB
- * @param array $mods course modules indexed by id (from get_all_mods)
+ * @param array $mods course modules indexed by id (from get_fast_modinfo()->get_cms())
* @return string HTML to output.
*/
private function section_activity_summary($section, $course, $mods) {
diff --git a/course/lib.php b/course/lib.php
index 7867376f313..1a246a20ab4 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -1209,56 +1209,30 @@ function get_array_of_activities($courseid) {
return $mod;
}
-
/**
- * Returns a number of useful structures for course displays
+ * Returns the localised human-readable names of all used modules
+ *
+ * @param bool $plural if true returns the plural forms of the names
+ * @return array where key is the module name (component name without 'mod_') and
+ * the value is the human-readable string. Array sorted alphabetically by value
*/
-function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
- global $CFG, $DB, $COURSE;
-
- $mods = array(); // course modules indexed by id
- $modnames = array(); // all course module names (except resource!)
- $modnamesplural= array(); // all course module names (plural form)
- $modnamesused = array(); // course module names used
-
- if ($allmods = $DB->get_records("modules")) {
- foreach ($allmods as $mod) {
- if (!file_exists("$CFG->dirroot/mod/$mod->name/lib.php")) {
- continue;
+function get_module_types_names($plural = false) {
+ static $modnames = null;
+ global $DB, $CFG;
+ if ($modnames === null) {
+ $modnames = array(0 => array(), 1 => array());
+ if ($allmods = $DB->get_records("modules")) {
+ foreach ($allmods as $mod) {
+ if (file_exists("$CFG->dirroot/mod/$mod->name/lib.php") && $mod->visible) {
+ $modnames[0][$mod->name] = get_string("modulename", "$mod->name");
+ $modnames[1][$mod->name] = get_string("modulenameplural", "$mod->name");
+ }
}
- if ($mod->visible) {
- $modnames[$mod->name] = get_string("modulename", "$mod->name");
- $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
- }
- }
- collatorlib::asort($modnames);
- } else {
- print_error("nomodules", 'debug');
- }
-
- $course = ($courseid==$COURSE->id) ? $COURSE : $DB->get_record('course',array('id'=>$courseid));
- $modinfo = get_fast_modinfo($course);
-
- if ($rawmods=$modinfo->cms) {
- foreach($rawmods as $mod) { // Index the mods
- if (empty($modnames[$mod->modname])) {
- continue;
- }
- $mods[$mod->id] = $mod;
- $mods[$mod->id]->modfullname = $modnames[$mod->modname];
- if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities', context_course::instance($courseid))) {
- continue;
- }
- // Check groupings
- if (!groups_course_module_visible($mod)) {
- continue;
- }
- $modnamesused[$mod->modname] = $modnames[$mod->modname];
- }
- if ($modnamesused) {
- collatorlib::asort($modnamesused);
+ collatorlib::asort($modnames[0]);
+ collatorlib::asort($modnames[1]);
}
}
+ return $modnames[(int)$plural];
}
/**
@@ -1746,18 +1720,28 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
*
* @param stdClass $course The course
* @param int $section relative section number (field course_sections.section)
- * @param array $modnames An array containing the list of modules and their names
+ * @param null|array $modnames An array containing the list of modules and their names
+ * if omitted will be taken from get_module_types_names()
* @param bool $vertical Vertical orientation
* @param bool $return Return the menus or send them to output
* @param int $sectionreturn The section to link back to
* @return void|string depending on $return
*/
-function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false, $sectionreturn=null) {
+function print_section_add_menus($course, $section, $modnames = null, $vertical=false, $return=false, $sectionreturn=null) {
global $CFG, $OUTPUT;
- // check to see if user can add menus
- if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))) {
- return false;
+ if ($modnames === null) {
+ $modnames = get_module_types_names();
+ }
+
+ // check to see if user can add menus and there are modules to add
+ if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
+ || empty($modnames)) {
+ if ($return) {
+ return '';
+ } else {
+ return false;
+ }
}
// Retrieve all modules with associated metadata
diff --git a/course/recent.php b/course/recent.php
index a7deb367dde..c6f6306db5e 100644
--- a/course/recent.php
+++ b/course/recent.php
@@ -83,7 +83,7 @@ echo $OUTPUT->heading(format_string($course->fullname) . ": $userinfo", 2);
$mform->display();
$modinfo = get_fast_modinfo($course);
-get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
+$modnames = get_module_types_names();
if (has_capability('moodle/course:viewhiddensections', $context)) {
$hiddenfilter = "";
diff --git a/course/view.php b/course/view.php
index f0c307983ed..3b1842057ef 100644
--- a/course/view.php
+++ b/course/view.php
@@ -242,30 +242,17 @@
// Course wrapper start.
echo html_writer::start_tag('div', array('class'=>'course-content'));
- $modinfo = get_fast_modinfo($COURSE);
- get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
- foreach($mods as $modid=>$unused) {
- if (!isset($modinfo->cms[$modid])) {
- rebuild_course_cache($course->id);
- $modinfo = get_fast_modinfo($COURSE);
- debugging('Rebuilding course cache', DEBUG_DEVELOPER);
- break;
- }
- }
+ // make sure that section 0 exists (this function will create one if it is missing)
+ $section0 = get_course_section(0, $course->id);
- if (!$sections = $modinfo->get_section_info_all()) { // No sections found
- $section = new stdClass;
- $section->course = $course->id; // Create a default section.
- $section->section = 0;
- $section->visible = 1;
- $section->summaryformat = FORMAT_HTML;
- $section->id = $DB->insert_record('course_sections', $section);
- rebuild_course_cache($course->id);
- $modinfo = get_fast_modinfo($COURSE);
- if (!$sections = $modinfo->get_section_info_all()) { // Try again
- print_error('cannotcreateorfindstructs', 'error');
- }
- }
+ // get information about course modules and existing module types
+ // format.php in course formats may rely on presence of these variables
+ $modinfo = get_fast_modinfo($course);
+ $modnames = get_module_types_names();
+ $modnamesplural = get_module_types_names(true);
+ $modnamesused = $modinfo->get_used_module_names();
+ $mods = $modinfo->get_cms();
+ $sections = $modinfo->get_section_info_all();
// CAUTION, hacky fundamental variable defintion to follow!
// Note that because of the way course fromats are constructed though
diff --git a/index.php b/index.php
index dd350322440..34418d7f381 100644
--- a/index.php
+++ b/index.php
@@ -98,26 +98,24 @@
echo $OUTPUT->header();
/// Print Section or custom info
- get_all_mods($SITE->id, $mods, $modnames, $modnamesplural, $modnamesused);
+ $modinfo = get_fast_modinfo($SITE);
+ $modnames = get_module_types_names();
+ $modnamesplural = get_module_types_names(true);
+ $modnamesused = $modinfo->get_used_module_names();
+ $mods = $modinfo->get_cms();
+
if (!empty($CFG->customfrontpageinclude)) {
include($CFG->customfrontpageinclude);
- } else if ($SITE->numsections > 0) {
-
- if (!$section = $DB->get_record('course_sections', array('course'=>$SITE->id, 'section'=>1))) {
- $DB->delete_records('course_sections', array('course'=>$SITE->id, 'section'=>1)); // Just in case
- $section = new stdClass();
- $section->course = $SITE->id;
- $section->section = 1;
- $section->summary = '';
- $section->summaryformat = FORMAT_HTML;
- $section->sequence = '';
- $section->visible = 1;
- $section->id = $DB->insert_record('course_sections', $section);
- rebuild_course_cache($SITE->id, true);
+ } else {
+ if ($editing) {
+ // make sure section with number 1 exists, this function will create section
+ get_course_section(1, $SITE->id);
+ // re-request modinfo in case section was created
+ $modinfo = get_fast_modinfo($SITE);
}
-
- if (!empty($section->sequence) or !empty($section->summary) or $editing) {
+ $section = $modinfo->get_section_info(1);
+ if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
echo $OUTPUT->box_start('generalbox sitetopic');
/// If currently moving a file then show the current clipboard
diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php
index ed4b9f3fb7b..3615124daf5 100644
--- a/lib/deprecatedlib.php
+++ b/lib/deprecatedlib.php
@@ -2977,3 +2977,39 @@ function add_mod_to_section($mod, $beforemod=NULL) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
return course_add_cm_to_section($course, $mod->coursemodule, $mod->section, $beforemod);
}
+
+/**
+ * Returns a number of useful structures for course displays
+ *
+ * Function get_all_mods() is deprecated in 2.4
+ * Instead of:
+ *
+ * get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
+ *
+ * please use:
+ *
+ * $mods = get_fast_modinfo($course)->get_cms();
+ * $modnames = get_module_types_names();
+ * $modnamesplural = get_module_types_names(true);
+ * $modnamesused = get_fast_modinfo($course)->get_used_module_names();
+ *
+ *
+ * @deprecated since 2.4
+ *
+ * @param int $courseid id of the course to get info about
+ * @param array $mods (return) list of course modules
+ * @param array $modnames (return) list of names of all module types installed and available
+ * @param array $modnamesplural (return) list of names of all module types installed and available in the plural form
+ * @param array $modnamesused (return) list of names of all module types used in the course
+ */
+function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
+ debugging('Function get_all_mods() is deprecated. Use get_fast_modinfo() and get_module_types_names() instead. See phpdocs for details', DEBUG_DEVELOPER);
+
+ global $COURSE;
+ $modnames = get_module_types_names();
+ $modnamesplural= get_module_types_names(true);
+ $course = ($courseid==$COURSE->id) ? $COURSE : $DB->get_record('course',array('id'=>$courseid));
+ $modinfo = get_fast_modinfo($course);
+ $mods = $modinfo->get_cms();
+ $modnamesused = $modinfo->get_used_module_names();
+}
diff --git a/lib/modinfolib.php b/lib/modinfolib.php
index b2c69fb0a0d..d24ec6dff73 100644
--- a/lib/modinfolib.php
+++ b/lib/modinfolib.php
@@ -158,6 +158,24 @@ class course_modinfo extends stdClass {
return $this->instances;
}
+ /**
+ * Returns array of localised human-readable module names used in this course
+ *
+ * @param bool $plural if true returns the plural form of modules names
+ * @return array
+ */
+ public function get_used_module_names($plural = false) {
+ $modnames = get_module_types_names($plural);
+ $modnamesused = array();
+ foreach ($this->get_cms() as $cmid => $mod) {
+ if (isset($modnames[$mod->modname]) && $mod->uservisible) {
+ $modnamesused[$mod->modname] = $modnames[$mod->modname];
+ }
+ }
+ collatorlib::asort($modnamesused);
+ return $modnamesused;
+ }
+
/**
* Obtains all instances of a particular module on this course.
* @param $modname Name of module (not full frankenstyle) e.g. 'label'
@@ -610,14 +628,6 @@ class cm_info extends stdClass {
*/
public $conditionsfield;
- /**
- * Plural name of module type, e.g. 'Forums' - from lang file
- * @deprecated Do not use this value (you can obtain it by calling get_string instead); it
- * will be removed in a future version (see later TODO in this file)
- * @var string
- */
- public $modplural;
-
/**
* True if this course-module is available to students i.e. if all availability conditions
* are met - obtained dynamically
@@ -692,6 +702,24 @@ class cm_info extends stdClass {
*/
private $afterediticons;
+ /**
+ * Magic method getter
+ *
+ * @param string $name
+ * @return mixed
+ */
+ public function __get($name) {
+ switch ($name) {
+ case 'modplural':
+ return $this->get_module_type_name(true);
+ case 'modfullname':
+ return $this->get_module_type_name();
+ default:
+ debugging('Invalid cm_info property accessed: '.$name);
+ return null;
+ }
+ }
+
/**
* @return bool True if this module has a 'view' page that should be linked to in navigation
* etc (note: modules may still have a view.php file, but return false if this is not
@@ -793,6 +821,21 @@ class cm_info extends stdClass {
return $icon;
}
+ /**
+ * Returns a localised human-readable name of the module type
+ *
+ * @param bool $plural return plural form
+ * @return string
+ */
+ public function get_module_type_name($plural = false) {
+ $modnames = get_module_types_names($plural);
+ if (isset($modnames[$this->modname])) {
+ return $modnames[$this->modname];
+ } else {
+ return null;
+ }
+ }
+
/**
* @return course_modinfo Modinfo object that this came from
*/
@@ -1013,16 +1056,6 @@ class cm_info extends stdClass {
$this->conditionsfield = isset($mod->conditionsfield)
? $mod->conditionsfield : array();
- // Get module plural name.
- // TODO This was a very old performance hack and should now be removed as the information
- // certainly doesn't belong in modinfo. On a 'normal' page this is only used in the
- // activity_modules block, so if it needs caching, it should be cached there.
- static $modplurals;
- if (!isset($modplurals[$this->modname])) {
- $modplurals[$this->modname] = get_string('modulenameplural', $this->modname);
- }
- $this->modplural = $modplurals[$this->modname];
-
static $modviews;
if (!isset($modviews[$this->modname])) {
$modviews[$this->modname] = !plugin_supports('mod', $this->modname,
diff --git a/lib/navigationlib.php b/lib/navigationlib.php
index e5dc06f4f8b..1a5a9320c43 100644
--- a/lib/navigationlib.php
+++ b/lib/navigationlib.php
@@ -3427,10 +3427,9 @@ class settings_navigation extends navigation_node {
*/
protected function get_course_modules($course) {
global $CFG;
- $mods = $modnames = $modnamesplural = $modnamesused = array();
// This function is included when we include course/lib.php at the top
// of this file
- get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
+ $modnames = get_module_types_names();
$resources = array();
$activities = array();
foreach($modnames as $modname=>$modnamestr) {