original_string: private function group_posts_by_discussion(array $forums, array $discussions, array $posts) : array {
          return array_reduce($posts, function($carry, $post) use ($forums, $discussions) {
              $discussionid = $post->get_discussion_id();
              if (!isset($discussions[$discussionid])) {
                  throw new moodle_exception('Unable to find discussion with id ' . $discussionid);
              }
  
              if (isset($carry[$discussionid])) {
                  $carry[$discussionid]['posts'][] = $post;
              } else {
                  $discussion = $discussions[$discussionid];
                  $forumid = $discussion->get_forum_id();
  
                  if (!isset($forums[$forumid])) {
                      throw new moodle_exception('Unable to find forum with id ' . $forumid);
                  }
  
                  $carry[$discussionid] = [
                      'forum' => $forums[$forumid],
                      'discussion' => $discussions[$discussionid],
                      'posts' => [$post]
                  ];
              }
  
              return $carry;
          }, []);
      }
code: private function group_posts_by_discussion(array $forums, array $discussions, array $posts) : array {
          return array_reduce($posts, function($carry, $post) use ($forums, $discussions) {
              $discussionid = $post->get_discussion_id();
              if (!isset($discussions[$discussionid])) {
                  throw new moodle_exception('Unable to find discussion with id ' . $discussionid);
              }
  
              if (isset($carry[$discussionid])) {
                  $carry[$discussionid]['posts'][] = $post;
              } else {
                  $discussion = $discussions[$discussionid];
                  $forumid = $discussion->get_forum_id();
  
                  if (!isset($forums[$forumid])) {
                      throw new moodle_exception('Unable to find forum with id ' . $forumid);
                  }
  
                  $carry[$discussionid] = [
                      'forum' => $forums[$forumid],
                      'discussion' => $discussions[$discussionid],
                      'posts' => [$post]
                  ];
              }
  
              return $carry;
          }, []);
      }
code_tokens: private function group_posts_by_discussion ( array $ forums , array $ discussions , array $ posts ) : array { return array_reduce ( $ posts , function ( $ carry , $ post ) use ( $ forums , $ discussions ) { $ discussionid = $ post -> get_discussion_id ( ) ; if ( ! isset ( $ discussions [ $ discussionid ] ) ) { throw new moodle_exception ( 'Unable to find discussion with id ' . $ discussionid ) ; } if ( isset ( $ carry [ $ discussionid ] ) ) { $ carry [ $ discussionid ] [ 'posts' ] [ ] = $ post ; } else { $ discussion = $ discussions [ $ discussionid ] ; $ forumid = $ discussion -> get_forum_id ( ) ; if ( ! isset ( $ forums [ $ forumid ] ) ) { throw new moodle_exception ( 'Unable to find forum with id ' . $ forumid ) ; } $ carry [ $ discussionid ] = [ 'forum' => $ forums [ $ forumid ] , 'discussion' => $ discussions [ $ discussionid ] , 'posts' => [ $ post ] ] ; } return $ carry ; } , [ ] ) ; }
docstring: Group the posts by which discussion they belong to in order for them to be processed
  in chunks by the exporting.
  
  Returns a list of groups where each group has a forum, discussion, and list of posts.
  E.g.
  [
  [
  'forum' => <forum_entity>,
  'discussion' => <discussion_entity>,
  'posts' => [
  <post_entity in discussion>,
  <post_entity in discussion>,
  <post_entity in discussion>
  ]
  ]
  ]
  
  @param forum_entity[] $forums A list of all forums that each of the $discussions belong to, indexed by id.
  @param discussion_entity[] $discussions A list of all discussions that each of the $posts belong to, indexed by id.
  @param post_entity[] $posts The list of posts to process.
  @return array List of grouped posts. Each group has a discussion, forum, and posts.
docstring_tokens: Group the posts by which discussion they belong to in order for them to be processed in chunks by the exporting .