MDL-59323 XMLDB: Code style fixes
[moodle.git] / admin / tool / xmldb / actions / check_defaults / check_defaults.class.php
CommitLineData
0df0df23 1<?php
f25d53a9
EL
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/>.
dfac8649 16
f25d53a9 17/**
85d6dd38 18 * @package tool_xmldb
b007a5d0
PS
19 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f25d53a9 21 */
dfac8649 22
94904cc3 23defined('MOODLE_INTERNAL') || die();
24
f25d53a9
EL
25/**
26 * This class will check all the default values existing in the DB
27 * match those specified in the xml specs
28 * and providing one SQL script to fix all them.
29 *
85d6dd38 30 * @package tool_xmldb
b007a5d0
PS
31 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f25d53a9 33 */
9f5e5dee 34class check_defaults extends XMLDBCheckAction {
dfac8649 35
36 /**
37 * Init method, every subclass will have its own
38 */
94904cc3 39 public function init() {
9f5e5dee 40 $this->introstr = 'confirmcheckdefaults';
dfac8649 41 parent::init();
42
94904cc3 43 // Set own core attributes.
dfac8649 44
94904cc3 45 // Set own custom attributes.
dfac8649 46
94904cc3 47 // Get needed strings.
dfac8649 48 $this->loadStrings(array(
b007a5d0
PS
49 'wrongdefaults' => 'tool_xmldb',
50 'nowrongdefaultsfound' => 'tool_xmldb',
51 'yeswrongdefaultsfound' => 'tool_xmldb',
52 'expected' => 'tool_xmldb',
53 'actual' => 'tool_xmldb',
dfac8649 54 ));
55 }
56
94904cc3 57 protected function check_table(xmldb_table $xmldbtable, array $metacolumns) {
9f5e5dee 58 $o = '';
94904cc3 59 $wrongfields = array();
dfac8649 60
94904cc3 61 // Get and process XMLDB fields.
62 if ($xmldbfields = $xmldbtable->getFields()) {
63 $o .= ' <ul>';
64 foreach ($xmldbfields as $xmldbfield) {
dfac8649 65
94904cc3 66 // Get the default value for the field.
67 $xmldbdefault = $xmldbfield->getDefault();
f33e1ed4 68
94904cc3 69 // If the metadata for that column doesn't exist or 'id' field found, skip.
70 if (!isset($metacolumns[$xmldbfield->getName()]) or $xmldbfield->getName() == 'id') {
9f5e5dee 71 continue;
72 }
dfac8649 73
94904cc3 74 // To variable for better handling.
75 $metacolumn = $metacolumns[$xmldbfield->getName()];
dfac8649 76
94904cc3 77 // Going to check this field in DB.
78 $o .= ' <li>' . $this->str['field'] . ': ' . $xmldbfield->getName() . ' ';
f33e1ed4 79
94904cc3 80 // Get the value of the physical default (or blank if there isn't one).
81 if ($metacolumn->has_default == 1) {
9f5e5dee 82 $physicaldefault = $metacolumn->default_value;
94904cc3 83 } else {
9f5e5dee 84 $physicaldefault = '';
85 }
f33e1ed4 86
94904cc3 87 // There *is* a default and it's wrong.
9f5e5dee 88 if ($physicaldefault != $xmldbdefault) {
89 $info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
90 " '$physicaldefault')";
94904cc3 91 $o .= '<font color="red">' . $this->str['wrong'] . " $info</font>";
92 // Add the wrong field to the list.
a226a972 93 $obj = new stdClass();
94904cc3 94 $obj->table = $xmldbtable;
95 $obj->field = $xmldbfield;
9f5e5dee 96 $obj->physicaldefault = $physicaldefault;
97 $obj->xmldbdefault = $xmldbdefault;
94904cc3 98 $wrongfields[] = $obj;
9f5e5dee 99 } else {
94904cc3 100 $o .= '<font color="green">' . $this->str['ok'] . '</font>';
dfac8649 101 }
94904cc3 102 $o .= '</li>';
dfac8649 103 }
94904cc3 104 $o .= ' </ul>';
9f5e5dee 105 }
dfac8649 106
94904cc3 107 return array($o, $wrongfields);
9f5e5dee 108 }
f33e1ed4 109
94904cc3 110 protected function display_results(array $wrongfields) {
9f5e5dee 111 global $DB;
112 $dbman = $DB->get_manager();
dfac8649 113
9f5e5dee 114 $s = '';
ac6efbaf 115 $r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
94904cc3 116 $r .= ' <tr><td class="generalboxcontent">';
117 $r .= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
118 $r .= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrongfields) . '</p>';
119 $r .= ' </td></tr>';
120 $r .= ' <tr><td class="generalboxcontent">';
121
122 // If we have found wrong defaults inform about them.
123 if (count($wrongfields)) {
124 $r .= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
125 $r .= ' <ul>';
126 foreach ($wrongfields as $obj) {
127 $xmldbtable = $obj->table;
128 $xmldbfield = $obj->field;
9f5e5dee 129 $physicaldefault = $obj->physicaldefault;
130 $xmldbdefault = $obj->xmldbdefault;
131
94904cc3 132 // Get the alter table command.
133 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldbtable, $xmldbfield);
9f5e5dee 134
94904cc3 135 $r .= ' <li>' . $this->str['table'] . ': ' . $xmldbtable->getName() . '. ' .
136 $this->str['field'] . ': ' . $xmldbfield->getName() . ', ' .
137 $this->str['expected'] . ' ' . $xmldbtext . ' ' .
138 $this->str['actual'] . ' ' . $physicaltext . '</li>';
139 // Add to output if we have sentences.
9f5e5dee 140 if ($sqlarr) {
141 $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
94904cc3 142 $s .= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
dfac8649 143 }
dfac8649 144 }
94904cc3 145 $r .= ' </ul>';
146 // Add the SQL statements (all together).
147 $r .= '<hr />' . $s;
9f5e5dee 148 } else {
94904cc3 149 $r .= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
dfac8649 150 }
94904cc3 151 $r .= ' </td></tr>';
152 $r .= ' <tr><td class="generalboxcontent">';
153 // Add the complete log message.
154 $r .= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
155 $r .= ' </td></tr>';
156 $r .= '</table>';
9f5e5dee 157
158 return $r;
dfac8649 159 }
160}