1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Discussion data mapper.
19 *
20 * @package    mod_forum
21 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace mod_forum\local\data_mappers\legacy;
26
27defined('MOODLE_INTERNAL') || die();
28
29use mod_forum\local\entities\discussion as discussion_entity;
30use stdClass;
31
32/**
33 * Convert a discussion entity into an stdClass.
34 *
35 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
36 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class discussion {
39    /**
40     * Convert a list of discussion entities into stdClasses.
41     *
42     * @param discussion_entity[] $authors The authors to convert.
43     * @return stdClass[]
44     */
45    public function to_legacy_objects(array $discussions) : array {
46        return array_map(function(discussion_entity $discussion) {
47            return (object) [
48                'id' => $discussion->get_id(),
49                'course' => $discussion->get_course_id(),
50                'forum' => $discussion->get_forum_id(),
51                'name' => $discussion->get_name(),
52                'firstpost' => $discussion->get_first_post_id(),
53                'userid' => $discussion->get_user_id(),
54                'groupid' => $discussion->get_group_id(),
55                'assessed' => $discussion->is_assessed(),
56                'timemodified' => $discussion->get_time_modified(),
57                'usermodified' => $discussion->get_user_modified(),
58                'timestart' => $discussion->get_time_start(),
59                'timeend' => $discussion->get_time_end(),
60                'pinned' => $discussion->is_pinned(),
61                'timelocked' => $discussion->get_locked()
62            ];
63        }, $discussions);
64    }
65
66    /**
67     * Convert a discussion entity into an stdClass.
68     *
69     * @param discussion_entity $discussion The discussion to convert.
70     * @return stdClass
71     */
72    public function to_legacy_object(discussion_entity $discussion) : stdClass {
73        return $this->to_legacy_objects([$discussion])[0];
74    }
75}
76