Skip to content

Commit

Permalink
MDL-61937 mod_data: generator for all field types
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed May 4, 2018
1 parent ed54f4e commit a7a6be9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
4 changes: 4 additions & 0 deletions mod/data/field/file/field.class.php
Expand Up @@ -156,6 +156,10 @@ function update_content($recordid, $value, $name='') {

// Should always be available since it is set by display_add_field before initializing the draft area.
$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
if (!$content) {
$content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
$content->id = $DB->insert_record('data_content', $content);
}

file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);

Expand Down
4 changes: 4 additions & 0 deletions mod/data/field/picture/field.class.php
Expand Up @@ -225,6 +225,10 @@ function update_content($recordid, $value, $name='') {

// Should always be available since it is set by display_add_field before initializing the draft area.
$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
if (!$content) {
$content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
$content->id = $DB->insert_record('data_content', $content);
}

$names = explode('_', $name);
switch ($names[2]) {
Expand Down
83 changes: 59 additions & 24 deletions mod/data/tests/generator/lib.php
Expand Up @@ -29,8 +29,6 @@
/**
* Data generator class for mod_data.
*
* Currently, the field types in the ignoredfieldtypes array aren't supported.
*
* @package mod_data
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
Expand All @@ -48,12 +46,6 @@ class mod_data_generator extends testing_module_generator {
*/
protected $databaserecordcount = 0;

/**
* @var The field types which not handled by the generator as of now.
*/
protected $ignoredfieldtypes = array('latlong', 'file', 'picture');


/**
* To be called from data reset code only,
* do not use in tests.
Expand Down Expand Up @@ -89,7 +81,6 @@ public function create_instance($record = null, array $options = null) {

/**
* Creates a field for a mod_data instance.
* Currently, the field types in the ignoredfieldtypes array aren't supported.
*
* @param StdClass $record
* @param mod_data $data
Expand All @@ -98,12 +89,6 @@ public function create_instance($record = null, array $options = null) {
public function create_field(stdClass $record = null, $data = null) {
$record = (array) $record;

if (in_array($record['type'], $this->ignoredfieldtypes)) {
throw new coding_exception('$record\'s type value must not be same as values in ignoredfieldtypes
in phpunit_util::create_field()');
return false;
}

$this->databasefieldcount++;

if (!isset($data->course)) {
Expand Down Expand Up @@ -143,6 +128,8 @@ public function create_field(stdClass $record = null, $data = null) {
$record['param1'] = implode("\n", array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4'));
} else if (($record['type'] === 'text') || ($record['type'] === 'url')) {
$record['param1'] = 1;
} else if ($record['type'] == 'latlong') {
$record['param1'] = 'Google Maps';
} else {
$record['param1'] = '';
}
Expand All @@ -152,6 +139,8 @@ public function create_field(stdClass $record = null, $data = null) {

if ($record['type'] === 'textarea') {
$record['param2'] = 60;
} else if ($record['type'] == 'latlong') {
$record['param2'] = -1;
} else {
$record['param2'] = '';
}
Expand All @@ -161,6 +150,8 @@ public function create_field(stdClass $record = null, $data = null) {

if (($record['type'] === 'textarea')) {
$record['param3'] = 35;
} else if ($record['type'] == 'picture' || $record['type'] == 'file') {
$record['param3'] = 0;
} else {
$record['param3'] = '';
}
Expand Down Expand Up @@ -193,7 +184,6 @@ public function create_field(stdClass $record = null, $data = null) {
* Creates a field for a mod_data instance.
* Keep in mind the default data field params created in create_field() function!
* ...if you haven't provided your own custom data field parameters there.
* Currently, the field types in the ignoredfieldtypes array aren't supported.
* The developers using the generator must adhere to the following format :
*
* Syntax : $contents[ fieldid ] = fieldvalue
Expand All @@ -206,16 +196,19 @@ public function create_field(stdClass $record = null, $data = null) {
* $contents['text'] = 'text'
* $contents['textarea'] = 'text'
* $contents['url'] = 'example.url' or array('example.url', 'urlname')
* $contents['latlong'] = array('value for lattitude', 'value for longitude')
* $contents['file'] = 'filename or draftitemid'
* $contents['picture'] = array('filename or draftitemid', 'alternative text')
*
* @param mod_data $data
* @param stdClass $data record from table {data}
* @param array $contents
* @param int $groupid
* @param array $tags
* @param array $options
* @return data_field_{type}
* @return int id of the generated record in table {data_records}
*/
public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null) {
global $DB;
global $DB, $USER, $CFG;

$this->databaserecordcount++;

Expand All @@ -233,10 +226,6 @@ public function create_entry($data, array $contents, $groupid = 0, $tags = [], a
foreach ($fields as $field) {
$fieldhascontent = true;

if (in_array($field->type, $this->ignoredfieldtypes)) {
continue;
}

$field = data_get_field($field, $data);

$fieldid = $field->field->id;
Expand Down Expand Up @@ -293,6 +282,52 @@ public function create_entry($data, array $contents, $groupid = 0, $tags = [], a
$fieldhascontent = false;
}

} else if ($field->type === 'latlong') {
$values = array();

foreach ($contents[$fieldid] as $key => $value) {
$values['field_' . $fieldid . '_' . $key] = $value;
}

$contents[$fieldid] = $values;
$fieldname = 'field_' . $fieldid . '_0';
if (!$field->notemptyfield($values[$fieldname], $fieldname)) {
$fieldhascontent = false;
}

} else if ($field->type === 'file' || $field->type === 'picture') {
if (is_array($contents[$fieldid])) {
list($itemid, $alttext) = $contents[$fieldid];
} else {
$itemid = $contents[$fieldid];
$alttext = '';
}

if (strlen($itemid) && !is_numeric($itemid)) {
// We expect draftarea item id here but it can also be a filename, in this case provider will generate file.
$filename = $itemid;
$usercontext = context_user::instance($USER->id);
$itemid = file_get_unused_draft_itemid();
get_file_storage()->create_file_from_string(['component' => 'user', 'filearea' => 'draft',
'contextid' => $usercontext->id, 'itemid' => $itemid, 'filepath' => '/',
'filename' => $filename],
file_get_contents($CFG->dirroot.'/mod/data/pix/icon.png'));
}

$fieldname = 'field_' . $fieldid . '_file';
if ($field->type === 'file') {
$contents[$fieldid] = $itemid;
} else {
$contents[$fieldid] = [
$fieldname => $itemid,
'field_' . $fieldid . '_alttext' => $alttext
];
}

if (!$field->notemptyfield($itemid, $fieldname)) {
$fieldhascontent = false;
}

} else {
if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) {
continue;
Expand All @@ -307,7 +342,7 @@ public function create_entry($data, array $contents, $groupid = 0, $tags = [], a
foreach ($contents as $fieldid => $content) {
$field = data_get_field_from_id($fieldid, $data);

if (is_array($content) and in_array($field->type, array('date', 'textarea', 'url'))) {
if (is_array($content) and in_array($field->type, array('date', 'textarea', 'url', 'picture', 'latlong'))) {

foreach ($content as $fieldname => $value) {
$field->update_content($recordid, $value, $fieldname);
Expand Down
7 changes: 6 additions & 1 deletion mod/data/tests/generator_test.php
Expand Up @@ -159,7 +159,8 @@ public function test_create_entry() {
$context = context_module::instance($cm->id);
$this->assertEquals($data->cmid, $context->instanceid);

$fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url');
$fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
'latlong', 'file', 'picture');

$count = 1;

Expand Down Expand Up @@ -192,6 +193,9 @@ public function test_create_entry() {
$contents[] = 'text for testing';
$contents[] = '<p>text area testing<br /></p>';
$contents[] = array('example.url', 'sampleurl');
$contents[] = [-31.9489873, 115.8382036]; // Latlong.
$contents[] = 'Filename.pdf'; // File - filename.
$contents[] = array('Cat1234.jpg', 'Cat'); // Picture - filename with alt text.
$count = 0;
$fieldcontents = array();
foreach ($fields as $fieldrecord) {
Expand All @@ -200,6 +204,7 @@ public function test_create_entry() {

$tags = ['Cats', 'mice'];

$this->setUser($user1);
$datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data,
$fieldcontents, $groupa->id, $tags);

Expand Down

0 comments on commit a7a6be9

Please sign in to comment.