fixed syntax error ;-)
[moodle.git] / lib / dmllib.php
CommitLineData
7e13be08 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
ddaadc7c 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
7e13be08 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
eef868d1 26/// This library contains all the Data Manipulation Language (DML) functions
7e13be08 27/// used to interact with the DB. All the dunctions in this library must be
28/// generic and work against the major number of RDBMS possible. This is the
29/// list of currently supported and tested DBs: mysql, postresql, mssql, oracle
30
9f948d11 31/// This library is automatically included by Moodle core so you never need to
32/// include it yourself.
7e13be08 33
34/// For more info about the functions available in this library, please visit:
35/// http://docs.moodle.org/en/DML_functions
36/// (feel free to modify, improve and document such page, thanks!)
37
8aff8482 38require_once($CFG->libdir.'/dml/moodle_database.php');
39
40/**
41 * Sets up global $DB moodle_database instance
42 * @return void
43 */
44function setup_DB() {
45 global $CFG, $DB;
46
47 if (isset($DB)) {
48 return;
49 }
50
51 if (!isset($CFG->dbuser)) {
52 $CFG->dbuser = '';
53 }
54
55 if (!isset($CFG->dbpass)) {
56 $CFG->dbpass = '';
57 }
58
59 if (!isset($CFG->dbname)) {
60 $CFG->dbname = '';
61 }
62
63 if (!isset($CFG->dbpersist)) {
64 $CFG->dbpersist = false;
65 }
66
67 if (!isset($CFG->dblibrary)) {
68 $CFG->dblibrary = 'adodb';
69 }
70
71 if (!isset($CFG->dboptions)) {
72 $CFG->dboptions = array();
73 }
74
75 $DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
76
77 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
78
79 $driverstatus = $DB->driver_installed();
80
81 if ($driverstatus !== true) {
82 print_error('dbdriverproblem', 'error', '', $driverstatus);
83 }
84
85 if (debugging('', DEBUG_ALL)) {
86 // catch errors
87 ob_start();
88 } else {
89 $prevdebug = error_reporting(0);
90 }
91 if (!$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix, $CFG->dboptions)) {
92 if (debugging('', DEBUG_ALL)) {
93 if ($dberr = ob_get_contents()) {
94 $dberr = '<p><em>'.$dberr.'</em></p>';
95 }
96 ob_end_clean();
97 } else {
98 $dberr = '';
99 }
100 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
101 if (file_exists($CFG->dataroot.'/emailcount')){
102 $fp = fopen($CFG->dataroot.'/emailcount', 'r');
103 $content = fread($fp, 24);
104 fclose($fp);
105 if((time() - (int)$content) > 600){
106 @mail($CFG->emailconnectionerrorsto,
107 'WARNING: Database connection error: '.$CFG->wwwroot,
108 'Connection error: '.$CFG->wwwroot);
109 $fp = fopen($CFG->dataroot.'/emailcount', 'w');
110 fwrite($fp, time());
111 }
112 } else {
113 @mail($CFG->emailconnectionerrorsto,
114 'WARNING: Database connection error: '.$CFG->wwwroot,
115 'Connection error: '.$CFG->wwwroot);
116 $fp = fopen($CFG->dataroot.'/emailcount', 'w');
117 fwrite($fp, time());
118 }
119 }
120 print_error('dbconnectionfailed', 'error', '', $dberr);
121 }
122 if (debugging('', DEBUG_ALL)) {
123 ob_end_clean();
124 } else {
125 error_reporting($prevdebug);
126 }
127
128 return true;
129}