[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/xmldb/actions/check_defaults/ -> check_defaults.class.php (source)

   1  <?php
   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/>.
  16  
  17  /**
  18   * @package    tool_xmldb
  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
  21   */
  22  
  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   *
  28   * @package    tool_xmldb
  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
  31   */
  32  class check_defaults extends XMLDBCheckAction {
  33  
  34      /**
  35       * Init method, every subclass will have its own
  36       */
  37      function init() {
  38          $this->introstr = 'confirmcheckdefaults';
  39          parent::init();
  40  
  41          // Set own core attributes
  42  
  43          // Set own custom attributes
  44  
  45          // Get needed strings
  46          $this->loadStrings(array(
  47              'wrongdefaults' => 'tool_xmldb',
  48              'nowrongdefaultsfound' => 'tool_xmldb',
  49              'yeswrongdefaultsfound' => 'tool_xmldb',
  50              'expected' => 'tool_xmldb',
  51              'actual' => 'tool_xmldb',
  52          ));
  53      }
  54  
  55      protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
  56          $o = '';
  57          $wrong_fields = array();
  58  
  59          // Get and process XMLDB fields
  60          if ($xmldb_fields = $xmldb_table->getFields()) {
  61              $o.='        <ul>';
  62              foreach ($xmldb_fields as $xmldb_field) {
  63  
  64                  // Get the default value for the field
  65                  $xmldbdefault = $xmldb_field->getDefault();
  66  
  67                  // If the metadata for that column doesn't exist or 'id' field found, skip
  68                  if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
  69                      continue;
  70                  }
  71  
  72                  // To variable for better handling
  73                  $metacolumn = $metacolumns[$xmldb_field->getName()];
  74  
  75                  // Going to check this field in DB
  76                  $o.='            <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
  77  
  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                  }
  85  
  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>";
  91                      // Add the wrong field to the list
  92                      $obj = new stdClass();
  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>';
 100                  }
 101                  $o.='</li>';
 102              }
 103              $o.='        </ul>';
 104          }
 105  
 106          return array($o, $wrong_fields);
 107      }
 108  
 109      protected function display_results(array $wrong_fields) {
 110          global $DB;
 111          $dbman = $DB->get_manager();
 112  
 113          $s = '';
 114          $r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
 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  
 121          // If we have found wrong defaults inform about them
 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>';
 138                  // Add to output if we have sentences
 139                  if ($sqlarr) {
 140                      $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
 141                      $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
 142                  }
 143              }
 144              $r.= '        </ul>';
 145              // Add the SQL statements (all together)
 146              $r.= '<hr />' . $s;
 147          } else {
 148              $r.= '    <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
 149          }
 150          $r.= '  </td></tr>';
 151          $r.= '  <tr><td class="generalboxcontent">';
 152          // Add the complete log message
 153          $r.= '    <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
 154          $r.= '  </td></tr>';
 155          $r.= '</table>';
 156  
 157          return $r;
 158      }
 159  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1