MDL-67585 core_course: Use the content_item_service to build the picker
authorJake Dallimore <jake@moodle.com>
Tue, 21 Jan 2020 06:14:18 +0000 (14:14 +0800)
committerJake Dallimore <jake@moodle.com>
Thu, 20 Feb 2020 01:28:57 +0000 (09:28 +0800)
Replace get_module_metadata calls with calls to the new
content_item_service class.

course/format/singleactivity/lib.php
course/renderer.php

index 5db9dea..5409816 100644 (file)
@@ -383,10 +383,12 @@ class format_singleactivity extends format_base {
      * @return bool|null (null if the check is not possible)
      */
     public function activity_has_subtypes() {
      * @return bool|null (null if the check is not possible)
      */
     public function activity_has_subtypes() {
+        global $PAGE, $USER;
         if (!($modname = $this->get_activitytype())) {
             return null;
         }
         if (!($modname = $this->get_activitytype())) {
             return null;
         }
-        $metadata = get_module_metadata($this->get_course(), self::get_supported_activities());
+        $contentitemservice = new \core_course\local\service\content_item_service($PAGE->get_renderer('course'));
+        $metadata = $contentitemservice->get_content_items_for_user_in_course($USER, $this->get_course());
         foreach ($metadata as $key => $moduledata) {
             if (preg_match('/^'.$modname.':/', $key)) {
                 return true;
         foreach ($metadata as $key => $moduledata) {
             if (preg_match('/^'.$modname.':/', $key)) {
                 return true;
index 83a329a..fe3cb92 100644 (file)
@@ -268,20 +268,33 @@ class core_course_renderer extends plugin_renderer_base {
      * @return string
      */
     function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
      * @return string
      */
     function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
-        global $CFG;
+        global $CFG, $PAGE, $USER;
 
         $vertical = !empty($displayoptions['inblock']);
 
 
         $vertical = !empty($displayoptions['inblock']);
 
-        // check to see if user can add menus and there are modules to add
+        // Check to see if user can add menus.
         if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
         if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
-                || !$this->page->user_is_editing()
-                || !($modnames = get_module_types_names()) || empty($modnames)) {
+                || !$this->page->user_is_editing()) {
             return '';
         }
 
         // Retrieve all modules with associated metadata
             return '';
         }
 
         // Retrieve all modules with associated metadata
-        $modules = get_module_metadata($course, $modnames, $sectionreturn);
-        $urlparams = array('section' => $section);
+        $contentitemservice = new \core_course\local\service\content_item_service(
+            new \core_course\local\repository\caching_content_item_readonly_repository(
+                \cache::make('core', 'user_course_content_items'),
+                new \core_course\local\repository\content_item_readonly_repository()
+            )
+        );
+        $urlparams = ['section' => $section];
+        if (!is_null($sectionreturn)) {
+            $urlparams['sr'] = $sectionreturn;
+        }
+        $modules = $contentitemservice->get_content_items_for_user_in_course($USER, $course, $urlparams);
+
+        // Return if there are no content items to add.
+        if (empty($modules)) {
+            return '';
+        }
 
         // We'll sort resources and activities into two lists
         $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
 
         // We'll sort resources and activities into two lists
         $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
@@ -294,7 +307,7 @@ class core_course_renderer extends plugin_renderer_base {
                 // System modules cannot be added by user, do not add to dropdown.
                 continue;
             }
                 // System modules cannot be added by user, do not add to dropdown.
                 continue;
             }
-            $link = $module->link->out(true, $urlparams);
+            $link = $module->link;
             $activities[$activityclass][$link] = $module->title;
         }
 
             $activities[$activityclass][$link] = $module->title;
         }