3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden!');
6 require_once($CFG->libdir
. '/formslib.php');
7 require_once($CFG->libdir
. '/csvlib.class.php');
9 class mod_data_export_form
extends moodleform
{
10 var $_datafields = array();
14 // @param string $url: the url to post to
15 // @param array $datafields: objects in this database
16 public function __construct($url, $datafields, $cm, $data) {
17 $this->_datafields
= $datafields;
20 parent
::__construct($url);
24 * Old syntax of class constructor. Deprecated in PHP7.
26 * @deprecated since Moodle 3.1
28 public function mod_data_export_form($url, $datafields, $cm, $data) {
29 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
30 self
::__construct($url, $datafields, $cm, $data);
33 function definition() {
34 $mform =& $this->_form
;
35 $mform->addElement('header', 'exportformat', get_string('chooseexportformat', 'data'));
37 $optionattrs = ['class' => 'mt-1 mb-1'];
39 // Export format type radio group.
40 $typesarray = array();
41 $typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('csvwithselecteddelimiter', 'data'), 'csv',
43 // Temporarily commenting out Excel export option. See MDL-19864.
44 //$typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('excel', 'data'), 'xls');
45 $typesarray[] = $mform->createElement('radio', 'exporttype', null, get_string('ods', 'data'), 'ods', $optionattrs);
46 $mform->addGroup($typesarray, 'exportar', get_string('exportformat', 'data'), null, false);
47 $mform->addRule('exportar', null, 'required');
48 $mform->setDefault('exporttype', 'csv');
50 // CSV delimiter list.
51 $choices = csv_import_reader
::get_delimiter_list();
52 $key = array_search(';', $choices);
54 // Array $choices contains the semicolon -> drop it (because its encrypted form also contains a semicolon):
55 unset($choices[$key]);
57 $mform->addElement('select', 'delimiter_name', get_string('fielddelimiter', 'data'), $choices);
58 $mform->hideIf('delimiter_name', 'exporttype', 'neq', 'csv');
59 if (array_key_exists('cfg', $choices)) {
60 $mform->setDefault('delimiter_name', 'cfg');
61 } else if (get_string('listsep', 'langconfig') == ';') {
62 $mform->setDefault('delimiter_name', 'semicolon');
64 $mform->setDefault('delimiter_name', 'comma');
67 // Fields to be exported.
68 $mform->addElement('header', 'exportfieldsheader', get_string('chooseexportfields', 'data'));
69 $mform->setExpanded('exportfieldsheader');
70 $numfieldsthatcanbeselected = 0;
72 $unsupportedfields = [];
73 foreach ($this->_datafields
as $field) {
74 $label = get_string('fieldnametype', 'data', (object)['name' => $field->field
->name
, 'type' => $field->name()]);
75 if ($field->text_export_supported()) {
76 $numfieldsthatcanbeselected++
;
77 $exportfields[] = $mform->createElement('advcheckbox', 'field_' . $field->field
->id
, '', $label,
78 array_merge(['group' => 1], $optionattrs));
79 $mform->setDefault('field_' . $field->field
->id
, 1);
81 $unsupportedfields[] = $label;
84 $mform->addGroup($exportfields, 'exportfields', get_string('selectfields', 'data'), ['<br>'], false);
86 if ($numfieldsthatcanbeselected > 1) {
87 $this->add_checkbox_controller(1, null, null, 1);
90 // List fields that cannot be exported.
91 if (!empty($unsupportedfields)) {
92 $unsupportedfieldslist = html_writer
::tag('p', get_string('unsupportedfieldslist', 'data'), ['class' => 'mt-1']);
93 $unsupportedfieldslist .= html_writer
::alist($unsupportedfields);
94 $mform->addElement('static', 'unsupportedfields', get_string('unsupportedfields', 'data'), $unsupportedfieldslist);
98 $mform->addElement('header', 'exportoptionsheader', get_string('exportoptions', 'data'));
99 $mform->setExpanded('exportoptionsheader');
101 if (core_tag_tag
::is_enabled('mod_data', 'data_records')) {
102 $exportoptions[] = $mform->createElement('checkbox', 'exporttags', get_string('includetags', 'data'), '', $optionattrs);
103 $mform->setDefault('exporttags', 1);
105 $context = context_module
::instance($this->_cm
->id
);
106 if (has_capability('mod/data:exportuserinfo', $context)) {
107 $exportoptions[] = $mform->createElement('checkbox', 'exportuser', get_string('includeuserdetails', 'data'), '',
110 $exportoptions[] = $mform->createElement('checkbox', 'exporttime', get_string('includetime', 'data'), '', $optionattrs);
111 if ($this->_data
->approval
) {
112 $exportoptions[] = $mform->createElement('checkbox', 'exportapproval', get_string('includeapproval', 'data'), '',
115 $exportoptions[] = $mform->createElement('checkbox', 'includefiles', get_string('includefiles', 'data'), '', $optionattrs);
116 $mform->setDefault('includefiles', 1);
117 $mform->addGroup($exportoptions, 'exportoptions', get_string('selectexportoptions', 'data'), ['<br>'], false);
119 $this->add_action_buttons(true, get_string('exportentries', 'data'));