New library for admin-only functions
[moodle.git] / lib / adminlib.php
CommitLineData
173cc1c3 1<?php // $Id$
2 // Contains functions that only administrators will ever need to use
3
4function upgrade_enrol_plugins($return) {
5 global $CFG;
6
7 if (!$mods = get_list_of_plugins("enrol") ) {
8 error("No modules installed!");
9 }
10
11 foreach ($mods as $mod) {
12
13 $fullmod = "$CFG->dirroot/enrol/$mod";
14
15 unset($module);
16
17 if ( is_readable("$fullmod/version.php")) {
18 include_once("$fullmod/version.php"); // defines $module with version etc
19 } else {
20 continue; // Nothing to do.
21 }
22
23 if ( is_readable("$fullmod/db/$CFG->dbtype.php")) {
24 include_once("$fullmod/db/$CFG->dbtype.php"); // defines upgrading function
25 } else {
26 continue;
27 }
28
29 if (!isset($module)) {
30 continue;
31 }
32
33 if (!empty($module->requires)) {
34 if ($module->requires > $CFG->version) {
35 $info->modulename = $mod;
36 $info->moduleversion = $module->version;
37 $info->currentmoodle = $CFG->version;
38 $info->requiremoodle = $module->requires;
39 notify(get_string('modulerequirementsnotmet', 'error', $info));
40 unset($info);
41 continue;
42 }
43 }
44
45 $module->name = $mod; // The name MUST match the directory
46
47 $moduleversion = 'enrol_'.$mod.'_version';
48
49 if (!isset($CFG->$moduleversion)) {
50 set_config($moduleversion, 0);
51 }
52
53 if ($CFG->$moduleversion == $module->version) {
54 // do nothing
55 } else if ($CFG->$moduleversion < $module->version) {
56 if (empty($updated_modules)) {
57 $strmodulesetup = get_string("modulesetup");
58 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, "", "", false, "&nbsp;", "&nbsp;");
59 }
60 print_heading("$module->name module needs upgrading");
61 $upgrade_function = $module->name."_upgrade";
62 if (function_exists($upgrade_function)) {
63 $db->debug=true;
64 if ($upgrade_function($currmodule->version)) {
65 $db->debug=false;
66 // OK so far, now update the modules record
67 set_config($moduleversion, $module->version);
68 notify(get_string("modulesuccess", "", $module->name), "green");
69 echo "<hr />";
70 } else {
71 $db->debug=false;
72 notify("Upgrading $module->name from $currmodule->version to $module->version FAILED!");
73 }
74 }
75 $updated_modules = true;
76 } else {
77 error("Version mismatch: $module->name can't downgrade $currmodule->version -> $module->version !");
78 }
79 }
80
81 if (!empty($updated_modules)) {
82 print_continue($return);
83 die;
84 }
85}
86
87function upgrade_activity_modules($return) {
88/// Find and check all modules and load them up or upgrade them if necessary
89
90 global $CFG;
91
92 if (!$mods = get_list_of_plugins("mod") ) {
93 error("No modules installed!");
94 }
95
96 foreach ($mods as $mod) {
97
98 if ($mod == "NEWMODULE") { // Someone has unzipped the template, ignore it
99 continue;
100 }
101
102 $fullmod = "$CFG->dirroot/mod/$mod";
103
104 unset($module);
105
106 if ( is_readable("$fullmod/version.php")) {
107 include_once("$fullmod/version.php"); # defines $module with version etc
108 } else {
109 notify("Module $mod: $fullmod/version.php was not readable");
110 continue;
111 }
112
113 if ( is_readable("$fullmod/db/$CFG->dbtype.php")) {
114 include_once("$fullmod/db/$CFG->dbtype.php"); # defines upgrading function
115 } else {
116 notify("Module $mod: $fullmod/db/$CFG->dbtype.php was not readable");
117 continue;
118 }
119
120 if (!isset($module)) {
121 continue;
122 }
123
124 if (!empty($module->requires)) {
125 if ($module->requires > $CFG->version) {
126 $info->modulename = $mod;
127 $info->moduleversion = $module->version;
128 $info->currentmoodle = $CFG->version;
129 $info->requiremoodle = $module->requires;
130 notify(get_string('modulerequirementsnotmet', 'error', $info));
131 unset($info);
132 continue;
133 }
134 }
135
136 $module->name = $mod; // The name MUST match the directory
137
138 if ($currmodule = get_record("modules", "name", $module->name)) {
139 if ($currmodule->version == $module->version) {
140 // do nothing
141 } else if ($currmodule->version < $module->version) {
142 if (empty($updated_modules)) {
143 $strmodulesetup = get_string("modulesetup");
144 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, "", "", false, "&nbsp;", "&nbsp;");
145 }
146 print_heading("$module->name module needs upgrading");
147 $upgrade_function = $module->name."_upgrade";
148 if (function_exists($upgrade_function)) {
149 $db->debug=true;
150 if ($upgrade_function($currmodule->version, $module)) {
151 $db->debug=false;
152 // OK so far, now update the modules record
153 $module->id = $currmodule->id;
154 if (! update_record("modules", $module)) {
155 error("Could not update $module->name record in modules table!");
156 }
157 notify(get_string("modulesuccess", "", $module->name), "green");
158 echo "<hr />";
159 } else {
160 $db->debug=false;
161 notify("Upgrading $module->name from $currmodule->version to $module->version FAILED!");
162 }
163 }
164 $updated_modules = true;
165 } else {
166 error("Version mismatch: $module->name can't downgrade $currmodule->version -> $module->version !");
167 }
168
169 } else { // module not installed yet, so install it
170 if (empty($updated_modules)) {
171 $strmodulesetup = get_string("modulesetup");
172 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, "", "", false, "&nbsp;", "&nbsp;");
173 }
174 print_heading($module->name);
175 $updated_modules = true;
176 $db->debug = true;
177 @set_time_limit(0); // To allow slow databases to complete the long SQL
178 if (modify_database("$fullmod/db/$CFG->dbtype.sql")) {
179 $db->debug = false;
180 if ($module->id = insert_record("modules", $module)) {
181 notify(get_string("modulesuccess", "", $module->name), "green");
182 echo "<hr />";
183 } else {
184 error("$module->name module could not be added to the module list!");
185 }
186 } else {
187 error("$module->name tables could NOT be set up successfully!");
188 }
189 }
190 }
191
192 if (!empty($updated_modules)) {
193 print_continue($return);
194 die;
195 }
196}
197
198?>