diff --git a/filter/mathjaxloader/filter.php b/filter/mathjaxloader/filter.php index 0906e18fb49..9fab93b9e79 100644 --- a/filter/mathjaxloader/filter.php +++ b/filter/mathjaxloader/filter.php @@ -140,7 +140,13 @@ class filter_mathjaxloader extends moodle_text_filter { if ($hasextra) { // If custom dilimeters are used, wrap whole text to prevent autolinking. $text = '' . $text . ''; - } else { + } else if (preg_match('/\\\\[[(]/', $text) || preg_match('/\$\$/', $text)) { + // Only parse the text if there are mathjax symbols in it. The recognized + // math environments are \[ \] and $$ $$ for display mathematics and \( \) + // for inline mathematics. + // Note: 2 separate regexes seems to perform better here than using a single + // regex with groupings. + // Wrap display and inline math environments in nolink spans. // Do not wrap nested environments, i.e., if inline math is nested // inside display math, only the outer display math is wrapped in diff --git a/lang/en/cache.php b/lang/en/cache.php index 441b1910acf..73acbeb0803 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -65,6 +65,7 @@ $string['cachedef_observers'] = 'Event observers'; $string['cachedef_plugin_functions'] = 'Plugins available callbacks'; $string['cachedef_plugin_manager'] = 'Plugin info manager'; $string['cachedef_presignup'] = 'Pre sign-up data for particular unregistered user'; +$string['cachedef_portfolio_add_button_portfolio_instances'] = 'Portfolio instances for portfolio_add_button class'; $string['cachedef_postprocessedcss'] = 'Post processed CSS'; $string['cachedef_tagindexbuilder'] = 'Search results for tagged items'; $string['cachedef_questiondata'] = 'Question definitions'; diff --git a/lib/db/caches.php b/lib/db/caches.php index 71ebe33add0..7247f54386a 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -391,4 +391,13 @@ $definitions = array( 'simplekeys' => true, 'simpledata' => true, ), + + // Cache the list of portfolio instances for the logged in user + // in the portfolio_add_button constructor to avoid loading the + // same data multiple times. + 'portfolio_add_button_portfolio_instances' => [ + 'mode' => cache_store::MODE_REQUEST, + 'simplekeys' => true, + 'staticacceleration' => true + ], ); diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 68d5fe500bf..cc00cc5b8a2 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -117,7 +117,14 @@ class portfolio_add_button { debugging('Building portfolio add button while portfolios is disabled. This code can be optimised.', DEBUG_DEVELOPER); } - $this->instances = portfolio_instances(); + $cache = cache::make('core', 'portfolio_add_button_portfolio_instances'); + $instances = $cache->get('instances'); + if ($instances === false) { + $instances = portfolio_instances(); + $cache->set('instances', $instances); + } + + $this->instances = $instances; if (empty($options)) { return true; } diff --git a/mod/forum/classes/local/builders/exported_discussion_summaries.php b/mod/forum/classes/local/builders/exported_discussion_summaries.php index 7859c4796d1..bb792288268 100644 --- a/mod/forum/classes/local/builders/exported_discussion_summaries.php +++ b/mod/forum/classes/local/builders/exported_discussion_summaries.php @@ -105,7 +105,7 @@ class exported_discussion_summaries { * * @param stdClass $user The user to export the posts for. * @param forum_entity $forum The forum that each of the $discussions belong to - * @param discussion_entity[] $discussions A list of all discussions that each of the $posts belong to + * @param discussion_summary_entity[] $discussions A list of all discussion summaries to export * @return stdClass[] List of exported posts in the same order as the $posts array. */ public function build( @@ -125,6 +125,12 @@ class exported_discussion_summaries { $replycounts = $postvault->get_reply_count_for_discussion_ids($user, $discussionids, $canseeanyprivatereply); $latestposts = $postvault->get_latest_post_id_for_discussion_ids($user, $discussionids, $canseeanyprivatereply); + $postauthorids = array_unique(array_reduce($discussions, function($carry, $summary) { + $firstpostauthorid = $summary->get_first_post_author()->get_id(); + $lastpostauthorid = $summary->get_latest_post_author()->get_id(); + return array_merge($carry, [$firstpostauthorid, $lastpostauthorid]); + }, [])); + $postauthorcontextids = $this->get_author_context_ids($postauthorids); $unreadcounts = []; $favourites = $this->get_favourites($user); @@ -144,6 +150,7 @@ class exported_discussion_summaries { $replycounts, $unreadcounts, $latestposts, + $postauthorcontextids, $favourites ); @@ -224,4 +231,15 @@ class exported_discussion_summaries { return $authorgroups; } + + /** + * Get the user context ids for each of the authors. + * + * @param int[] $authorids The list of author ids to fetch context ids for. + * @return int[] Context ids indexed by author id + */ + private function get_author_context_ids(array $authorids) : array { + $authorvault = $this->vaultfactory->get_author_vault(); + return $authorvault->get_context_ids_for_author_ids($authorids); + } } diff --git a/mod/forum/classes/local/builders/exported_posts.php b/mod/forum/classes/local/builders/exported_posts.php index 2d8934d971c..9e25386629b 100644 --- a/mod/forum/classes/local/builders/exported_posts.php +++ b/mod/forum/classes/local/builders/exported_posts.php @@ -137,6 +137,7 @@ class exported_posts { $groupedposts = $this->group_posts_by_discussion($forums, $discussions, $posts); // Load all of the resources we need in order to export the posts. $authorsbyid = $this->get_authors_for_posts($posts); + $authorcontextids = $this->get_author_context_ids(array_keys($authorsbyid)); $attachmentsbypostid = $this->get_attachments_for_posts($groupedposts); $groupsbycourseandauthorid = $this->get_author_groups_from_posts($groupedposts); $tagsbypostid = $this->get_tags_from_posts($posts); @@ -161,6 +162,7 @@ class exported_posts { $discussion, $groupedposts, $authorsbyid, + $authorcontextids, $attachmentsbypostid, $groupsbycourseandauthorid[$courseid], $readreceiptcollectionbyforumid[$forumid] ?? null, @@ -248,6 +250,17 @@ class exported_posts { return $authorvault->get_authors_for_posts($posts); } + /** + * Get the user context ids for each of the authors. + * + * @param int[] $authorids The list of author ids to fetch context ids for. + * @return int[] Context ids indexed by author id + */ + private function get_author_context_ids(array $authorids) : array { + $authorvault = $this->vaultfactory->get_author_vault(); + return $authorvault->get_context_ids_for_author_ids($authorids); + } + /** * Load the list of all attachments for the posts. The list of attachments will be * indexed by the post id. diff --git a/mod/forum/classes/local/exporters/author.php b/mod/forum/classes/local/exporters/author.php index fb648463e20..203fc82a350 100644 --- a/mod/forum/classes/local/exporters/author.php +++ b/mod/forum/classes/local/exporters/author.php @@ -42,6 +42,8 @@ require_once($CFG->dirroot . '/mod/forum/lib.php'); class author extends exporter { /** @var author_entity $author Author entity */ private $author; + /** @var int $authorcontextid The context id for the author entity */ + private $authorcontextid; /** @var array $authorgroups List of groups that the author belongs to */ private $authorgroups; /** @var bool $canview Should the author be anonymised? */ @@ -51,12 +53,20 @@ class author extends exporter { * Constructor. * * @param author_entity $author The author entity to export + * @param int $authorcontextid The context id for the author entity to export * @param stdClass[] $authorgroups The list of groups that the author belongs to * @param bool $canview Can the requesting user view this author or should it be anonymised? * @param array $related The related data for the export. */ - public function __construct(author_entity $author, array $authorgroups = [], bool $canview = true, array $related = []) { + public function __construct( + author_entity $author, + int $authorcontextid, + array $authorgroups = [], + bool $canview = true, + array $related = [] + ) { $this->author = $author; + $this->authorcontextid = $authorcontextid; $this->authorgroups = $authorgroups; $this->canview = $canview; return parent::__construct([], $related); @@ -128,6 +138,7 @@ class author extends exporter { */ protected function get_other_values(renderer_base $output) { $author = $this->author; + $authorcontextid = $this->authorcontextid; $urlfactory = $this->related['urlfactory']; if ($this->canview) { @@ -148,7 +159,7 @@ class author extends exporter { 'groups' => $groups, 'urls' => [ 'profile' => ($urlfactory->get_author_profile_url($author))->out(false), - 'profileimage' => ($urlfactory->get_author_profile_image_url($author))->out(false) + 'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false) ] ]; } else { diff --git a/mod/forum/classes/local/exporters/discussion_summaries.php b/mod/forum/classes/local/exporters/discussion_summaries.php index 13eac7e8527..e5fbb36820a 100644 --- a/mod/forum/classes/local/exporters/discussion_summaries.php +++ b/mod/forum/classes/local/exporters/discussion_summaries.php @@ -56,6 +56,9 @@ class discussion_summaries extends exporter { /** @var array The latest post in each discussion */ private $latestpostids; + /** @var int[] The context ids for the first and latest post authors (indexed by author id) */ + private $postauthorcontextids; + /** * Constructor. * @@ -65,6 +68,7 @@ class discussion_summaries extends exporter { * @param int[] $discussionreplycount Discussion reply counts indexed by dicussion id * @param int[] $discussionunreadcount Discussion unread counts indexed by dicussion id * @param int[] $latestpostids List of latest post ids indexed by discussion id + * @param int[] $postauthorcontextids The context ids for the first and latest post authors (indexed by author id) * @param array $related The related */ public function __construct( @@ -74,6 +78,7 @@ class discussion_summaries extends exporter { array $discussionreplycount, array $discussionunreadcount, array $latestpostids, + array $postauthorcontextids, array $related = [] ) { $this->discussions = $discussions; @@ -82,6 +87,7 @@ class discussion_summaries extends exporter { $this->discussionreplycount = $discussionreplycount; $this->discussionunreadcount = $discussionunreadcount; $this->latestpostids = $latestpostids; + $this->postauthorcontextids = $postauthorcontextids; return parent::__construct([], $related); } @@ -126,6 +132,8 @@ class discussion_summaries extends exporter { $replycount, $unreadcount, $latestpostid, + $this->postauthorcontextids[$discussion->get_first_post_author()->get_id()], + $this->postauthorcontextids[$discussion->get_latest_post_author()->get_id()], $related ); $exporteddiscussions[] = $exporter->export($output); diff --git a/mod/forum/classes/local/exporters/discussion_summary.php b/mod/forum/classes/local/exporters/discussion_summary.php index 6ba2bcb531c..3d009ee222d 100644 --- a/mod/forum/classes/local/exporters/discussion_summary.php +++ b/mod/forum/classes/local/exporters/discussion_summary.php @@ -55,6 +55,12 @@ class discussion_summary extends exporter { /** @var int The latest post id in the discussion */ private $latestpostid; + /** @var int The context id for the author of the first post */ + private $firstpostauthorcontextid; + + /** @var int The context id for the author of the latest post */ + private $latestpostauthorcontextid; + /** * Constructor. * @@ -64,6 +70,8 @@ class discussion_summary extends exporter { * @param int $replycount The number of replies to the discussion * @param int $unreadcount number of unread posts if the user is tracking these * @param int $latestpostid The latest post id in the discussion + * @param int $firstpostauthorcontextid The context id for the author of the first post + * @param int $latestpostauthorcontextid The context id for the author of the latest post * @param array $related The related objects */ public function __construct( @@ -73,6 +81,8 @@ class discussion_summary extends exporter { int $replycount, int $unreadcount, int $latestpostid, + int $firstpostauthorcontextid, + int $latestpostauthorcontextid, array $related = [] ) { $this->summary = $summary; @@ -81,6 +91,8 @@ class discussion_summary extends exporter { $this->replycount = $replycount; $this->unreadcount = $unreadcount; $this->latestpostid = $latestpostid; + $this->firstpostauthorcontextid = $firstpostauthorcontextid; + $this->latestpostauthorcontextid = $latestpostauthorcontextid; return parent::__construct([], $related); } @@ -131,6 +143,7 @@ class discussion_summary extends exporter { $firstpostauthor = new author( $this->summary->get_first_post_author(), + $this->firstpostauthorcontextid, $this->groupsbyauthorid[$this->summary->get_first_post_author()->get_id()], $capabilitymanager->can_view_post( $user, @@ -142,6 +155,7 @@ class discussion_summary extends exporter { $latestpostauthor = new author( $this->summary->get_latest_post_author(), + $this->latestpostauthorcontextid, [], $capabilitymanager->can_view_post( $user, diff --git a/mod/forum/classes/local/exporters/post.php b/mod/forum/classes/local/exporters/post.php index 00716d6a940..ea097b4aa8c 100644 --- a/mod/forum/classes/local/exporters/post.php +++ b/mod/forum/classes/local/exporters/post.php @@ -333,6 +333,7 @@ class post extends exporter { $forum = $this->related['forum']; $discussion = $this->related['discussion']; $author = $this->related['author']; + $authorcontextid = $this->related['authorcontextid']; $user = $this->related['user']; $readreceiptcollection = $this->related['readreceiptcollection']; $rating = $this->related['rating']; @@ -368,7 +369,13 @@ class post extends exporter { $markasunreadurl = $cancontrolreadstatus ? $urlfactory->get_mark_post_as_unread_url_from_post($post) : null; $discussurl = $canview ? $urlfactory->get_discussion_view_url_from_post($post) : null; - $authorexporter = new author_exporter($author, $authorgroups, ($canview && !$isdeleted), $this->related); + $authorexporter = new author_exporter( + $author, + $authorcontextid, + $authorgroups, + ($canview && !$isdeleted), + $this->related + ); $exportedauthor = $authorexporter->export($output); // Only bother loading the content if the user can see it. $loadcontent = $canview && !$isdeleted; @@ -448,6 +455,7 @@ class post extends exporter { 'forum' => 'mod_forum\local\entities\forum', 'discussion' => 'mod_forum\local\entities\discussion', 'author' => 'mod_forum\local\entities\author', + 'authorcontextid' => 'int?', 'user' => 'stdClass', 'context' => 'context', 'authorgroups' => 'stdClass[]', diff --git a/mod/forum/classes/local/exporters/posts.php b/mod/forum/classes/local/exporters/posts.php index f00e3fc82a3..1ba0a76dad5 100644 --- a/mod/forum/classes/local/exporters/posts.php +++ b/mod/forum/classes/local/exporters/posts.php @@ -45,6 +45,8 @@ class posts extends exporter { private $posts; /** @var author_entity[] $authorsbyid List of authors for the posts indexed by author id */ private $authorsbyid; + /** @var int[] $authorcontextids List of authors context ids indexed by author id */ + private $authorcontextids; /** @var array $attachmentsbypostid List of attachments indexed by post id */ private $attachmentsbypostid; /** @var array $groupsbyauthorid List of author's groups indexed by author id */ @@ -59,6 +61,7 @@ class posts extends exporter { * * @param post_entity[] $posts List of posts to export * @param author_entity[] $authorsbyid List of authors for the posts indexed by author id + * @param int[] $authorcontextids List of authors context ids indexed by author id * @param array $attachmentsbypostid List of attachments indexed by post id * @param array $groupsbyauthorid List of author's groups indexed by author id * @param array $tagsbypostid List of tags indexed by post id @@ -68,6 +71,7 @@ class posts extends exporter { public function __construct( array $posts, array $authorsbyid = [], + array $authorcontextids = [], array $attachmentsbypostid = [], array $groupsbyauthorid = [], array $tagsbypostid = [], @@ -76,6 +80,7 @@ class posts extends exporter { ) { $this->posts = $posts; $this->authorsbyid = $authorsbyid; + $this->authorcontextids = $authorcontextids; $this->attachmentsbypostid = $attachmentsbypostid; $this->groupsbyauthorid = $groupsbyauthorid; $this->tagsbypostid = $tagsbypostid; @@ -106,6 +111,7 @@ class posts extends exporter { protected function get_other_values(renderer_base $output) { $related = $this->related; $authorsbyid = $this->authorsbyid; + $authorcontextids = $this->authorcontextids; $attachmentsbypostid = $this->attachmentsbypostid; $groupsbyauthorid = $this->groupsbyauthorid; $tagsbypostid = $this->tagsbypostid; @@ -114,6 +120,7 @@ class posts extends exporter { function($post) use ( $related, $authorsbyid, + $authorcontextids, $attachmentsbypostid, $groupsbyauthorid, $tagsbypostid, @@ -123,12 +130,14 @@ class posts extends exporter { $authorid = $post->get_author_id(); $postid = $post->get_id(); $author = isset($authorsbyid[$authorid]) ? $authorsbyid[$authorid] : []; + $authorcontextid = isset($authorcontextids[$authorid]) ? $authorcontextids[$authorid] : null; $attachments = isset($attachmentsbypostid[$postid]) ? $attachmentsbypostid[$postid] : []; $authorgroups = isset($groupsbyauthorid[$authorid]) ? $groupsbyauthorid[$authorid] : []; $tags = isset($tagsbypostid[$postid]) ? $tagsbypostid[$postid] : []; $rating = isset($ratingbypostid[$postid]) ? $ratingbypostid[$postid] : null; $exporter = new post_exporter($post, array_merge($related, [ 'author' => $author, + 'authorcontextid' => $authorcontextid, 'attachments' => $attachments, 'authorgroups' => $authorgroups, 'tags' => $tags, diff --git a/mod/forum/classes/local/factories/exporter.php b/mod/forum/classes/local/factories/exporter.php index 8aaea94e265..e7c405a9dda 100644 --- a/mod/forum/classes/local/factories/exporter.php +++ b/mod/forum/classes/local/factories/exporter.php @@ -152,12 +152,14 @@ class exporter { * * @param stdClass $user The user viewing the forum * @param forum_entity $forum The forum being viewed - * @param discussion_entity[] $discussions The set of discussions to be shown + * @param discussion_summary_entity[] $discussions The set of discussion summaries to export * @param stdClass[] $groupsbyauthorid The set of groups in an associative array for each author * @param stdClass[] $groupsbyid The set of groups in the forum in an associative array for each group * @param int[] $discussionreplycount The number of replies for each discussion * @param int[] $discussionunreadcount The number of unread posts for each discussion * @param int[] $latestpostids The latest post id for each discussion + * @param int[] $postauthorcontextids The context ids for the first and last post authors (indexed by author id) + * @param int[] $favourites The list of discussion ids that have been favourited * @return discussion_summaries_exporter */ public function get_discussion_summaries_exporter( @@ -168,7 +170,8 @@ class exporter { array $groupsbyauthorid = [], array $discussionreplycount = [], array $discussionunreadcount = [], - array $latestpostid = [], + array $latestpostids = [], + array $postauthorcontextids = [], array $favourites = [] ) : discussion_summaries_exporter { return new discussion_summaries_exporter( @@ -177,7 +180,8 @@ class exporter { $groupsbyauthorid, $discussionreplycount, $discussionunreadcount, - $latestpostid, + $latestpostids, + $postauthorcontextids, [ 'legacydatamapperfactory' => $this->legacydatamapperfactory, 'context' => $forum->get_context(), @@ -207,6 +211,7 @@ class exporter { * @param discussion_entity $discussion The discussion that the post is in * @param post_entity[] $posts The set of posts to be exported * @param author_entity[] $authorsbyid List of authors indexed by author id + * @param int[] $authorcontextids List of authors context ids indexed by author id * @param array $attachmentsbypostid List of attachments for each post indexed by post id * @param array $groupsbyauthorid List of groups for the post authors indexed by author id * @param post_read_receipt_collection_entity|null $readreceiptcollection Details of read receipts for each post @@ -221,6 +226,7 @@ class exporter { discussion_entity $discussion, array $posts, array $authorsbyid = [], + array $authorcontextids = [], array $attachmentsbypostid = [], array $groupsbyauthorid = [], post_read_receipt_collection_entity $readreceiptcollection = null, @@ -228,16 +234,25 @@ class exporter { array $ratingbypostid = [], bool $includehtml = false ) : posts_exporter { - return new posts_exporter($posts, $authorsbyid, $attachmentsbypostid, $groupsbyauthorid, $tagsbypostid, $ratingbypostid, [ - 'capabilitymanager' => $this->managerfactory->get_capability_manager($forum), - 'urlfactory' => $this->urlfactory, - 'forum' => $forum, - 'discussion' => $discussion, - 'user' => $user, - 'context' => $forum->get_context(), - 'readreceiptcollection' => $readreceiptcollection, - 'includehtml' => $includehtml - ]); + return new posts_exporter( + $posts, + $authorsbyid, + $authorcontextids, + $attachmentsbypostid, + $groupsbyauthorid, + $tagsbypostid, + $ratingbypostid, + [ + 'capabilitymanager' => $this->managerfactory->get_capability_manager($forum), + 'urlfactory' => $this->urlfactory, + 'forum' => $forum, + 'discussion' => $discussion, + 'user' => $user, + 'context' => $forum->get_context(), + 'readreceiptcollection' => $readreceiptcollection, + 'includehtml' => $includehtml + ] + ); } /** diff --git a/mod/forum/classes/local/factories/url.php b/mod/forum/classes/local/factories/url.php index 2d9f7a7586e..eaab4f59cbe 100644 --- a/mod/forum/classes/local/factories/url.php +++ b/mod/forum/classes/local/factories/url.php @@ -393,16 +393,19 @@ class url { } /** - * Get the url to view the author's profile image. + * Get the url to view the author's profile image. The author's context id should be + * provided to prevent the code from needing to load it. * * @param author_entity $author The author + * @param int|null $authorcontextid The author context id * @return moodle_url */ - public function get_author_profile_image_url(author_entity $author) : moodle_url { + public function get_author_profile_image_url(author_entity $author, int $authorcontextid = null) : moodle_url { global $PAGE; $datamapper = $this->legacydatamapperfactory->get_author_data_mapper(); $record = $datamapper->to_legacy_object($author); + $record->contextid = $authorcontextid; $userpicture = new user_picture($record); $userpicture->size = 2; diff --git a/mod/forum/classes/local/renderers/discussion.php b/mod/forum/classes/local/renderers/discussion.php index 8b7e6249186..084122a7af7 100644 --- a/mod/forum/classes/local/renderers/discussion.php +++ b/mod/forum/classes/local/renderers/discussion.php @@ -284,7 +284,7 @@ class discussion { * * @return string */ - private function get_move_discussion_html() : string { + private function get_move_discussion_html() : ?string { global $DB; $forum = $this->forum; diff --git a/mod/forum/classes/local/vaults/author.php b/mod/forum/classes/local/vaults/author.php index bd0f78b90a0..730f0e1b9f0 100644 --- a/mod/forum/classes/local/vaults/author.php +++ b/mod/forum/classes/local/vaults/author.php @@ -99,4 +99,22 @@ class author extends db_table_vault { $authorids = array_keys($authorids); return $this->get_from_ids($authorids); } + + /** + * Get the context ids for a set of author ids. The results are indexed + * by the author id. + * + * @param int[] $authorids The list of author ids to fetch. + * @return int[] Results indexed by author id. + */ + public function get_context_ids_for_author_ids(array $authorids) : array { + $db = $this->get_db(); + [$insql, $params] = $db->get_in_or_equal($authorids); + $sql = "SELECT instanceid, id FROM {context} WHERE contextlevel = ? AND instanceid {$insql}"; + $records = $db->get_records_sql($sql, array_merge([CONTEXT_USER], $params)); + return array_reduce($authorids, function($carry, $id) use ($records) { + $carry[$id] = isset($records[$id]) ? (int) $records[$id]->id : null; + return $carry; + }, []); + } } diff --git a/mod/forum/tests/exporters_author_test.php b/mod/forum/tests/exporters_author_test.php index 5dc0baa383b..9942e7f26e1 100644 --- a/mod/forum/tests/exporters_author_test.php +++ b/mod/forum/tests/exporters_author_test.php @@ -60,7 +60,7 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { 'test@example.com' ); - $exporter = new author_exporter($author, [], true, [ + $exporter = new author_exporter($author, 1, [], true, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), 'context' => $context ]); @@ -100,7 +100,7 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { $group = $datagenerator->create_group(['courseid' => $course->id]); - $exporter = new author_exporter($author, [$group], true, [ + $exporter = new author_exporter($author, 1, [$group], true, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), 'context' => $context ]); @@ -137,7 +137,7 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { $group = $datagenerator->create_group(['courseid' => $course->id]); - $exporter = new author_exporter($author, [$group], false, [ + $exporter = new author_exporter($author, 1, [$group], false, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), 'context' => $context ]); diff --git a/mod/forum/tests/exporters_post_test.php b/mod/forum/tests/exporters_post_test.php index 561b0002808..daf675a3b5e 100644 --- a/mod/forum/tests/exporters_post_test.php +++ b/mod/forum/tests/exporters_post_test.php @@ -118,6 +118,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { $discussion = $entityfactory->get_discussion_from_stdclass($discussion); $post = $entityfactory->get_post_from_stdclass($post); $author = $entityfactory->get_author_from_stdclass($user); + $authorcontext = context_user::instance($author->get_id()); $exporter = new post_exporter($post, [ 'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(), @@ -127,6 +128,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { 'forum' => $forum, 'discussion' => $discussion, 'author' => $author, + 'authorcontextid' => $authorcontext->id, 'user' => $user, 'context' => $context, 'authorgroups' => [], @@ -246,6 +248,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { $discussion = $entityfactory->get_discussion_from_stdclass($discussion); $post = $entityfactory->get_post_from_stdclass($post); $author = $entityfactory->get_author_from_stdclass($user); + $authorcontext = context_user::instance($author->get_id()); $exporter = new post_exporter($post, [ 'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(), @@ -255,6 +258,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { 'forum' => $forum, 'discussion' => $discussion, 'author' => $author, + 'authorcontextid' => $authorcontext->id, 'user' => $user, 'context' => $context, 'authorgroups' => [], @@ -352,6 +356,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { $discussion = $entityfactory->get_discussion_from_stdclass($discussion); $post = $entityfactory->get_post_from_stdclass($post); $author = $entityfactory->get_author_from_stdclass($user); + $authorcontext = context_user::instance($author->get_id()); $exporter = new post_exporter($post, [ 'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(), @@ -361,6 +366,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase { 'forum' => $forum, 'discussion' => $discussion, 'author' => $author, + 'authorcontextid' => $authorcontext->id, 'user' => $user, 'context' => $context, 'authorgroups' => [], diff --git a/mod/forum/tests/vaults_author_test.php b/mod/forum/tests/vaults_author_test.php index 86d8b21a219..b35d0ea59c4 100644 --- a/mod/forum/tests/vaults_author_test.php +++ b/mod/forum/tests/vaults_author_test.php @@ -46,4 +46,38 @@ class mod_forum_vaults_author_testcase extends advanced_testcase { $this->assertEquals($user->id, $author->get_id()); } + + /** + * Test get_context_ids_for_author_ids. + */ + public function test_get_context_ids_for_author_ids() { + $this->resetAfterTest(); + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + $userid1 = $user1->id; + $userid2 = $user2->id; + $userid3 = $user3->id; + $fakeuserid = $user3->id + 1000; + $vaultfactory = \mod_forum\local\container::get_vault_factory(); + $authorvault = $vaultfactory->get_author_vault(); + $user1context = context_user::instance($user1->id); + $user2context = context_user::instance($user2->id); + $user3context = context_user::instance($user3->id); + $user1contextid = $user1context->id; + $user2contextid = $user2context->id; + $user3contextid = $user3context->id; + $fakeusercontextid = null; + $userids = [$userid1, $userid2, $userid3, $fakeuserid]; + + $expected = [ + $userid1 => $user1contextid, + $userid2 => $user2contextid, + $userid3 => $user3contextid, + $fakeuserid => $fakeusercontextid + ]; + + $this->assertEquals($expected, $authorvault->get_context_ids_for_author_ids($userids)); + } }