Skip to content

Commit

Permalink
MDL-57455 mod_data: Import and export tags
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhancox authored and mdjnelson committed Oct 12, 2017
1 parent d45c7a6 commit 04f7b07
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion mod/data/export.php
Expand Up @@ -32,6 +32,7 @@
$exportuser = optional_param('exportuser', false, PARAM_BOOL); // Flag for exporting user details
$exporttime = optional_param('exporttime', false, PARAM_BOOL); // Flag for exporting date/time information
$exportapproval = optional_param('exportapproval', false, PARAM_BOOL); // Flag for exporting user details
$tags = optional_param('exporttags', false, PARAM_BOOL); // Flag for exporting user details.

$PAGE->set_url('/mod/data/export.php', array('d'=>$d));

Expand Down Expand Up @@ -111,7 +112,7 @@
$currentgroup = groups_get_activity_group($cm);

$exportdata = data_get_exportdata($data->id, $fields, $selectedfields, $currentgroup, $context,
$exportuser, $exporttime, $exportapproval);
$exportuser, $exporttime, $exportapproval, $tags);
$count = count($exportdata);
switch ($formdata['exporttype']) {
case 'csv':
Expand Down
5 changes: 5 additions & 0 deletions mod/data/export_form.php
Expand Up @@ -85,6 +85,11 @@ function definition() {
if ($this->_data->approval) {
$mform->addElement('checkbox', 'exportapproval', get_string('includeapproval', 'data'));
}

if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
$mform->addElement('checkbox', 'exporttags', get_string('includetags', 'data'));
}

$this->add_action_buttons(true, get_string('exportentries', 'data'));
}

Expand Down
17 changes: 16 additions & 1 deletion mod/data/import.php
Expand Up @@ -116,7 +116,7 @@
$errorfield = '';
$safetoskipfields = array(get_string('user'), get_string('username'), get_string('email'),
get_string('timeadded', 'data'), get_string('timemodified', 'data'),
get_string('approved', 'data'));
get_string('approved', 'data'), get_string('tags', 'data'));
foreach ($fieldnames as $name => $id) {
if (!isset($rawfields[$name])) {
if (!in_array($name, $safetoskipfields)) {
Expand Down Expand Up @@ -156,6 +156,21 @@
$DB->insert_record('data_content', $content);
}
}

if (core_tag_tag::is_enabled('mod_data', 'data_records') &&
isset($fieldnames[get_string('tags', 'data')])) {
$columnindex = $fieldnames[get_string('tags', 'data')];
$rawtags = $record[$columnindex];
$tags = explode(',', $rawtags);
foreach ($tags as $tag) {
$tag = trim($tag);
if (empty($tag)) {
continue;
}
core_tag_tag::add_item_tag('mod_data', 'data_records', $recordid, $context, $tag);
}
}

$recordsadded++;
print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
}
Expand Down
2 changes: 2 additions & 0 deletions mod/data/lang/en/data.php
Expand Up @@ -201,6 +201,7 @@
$string['importentries'] = 'Import entries';
$string['importsuccess'] = 'The preset has been successfully applied.';
$string['includeapproval'] = 'Include approval status';
$string['includetags'] = 'Include tags';
$string['includetime'] = 'Include time added/modified';
$string['includeuserdetails'] = 'Include user details';
$string['indicator:cognitivedepth'] = 'Database cognitive';
Expand Down Expand Up @@ -346,6 +347,7 @@
$string['subplugintype_datapreset'] = 'Preset';
$string['subplugintype_datapreset_plural'] = 'Presets';
$string['tagarea_data_records'] = 'Data records';
$string['tags'] = 'tags';
$string['teachersandstudents'] = '{$a->teachers} and {$a->students}';
$string['templates'] = 'Templates';
$string['templatesaved'] = 'Template saved';
Expand Down
10 changes: 9 additions & 1 deletion mod/data/lib.php
Expand Up @@ -3103,10 +3103,11 @@ function data_export_ods($export, $dataname, $count) {
* @param bool $userdetails whether to include the details of the record author
* @param bool $time whether to include time created/modified
* @param bool $approval whether to include approval status
* @param bool $tags whether to include tags
* @return array
*/
function data_get_exportdata($dataid, $fields, $selectedfields, $currentgroup=0, $context=null,
$userdetails=false, $time=false, $approval=false) {
$userdetails=false, $time=false, $approval=false, $tags = false) {
global $DB;

if (is_null($context)) {
Expand Down Expand Up @@ -3138,6 +3139,9 @@ function data_get_exportdata($dataid, $fields, $selectedfields, $currentgroup=0,
if ($approval) {
$exportdata[0][] = get_string('approved', 'data');
}
if ($tags) {
$exportdata[0][] = get_string('tags', 'data');
}

$datarecords = $DB->get_records('data_records', array('dataid'=>$dataid));
ksort($datarecords);
Expand Down Expand Up @@ -3173,6 +3177,10 @@ function data_get_exportdata($dataid, $fields, $selectedfields, $currentgroup=0,
if ($approval) { // Add approval status
$exportdata[$line][] = (int) $record->approved;
}
if ($tags) {
$itemtags = \core_tag_tag::get_item_tags_array('mod_data', 'data_records', $record->id);
$exportdata[$line][] = implode(', ', $itemtags);
}
}
$line++;
}
Expand Down

0 comments on commit 04f7b07

Please sign in to comment.