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
 * Customfield data generator.
19
 *
20
 * @package    core_customfield
21
 * @category   test
22
 * @copyright  2018 Ruslan Kabalin
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use \core_customfield\category_controller;
29
use \core_customfield\field_controller;
30
use \core_customfield\api;
31
 
32
/**
33
 * Customfield data generator class.
34
 *
35
 * @package    core_customfield
36
 * @category   test
37
 * @copyright  2018 Ruslan Kabalin
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class core_customfield_generator extends component_generator_base {
41
 
42
    /** @var int Number of created categories. */
43
    protected $categorycount = 0;
44
 
45
    /** @var int Number of created fields. */
46
    protected $fieldcount = 0;
47
 
48
    /**
49
     * Create a new category.
50
     *
51
     * @param array|stdClass $record
52
     * @return category_controller
53
     */
54
    public function create_category($record = null) {
55
        $this->categorycount++;
56
        $i = $this->categorycount;
57
        $record = (object) $record;
58
 
59
        if (!isset($record->name)) {
60
            $record->name = "Category $i";
61
        }
62
        if (!isset($record->component)) {
63
            $record->component = 'core_course';
64
        }
65
        if (!isset($record->area)) {
66
            $record->area = 'course';
67
        }
68
        if (!isset($record->itemid)) {
69
            $record->itemid = 0;
70
        }
71
 
72
        $handler = \core_customfield\handler::get_handler($record->component, $record->area, $record->itemid);
73
        $categoryid = $handler->create_category($record->name);
74
        return $handler->get_categories_with_fields()[$categoryid];
75
    }
76
 
77
    /**
78
     * Create a new field.
79
     *
80
     * @param array|stdClass $record
81
     * @return field_controller
82
     */
83
    public function create_field($record): field_controller {
84
        $this->fieldcount++;
85
        $i = $this->fieldcount;
86
        $record = (object) $record;
87
 
88
        if (empty($record->categoryid)) {
89
            throw new coding_exception('The categoryid value is required.');
90
        }
91
        $category = category_controller::create($record->categoryid);
92
        $handler = $category->get_handler();
93
 
94
        if (!isset($record->name)) {
95
            $record->name = "Field $i";
96
        }
97
        if (!isset($record->shortname)) {
98
            $record->shortname = "fld$i";
99
        }
100
        if (!property_exists($record, 'description')) {
101
            $record->description = "Field $i description";
102
        }
103
        if (!isset($record->descriptionformat)) {
104
            $record->descriptionformat = FORMAT_HTML;
105
        }
106
        if (!isset($record->type)) {
107
            $record->type = 'text';
108
        }
109
        if (!isset($record->sortorder)) {
110
            $record->sortorder = 0;
111
        }
112
 
113
        if (empty($record->configdata)) {
114
            $configdata = [];
115
        } else if (is_array($record->configdata)) {
116
            $configdata = $record->configdata;
117
        } else {
118
            $configdata = @json_decode($record->configdata, true);
119
            $configdata = $configdata ?? [];
120
        }
121
        $configdata += [
122
            'required' => 0,
123
            'uniquevalues' => 0,
124
            'locked' => 0,
125
            'visibility' => 2,
126
            'defaultvalue' => '',
127
            'defaultvalueformat' => FORMAT_MOODLE,
128
            'displaysize' => 0,
129
            'maxlength' => 0,
130
            'ispassword' => 0,
131
            'link' => '',
132
            'linktarget' => '',
133
            'checkbydefault' => 0,
134
            'startyear' => 2000,
135
            'endyear' => 3000,
136
            'includetime' => 1,
137
        ];
138
        $record->configdata = json_encode($configdata);
139
 
140
        $field = field_controller::create(0, (object)['type' => $record->type], $category);
141
        $handler->save_field_configuration($field, $record);
142
        return $handler->get_categories_with_fields()[$field->get('categoryid')]->get_fields()[$field->get('id')];
143
    }
144
 
145
    /**
146
     * Adds instance data for one field
147
     *
148
     * @param field_controller $field
149
     * @param int $instanceid
150
     * @param mixed $value
151
     * @return \core_customfield\data_controller
152
     */
153
    public function add_instance_data(field_controller $field, int $instanceid, $value): \core_customfield\data_controller {
154
        $data = \core_customfield\data_controller::create(0, (object)['instanceid' => $instanceid], $field);
155
        $data->set('contextid', $data->get_context()->id);
156
 
157
        $rc = new ReflectionClass(get_class($data));
158
        $rcm = $rc->getMethod('get_form_element_name');
159
        $formelementname = $rcm->invokeArgs($data, []);
160
        $record = (object)[$formelementname => $value];
161
        $data->instance_form_save($record);
162
        return $data;
163
    }
164
}