Convert a list of discussion entities into stdClasses.

@param discussion_entity[] $authors The authors to convert.
@return stdClass[]
761253
public function to_legacy_objects(array $discussions) : array {
        return array_map(function(discussion_entity $discussion) {
            return (object) [
                'id' => $discussion->get_id(),
                'course' => $discussion->get_course_id(),
                'forum' => $discussion->get_forum_id(),
                'name' => $discussion->get_name(),
                'firstpost' => $discussion->get_first_post_id(),
                'userid' => $discussion->get_user_id(),
                'groupid' => $discussion->get_group_id(),
                'assessed' => $discussion->is_assessed(),
                'timemodified' => $discussion->get_time_modified(),
                'usermodified' => $discussion->get_user_modified(),
                'timestart' => $discussion->get_time_start(),
                'timeend' => $discussion->get_time_end(),
                'pinned' => $discussion->is_pinned(),
                'timelocked' => $discussion->get_locked()
            ];
        }, $discussions);
    }
701504
public function authors( $author, $post ) {
		$authors = array();
		$cauthors = get_coauthors($post->ID);
		foreach ( $cauthors as $author ) {
			$uid = $this->get_user_uid( $author );
			if ( $uid ) {
				$authors[] = new \Timber\User($uid);
			} else {
				$authors[] = new CoAuthorsPlusUser($author);
			}
		}
		return $authors;
	}
626777
private function fetchByIdentifier(array $directReads, SalesChannelContext $context): array
    {
        $entities = [];

        foreach ($directReads as $definitionClass => $ids) {
            $definition = $this->definitionRegistry->get($definitionClass);

            $repository = $this->getSalesChannelApiRepository($definition);

            if ($repository) {
                $entities[$definitionClass] = $repository->search(new Criteria($ids), $context);
            } else {
                $repository = $this->getApiRepository($definition);
                $entities[$definitionClass] = $repository->search(new Criteria($ids), $context->getContext());
            }
        }

        return $entities;
    }
766879
public function can_view_post(stdClass $user, discussion_entity $discussion, post_entity $post) : bool {
        if (!$this->can_view_post_shell($user, $post)) {
            return false;
        }

        $forum = $this->get_forum();
        $forumrecord = $this->get_forum_record();
        $discussionrecord = $this->get_discussion_record($discussion);
        $postrecord = $this->get_post_record($post);
        $coursemodule = $forum->get_course_module_record();
        return forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
    }
395000
public function update_discussion(discussion_entity $discussion) : ?discussion_entity {
        $discussionrecord = $this->get_legacy_factory()->to_legacy_object($discussion);
        if ($this->get_db()->update_record('forum_discussions', $discussionrecord)) {
            $records = $this->transform_db_records_to_entities([$discussionrecord]);

            return count($records) ? array_shift($records) : null;
        }

        return null;
    }
254996
private function replaceGroupUseStatements(array $nodes): array
    {
        foreach ($nodes as $node) {
            if (false === ($node instanceof Namespace_)) {
                continue;
            }

            /** @var Namespace_ $node */
            $statements = $node->stmts;

            $newStatements = [];

            foreach ($statements as $statement) {
                if ($statement instanceof GroupUse) {
                    $uses_ = $this->createUses_($statement);

                    array_splice($newStatements, count($newStatements), 0, $uses_);
                } else {
                    $newStatements[] = $statement;
                }
            }

            $node->stmts = $newStatements;
        }

        return $nodes;
    }
66687
protected function fill_discussion_cache($discussionids) {
        global $DB;

        if (empty($discussionids)) {
            $this->discussion = [];
        } else {

            $requiredfields = [
                    'id',
                    'groupid',
                    'firstpost',
                    'timestart',
                    'timeend',
                ];

            list($in, $params) = $DB->get_in_or_equal($discussionids);
            $this->discussions = $DB->get_records_select(
                    'forum_discussions', "id $in", $params, '', implode(', ', $requiredfields));
        }
    }
1331329
protected function wrapInEntities(array $entities)
    {
        $results = array();
        foreach ($entities as $entity) {
            $results[] = self::getEntity($entity);
        }

        return $results;
    }
459503
public function get_discussions_in_forum_vault() : discussion_list_vault {
        return new discussion_list_vault(
            $this->db,
            $this->entityfactory,
            $this->legacymapper->get_legacy_data_mapper_for_vault('discussion')
        );
    }
