|
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 00035 class XMLDBAction { 00036 00037 var $does_generate; //Type of value returned by the invoke method 00038 //ACTION_GENERATE_HTML have contents to show 00039 //set by each specialized invoke 00040 00041 var $title; //Title of the Action (class name, by default) 00042 //set by parent init automatically 00043 00044 var $str; //Strings used by the action 00045 //set by each specialized init, calling loadStrings 00046 00047 var $output; //Output of the action 00048 //set by each specialized invoke, get with getOutput 00049 00050 var $errormsg; //Last Error produced. Check when any invoke returns false 00051 //get with getError 00052 00053 var $postaction; //Action to execute at the end of the invoke script 00054 00055 var $sesskey_protected; // Actions must be protected by sesskey mechanism 00056 00060 function XMLDBAction() { 00061 $this->init(); 00062 } 00063 00067 function __construct() { 00068 $this->XMLDBAction(); 00069 } 00070 00075 function init() { 00076 $this->does_generate = ACTION_NONE; 00077 $this->title = strtolower(get_class($this)); 00078 $this->str = array(); 00079 $this->output = NULL; 00080 $this->errormsg = NULL; 00081 $this->subaction = NULL; 00082 $this->sesskey_protected = true; 00083 } 00084 00088 function getDoesGenerate() { 00089 return $this->does_generate; 00090 } 00091 00096 function getError() { 00097 return $this->errormsg; 00098 } 00099 00104 function getOutput() { 00105 return $this->output; 00106 } 00107 00112 function getPostAction() { 00113 return $this->postaction; 00114 } 00115 00120 function getTitle() { 00121 return $this->str['title']; 00122 } 00123 00128 function loadStrings($strings) { 00129 // Load some commonly used strings 00130 if (get_string_manager()->string_exists($this->title, 'tool_xmldb')) { 00131 $this->str['title'] = get_string($this->title, 'tool_xmldb'); 00132 } else { 00133 $this->str['title'] = $this->title; 00134 } 00135 00136 // Now process the $strings array loading it in the $str atribute 00137 if ($strings) { 00138 foreach ($strings as $key => $module) { 00139 $this->str[$key] = get_string($key, $module); 00140 } 00141 } 00142 } 00143 00148 function invoke() { 00149 00150 global $SESSION; 00151 00152 // Sesskey protection 00153 if ($this->sesskey_protected) { 00154 require_sesskey(); 00155 } 00156 00157 // If we are used any dir, save it in the lastused session object 00158 // Some actions can use it to perform positioning 00159 if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) { 00160 $SESSION->lastused = $lastused; 00161 } 00162 00163 $this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT); 00164 // Avoid being recursive 00165 if ($this->title == $this->postaction) { 00166 $this->postaction = NULL; 00167 } 00168 } 00169 00173 function launch($action) { 00174 00175 global $CFG; 00176 00177 // Get the action path and invoke it 00178 $actionsroot = "$CFG->dirroot/$CFG->admin/tool/xmldb/actions"; 00179 $actionclass = $action . '.class.php'; 00180 $actionpath = "$actionsroot/$action/$actionclass"; 00181 00182 // Load and invoke the proper action 00183 $result = false; 00184 if (file_exists($actionpath) && is_readable($actionpath)) { 00185 require_once($actionpath); 00186 if ($xmldb_action = new $action) { 00187 $result = $xmldb_action->invoke(); 00188 if ($result) { 00189 if ($xmldb_action->does_generate != ACTION_NONE && 00190 $xmldb_action->getOutput()) { 00191 $this->does_generate = $xmldb_action->does_generate; 00192 $this->title = $xmldb_action->title; 00193 $this->str = $xmldb_action->str; 00194 $this->output .= $xmldb_action->getOutput(); 00195 } 00196 } else { 00197 $this->errormsg = $xmldb_action->getError(); 00198 } 00199 } else { 00200 $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)"; 00201 } 00202 } else { 00203 $this->errormsg = "Error: wrong action specified ($action)"; 00204 } 00205 return $result; 00206 } 00207 00217 function upgrade_savepoint_php($structure) { 00218 00219 $path = $structure->getPath(); 00220 00221 // Trim "db" from path 00222 $path = dirname($path); 00223 00224 // Get pluginname, plugindir and plugintype 00225 $pluginname = basename($path); 00226 if ($path == 'lib') { // exception for lib (not proper plugin) 00227 $plugindir = 'lib'; 00228 $plugintype = 'lib'; 00229 } else { // rest of plugins 00230 // TODO: this is not nice and may fail, plugintype should be passed around somehow instead 00231 $plugintypes = get_plugin_types(false); 00232 $plugindir = dirname($path); 00233 $plugindir = str_replace('\\', '/', $plugindir); 00234 $plugintype = array_search($plugindir, $plugintypes); 00235 } 00236 00237 $result = ''; 00238 00239 switch ($plugintype ) { 00240 case 'lib': // has own savepoint function 00241 $result = XMLDB_LINEFEED . 00242 ' // Main savepoint reached' . XMLDB_LINEFEED . 00243 ' upgrade_main_savepoint(true, XXXXXXXXXX);' . XMLDB_LINEFEED; 00244 break; 00245 case 'mod': // has own savepoint function 00246 $result = XMLDB_LINEFEED . 00247 ' // ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED . 00248 ' upgrade_mod_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED; 00249 break; 00250 case 'block': // has own savepoint function 00251 $result = XMLDB_LINEFEED . 00252 ' // ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED . 00253 ' upgrade_block_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED; 00254 break; 00255 default: // rest of plugins 00256 $result = XMLDB_LINEFEED . 00257 ' // ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED . 00258 ' upgrade_plugin_savepoint(true, XXXXXXXXXX, ' . "'$plugintype'" . ', ' . "'$pluginname'" . ');' . XMLDB_LINEFEED; 00259 } 00260 return $result; 00261 } 00262 }