| 1 | efrain | 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 display the XML for one key being edited
 | 
        
           |  |  | 25 |  *
 | 
        
           |  |  | 26 |  * @package    tool_xmldb
 | 
        
           |  |  | 27 |  * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
 | 
        
           |  |  | 28 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 29 |  */
 | 
        
           |  |  | 30 | class view_key_xml extends XMLDBAction {
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      * Init method, every subclass will have its own
 | 
        
           |  |  | 34 |      */
 | 
        
           |  |  | 35 |     function init() {
 | 
        
           |  |  | 36 |         parent::init();
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |         // Set own custom attributes
 | 
        
           |  |  | 39 |         $this->sesskey_protected = false; // This action doesn't need sesskey protection
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |         // Get needed strings
 | 
        
           |  |  | 42 |         $this->loadStrings(array(
 | 
        
           |  |  | 43 |             // 'key' => 'module',
 | 
        
           |  |  | 44 |         ));
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /**
 | 
        
           |  |  | 48 |      * Invoke method, every class will have its own
 | 
        
           |  |  | 49 |      * returns true/false on completion, setting both
 | 
        
           |  |  | 50 |      * errormsg and output as necessary
 | 
        
           |  |  | 51 |      */
 | 
        
           |  |  | 52 |     function invoke() {
 | 
        
           |  |  | 53 |         parent::invoke();
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |         $result = true;
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |         // Set own core attributes
 | 
        
           |  |  | 58 |         $this->does_generate = ACTION_GENERATE_XML;
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         // These are always here
 | 
        
           |  |  | 61 |         global $CFG, $XMLDB;
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         // Do the job, setting result as needed
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         // Get the file parameter
 | 
        
           |  |  | 66 |         $key =  required_param('key', PARAM_PATH);
 | 
        
           |  |  | 67 |         $table =  required_param('table', PARAM_PATH);
 | 
        
           |  |  | 68 |         $select = required_param('select', PARAM_ALPHA); //original/edited
 | 
        
           |  |  | 69 |         // Get the dir containing the file
 | 
        
           |  |  | 70 |         $dirpath = required_param('dir', PARAM_PATH);
 | 
        
           |  |  | 71 |         $dirpath = $CFG->dirroot . $dirpath;
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |         // Get the correct dir
 | 
        
           |  |  | 74 |         if ($select == 'original') {
 | 
        
           |  |  | 75 |             if (!empty($XMLDB->dbdirs)) {
 | 
        
           |  |  | 76 |                 $base = $XMLDB->dbdirs[$dirpath];
 | 
        
           |  |  | 77 |             }
 | 
        
           |  |  | 78 |         } else if ($select == 'edited') {
 | 
        
           |  |  | 79 |             if (!empty($XMLDB->editeddirs)) {
 | 
        
           |  |  | 80 |                 $base = $XMLDB->editeddirs[$dirpath];
 | 
        
           |  |  | 81 |             }
 | 
        
           |  |  | 82 |         } else {
 | 
        
           |  |  | 83 |             $this->errormsg = 'Cannot access to ' . $select . ' info';
 | 
        
           |  |  | 84 |             $result = false;
 | 
        
           |  |  | 85 |         }
 | 
        
           |  |  | 86 |         if ($base) {
 | 
        
           |  |  | 87 |             // Only if the directory exists and it has been loaded
 | 
        
           |  |  | 88 |             if (!$base->path_exists || !$base->xml_loaded) {
 | 
        
           |  |  | 89 |                 $this->errormsg = 'Directory ' . $dirpath . ' not loaded';
 | 
        
           |  |  | 90 |                 return false;
 | 
        
           |  |  | 91 |             }
 | 
        
           |  |  | 92 |         } else {
 | 
        
           |  |  | 93 |             $this->errormsg = 'Problem handling ' . $select . ' files';
 | 
        
           |  |  | 94 |             return false;
 | 
        
           |  |  | 95 |         }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         // Get the structure
 | 
        
           |  |  | 98 |         if ($result) {
 | 
        
           |  |  | 99 |             if (!$structure = $base->xml_file->getStructure()) {
 | 
        
           |  |  | 100 |                 $this->errormsg = 'Error retrieving ' . $select . ' structure';
 | 
        
           |  |  | 101 |                 $result = false;
 | 
        
           |  |  | 102 |             }
 | 
        
           |  |  | 103 |         }
 | 
        
           |  |  | 104 |         // Get the tables
 | 
        
           |  |  | 105 |         if ($result) {
 | 
        
           |  |  | 106 |             if (!$tables = $structure->getTables()) {
 | 
        
           |  |  | 107 |                 $this->errormsg = 'Error retrieving ' . $select . ' tables';
 | 
        
           |  |  | 108 |                 $result = false;
 | 
        
           |  |  | 109 |             }
 | 
        
           |  |  | 110 |         }
 | 
        
           |  |  | 111 |         // Get the table
 | 
        
           |  |  | 112 |         if ($result && !$t = $structure->getTable($table)) {
 | 
        
           |  |  | 113 |             $this->errormsg = 'Error retrieving ' . $table . ' table';
 | 
        
           |  |  | 114 |             $result = false;
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |         // Get the keys
 | 
        
           |  |  | 117 |         if ($result) {
 | 
        
           |  |  | 118 |             if (!$keys = $t->getKeys()) {
 | 
        
           |  |  | 119 |                 $this->errormsg = 'Error retrieving ' . $select . ' keys';
 | 
        
           |  |  | 120 |                 $result = false;
 | 
        
           |  |  | 121 |             }
 | 
        
           |  |  | 122 |         }
 | 
        
           |  |  | 123 |         // Get the key
 | 
        
           |  |  | 124 |         if ($result && !$k = $t->getKey($key)) {
 | 
        
           |  |  | 125 |             $this->errormsg = 'Error retrieving ' . $key . ' key';
 | 
        
           |  |  | 126 |             $result = false;
 | 
        
           |  |  | 127 |         }
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 |         if ($result) {
 | 
        
           |  |  | 130 |             // Everything is ok. Generate the XML output
 | 
        
           |  |  | 131 |             $this->output = $k->xmlOutput();
 | 
        
           |  |  | 132 |         } else {
 | 
        
           |  |  | 133 |             // Switch to HTML and error
 | 
        
           |  |  | 134 |             $this->does_generate = ACTION_GENERATE_HTML;
 | 
        
           |  |  | 135 |         }
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |         // Return ok if arrived here
 | 
        
           |  |  | 138 |         return $result;
 | 
        
           |  |  | 139 |     }
 | 
        
           |  |  | 140 | }
 | 
        
           |  |  | 141 |   |