609692
protected function transform_db_records_to_entities(array $records) {
        $preprocessors = $this->get_preprocessors();
        $result = array_map(function($record) {
            return ['record' => $record];
        }, $records);

        $result = array_reduce(array_keys($preprocessors), function($carry, $preprocessor) use ($records, $preprocessors) {
            $step = $preprocessors[$preprocessor];
            $dependencies = $step->execute($records);

            foreach ($dependencies as $index => $dependency) {
                // Add the new dependency to the list.
                $carry[$index] = array_merge($carry[$index], [$preprocessor => $dependency]);
            }

            return $carry;
        }, $result);

        return $this->from_db_records($result);
    }
650388
public static function get_discussion_posts(int $discussionid, ?string $sortby, ?string $sortdirection) {
        global $USER;
        // Validate the parameter.
        $params = self::validate_parameters(self::get_discussion_posts_parameters(), [
                'discussionid' => $discussionid,
                'sortby' => $sortby,
                'sortdirection' => $sortdirection,
            ]);
        $warnings = [];

        $vaultfactory = mod_forum\local\container::get_vault_factory();

        $discussionvault = $vaultfactory->get_discussion_vault();
        $discussion = $discussionvault->get_from_id($params['discussionid']);

        $forumvault = $vaultfactory->get_forum_vault();
        $forum = $forumvault->get_from_id($discussion->get_forum_id());

        $sortby = $params['sortby'];
        $sortdirection = $params['sortdirection'];
        $sortallowedvalues = ['id', 'created', 'modified'];
        $directionallowedvalues = ['ASC', 'DESC'];

        if (!in_array(strtolower($sortby), $sortallowedvalues)) {
            throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' .
                'allowed values are: ' . implode(', ', $sortallowedvalues));
        }

        $sortdirection = strtoupper($sortdirection);
        if (!in_array($sortdirection, $directionallowedvalues)) {
            throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' .
                'allowed values are: ' . implode(',', $directionallowedvalues));
        }

        $managerfactory = mod_forum\local\container::get_manager_factory();
        $capabilitymanager = $managerfactory->get_capability_manager($forum);

        $postvault = $vaultfactory->get_post_vault();
        $posts = $postvault->get_from_discussion_id(
                $USER,
                $discussion->get_id(),
                $capabilitymanager->can_view_any_private_reply($USER),
                "{$sortby} {$sortdirection}"
            );

        $builderfactory = mod_forum\local\container::get_builder_factory();
        $postbuilder = $builderfactory->get_exported_posts_builder();

        $legacydatamapper = mod_forum\local\container::get_legacy_data_mapper_factory();

        return [
            'posts' => $postbuilder->build($USER, [$forum], [$discussion], $posts),
            'ratinginfo' => \core_rating\external\util::get_rating_info(
                $legacydatamapper->get_forum_data_mapper()->to_legacy_object($forum),
                $forum->get_context(),
                'mod_forum',
                'post',
                $legacydatamapper->get_post_data_mapper()->to_legacy_objects($posts)
            ),
            'warnings' => $warnings,
        ];
    }
1006050
public function can_edit_post(stdClass $user, discussion_entity $discussion, post_entity $post) : bool {
        global $CFG;

        $context = $this->get_context();
        $ownpost = $post->is_owned_by_user($user);
        $ineditingtime = $post->get_age() < $CFG->maxeditingtime;

        switch ($this->forum->get_type()) {
            case 'news':
                // Allow editing of news posts once the discussion has started.
                $ineditingtime = !$post->has_parent() && $discussion->has_started();
                break;
            case 'single':
                return $discussion->is_first_post($post) && has_capability('moodle/course:manageactivities', $context, $user);
        }

        return ($ownpost && $ineditingtime) || has_capability('mod/forum:editanypost', $context, $user);
    }
925495
protected function createAuthors(array $authors = null)
    {
        if (null !== $authors) {
            foreach ($authors as $index => $author) {
                $authors[$index] = $this->createAuthor(
                    new ObjectAccess($author)
                );
            }
        }

        return $authors;
    }
