Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
19
 * Forum module data generator class
20
 *
21
 * @package    mod_forum
22
 * @category   test
23
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class mod_forum_generator extends testing_module_generator {
27
    /**
28
     * @var int keep track of how many forum discussions have been created.
29
     */
30
    protected $forumdiscussioncount = 0;
31
 
32
    /**
33
     * @var int keep track of how many forum posts have been created.
34
     */
35
    protected $forumpostcount = 0;
36
 
37
    /**
38
     * @var int keep track of how many forum subscriptions have been created.
39
     */
40
    protected $forumsubscriptionscount = 0;
41
 
42
    /**
43
     * Get the clock implementation to use when generating data.
44
     *
45
     * @return \core\clock
46
     */
47
    protected function get_clock(): \core\clock {
48
        return \core\di::get(\core\clock::class);
49
    }
50
 
51
    /**
52
     * To be called from data reset code only,
53
     * do not use in tests.
54
     * @return void
55
     */
56
    public function reset() {
57
        $this->forumdiscussioncount = 0;
58
        $this->forumpostcount = 0;
59
        $this->forumsubscriptionscount = 0;
60
 
61
        parent::reset();
62
    }
63
 
64
    public function create_instance($record = null, array $options = null) {
65
        global $CFG;
66
        require_once($CFG->dirroot.'/mod/forum/lib.php');
67
        $record = (object)(array)$record;
68
 
69
        if (!isset($record->type)) {
70
            $record->type = 'general';
71
        }
72
        if (!isset($record->assessed)) {
73
            $record->assessed = 0;
74
        }
75
        if (!isset($record->scale)) {
76
            $record->scale = 0;
77
        }
78
        if (!isset($record->forcesubscribe)) {
79
            $record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
80
        }
81
        if (!isset($record->grade_forum)) {
82
            $record->grade_forum = 0;
83
        }
84
 
85
        return parent::create_instance($record, (array)$options);
86
    }
87
 
88
    /**
89
     * Function to create a dummy subscription.
90
     *
91
     * @param array|stdClass $record
92
     * @return stdClass the subscription object
93
     */
94
    public function create_subscription($record = null) {
95
        global $DB;
96
 
97
        // Increment the forum subscription count.
98
        $this->forumsubscriptionscount++;
99
 
100
        $record = (array)$record;
101
 
102
        if (!isset($record['course'])) {
103
            throw new coding_exception('course must be present in phpunit_util::create_subscription() $record');
104
        }
105
 
106
        if (!isset($record['forum'])) {
107
            throw new coding_exception('forum must be present in phpunit_util::create_subscription() $record');
108
        }
109
 
110
        if (!isset($record['userid'])) {
111
            throw new coding_exception('userid must be present in phpunit_util::create_subscription() $record');
112
        }
113
 
114
        $record = (object)$record;
115
 
116
        // Add the subscription.
117
        $record->id = $DB->insert_record('forum_subscriptions', $record);
118
 
119
        return $record;
120
    }
121
 
122
    /**
123
     * Function to create a dummy discussion.
124
     *
125
     * @param array|stdClass $record
126
     * @return stdClass the discussion object
127
     */
128
    public function create_discussion($record = null) {
129
        global $DB;
130
 
131
        // Increment the forum discussion count.
132
        $this->forumdiscussioncount++;
133
 
134
        $record = (array) $record;
135
 
136
        if (!isset($record['course'])) {
137
            throw new coding_exception('course must be present in phpunit_util::create_discussion() $record');
138
        }
139
 
140
        if (!isset($record['forum'])) {
141
            throw new coding_exception('forum must be present in phpunit_util::create_discussion() $record');
142
        }
143
 
144
        if (!isset($record['userid'])) {
145
            throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record');
146
        }
147
 
148
        if (!isset($record['name'])) {
149
            $record['name'] = "Discussion " . $this->forumdiscussioncount;
150
        }
151
 
152
        if (!isset($record['subject'])) {
153
            $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
154
        }
155
 
156
        if (!isset($record['message'])) {
157
            $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
158
        }
159
 
160
        if (!isset($record['messageformat'])) {
161
            $record['messageformat'] = editors_get_preferred_format();
162
        }
163
 
164
        if (!isset($record['messagetrust'])) {
165
            $record['messagetrust'] = "";
166
        }
167
 
168
        if (!isset($record['assessed'])) {
169
            $record['assessed'] = '1';
170
        }
171
 
172
        if (!isset($record['groupid'])) {
173
            $record['groupid'] = "-1";
174
        }
175
 
176
        if (!isset($record['timestart'])) {
177
            $record['timestart'] = "0";
178
        }
179
 
180
        if (!isset($record['timeend'])) {
181
            $record['timeend'] = "0";
182
        }
183
 
184
        if (!isset($record['mailnow'])) {
185
            $record['mailnow'] = "0";
186
        }
187
 
188
        if (!isset($record['timecreated'])) {
189
            $record['timecreated'] = $this->get_clock()->now()->getTimestamp();
190
        }
191
 
192
        if (isset($record['timemodified'])) {
193
            $timemodified = $record['timemodified'];
194
        } else {
195
            $timemodified = $record['timecreated'];
196
        }
197
 
198
        if (!isset($record['pinned'])) {
199
            $record['pinned'] = FORUM_DISCUSSION_UNPINNED;
200
        }
201
 
202
        if (!isset($record['timelocked'])) {
203
            $record['timelocked'] = 0;
204
        }
205
 
206
        if (isset($record['mailed'])) {
207
            $mailed = $record['mailed'];
208
        }
209
 
210
        $record = (object) $record;
211
 
212
        // Add the discussion.
213
        $record->id = forum_add_discussion($record, null, null, $record->userid);
214
 
215
        $post = $DB->get_record('forum_posts', array('discussion' => $record->id));
216
 
217
        if (isset($timemodified) || isset($mailed)) {
218
            if (isset($mailed)) {
219
                $post->mailed = $mailed;
220
            }
221
 
222
            if (isset($timemodified)) {
223
                // Enforce the time modified.
224
                $record->timemodified = $timemodified;
225
                $post->modified = $post->created = $timemodified;
226
 
227
                $DB->update_record('forum_discussions', $record);
228
            }
229
 
230
            $DB->update_record('forum_posts', $post);
231
        }
232
 
233
        if (property_exists($record, 'tags')) {
234
            $cm = get_coursemodule_from_instance('forum', $record->forum);
235
            $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
236
 
237
            core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $post->id,
238
                context_module::instance($cm->id), $tags);
239
        }
240
 
241
        return $record;
242
    }
