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