From 2f040002eeeaad63834836e6a414a37589d08382 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Tue, 21 Jan 2020 14:14:18 +0800 Subject: [PATCH] MDL-67585 core_course: Use the content_item_service to build the picker Replace get_module_metadata calls with calls to the new content_item_service class. --- course/format/singleactivity/lib.php | 4 +++- course/renderer.php | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/course/format/singleactivity/lib.php b/course/format/singleactivity/lib.php index 5db9dea89ea..5409816e6d9 100644 --- a/course/format/singleactivity/lib.php +++ b/course/format/singleactivity/lib.php @@ -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() { + global $PAGE, $USER; 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; diff --git a/course/renderer.php b/course/renderer.php index 83a329aaf30..fe3cb929bc4 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -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()) { - global $CFG; + global $CFG, $PAGE, $USER; $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)) - || !$this->page->user_is_editing() - || !($modnames = get_module_types_names()) || empty($modnames)) { + || !$this->page->user_is_editing()) { 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()); @@ -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; } - $link = $module->link->out(true, $urlparams); + $link = $module->link; $activities[$activityclass][$link] = $module->title; } -- 2.43.0