From 0fd34211df51bd0e1deee17c6c56b0764be5e4bb Mon Sep 17 00:00:00 2001 From: ferran Date: Sat, 9 Aug 2025 10:22:21 +0200 Subject: [PATCH] MDL-86254 core_comment: deprecate locallib classes --- .upgradenotes/MDL-86254-2025080908400007.yml | 8 +++ public/comment/classes/manager.php | 50 +++++++++++++++++++ public/comment/locallib.php | 24 +++++++++ .../comment/tests/privacy/provider_test.php | 5 -- public/course/lib.php | 6 +-- 5 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 .upgradenotes/MDL-86254-2025080908400007.yml diff --git a/.upgradenotes/MDL-86254-2025080908400007.yml b/.upgradenotes/MDL-86254-2025080908400007.yml new file mode 100644 index 00000000000..f91cc908ad5 --- /dev/null +++ b/.upgradenotes/MDL-86254-2025080908400007.yml @@ -0,0 +1,8 @@ +issueNumber: MDL-86254 +notes: + core_comment: + - message: >- + The `public/comment/locallib.php` file and the `comment_manager` class + have been deprecated. All related functionality should now be accessed + via the `\core_comment\manager` class. + type: deprecated diff --git a/public/comment/classes/manager.php b/public/comment/classes/manager.php index 5ce11a498eb..e5692de0e44 100644 --- a/public/comment/classes/manager.php +++ b/public/comment/classes/manager.php @@ -1072,6 +1072,56 @@ class manager { return ($hascapability || ($owncomment && $this->can_post())); } + /** + * Get comments created since a given time. + * + * @param stdClass $course course object + * @param stdClass $context context object + * @param string $component component name + * @param int $since the time to check + * @param \cm_info|null $cm optional course module object + * @return array list of comments db records since the given timelimit + * @since Moodle 3.2 + */ + public static function get_component_comments_since( + stdClass $course, + stdClass $context, + string $component, + int $since, + ?\cm_info $cm = null + ): array { + global $DB; + + $result = []; + $where = 'contextid = ? AND component = ? AND timecreated > ?'; + $comments = $DB->get_records_select('comments', $where, [$context->id, $component, $since]); + + // Check item by item if we have permissions. + $managersviewstatus = []; + foreach ($comments as $comment) { + $cachedkey = $comment->commentarea . '/' . $comment->itemid; + + if (!isset($managersviewstatus[$cachedkey])) { + $args = (object)[ + 'area' => $comment->commentarea, + 'itemid' => $comment->itemid, + 'context' => $context, + 'course' => $course, + 'client_id' => 0, + 'component' => $component, + 'cm' => $cm, + ]; + $manager = new self($args); + $managersviewstatus[$cachedkey] = $manager->can_view(); + } + + if ($managersviewstatus[$cachedkey]) { + $result[$comment->id] = $comment; + } + } + return $result; + } + /** * Returns the component associated with the comment. * diff --git a/public/comment/locallib.php b/public/comment/locallib.php index 8da1249f5c1..b4228f44394 100644 --- a/public/comment/locallib.php +++ b/public/comment/locallib.php @@ -15,12 +15,20 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +defined('MOODLE_INTERNAL') || die(); + +debugging( + 'This file has been deprecated. Please, use \core_comment\manager instead', + DEBUG_DEVELOPER, +); + /** * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments) * * @package core_comment * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @todo Remove this class and the file in Moodle 6.0 (MDL-86257) */ class comment_manager { @@ -48,8 +56,16 @@ class comment_manager { /** * Constructs the comment_manage object */ + #[\core\attribute\deprecated( + replacement: 'core_comment\manager', + since: '5.1', + mdl: 'MDL-86254', + )] public function __construct() { global $CFG; + + \core\deprecation::emit_deprecation(__FUNCTION__); + $this->perpage = $CFG->commentsperpage; } @@ -245,10 +261,18 @@ class comment_manager { * @param stdClass|\cm_info|null $cm course module object * @return array list of comments db records since the given timelimit * @since Moodle 3.2 + * */ + #[\core\attribute\deprecated( + replacement: 'core_comment\manager::get_component_comments_since', + since: '5.1', + mdl: 'MDL-86254', + )] public function get_component_comments_since($course, $context, $component, $since, $cm = null) { global $DB; + \core\deprecation::emit_deprecation(__FUNCTION__); + $commentssince = array(); $where = 'contextid = ? AND component = ? AND timecreated > ?'; $comments = $DB->get_records_select('comments', $where, array($context->id, $component, $since)); diff --git a/public/comment/tests/privacy/provider_test.php b/public/comment/tests/privacy/provider_test.php index ae551d4e8ba..7a4336d6ec1 100644 --- a/public/comment/tests/privacy/provider_test.php +++ b/public/comment/tests/privacy/provider_test.php @@ -23,11 +23,6 @@ */ namespace core_comment\privacy; -defined('MOODLE_INTERNAL') || die(); -global $CFG; - -require_once($CFG->dirroot . '/comment/locallib.php'); - use core_privacy\local\request\approved_userlist; use core_privacy\tests\provider_testcase; use core_privacy\tests\request\approved_contextlist; diff --git a/public/course/lib.php b/public/course/lib.php index 423011cb300..2ef5dde71fb 100644 --- a/public/course/lib.php +++ b/public/course/lib.php @@ -4426,10 +4426,8 @@ function course_check_module_updates_since($cm, $from, $fileareas = array(), $fi // Check comments. if (plugin_supports('mod', $cm->modname, FEATURE_COMMENT) and (empty($filter) or in_array('comments', $filter))) { - $updates->comments = (object) array('updated' => false); - require_once($CFG->dirroot . '/comment/locallib.php'); - $manager = new comment_manager(); - $comments = $manager->get_component_comments_since($course, $context, $component, $from, $cm); + $updates->comments = (object) ['updated' => false]; + $comments = core_comment\manager::get_component_comments_since($course, $context, $component, $from, $cm); if (!empty($comments)) { $updates->comments->updated = true; $updates->comments->itemids = array_keys($comments);