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
 * Generator for the core_contentbank subsystem.
19
 *
20
 * @package    core_contentbank
21
 * @category   test
22
 * @copyright  2020 Sara Arjona <sara@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use core_contentbank\content;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
32
 
33
/**
34
 * Generator for the core_contentbank subsystem.
35
 *
36
 * @package    core_contentbank
37
 * @copyright  2020 Sara Arjona <sara@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class core_contentbank_generator extends \component_generator_base {
41
 
42
    /**
43
     * Populate contentbank database tables with relevant data to simulate the process of adding items to the content bank.
44
     *
45
     * @param string $contenttype Content bank plugin type to add. If none is defined, contenttype_testable is used.
46
     * @param int $itemstocreate Number of items to add to the content bank.
47
     * @param int $userid The user identifier creating the content.
48
     * @param context $context The context where the content will be created.
49
     * @param bool $convert2class Whether the class should return stdClass or plugin instance.
50
     * @param string $filepath The filepath of the file associated to the content to create.
51
     * @param string $contentname The name of the content that will be created.
52
     * @param int $visibility The visibility of the content that will be created.
53
     * @return array An array with all the records added to the content bank.
54
     */
55
    public function generate_contentbank_data(?string $contenttype, int $itemstocreate = 1, int $userid = 0,
56
            ?\context $context = null, bool $convert2class = true, string $filepath = 'contentfile.h5p',
57
            string $contentname = 'Test content ', int $visibility = content::VISIBILITY_PUBLIC): array {
58
        global $DB, $USER;
59
 
60
        $records = [];
61
 
62
        $contenttype = $contenttype ?? 'contenttype_testable';
63
        $contenttypeclass = "\\$contenttype\\contenttype";
64
        if (!class_exists($contenttypeclass)) {
65
            // Early return with empty array because the contenttype doesn't exist.
66
            return $records;
67
        }
68
        if (empty($context)) {
69
            $context = \context_system::instance();
70
        }
71
        $type = new $contenttypeclass($context);
72
        $fs = get_file_storage();
73
        for ($i = 0; $i < $itemstocreate; $i++) {
74
            // Create content.
75
            $record = new stdClass();
76
            // If only 1 item is being created, do not add a number suffix to the content name.
77
            $record->name = ($itemstocreate === 1) ? $contentname : $contentname . $i;
78
            $record->configdata = '';
79
            $record->usercreated = $userid ?? $USER->id;
80
            $record->visibility = $visibility;
81
 
82
            $content = $type->create_content($record);
83
            $record = $content->get_content();
84
 
85
            // Create a dummy file.
86
            $filerecord = array(
87
                'contextid' => $context->id,
88
                'component' => 'contentbank',
89
                'filearea' => 'public',
90
                'itemid' => $record->id,
91
                'filepath' => '/',
92
                'filename' => basename($filepath)
93
            );
94
            if (file_exists($filepath)) {
95
                $fs->create_file_from_pathname($filerecord, $filepath);
96
            } else {
97
                $fs->create_file_from_string($filerecord, 'Dummy content ' . $i);
98
            }
99
 
100
            // Prepare the return value.
101
            if ($convert2class) {
102
                $records[$record->id] = $content;
103
            } else {
104
                $records[$record->id] = $record;
105
            }
106
        }
107
 
108
        return $records;
109
    }
110
}