Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/dmllib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00041 defined('MOODLE_INTERNAL') || die();
00042 
00043 // Require the essential
00044 require_once($CFG->libdir.'/dml/moodle_database.php');
00045 
00047 define('IGNORE_MISSING', 0);
00049 define('IGNORE_MULTIPLE', 1);
00051 define('MUST_EXIST', 2);
00052 
00056 class dml_exception extends moodle_exception {
00062     function __construct($errorcode, $a=NULL, $debuginfo=null) {
00063         parent::__construct($errorcode, '', '', $a, $debuginfo);
00064     }
00065 }
00066 
00070 class dml_connection_exception extends dml_exception {
00075     function __construct($error) {
00076         $errorinfo = $error;
00077         parent::__construct('dbconnectionfailed', NULL, $errorinfo);
00078     }
00079 }
00080 
00084 class dml_sessionwait_exception extends dml_exception {
00088     function __construct() {
00089         parent::__construct('sessionwaiterr');
00090     }
00091 }
00092 
00096 class dml_read_exception extends dml_exception {
00098     public $error;
00100     public $sql;
00102     public $params;
00103 
00110     function __construct($error, $sql=null, array $params=null) {
00111         $this->error  = $error;
00112         $this->sql    = $sql;
00113         $this->params = $params;
00114         $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
00115         parent::__construct('dmlreadexception', NULL, $errorinfo);
00116     }
00117 }
00118 
00122 class dml_multiple_records_exception extends dml_exception {
00124     public $sql;
00126     public $params;
00127 
00134     function __construct($sql='', array $params=null) {
00135         $errorinfo = $sql."\n[".var_export($params, true).']';
00136         parent::__construct('multiplerecordsfound', null, $errorinfo);
00137     }
00138 }
00139 
00143 class dml_missing_record_exception extends dml_exception {
00145     public $table;
00147     public $sql;
00149     public $params;
00150 
00157     function __construct($tablename, $sql='', array $params=null) {
00158         if (empty($tablename)) {
00159             $tablename = null;
00160         }
00161         $this->tablename = $tablename;
00162         $this->sql       = $sql;
00163         $this->params    = $params;
00164 
00165         switch ($tablename) {
00166             case null:
00167                 $errcode = 'invalidrecordunknown';
00168                 break;
00169             case 'course':
00170                 $errcode = empty($sql) ? 'invalidcourseid' : 'invalidrecord';
00171                 break;
00172             case 'course_module':
00173                 $errcode = 'invalidcoursemodule';
00174                 break;
00175             case 'user':
00176                 $errcode = 'invaliduser';
00177                 break;
00178             default:
00179                 $errcode = 'invalidrecord';
00180                 break;
00181         }
00182         $errorinfo = $sql."\n[".var_export($params, true).']';
00183         parent::__construct($errcode, $tablename, $errorinfo);
00184     }
00185 }
00186 
00190 class dml_write_exception extends dml_exception {
00192     public $error;
00194     public $sql;
00196     public $params;
00197 
00204     function __construct($error, $sql=null, array $params=null) {
00205         $this->error  = $error;
00206         $this->sql    = $sql;
00207         $this->params = $params;
00208         $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
00209         parent::__construct('dmlwriteexception', NULL, $errorinfo);
00210     }
00211 }
00212 
00216 class dml_transaction_exception extends dml_exception {
00218     public $transaction;
00219 
00224     function __construct($debuginfo=null, $transaction=null) {
00225         $this->transaction = $transaction; // TODO: MDL-20625 use the info from $transaction for debugging purposes
00226         parent::__construct('dmltransactionexception', NULL, $debuginfo);
00227     }
00228 }
00229 
00237 function setup_DB() {
00238     global $CFG, $DB;
00239 
00240     if (isset($DB)) {
00241         return;
00242     }
00243 
00244     if (!isset($CFG->dbuser)) {
00245         $CFG->dbuser = '';
00246     }
00247 
00248     if (!isset($CFG->dbpass)) {
00249         $CFG->dbpass = '';
00250     }
00251 
00252     if (!isset($CFG->dbname)) {
00253         $CFG->dbname = '';
00254     }
00255 
00256     if (!isset($CFG->dblibrary)) {
00257         $CFG->dblibrary = 'native';
00258         // use new drivers instead of the old adodb driver names
00259         switch ($CFG->dbtype) {
00260             case 'postgres7' :
00261                 $CFG->dbtype = 'pgsql';
00262                 break;
00263 
00264             case 'mssql_n':
00265                 $CFG->dbtype = 'mssql';
00266                 break;
00267 
00268             case 'oci8po':
00269                 $CFG->dbtype = 'oci';
00270                 break;
00271 
00272             case 'mysql' :
00273                 $CFG->dbtype = 'mysqli';
00274                 break;
00275         }
00276     }
00277 
00278     if (!isset($CFG->dboptions)) {
00279         $CFG->dboptions = array();
00280     }
00281 
00282     if (isset($CFG->dbpersist)) {
00283         $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
00284     }
00285 
00286     if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
00287         throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
00288     }
00289 
00290     try {
00291         $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
00292     } catch (moodle_exception $e) {
00293         if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
00294             if (file_exists($CFG->dataroot.'/emailcount')){
00295                 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
00296                 $content = @fread($fp, 24);
00297                 @fclose($fp);
00298                 if((time() - (int)$content) > 600){
00299                     //email directly rather than using messaging
00300                     @mail($CFG->emailconnectionerrorsto,
00301                         'WARNING: Database connection error: '.$CFG->wwwroot,
00302                         'Connection error: '.$CFG->wwwroot);
00303                     $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
00304                     @fwrite($fp, time());
00305                 }
00306             } else {
00307                //email directly rather than using messaging
00308                @mail($CFG->emailconnectionerrorsto,
00309                     'WARNING: Database connection error: '.$CFG->wwwroot,
00310                     'Connection error: '.$CFG->wwwroot);
00311                $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
00312                @fwrite($fp, time());
00313             }
00314         }
00315         // rethrow the exception
00316         throw $e;
00317     }
00318 
00319     $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
00320 
00321     return true;
00322 }
 All Data Structures Namespaces Files Functions Variables Enumerations