From 495b4b412dc77e2e2780010b964ff6628160d6d9 Mon Sep 17 00:00:00 2001 From: Michael Aherne Date: Tue, 15 Jan 2019 14:36:19 +0000 Subject: [PATCH] MDL-64587 xmldb: XMLDB option to add mandatory persistent fields. --- .../add_persistent_mandatory.class.php | 153 ++++++++++++++++++ .../actions/edit_table/edit_table.class.php | 10 ++ admin/tool/xmldb/lang/en/tool_xmldb.php | 4 + 3 files changed, 167 insertions(+) create mode 100644 admin/tool/xmldb/actions/add_persistent_mandatory/add_persistent_mandatory.class.php diff --git a/admin/tool/xmldb/actions/add_persistent_mandatory/add_persistent_mandatory.class.php b/admin/tool/xmldb/actions/add_persistent_mandatory/add_persistent_mandatory.class.php new file mode 100644 index 00000000000..05a819f4633 --- /dev/null +++ b/admin/tool/xmldb/actions/add_persistent_mandatory/add_persistent_mandatory.class.php @@ -0,0 +1,153 @@ +. + +/** + * @package tool_xmldb + * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Add the mandatory fields for persistent to the table. + * + * @package tool_xmldb + * @copyright 2019 Michael Aherne + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class add_persistent_mandatory extends XMLDBAction { + + function init() { + + parent::init(); + + // Get needed strings. + $this->loadStrings(array( + 'addpersistent' => 'tool_xmldb', + 'persistentfieldsconfirm' => 'tool_xmldb', + 'persistentfieldscomplete' => 'tool_xmldb', + 'persistentfieldsexist' => 'tool_xmldb', + 'continue' => 'core' + )); + + } + + function getTitle() { + return $this->str['addpersistent']; + } + + function invoke() { + + parent::invoke(); + + $this->does_generate = ACTION_GENERATE_HTML; + + global $CFG, $XMLDB, $OUTPUT; + + $dir = required_param('dir', PARAM_PATH); + $dirpath = $CFG->dirroot . $dir; + + if (empty($XMLDB->dbdirs)) { + return false; + } + + if (!empty($XMLDB->editeddirs)) { + $editeddir = $XMLDB->editeddirs[$dirpath]; + $structure = $editeddir->xml_file->getStructure(); + } + + $tableparam = required_param('table', PARAM_ALPHANUMEXT); + + /** @var xmldb_table $table */ + $table = $structure->getTable($tableparam); + + $result = true; + // Launch postaction if exists (leave this here!) + if ($this->getPostAction() && $result) { + return $this->launch($this->getPostAction()); + } + + $confirm = optional_param('confirm', false, PARAM_BOOL); + + $fields = ['usermodified', 'timecreated', 'timemodified']; + $existing = []; + foreach ($fields as $field) { + if ($table->getField($field)) { + $existing[] = $field; + } + } + + $returnurl = new \moodle_url('/admin/tool/xmldb/index.php', [ + 'table' => $tableparam, + 'dir' => $dir, + 'action' => 'edit_table' + ]); + + $continuebutton = $OUTPUT->single_button($returnurl, $this->str['continue']); + + if (!$confirm) { + + if (!empty($existing)) { + + $message = html_writer::span($this->str['persistentfieldsexist']); + $message .= html_writer::alist($existing); + $this->output .= $OUTPUT->notification($message); + + if (count($existing) == count($fields)) { + $this->output .= $continuebutton; + return true; + } + } + + $confirmurl = new \moodle_url('/admin/tool/xmldb/index.php', [ + 'table' => $tableparam, + 'dir' => $dir, + 'action' => 'add_persistent_mandatory', + 'sesskey' => sesskey(), + 'confirm' => '1' + ]); + + $message = html_writer::span($this->str['persistentfieldsconfirm']); + $message .= html_writer::alist(array_diff($fields, $existing)); + $this->output .= $OUTPUT->confirm($message, $confirmurl, $returnurl); + + } else { + + $fieldsadded = []; + foreach ($fields as $field) { + if (!in_array($field, $existing)) { + $fieldsadded[] = $field; + $table->add_field($field, XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, 0); + } + } + + if (!$table->getKey('usermodified')) { + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']); + } + + $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); + $structure->setChanged(true); + + $message = html_writer::span($this->str['persistentfieldscomplete']); + $message .= html_writer::alist(array_diff($fields, $existing)); + $this->output .= $OUTPUT->notification($message, 'success'); + + $this->output .= $continuebutton; + } + + return $result; + } + +} diff --git a/admin/tool/xmldb/actions/edit_table/edit_table.class.php b/admin/tool/xmldb/actions/edit_table/edit_table.class.php index 5cd4bcdc1a3..aa82f99d82b 100644 --- a/admin/tool/xmldb/actions/edit_table/edit_table.class.php +++ b/admin/tool/xmldb/actions/edit_table/edit_table.class.php @@ -44,6 +44,7 @@ class edit_table extends XMLDBAction { // Get needed strings $this->loadStrings(array( + 'addpersistent' => 'tool_xmldb', 'change' => 'tool_xmldb', 'vieworiginal' => 'tool_xmldb', 'viewedited' => 'tool_xmldb', @@ -177,6 +178,15 @@ class edit_table extends XMLDBAction { $b .= '[' .$this->str['viewsqlcode'] . ']'; // The view php code button $b .= ' [' . $this->str['viewphpcode'] . ']'; + // The add persistent fields button. + $url = new \moodle_url('/admin/tool/xmldb/index.php', [ + 'action' => 'add_persistent_mandatory', + 'sesskey' => sesskey(), + 'table' => $tableparam, + 'dir'=> str_replace($CFG->dirroot, '', $dirpath) + ]); + $b .= ' ' . \html_writer::link($url, '[' . $this->str['addpersistent'] . ']'); + // The save button (if possible) if ($cansavenow) { $b .= ' [' . $this->str['save'] . ']'; diff --git a/admin/tool/xmldb/lang/en/tool_xmldb.php b/admin/tool/xmldb/lang/en/tool_xmldb.php index d531a86bb55..d2efeabdea9 100644 --- a/admin/tool/xmldb/lang/en/tool_xmldb.php +++ b/admin/tool/xmldb/lang/en/tool_xmldb.php @@ -23,6 +23,7 @@ */ $string['actual'] = 'Actual'; +$string['addpersistent'] = 'Add mandatory persistent fields'; $string['aftertable'] = 'After table:'; $string['back'] = 'Back'; $string['backtomainview'] = 'Back to main'; @@ -169,6 +170,9 @@ $string['numberincorrectwholepart'] = 'Too big whole number part for number fiel $string['pendingchanges'] = 'Note: You have performed changes to this file. They can be saved at any moment.'; $string['pendingchangescannotbesaved'] = 'There are changes in this file but they cannot be saved! Please verify that both the directory and the "install.xml" within it have write permissions for the web server.'; $string['pendingchangescannotbesavedreload'] = 'There are changes in this file but they cannot be saved! Please verify that both the directory and the "install.xml" within it have write permissions for the web server. Then reload this page and you should be able to save those changes.'; +$string['persistentfieldsconfirm'] = 'Do you want to add the following fields: '; +$string['persistentfieldscomplete'] = 'The following fields have been added: '; +$string['persistentfieldsexist'] = 'The following fields already exist: '; $string['pluginname'] = 'XMLDB editor'; $string['primarykeyonlyallownotnullfields'] = 'Primary keys cannot be null'; $string['reserved'] = 'Reserved';