Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 * Data generator class for mod_data. 19 * 20 * @package mod_data 21 * @category test 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Data generator class for mod_data. 31 * 32 * @package mod_data 33 * @category test 34 * @copyright 2012 Petr Skoda {@link http://skodak.org} 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class mod_data_generator extends testing_module_generator { 38 39 /** 40 * @var int keep track of how many database fields have been created. 41 */ 42 protected $databasefieldcount = 0; 43 44 /** 45 * @var int keep track of how many database records have been created. 46 */ 47 protected $databaserecordcount = 0; 48 49 /** 50 * To be called from data reset code only, 51 * do not use in tests. 52 * @return void 53 */ 54 public function reset() { 55 $this->databasefieldcount = 0; 56 $this->databaserecordcount = 0; 57 58 parent::reset(); 59 } 60 61 /** 62 * Creates a mod_data instance 63 * 64 * @param array $record 65 * @param array $options 66 * @return StdClass 67 */ 68 public function create_instance($record = null, array $options = null) { 69 // Note, the parent class does not type $record to cast to array and then to object. 70 $record = (object) (array) $record; 71 72 if (!isset($record->assessed)) { 73 $record->assessed = 0; 74 } 75 if (!isset($record->scale)) { 76 $record->scale = 0; 77 } 78 79 return parent::create_instance((array) $record, $options); 80 } 81 82 /** 83 * Creates a field for a mod_data instance. 84 * 85 * @param StdClass $record 86 * @param mod_data $data 87 * @return data_field_{type} 88 */ 89 public function create_field(stdClass $record = null, $data = null) { 90 $record = (array) $record; 91 92 $this->databasefieldcount++; 93 94 if (!isset($data->course)) { 95 throw new coding_exception('course must be present in phpunit_util::create_field() $data'); 96 } 97 98 if (!isset($data->id)) { 99 throw new coding_exception('dataid must be present in phpunit_util::create_field() $data'); 100 } else { 101 $record['dataid'] = $data->id; 102 } 103 104 if (!isset($record['type'])) { 105 throw new coding_exception('type must be present in phpunit_util::create_field() $record'); 106 } 107 108 if (!isset($record['required'])) { 109 $record['required'] = 0; 110 } 111 112 if (!isset($record['name'])) { 113 $record['name'] = "testField - " . $this->databasefieldcount; 114 } 115 116 if (!isset($record['description'])) { 117 $record['description'] = " This is testField - " . $this->databasefieldcount; 118 } 119 120 if (!isset($record['param1'])) { 121 if ($record['type'] == 'checkbox') { 122 $record['param1'] = implode("\n", array('opt1', 'opt2', 'opt3', 'opt4')); 123 } else if ($record['type'] == 'radiobutton') { 124 $record['param1'] = implode("\n", array('radioopt1', 'radioopt2', 'radioopt3', 'radioopt4')); 125 } else if ($record['type'] == 'menu') { 126 $record['param1'] = implode("\n", array('menu1', 'menu2', 'menu3', 'menu4')); 127 } else if ($record['type'] == 'multimenu') { 128 $record['param1'] = implode("\n", array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4')); 129 } else if (($record['type'] === 'text') || ($record['type'] === 'url')) { 130 $record['param1'] = 1; 131 } else if ($record['type'] == 'latlong') { 132 $record['param1'] = 'Google Maps'; 133 } else { 134 $record['param1'] = ''; 135 } 136 } 137 138 if (!isset($record['param2'])) { 139 140 if ($record['type'] === 'textarea') { 141 $record['param2'] = 60; 142 } else if ($record['type'] == 'latlong') { 143 $record['param2'] = -1; 144 } else { 145 $record['param2'] = ''; 146 } 147 } 148 149 if (!isset($record['param3'])) { 150 151 if (($record['type'] === 'textarea')) { 152 $record['param3'] = 35; 153 } else if ($record['type'] == 'picture' || $record['type'] == 'file') { 154 $record['param3'] = 0; 155 } else { 156 $record['param3'] = ''; 157 } 158 } 159 160 if (!isset($record['param4'])) { 161 162 if (($record['type'] === 'textarea')) { 163 $record['param4'] = 1; 164 } 165 } 166 167 if (!isset($record['param5'])) { 168 if (($record['type'] === 'textarea')) { 169 $record['param5'] = 0; 170 } 171 } 172 173 $record = (object) $record; 174 175 $field = data_get_field($record, $data); 176 $field->insert_field(); 177 178 data_generate_default_template($data, 'addtemplate', 0, false, true); 179 180 return $field; 181 } 182 183 /** 184 * Creates a field for a mod_data instance. 185 * Keep in mind the default data field params created in create_field() function! 186 * ...if you haven't provided your own custom data field parameters there. 187 * The developers using the generator must adhere to the following format : 188 * 189 * Syntax : $contents[ fieldid ] = fieldvalue 190 * $contents['checkbox'] = array('val1', 'val2', 'val3' .....) 191 * $contents['data'] = 'dd-mm-yyyy' 192 * $contents['menu'] = 'value'; 193 * $contents['multimenu'] = array('val1', 'val2', 'val3' .....) 194 * $contents['number'] = 'numeric value' 195 * $contents['radiobuton'] = 'value' 196 * $contents['text'] = 'text' 197 * $contents['textarea'] = 'text' 198 * $contents['url'] = 'example.url' or array('example.url', 'urlname') 199 * $contents['latlong'] = array('value for lattitude', 'value for longitude') 200 * $contents['file'] = 'filename or draftitemid' 201 * $contents['picture'] = array('filename or draftitemid', 'alternative text') 202 * 203 * @param stdClass $data record from table {data} 204 * @param array $contents 205 * @param int $groupid 206 * @param array $tags 207 * @param array $options 208 * @return int id of the generated record in table {data_records} 209 */ 210 public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null) { 211 global $DB, $USER, $CFG; 212 213 $this->databaserecordcount++; 214 215 $recordid = data_add_record($data, $groupid); 216 217 if (isset($options['approved'])) { 218 data_approve_entry($recordid, !empty($options['approved'])); 219 } else { 220 $approved = null; 221 } 222 223 $fields = $DB->get_records('data_fields', array('dataid' => $data->id)); 224 225 // Validating whether required field are filled. 226 foreach ($fields as $field) { 227 $fieldhascontent = true; 228 229 $field = data_get_field($field, $data); 230 231 $fieldid = $field->field->id; 232 233 if ($field->type === 'date') { 234 $values = array(); 235 236 $temp = explode('-', $contents[$fieldid], 3); 237 238 $values['field_' . $fieldid . '_day'] = (int)trim($temp[0]); 239 $values['field_' . $fieldid . '_month'] = (int)trim($temp[1]); 240 $values['field_' . $fieldid . '_year'] = (int)trim($temp[2]); 241 242 // Year should be less than 2038, so it can be handled by 32 bit windows. 243 if ($values['field_' . $fieldid . '_year'] > 2038) { 244 throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond ' . 245 '2038. Please use year less than 2038.'); 246 } 247 248 $contents[$fieldid] = $values; 249 250 foreach ($values as $fieldname => $value) { 251 if (!$field->notemptyfield($value, $fieldname)) { 252 $fieldhascontent = false; 253 } 254 } 255 } else if ($field->type === 'textarea') { 256 $values = array(); 257 258 $values['field_' . $fieldid] = $contents[$fieldid]; 259 $values['field_' . $fieldid . '_content1'] = 1; 260 261 $contents[$fieldid] = $values; 262 263 $fieldname = 'field_' . $fieldid; 264 if (!$field->notemptyfield($values[$fieldname], $fieldname)) { 265 $fieldhascontent = false; 266 } 267 268 } else if ($field->type === 'url') { 269 $values = array(); 270 271 if (is_array($contents[$fieldid])) { 272 foreach ($contents[$fieldid] as $key => $value) { 273 $values['field_' . $fieldid . '_' . $key] = $value; 274 } 275 } else { 276 $values['field_' . $fieldid . '_0'] = $contents[$fieldid]; 277 } 278 279 $contents[$fieldid] = $values; 280 $fieldname = 'field_' . $fieldid . '_0'; 281 if (!$field->notemptyfield($values[$fieldname], $fieldname)) { 282 $fieldhascontent = false; 283 } 284 285 } else if ($field->type === 'latlong') { 286 $values = array(); 287 288 foreach ($contents[$fieldid] as $key => $value) { 289 $values['field_' . $fieldid . '_' . $key] = $value; 290 } 291 292 $contents[$fieldid] = $values; 293 $fieldname = 'field_' . $fieldid . '_0'; 294 if (!$field->notemptyfield($values[$fieldname], $fieldname)) { 295 $fieldhascontent = false; 296 } 297 298 } else if ($field->type === 'file' || $field->type === 'picture') { 299 if (is_array($contents[$fieldid])) { 300 list($itemid, $alttext) = $contents[$fieldid]; 301 } else { 302 $itemid = $contents[$fieldid]; 303 $alttext = ''; 304 } 305 306 if (strlen($itemid) && !is_numeric($itemid)) { 307 // We expect draftarea item id here but it can also be a filename, in this case provider will generate file. 308 $filename = $itemid; 309 $usercontext = context_user::instance($USER->id); 310 $itemid = file_get_unused_draft_itemid(); 311 get_file_storage()->create_file_from_string(['component' => 'user', 'filearea' => 'draft', 312 'contextid' => $usercontext->id, 'itemid' => $itemid, 'filepath' => '/', 313 'filename' => $filename], 314 file_get_contents($CFG->dirroot.'/mod/data/pix/icon.png')); 315 } 316 317 $fieldname = 'field_' . $fieldid . '_file'; 318 if ($field->type === 'file') { 319 $contents[$fieldid] = $itemid; 320 } else { 321 $contents[$fieldid] = [ 322 $fieldname => $itemid, 323 'field_' . $fieldid . '_alttext' => $alttext 324 ]; 325 } 326 327 if (!$field->notemptyfield($itemid, $fieldname)) { 328 $fieldhascontent = false; 329 } 330 331 } else { 332 if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) { 333 continue; 334 } 335 } 336 337 if ($field->field->required && !$fieldhascontent) { 338 return false; 339 } 340 } 341 342 foreach ($contents as $fieldid => $content) { 343 $field = data_get_field_from_id($fieldid, $data); 344 345 if (is_array($content) and in_array($field->type, array('date', 'textarea', 'url', 'picture', 'latlong'))) { 346 347 foreach ($content as $fieldname => $value) { 348 $field->update_content($recordid, $value, $fieldname); 349 } 350 351 } else { 352 $field->update_content($recordid, $content); 353 } 354 } 355 356 if (!empty($tags)) { 357 $cm = get_coursemodule_from_instance('data', $data->id); 358 core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, 359 context_module::instance($cm->id), $tags); 360 } 361 362 return $recordid; 363 } 364 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body