MDL-20849 - improved lang string, thanks, Helen. Merged from 19_STABLE
[moodle.git] / admin / xmldb / actions / check_defaults / check_defaults.class.php
CommitLineData
0df0df23 1<?php
dfac8649 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
46293bd7 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
dfac8649 11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
13// This program is free software; you can redistribute it and/or modify //
14// it under the terms of the GNU General Public License as published by //
15// the Free Software Foundation; either version 2 of the License, or //
16// (at your option) any later version. //
17// //
18// This program is distributed in the hope that it will be useful, //
19// but WITHOUT ANY WARRANTY; without even the implied warranty of //
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21// GNU General Public License for more details: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class will check all the default values existing in the DB
28/// match those specified in the xml specs
f33e1ed4 29/// and providing one SQL script to fix all them.
dfac8649 30
9f5e5dee 31class check_defaults extends XMLDBCheckAction {
dfac8649 32
33 /**
34 * Init method, every subclass will have its own
35 */
36 function init() {
9f5e5dee 37 $this->introstr = 'confirmcheckdefaults';
dfac8649 38 parent::init();
39
40 /// Set own core attributes
41
42 /// Set own custom attributes
43
44 /// Get needed strings
45 $this->loadStrings(array(
dfac8649 46 'wrongdefaults' => 'xmldb',
dfac8649 47 'nowrongdefaultsfound' => 'xmldb',
48 'yeswrongdefaultsfound' => 'xmldb',
ac1dc984 49 'expected' => 'xmldb',
50 'actual' => 'xmldb',
dfac8649 51 ));
52 }
53
9f5e5dee 54 protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
55 $o = '';
dfac8649 56 $wrong_fields = array();
57
9f5e5dee 58 /// Get and process XMLDB fields
59 if ($xmldb_fields = $xmldb_table->getFields()) {
60 $o.=' <ul>';
61 foreach ($xmldb_fields as $xmldb_field) {
dfac8649 62
9f5e5dee 63 // Get the default value for the field
64 $xmldbdefault = $xmldb_field->getDefault();
f33e1ed4 65
9f5e5dee 66 /// If the metadata for that column doesn't exist or 'id' field found, skip
67 if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
68 continue;
69 }
dfac8649 70
9f5e5dee 71 /// To variable for better handling
72 $metacolumn = $metacolumns[$xmldb_field->getName()];
dfac8649 73
9f5e5dee 74 /// Going to check this field in DB
75 $o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
f33e1ed4 76
9f5e5dee 77 // get the value of the physical default (or blank if there isn't one)
78 if ($metacolumn->has_default==1) {
79 $physicaldefault = $metacolumn->default_value;
80 }
81 else {
82 $physicaldefault = '';
83 }
f33e1ed4 84
9f5e5dee 85 // there *is* a default and it's wrong
86 if ($physicaldefault != $xmldbdefault) {
87 $info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
88 " '$physicaldefault')";
89 $o.='<font color="red">' . $this->str['wrong'] . " $info</font>";
90 /// Add the wrong field to the list
91 $obj = new object;
92 $obj->table = $xmldb_table;
93 $obj->field = $xmldb_field;
94 $obj->physicaldefault = $physicaldefault;
95 $obj->xmldbdefault = $xmldbdefault;
96 $wrong_fields[] = $obj;
97 } else {
98 $o.='<font color="green">' . $this->str['ok'] . '</font>';
dfac8649 99 }
9f5e5dee 100 $o.='</li>';
dfac8649 101 }
9f5e5dee 102 $o.=' </ul>';
103 }
dfac8649 104
9f5e5dee 105 return array($o, $wrong_fields);
106 }
f33e1ed4 107
9f5e5dee 108 protected function display_results(array $wrong_fields) {
109 global $DB;
110 $dbman = $DB->get_manager();
dfac8649 111
9f5e5dee 112 $s = '';
113 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
114 $r.= ' <tr><td class="generalboxcontent">';
115 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
116 $r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>';
117 $r.= ' </td></tr>';
118 $r.= ' <tr><td class="generalboxcontent">';
119
120 /// If we have found wrong defaults inform about them
121 if (count($wrong_fields)) {
122 $r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
123 $r.= ' <ul>';
124 foreach ($wrong_fields as $obj) {
125 $xmldb_table = $obj->table;
126 $xmldb_field = $obj->field;
127 $physicaldefault = $obj->physicaldefault;
128 $xmldbdefault = $obj->xmldbdefault;
129
130 // get the alter table command
131 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
132
133 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
134 $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
135 $this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' .
136 $this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>';
137 /// Add to output if we have sentences
138 if ($sqlarr) {
139 $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
140 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
dfac8649 141 }
dfac8649 142 }
9f5e5dee 143 $r.= ' </ul>';
144 /// Add the SQL statements (all together)
145 $r.= '<hr />' . $s;
146 } else {
147 $r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
dfac8649 148 }
9f5e5dee 149 $r.= ' </td></tr>';
150 $r.= ' <tr><td class="generalboxcontent">';
151 /// Add the complete log message
152 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
153 $r.= ' </td></tr>';
154 $r.= '</table>';
155
156 return $r;
dfac8649 157 }
158}