243
 
244
    /**
245
     * Function to create a dummy post.
246
     *
247
     * @param array|stdClass $record
248
     * @return stdClass the post object
249
     */
250
    public function create_post($record = null) {
251
        global $DB;
252
 
253
        // Increment the forum post count.
254
        $this->forumpostcount++;
255
 
256
        // Variable to store time.
257
        $time = time() + $this->forumpostcount;
258
 
259
        $record = (array) $record;
260
 
261
        if (!isset($record['discussion'])) {
262
            throw new coding_exception('discussion must be present in phpunit_util::create_post() $record');
263
        }
264
 
265
        if (!isset($record['userid'])) {
266
            throw new coding_exception('userid must be present in phpunit_util::create_post() $record');
267
        }
268
 
269
        if (!isset($record['parent'])) {
270
            $record['parent'] = 0;
271
        }
272
 
273
        if (!isset($record['subject'])) {
274
            $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
275
        }
276
 
277
        if (!isset($record['message'])) {
278
            $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
279
        }
280
 
281
        if (!isset($record['created'])) {
282
            // If we are using the system clock, then revert to the time + count approach.
283
            // Unfortunately a lot of Forum code relies on things not happening at the same time.
284
            // See MDL-80838 for more information on this issue.
285
 
286
            if ($this->get_clock() instanceof \core\system_clock) {
287
                $record['created'] = $time;
288
            } else {
289
                $record['created'] = $this->get_clock()->now()->getTimestamp();
290
            }
291
        }
292
 
293
        if (!isset($record['modified'])) {
294
            $record['modified'] = $record['created'];
295
        }
296
 
297
        if (!isset($record['mailed'])) {
298
            $record['mailed'] = 0;
299
        }
300
 
301
        if (!isset($record['messageformat'])) {
302
            $record['messageformat'] = 0;
303
        }
304
 
305
        if (!isset($record['messagetrust'])) {
306
            $record['messagetrust'] = 0;
307
        }
308
 
309
        if (!isset($record['attachment'])) {
310
            $record['attachment'] = "";
311
        }
312
 
313
        if (!isset($record['totalscore'])) {
314
            $record['totalscore'] = 0;
315
        }
316
 
317
        if (!isset($record['mailnow'])) {
318
            $record['mailnow'] = 0;
319
        }
320
 
321
        if (!isset($record['deleted'])) {
322
            $record['deleted'] = 0;
323
        }
324
 
325
        if (!isset($record['privatereplyto'])) {
326
            $record['privatereplyto'] = 0;
327
        }
328
 
329
        $record = (object) $record;
330
        \mod_forum\local\entities\post::add_message_counts($record);
331
 
332
        // Add the post.
333
        $record->id = $DB->insert_record('forum_posts', $record);
334
 
335
        if (property_exists($record, 'tags')) {
336
            $discussion = $DB->get_record('forum_discussions', ['id' => $record->discussion]);
337
            $cm = get_coursemodule_from_instance('forum', $discussion->forum);
338
            $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
339
 
340
            core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $record->id,
341
                context_module::instance($cm->id), $tags);
342
        }
343
 
344
        // Update the last post.
345
        forum_discussion_update_last_post($record->discussion);
346
 
347
        return $record;
348
    }
349
 
350
    public function create_content($instance, $record = array()) {
351
        global $USER, $DB;
352
        $record = (array)$record + array(
353
            'forum' => $instance->id,
354
            'userid' => $USER->id,
355
            'course' => $instance->course
356
        );
357
        if (empty($record['discussion']) && empty($record['parent'])) {
358
            // Create discussion.
359
            $discussion = $this->create_discussion($record);
360
            $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
361
        } else {
362
            // Create post.
363
            if (empty($record['parent'])) {
364
                $record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
365
            } else if (empty($record['discussion'])) {
366
                $record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
367
            }
368
            $post = $this->create_post($record);
369
        }
370
        return $post;
371
    }
372
 
373
    /**
374
     * Extracted from exporter/post.php
375
     *
376
     * Get the HTML to display as a subheading in a post.
377
     *
378
     * @param stdClass $exportedauthor The exported author object
379
     * @param int $timecreated The post time created timestamp if it's to be displayed
380
     * @return string
381
     */
382
    public function get_author_subheading_html(stdClass $exportedauthor, int $timecreated): string {
383
        $fullname = $exportedauthor->fullname;
384
        $profileurl = $exportedauthor->urls['profile'] ?? null;
385
        $name = $profileurl ? "<a href=\"{$profileurl}\">{$fullname}</a>" : $fullname;
386
        $date = userdate_htmltime($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
387
        return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
388
    }
389
}