344219
public function can_post_in_discussion(stdClass $user, discussion_entity $discussion) : bool {
        $forum = $this->get_forum();
        $forumrecord = $this->get_forum_record();
        $discussionrecord = $this->get_discussion_record($discussion);
        $context = $this->get_context();
        $coursemodule = $forum->get_course_module_record();
        $course = $forum->get_course_record();

        return forum_user_can_post($forumrecord, $discussionrecord, $user, $coursemodule, $course, $context);
    }
992861
protected function prepare_data(array $postids) {
        global $DB;

        if (empty($postids)) {
            return;
        }

        list($in, $params) = $DB->get_in_or_equal(array_values($postids));
        $sql = "SELECT p.*, f.id AS forum, f.course
                  FROM {forum_posts} p
            INNER JOIN {forum_discussions} d ON d.id = p.discussion
            INNER JOIN {forum} f ON f.id = d.forum
                 WHERE p.id {$in}";

        $posts = $DB->get_recordset_sql($sql, $params);
        $discussionids = [];
        $forumids = [];
        $courseids = [];
        $userids = [];
        foreach ($posts as $post) {
            $discussionids[] = $post->discussion;
            $forumids[] = $post->forum;
            $courseids[] = $post->course;
            $userids[] = $post->userid;
            unset($post->forum);
            if (!isset($this->posts[$post->discussion])) {
                $this->posts[$post->discussion] = [];
            }
            $this->posts[$post->discussion][$post->id] = $post;
        }
        $posts->close();

        if (empty($discussionids)) {
            // All posts have been removed since the task was queued.
            return;
        }

        // Fetch all discussions.
        list($in, $params) = $DB->get_in_or_equal(array_values($discussionids));
        $this->discussions = $DB->get_records_select('forum_discussions', "id {$in}", $params);
        foreach ($this->discussions as $discussion) {
            if (empty($this->forumdiscussions[$discussion->forum])) {
                $this->forumdiscussions[$discussion->forum] = [];
            }
            $this->forumdiscussions[$discussion->forum][] = $discussion->id;
        }

        // Fetch all forums.
        list($in, $params) = $DB->get_in_or_equal(array_values($forumids));
        $this->forums = $DB->get_records_select('forum', "id {$in}", $params);
        foreach ($this->forums as $forum) {
            if (empty($this->courseforums[$forum->course])) {
                $this->courseforums[$forum->course] = [];
            }
            $this->courseforums[$forum->course][] = $forum->id;
        }

        // Fetch all courses.
        list($in, $params) = $DB->get_in_or_equal(array_values($courseids));
        $this->courses = $DB->get_records_select('course', "id $in", $params);

        // Fetch all authors.
        list($in, $params) = $DB->get_in_or_equal(array_values($userids));
        $users = $DB->get_recordset_select('user', "id $in", $params);
        foreach ($users as $user) {
            $this->minimise_user_record($user);
            $this->users[$user->id] = $user;
        }
        $users->close();

        // Fill subscription caches for each forum.
        // These are per-user.
        foreach (array_values($forumids) as $id) {
            \mod_forum\subscriptions::fill_subscription_cache($id);
            \mod_forum\subscriptions::fill_discussion_subscription_cache($id);
        }
    }
822225
public function getPublishedDate($dates)
    {
        $defaults = [
            "revision" => [],
            "published" => null,
            "updated" => null,
            "created" => null,
        ];
        $dates = array_merge($defaults, $dates);
        
        if ($dates["revision"]) {
            return [t("Latest revision"), key($dates["revision"])];
        } elseif ($dates["published"]) {
            return [t("Published"), $dates["published"]];
        } elseif ($dates["updated"]) {
            return [t("Updated"), $dates["updated"]];
        } elseif ($dates["created"]) {
            return [t("Created"), $dates["created"]];
        }

        return [t("Missing pubdate."), null];
    }
149756
protected function from_db_records(array $results) {
        $entityfactory = $this->get_entity_factory();

        return array_map(function(array $result) use ($entityfactory) {
            [
                'discussion' => $discussion,
                'firstpost' => $firstpost,
                'firstpostauthor' => $firstpostauthor,
                'latestpostauthor' => $latestpostauthor,
            ] = $result;
            return $entityfactory->get_discussion_summary_from_stdclass(
                $discussion,
                $firstpost,
                $firstpostauthor,
                $latestpostauthor
            );
        }, $results);
    }