|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00034 class check_defaults extends XMLDBCheckAction { 00035 00039 function init() { 00040 $this->introstr = 'confirmcheckdefaults'; 00041 parent::init(); 00042 00043 // Set own core attributes 00044 00045 // Set own custom attributes 00046 00047 // Get needed strings 00048 $this->loadStrings(array( 00049 'wrongdefaults' => 'tool_xmldb', 00050 'nowrongdefaultsfound' => 'tool_xmldb', 00051 'yeswrongdefaultsfound' => 'tool_xmldb', 00052 'expected' => 'tool_xmldb', 00053 'actual' => 'tool_xmldb', 00054 )); 00055 } 00056 00057 protected function check_table(xmldb_table $xmldb_table, array $metacolumns) { 00058 $o = ''; 00059 $wrong_fields = array(); 00060 00061 // Get and process XMLDB fields 00062 if ($xmldb_fields = $xmldb_table->getFields()) { 00063 $o.=' <ul>'; 00064 foreach ($xmldb_fields as $xmldb_field) { 00065 00066 // Get the default value for the field 00067 $xmldbdefault = $xmldb_field->getDefault(); 00068 00069 // If the metadata for that column doesn't exist or 'id' field found, skip 00070 if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') { 00071 continue; 00072 } 00073 00074 // To variable for better handling 00075 $metacolumn = $metacolumns[$xmldb_field->getName()]; 00076 00077 // Going to check this field in DB 00078 $o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' '; 00079 00080 // get the value of the physical default (or blank if there isn't one) 00081 if ($metacolumn->has_default==1) { 00082 $physicaldefault = $metacolumn->default_value; 00083 } 00084 else { 00085 $physicaldefault = ''; 00086 } 00087 00088 // there *is* a default and it's wrong 00089 if ($physicaldefault != $xmldbdefault) { 00090 $info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual']. 00091 " '$physicaldefault')"; 00092 $o.='<font color="red">' . $this->str['wrong'] . " $info</font>"; 00093 // Add the wrong field to the list 00094 $obj = new stdClass(); 00095 $obj->table = $xmldb_table; 00096 $obj->field = $xmldb_field; 00097 $obj->physicaldefault = $physicaldefault; 00098 $obj->xmldbdefault = $xmldbdefault; 00099 $wrong_fields[] = $obj; 00100 } else { 00101 $o.='<font color="green">' . $this->str['ok'] . '</font>'; 00102 } 00103 $o.='</li>'; 00104 } 00105 $o.=' </ul>'; 00106 } 00107 00108 return array($o, $wrong_fields); 00109 } 00110 00111 protected function display_results(array $wrong_fields) { 00112 global $DB; 00113 $dbman = $DB->get_manager(); 00114 00115 $s = ''; 00116 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">'; 00117 $r.= ' <tr><td class="generalboxcontent">'; 00118 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>'; 00119 $r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>'; 00120 $r.= ' </td></tr>'; 00121 $r.= ' <tr><td class="generalboxcontent">'; 00122 00123 // If we have found wrong defaults inform about them 00124 if (count($wrong_fields)) { 00125 $r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>'; 00126 $r.= ' <ul>'; 00127 foreach ($wrong_fields as $obj) { 00128 $xmldb_table = $obj->table; 00129 $xmldb_field = $obj->field; 00130 $physicaldefault = $obj->physicaldefault; 00131 $xmldbdefault = $obj->xmldbdefault; 00132 00133 // get the alter table command 00134 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field); 00135 00136 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . 00137 $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' . 00138 $this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' . 00139 $this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>'; 00140 // Add to output if we have sentences 00141 if ($sqlarr) { 00142 $sqlarr = $dbman->generator->getEndedStatements($sqlarr); 00143 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />'; 00144 } 00145 } 00146 $r.= ' </ul>'; 00147 // Add the SQL statements (all together) 00148 $r.= '<hr />' . $s; 00149 } else { 00150 $r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>'; 00151 } 00152 $r.= ' </td></tr>'; 00153 $r.= ' <tr><td class="generalboxcontent">'; 00154 // Add the complete log message 00155 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>'; 00156 $r.= ' </td></tr>'; 00157 $r.= '</table>'; 00158 00159 return $r; 00160 } 00161 }