PageRenderTime 815ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/db/postgres7.php

https://bitbucket.org/ceu/moodle_demo
PHP | 1961 lines | 1583 code | 286 blank | 92 comment | 230 complexity | 0a607df8b3c56074b5be2f44fc48286d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?PHP //$Id: postgres7.php,v 1.237.2.2 2008/05/01 06:03:32 dongsheng Exp $
  2. // THIS FILE IS DEPRECATED! PLEASE DO NOT MAKE CHANGES TO IT!
  3. //
  4. // IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL
  5. // LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.
  6. //
  7. // This file is tailored to PostgreSQL 7
  8. function main_upgrade($oldversion=0) {
  9. global $CFG, $THEME, $db;
  10. $result = true;
  11. if ($oldversion < 2003010101) {
  12. delete_records("log_display", "module", "user");
  13. $new->module = "user";
  14. $new->action = "view";
  15. $new->mtable = "user";
  16. $new->field = "CONCAT(firstname,\" \",lastname)";
  17. insert_record("log_display", $new);
  18. delete_records("log_display", "module", "course");
  19. $new->module = "course";
  20. $new->action = "view";
  21. $new->mtable = "course";
  22. $new->field = "fullname";
  23. insert_record("log_display", $new);
  24. $new->action = "update";
  25. insert_record("log_display", $new);
  26. $new->action = "enrol";
  27. insert_record("log_display", $new);
  28. }
  29. //support user based course creating
  30. if ($oldversion < 2003032400) {
  31. execute_sql("CREATE TABLE {$CFG->prefix}user_coursecreators (
  32. id int8 SERIAL PRIMARY KEY,
  33. userid int8 NOT NULL default '0'
  34. )");
  35. }
  36. if ($oldversion < 2003041400) {
  37. table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score");
  38. }
  39. if ($oldversion < 2003042104) { // Try to update permissions of all files
  40. if ($files = get_directory_list($CFG->dataroot)) {
  41. echo "Attempting to update permissions for all files... ignore any errors.";
  42. foreach ($files as $file) {
  43. echo "$CFG->dataroot/$file<br />";
  44. @chmod("$CFG->dataroot/$file", $CFG->directorypermissions);
  45. }
  46. }
  47. }
  48. if ($oldversion < 2003042400) {
  49. // Rebuild all course caches, because of changes to do with visible variable
  50. if ($courses = get_records_sql("SELECT * FROM {$CFG->prefix}course")) {
  51. require_once("$CFG->dirroot/course/lib.php");
  52. foreach ($courses as $course) {
  53. $modinfo = serialize(get_array_of_activities($course->id));
  54. if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
  55. notify("Could not cache module information for course '" . format_string($course->fullname) . "'!");
  56. }
  57. }
  58. }
  59. }
  60. if ($oldversion < 2003042500) {
  61. // Convert all usernames to lowercase.
  62. $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user");
  63. $cerrors = "";
  64. $rarray = array();
  65. foreach ($users as $user) { // Check for possible conflicts
  66. $lcname = trim(moodle_strtolower($user->username));
  67. if (in_array($lcname, $rarray)) {
  68. $cerrors .= $user->id."->".$lcname.'<br/>' ;
  69. } else {
  70. array_push($rarray,$lcname);
  71. }
  72. }
  73. if ($cerrors != '') {
  74. notify("Error: Cannot convert usernames to lowercase.
  75. Following usernames would overlap (id->username):<br/> $cerrors .
  76. Please resolve overlapping errors.");
  77. $result = false;
  78. }
  79. $cerrors = "";
  80. echo "Checking userdatabase:<br />";
  81. foreach ($users as $user) {
  82. $lcname = trim(moodle_strtolower($user->username));
  83. if ($lcname != $user->username) {
  84. $convert = set_field("user" , "username" , $lcname, "id", $user->id);
  85. if (!$convert) {
  86. if ($cerrors){
  87. $cerrors .= ", ";
  88. }
  89. $cerrors .= $item;
  90. } else {
  91. echo ".";
  92. }
  93. }
  94. }
  95. if ($cerrors != '') {
  96. notify("There were errors when converting following usernames to lowercase.
  97. '$cerrors' . Sorry, but you will need to fix your database by hand.");
  98. $result = false;
  99. }
  100. }
  101. if ($oldversion < 2003042700) {
  102. /// Changing to multiple indexes
  103. execute_sql(" CREATE INDEX {$CFG->prefix}log_coursemoduleaction_idx ON {$CFG->prefix}log (course,module,action) ");
  104. execute_sql(" CREATE INDEX {$CFG->prefix}log_courseuserid_idx ON {$CFG->prefix}log (course,userid) ");
  105. }
  106. if ($oldversion < 2003042801) {
  107. execute_sql("CREATE TABLE {$CFG->prefix}course_display (
  108. id SERIAL PRIMARY KEY,
  109. course integer NOT NULL default '0',
  110. userid integer NOT NULL default '0',
  111. display integer NOT NULL default '0'
  112. )");
  113. execute_sql("CREATE INDEX {$CFG->prefix}course_display_courseuserid_idx ON {$CFG->prefix}course_display (course,userid)");
  114. }
  115. if ($oldversion < 2003050400) {
  116. table_column("course_sections", "", "visible", "integer", "1", "unsigned", "1", "", "");
  117. }
  118. if ($oldversion < 2003050401) {
  119. table_column("user", "", "lang", "VARCHAR", "5", "", "$CFG->lang" ,"NOT NULL","");
  120. }
  121. if ($oldversion < 2003050900) {
  122. table_column("modules", "", "visible", "integer", "1", "unsigned", "1", "", "");
  123. }
  124. if ($oldversion < 2003050902) {
  125. if (get_records("modules", "name", "pgassignment")) {
  126. print_simple_box("Note: the pgassignment module will soon be deleted from CVS! Go to the new 'Manage Modules' page and DELETE IT from your system", "center", "50%", "$THEME->cellheading", "20", "noticebox");
  127. }
  128. }
  129. if ($oldversion < 2003051600) {
  130. print_simple_box("Thanks for upgrading!<p>There are many changes since the last release. Please read the release notes carefully. If you are using CUSTOM themes you will need to edit them. You will also need to check your site's config.php file.", "center", "50%", "$THEME->cellheading", "20", "noticebox");
  131. }
  132. if ($oldversion < 2003052300) {
  133. table_column("user", "", "autosubscribe", "integer", "1", "unsigned", "1", "", "htmleditor");
  134. }
  135. if ($oldversion < 2003072100) {
  136. table_column("course", "", "visible", "integer", "1", "unsigned", "1", "", "marker");
  137. }
  138. if ($oldversion < 2003072101) {
  139. table_column("course_sections", "sequence", "sequence", "text", "", "", "", "", "");
  140. }
  141. if ($oldversion < 2003072800) {
  142. print_simple_box("The following database index improves performance, but can be quite large - if you are upgrading and you have problems with a limited quota you may want to delete this index later from the '{$CFG->prefix}log' table in your database", "center", "50%", "$THEME->cellheading", "20", "noticebox");
  143. flush();
  144. execute_sql(" CREATE INDEX {$CFG->prefix}log_timecoursemoduleaction_idx ON {$CFG->prefix}log (time,course,module,action) ");
  145. execute_sql(" CREATE INDEX {$CFG->prefix}user_students_courseuserid_idx ON {$CFG->prefix}user_students (course,userid) ");
  146. execute_sql(" CREATE INDEX {$CFG->prefix}user_teachers_courseuserid_idx ON {$CFG->prefix}user_teachers (course,userid) ");
  147. }
  148. if ($oldversion < 2003072802) {
  149. table_column("course_categories", "", "description", "text", "", "", "");
  150. table_column("course_categories", "", "parent", "integer", "10", "unsigned");
  151. table_column("course_categories", "", "sortorder", "integer", "10", "unsigned");
  152. table_column("course_categories", "", "courseorder", "text", "", "", "");
  153. table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1");
  154. table_column("course_categories", "", "timemodified", "integer", "10", "unsigned");
  155. }
  156. if ($oldversion < 2003080400) {
  157. notify("If the following command fails you may want to change the type manually, from TEXT to INTEGER. Moodle should keep working even if you don't.");
  158. table_column("course_categories", "courseorder", "courseorder", "integer", "10", "unsigned");
  159. table_column("course", "", "sortorder", "integer", "10", "unsigned", "0", "", "category");
  160. }
  161. if ($oldversion < 2003081502) {
  162. execute_sql(" CREATE TABLE {$CFG->prefix}scale (
  163. id SERIAL PRIMARY KEY,
  164. courseid integer NOT NULL default '0',
  165. userid integer NOT NULL default '0',
  166. name varchar(255) NOT NULL default '',
  167. scale text,
  168. description text,
  169. timemodified integer NOT NULL default '0'
  170. )");
  171. }
  172. if ($oldversion < 2003081503) {
  173. table_column("forum", "", "scale", "integer", "10", "unsigned", "0", "", "assessed");
  174. get_scales_menu(0); // Just to force the default scale to be created
  175. }
  176. if ($oldversion < 2003081600) {
  177. table_column("user_teachers", "", "editall", "integer", "1", "unsigned", "1", "", "role");
  178. table_column("user_teachers", "", "timemodified", "integer", "10", "unsigned", "0", "", "editall");
  179. }
  180. if ($oldversion < 2003081900) {
  181. table_column("course_categories", "courseorder", "coursecount", "integer", "10", "unsigned", "0");
  182. }
  183. if ($oldversion < 2003080700) {
  184. notify("Cleaning up categories and course ordering...");
  185. fix_course_sortorder();
  186. }
  187. if ($oldversion < 2003082001) {
  188. table_column("course", "", "showgrades", "integer", "2", "unsigned", "1", "", "format");
  189. }
  190. if ($oldversion < 2003082101) {
  191. execute_sql(" CREATE INDEX {$CFG->prefix}course_category_idx ON {$CFG->prefix}course (category) ");
  192. }
  193. if ($oldversion < 2003082702) {
  194. execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname,\" \",lastname)') ");
  195. }
  196. if ($oldversion < 2003091000) {
  197. # Old field that was never added!
  198. table_column("course", "", "showrecent", "integer", "10", "unsigned", "1", "", "numsections");
  199. }
  200. if ($oldversion < 2003091400) {
  201. table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score");
  202. }
  203. if ($oldversion < 2003092900) {
  204. table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker");
  205. }
  206. if ($oldversion < 2003102700) {
  207. table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
  208. table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
  209. $db->debug = false;
  210. $CFG->debug = 0;
  211. notify("Calculating access times. Please wait - this may take a long time on big sites...", "green");
  212. flush();
  213. if ($courses = get_records_select("course", "category > 0")) {
  214. foreach ($courses as $course) {
  215. notify("Processing " . format_string($course->fullname) . " ...", "green");
  216. flush();
  217. if ($users = get_records_select("user_teachers", "course = '$course->id'",
  218. "id", "id, userid, timeaccess")) {
  219. foreach ($users as $user) {
  220. $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log WHERE course = '$course->id' and userid = '$user->userid' ORDER by time DESC");
  221. if (empty($loginfo->time)) {
  222. $loginfo->time = 0;
  223. }
  224. execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$loginfo->time'
  225. WHERE userid = '$user->userid' AND course = '$course->id'", false);
  226. }
  227. }
  228. if ($users = get_records_select("user_students", "course = '$course->id'",
  229. "id", "id, userid, timeaccess")) {
  230. foreach ($users as $user) {
  231. $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log
  232. WHERE course = '$course->id' and userid = '$user->userid'
  233. ORDER by time DESC");
  234. if (empty($loginfo->time)) {
  235. $loginfo->time = 0;
  236. }
  237. execute_sql("UPDATE {$CFG->prefix}user_students
  238. SET timeaccess = '$loginfo->time'
  239. WHERE userid = '$user->userid' AND course = '$course->id'", false);
  240. }
  241. }
  242. }
  243. }
  244. notify("All courses complete.", "green");
  245. $db->debug = true;
  246. }
  247. if ($oldversion < 2003103100) {
  248. table_column("course", "", "showreports", "integer", "4", "unsigned", "0", "", "maxbytes");
  249. }
  250. if ($oldversion < 2003121600) {
  251. execute_sql("CREATE TABLE {$CFG->prefix}groups (
  252. id SERIAL PRIMARY KEY,
  253. courseid integer NOT NULL default '0',
  254. name varchar(255) NOT NULL default '',
  255. description text,
  256. lang varchar(10) NOT NULL default '',
  257. picture integer NOT NULL default '0',
  258. timecreated integer NOT NULL default '0',
  259. timemodified integer NOT NULL default '0'
  260. )");
  261. execute_sql("CREATE INDEX {$CFG->prefix}groups_idx ON {$CFG->prefix}groups (courseid) ");
  262. execute_sql("CREATE TABLE {$CFG->prefix}groups_members (
  263. id SERIAL PRIMARY KEY,
  264. groupid integer NOT NULL default '0',
  265. userid integer NOT NULL default '0',
  266. timeadded integer NOT NULL default '0'
  267. )");
  268. execute_sql("CREATE INDEX {$CFG->prefix}groups_members_idx ON {$CFG->prefix}groups_members (groupid) ");
  269. }
  270. if ($oldversion < 2003122600) {
  271. table_column("course", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
  272. table_column("course", "", "groupmodeforce", "integer", "4", "unsigned", "0", "", "groupmode");
  273. }
  274. if ($oldversion < 2004010900) {
  275. table_column("course_modules", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
  276. }
  277. if ($oldversion < 2004011700) {
  278. modify_database("", "CREATE TABLE prefix_event (
  279. id SERIAL PRIMARY KEY,
  280. name varchar(255) NOT NULL default '',
  281. description text,
  282. courseid integer NOT NULL default '0',
  283. groupid integer NOT NULL default '0',
  284. userid integer NOT NULL default '0',
  285. modulename varchar(20) NOT NULL default '',
  286. instance integer NOT NULL default '0',
  287. eventtype varchar(20) NOT NULL default '',
  288. timestart integer NOT NULL default '0',
  289. timeduration integer NOT NULL default '0',
  290. timemodified integer NOT NULL default '0'
  291. ); ");
  292. modify_database("", "CREATE INDEX prefix_event_courseid_idx ON prefix_event (courseid);");
  293. modify_database("", "CREATE INDEX prefix_event_userid_idx ON prefix_event (userid);");
  294. }
  295. if ($oldversion < 2004012800) {
  296. modify_database("", "CREATE TABLE prefix_user_preferences (
  297. id SERIAL PRIMARY KEY,
  298. userid integer NOT NULL default '0',
  299. name varchar(50) NOT NULL default '',
  300. value varchar(255) NOT NULL default ''
  301. ); ");
  302. modify_database("", "CREATE INDEX prefix_user_preferences_useridname_idx ON prefix_user_preferences (userid,name);");
  303. }
  304. if ($oldversion < 2004012900) {
  305. table_column("config", "value", "value", "text", "", "", "");
  306. }
  307. if ($oldversion < 2004013101) {
  308. table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module");
  309. set_config("upgrade", "logs");
  310. }
  311. if ($oldversion < 2004020900) {
  312. table_column("course", "", "lang", "varchar", "5", "", "", "", "groupmodeforce");
  313. }
  314. if ($oldversion < 2004020903) {
  315. modify_database("", "CREATE TABLE prefix_cache_text (
  316. id SERIAL PRIMARY KEY,
  317. md5key varchar(32) NOT NULL default '',
  318. formattedtext text,
  319. timemodified integer NOT NULL default '0'
  320. );");
  321. }
  322. if ($oldversion < 2004021000) {
  323. $textfilters = array();
  324. for ($i=1; $i<=10; $i++) {
  325. $variable = "textfilter$i";
  326. if (!empty($CFG->$variable)) { /// No more filters
  327. if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
  328. $textfilters[] = $CFG->$variable;
  329. }
  330. }
  331. }
  332. $textfilters = implode(',', $textfilters);
  333. if (empty($textfilters)) {
  334. $textfilters = 'mod/glossary/dynalink.php';
  335. }
  336. set_config('textfilters', $textfilters);
  337. }
  338. if ($oldversion < 2004021201) {
  339. modify_database("", "CREATE TABLE prefix_cache_filters (
  340. id SERIAL PRIMARY KEY,
  341. filter varchar(32) NOT NULL default '',
  342. version integer NOT NULL default '0',
  343. md5key varchar(32) NOT NULL default '',
  344. rawtext text,
  345. timemodified integer NOT NULL default '0'
  346. );");
  347. modify_database("", "CREATE INDEX prefix_cache_filters_filtermd5key_idx ON prefix_cache_filters (filter,md5key);");
  348. modify_database("", "CREATE INDEX prefix_cache_text_md5key_idx ON prefix_cache_text (md5key);");
  349. }
  350. if ($oldversion < 2004021500) {
  351. table_column("groups", "", "hidepicture", "integer", "2", "unsigned", "0", "", "picture");
  352. }
  353. if ($oldversion < 2004021700) {
  354. if (!empty($CFG->textfilters)) {
  355. $CFG->textfilters = str_replace("tex_filter.php", "filter.php", $CFG->textfilters);
  356. $CFG->textfilters = str_replace("multilang.php", "filter.php", $CFG->textfilters);
  357. $CFG->textfilters = str_replace("censor.php", "filter.php", $CFG->textfilters);
  358. $CFG->textfilters = str_replace("mediaplugin.php", "filter.php", $CFG->textfilters);
  359. $CFG->textfilters = str_replace("algebra_filter.php", "filter.php", $CFG->textfilters);
  360. $CFG->textfilters = str_replace("dynalink.php", "filter.php", $CFG->textfilters);
  361. set_config("textfilters", $CFG->textfilters);
  362. }
  363. }
  364. if ($oldversion < 2004022000) {
  365. table_column("user", "", "emailstop", "integer", "1", "unsigned", "0", "not null", "email");
  366. }
  367. if ($oldversion < 2004022200) { /// Final renaming I hope. :-)
  368. if (!empty($CFG->textfilters)) {
  369. $CFG->textfilters = str_replace("/filter.php", "", $CFG->textfilters);
  370. $CFG->textfilters = str_replace("mod/glossary/dynalink.php", "mod/glossary", $CFG->textfilters);
  371. $textfilters = explode(',', $CFG->textfilters);
  372. foreach ($textfilters as $key => $textfilter) {
  373. $textfilters[$key] = trim($textfilter);
  374. }
  375. set_config("textfilters", implode(',',$textfilters));
  376. }
  377. }
  378. if ($oldversion < 2004030702) { /// Because of the renaming of Czech language pack
  379. execute_sql("UPDATE {$CFG->prefix}user SET lang = 'cs' WHERE lang = 'cz'");
  380. execute_sql("UPDATE {$CFG->prefix}course SET lang = 'cs' WHERE lang = 'cz'");
  381. }
  382. if ($oldversion < 2004041800) { /// Integrate Block System from contrib
  383. table_column("course", "", "blockinfo", "varchar", "255", "", "", "not null", "modinfo");
  384. }
  385. if ($oldversion < 2004042600) { /// Rebuild course caches for resource icons
  386. //include_once("$CFG->dirroot/course/lib.php");
  387. //rebuild_course_cache();
  388. }
  389. if ($oldversion < 2004042700) { /// Increase size of lang fields
  390. table_column("user", "lang", "lang", "varchar", "10", "", "en");
  391. table_column("groups", "lang", "lang", "varchar", "10", "", "");
  392. table_column("course", "lang", "lang", "varchar", "10", "", "");
  393. }
  394. if ($oldversion < 2004042701) { /// Add hiddentopics field to control hidden topics behaviour
  395. #table_column("course", "", "hiddentopics", "integer", "1", "unsigned", "0", "not null", "visible");
  396. #See 'hiddensections' further down
  397. }
  398. if ($oldversion < 2004042702) { /// Add a format field for the description
  399. table_column("event", "", "format", "integer", "4", "unsigned", "0", "not null", "description");
  400. }
  401. if ($oldversion < 2004043001) { /// Add hiddentopics field to control hidden topics behaviour
  402. table_column("course", "", "hiddensections", "integer", "2", "unsigned", "0", "not null", "visible");
  403. }
  404. if ($oldversion < 2004050400) { /// add a visible field for events
  405. table_column("event", "", "visible", "smallint", "1", "", "1", "not null", "timeduration");
  406. if ($events = get_records('event')) {
  407. foreach($events as $event) {
  408. if ($moduleid = get_field('modules', 'id', 'name', $event->modulename)) {
  409. if (get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $event->instance) == 0) {
  410. set_field('event', 'visible', 0, 'id', $event->id);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. if ($oldversion < 2004052800) { /// First version tagged "1.4 development", version.php 1.227
  417. set_config('siteblocksadded', true); /// This will be used later by the block upgrade
  418. }
  419. if ($oldversion < 2004053000) { /// set defaults for site course
  420. $site = get_site();
  421. set_field('course', 'numsections', 0, 'id', $site->id);
  422. set_field('course', 'groupmodeforce', 1, 'id', $site->id);
  423. set_field('course', 'teacher', get_string('administrator'), 'id', $site->id);
  424. set_field('course', 'teachers', get_string('administrators'), 'id', $site->id);
  425. set_field('course', 'student', get_string('user'), 'id', $site->id);
  426. set_field('course', 'students', get_string('users'), 'id', $site->id);
  427. }
  428. if ($oldversion < 2004060100) {
  429. set_config('digestmailtime', 0);
  430. table_column('user', "", 'maildigest', 'smallint', '1', '', '0', 'not null', 'mailformat');
  431. }
  432. if ($oldversion < 2004062400) {
  433. table_column('user_teachers', "", 'timeend', 'int', '10', 'unsigned', '0', 'not null', 'editall');
  434. table_column('user_teachers', "", 'timestart', 'int', '10', 'unsigned', '0', 'not null', 'editall');
  435. }
  436. if ($oldversion < 2004062401) {
  437. table_column('course', '', 'idnumber', 'varchar', '100', '', '', 'not null', 'shortname');
  438. execute_sql('UPDATE '.$CFG->prefix.'course SET idnumber = shortname'); // By default
  439. }
  440. if ($oldversion < 2004062600) {
  441. table_column('course', '', 'cost', 'varchar', '10', '', '', 'not null', 'lang');
  442. }
  443. if ($oldversion < 2004072900) {
  444. table_column('course', '', 'enrolperiod', 'int', '10', 'unsigned', '0', 'not null', 'startdate');
  445. }
  446. if ($oldversion < 2004072901) { // Fixing error in schema
  447. if ($record = get_record('log_display', 'module', 'course', 'action', 'update')) {
  448. delete_records('log_display', 'module', 'course', 'action', 'update');
  449. insert_record('log_display', $record, false);
  450. }
  451. }
  452. if ($oldversion < 2004081200) { // Fixing version errors in some blocks
  453. set_field('blocks', 'version', 2004081200, 'name', 'admin');
  454. set_field('blocks', 'version', 2004081200, 'name', 'calendar_month');
  455. set_field('blocks', 'version', 2004081200, 'name', 'course_list');
  456. }
  457. if ($oldversion < 2004081500) { // Adding new "auth" field to user table to allow more flexibility
  458. table_column('user', '', 'auth', 'varchar', '20', '', 'manual', 'not null', 'id');
  459. execute_sql("UPDATE {$CFG->prefix}user SET auth = 'manual'"); // Set everyone to 'manual' to be sure
  460. if ($admins = get_admins()) { // Set all the NON-admins to whatever the current auth module is
  461. $adminlist = array();
  462. foreach ($admins as $user) {
  463. $adminlist[] = $user->id;
  464. }
  465. $adminlist = implode(',', $adminlist);
  466. execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE id NOT IN ($adminlist)");
  467. }
  468. }
  469. if ($oldversion < 2004082600) {
  470. //update auth-fields for external users
  471. // following code would not work in 1.8
  472. /* include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
  473. if (function_exists('auth_get_userlist')) {
  474. $externalusers = auth_get_userlist();
  475. if (!empty($externalusers)){
  476. $externalusers = '\''. implode('\',\'',$externalusers).'\'';
  477. execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE username IN ($externalusers)");
  478. }
  479. }*/
  480. }
  481. if ($oldversion < 2004082900) { // Make sure guest is "manual" too.
  482. set_field('user', 'auth', 'manual', 'username', 'guest');
  483. }
  484. /* Just commenteed unused fields out
  485. if ($oldversion < 2004090300) { // Add guid-field used in user syncronization
  486. table_column('user', '', 'guid', 'varchar', '128', '', '', '', 'auth');
  487. execute_sql("CREATE INDEX {$CFG->prefix}user_auth_guid_idx ON {$CFG->prefix}user (auth, guid)");
  488. }
  489. */
  490. if ($oldversion < 2004091900) { //Modify idnumber to hold longer keys
  491. set_field('user', 'auth', 'manual', 'username', 'guest');
  492. table_column('user', 'idnumber', 'idnumber', 'varchar', '64', '', '', '', '');
  493. execute_sql("DROP INDEX {$CFG->prefix}user_idnumber_idx ;",false);// added in case of conflicts with upgrade from 14stable
  494. execute_sql("DROP INDEX {$CFG->prefix}user_auth_idx ;",false);// added in case of conflicts with upgrade from 14stable
  495. execute_sql("CREATE INDEX {$CFG->prefix}user_idnumber_idx ON {$CFG->prefix}user (idnumber)");
  496. execute_sql("CREATE INDEX {$CFG->prefix}user_auth_idx ON {$CFG->prefix}user (auth)");
  497. }
  498. if ($oldversion < 2004092000) { //redoing this just to be sure that column type is text (postgres type changes didnt work when this was done first time)
  499. table_column("config", "value", "value", "text", "", "", "");
  500. }
  501. if ($oldversion < 2004093001) { // add new table for sessions storage
  502. execute_sql(" CREATE TABLE {$CFG->prefix}sessions (
  503. sesskey char(32) PRIMARY KEY,
  504. expiry integer NOT null,
  505. expireref varchar(64),
  506. data text NOT null
  507. );");
  508. execute_sql(" CREATE INDEX {$CFG->prefix}sessions_expiry_idx ON {$CFG->prefix}sessions (expiry)");
  509. }
  510. if ($oldversion < 2004111500) { // Update any users/courses using wrongly-named lang pack
  511. execute_sql("UPDATE {$CFG->prefix}user SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
  512. execute_sql("UPDATE {$CFG->prefix}course SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
  513. }
  514. if ($oldversion < 2004111700) { // add indexes- drop them first silently to avoid conflicts when upgrading.
  515. execute_sql("DROP INDEX {$CFG->prefix}course_idnumber_idx;",false);
  516. execute_sql("DROP INDEX {$CFG->prefix}course_shortname_idx;",false);
  517. execute_sql("DROP INDEX {$CFG->prefix}user_students_userid_idx;",false);
  518. execute_sql("DROP INDEX {$CFG->prefix}user_teachers_userid_idx;",false);
  519. modify_database("","CREATE INDEX {$CFG->prefix}course_idnumber_idx ON {$CFG->prefix}course (idnumber);" );
  520. modify_database("","CREATE INDEX {$CFG->prefix}course_shortname_idx ON {$CFG->prefix}course (shortname);" );
  521. modify_database("","CREATE INDEX {$CFG->prefix}user_students_userid_idx ON {$CFG->prefix}user_students (userid);");
  522. modify_database("","CREATE INDEX {$CFG->prefix}user_teachers_userid_idx ON {$CFG->prefix}user_teachers (userid);");
  523. }
  524. if ($oldversion < 2004111700) { // add an index to event for timestart and timeduration- drop them first silently to avoid conflicts when upgrading.
  525. execute_sql("DROP INDEX {$CFG->prefix}event_timestart_idx;",false);
  526. execute_sql("DROP INDEX {$CFG->prefix}event_timeduration_idx;",false);
  527. modify_database('','CREATE INDEX prefix_event_timestart_idx ON prefix_event (timestart);');
  528. modify_database('','CREATE INDEX prefix_event_timeduration_idx ON prefix_event (timeduration);');
  529. }
  530. if ($oldversion < 2004117000) { // add an index on the groups_members table- drop them first silently to avoid conflicts when upgrading.
  531. execute_sql("DROP INDEX {$CFG->prefix}groups_members_userid_idx;",false);
  532. modify_database('','CREATE INDEX prefix_groups_members_userid_idx ON prefix_groups_members (userid);');
  533. }
  534. if ($oldversion < 2004111700) { //add indexes on modules and course_modules- drop them first silently to avoid conflicts when upgrading.
  535. execute_sql("DROP INDEX {$CFG->prefix}course_modules_visible_idx;",false);
  536. execute_sql("DROP INDEX {$CFG->prefix}course_modules_course_idx;",false);
  537. execute_sql("DROP INDEX {$CFG->prefix}course_modules_module_idx;",false);
  538. execute_sql("DROP INDEX {$CFG->prefix}course_modules_instance_idx;",false);
  539. execute_sql("DROP INDEX {$CFG->prefix}course_modules_deleted_idx;",false);
  540. execute_sql("DROP INDEX {$CFG->prefix}modules_name_idx;",false);
  541. modify_database('','CREATE INDEX prefix_course_modules_visible_idx ON prefix_course_modules (visible);');
  542. modify_database('','CREATE INDEX prefix_course_modules_course_idx ON prefix_course_modules (course);');
  543. modify_database('','CREATE INDEX prefix_course_modules_module_idx ON prefix_course_modules (module);');
  544. modify_database('','CREATE INDEX prefix_course_modules_instance_idx ON prefix_course_modules (instance);');
  545. modify_database('','CREATE INDEX prefix_course_modules_deleted_idx ON prefix_course_modules (deleted);');
  546. modify_database('','CREATE INDEX prefix_modules_name_idx ON prefix_modules (name);');
  547. }
  548. if ($oldversion < 2004111700) { // add an index on user students timeaccess (used for sorting)- drop them first silently to avoid conflicts when upgrading
  549. execute_sql("DROP INDEX {$CFG->prefix}user_students_timeaccess_idx;",false);
  550. modify_database('','CREATE INDEX prefix_user_students_timeaccess_idx ON prefix_user_students (timeaccess);');
  551. }
  552. if ($oldversion < 2004111700) { //add indexes on faux foreign keys - drop them first silently to avoid conflicts when upgrading.
  553. execute_sql("DROP INDEX {$CFG->prefix}course_sections_coursesection_idx;",false);
  554. execute_sql("DROP INDEX {$CFG->prefix}scale_courseid_idx;",false);
  555. execute_sql("DROP INDEX {$CFG->prefix}user_admins_userid_idx;",false);
  556. execute_sql("DROP INDEX {$CFG->prefix}user_coursecreators_userid_idx;",false);
  557. modify_database('','CREATE INDEX prefix_course_sections_coursesection_idx ON prefix_course_sections (course,section);');
  558. modify_database('','CREATE INDEX prefix_scale_courseid_idx ON prefix_scale (courseid);');
  559. modify_database('','CREATE INDEX prefix_user_admins_userid_idx ON prefix_user_admins (userid);');
  560. modify_database('','CREATE INDEX prefix_user_coursecreators_userid_idx ON prefix_user_coursecreators (userid);');
  561. }
  562. if ($oldversion < 2004111700) { // make new indexes on user table.
  563. fix_course_sortorder(0,0,1);
  564. execute_sql("DROP INDEX {$CFG->prefix}course_category_idx;",false);
  565. execute_sql("DROP INDEX {$CFG->prefix}course_category_sortorder_uk;",false);
  566. modify_database('', "CREATE UNIQUE INDEX prefix_course_category_sortorder_uk ON prefix_course(category,sortorder)");
  567. execute_sql("DROP INDEX {$CFG->prefix}user_deleted_idx;",false);
  568. execute_sql("DROP INDEX {$CFG->prefix}user_confirmed_idx;",false);
  569. execute_sql("DROP INDEX {$CFG->prefix}user_firstname_idx;",false);
  570. execute_sql("DROP INDEX {$CFG->prefix}user_lastname_idx;",false);
  571. execute_sql("DROP INDEX {$CFG->prefix}user_city_idx;",false);
  572. execute_sql("DROP INDEX {$CFG->prefix}user_country_idx;",false);
  573. execute_sql("DROP INDEX {$CFG->prefix}user_lastaccess_idx;",false);
  574. modify_database("","CREATE INDEX prefix_user_deleted_idx ON prefix_user (deleted)");
  575. modify_database("","CREATE INDEX prefix_user_confirmed_idx ON prefix_user (confirmed)");
  576. modify_database("","CREATE INDEX prefix_user_firstname_idx ON prefix_user (firstname)");
  577. modify_database("","CREATE INDEX prefix_user_lastname_idx ON prefix_user (lastname)");
  578. modify_database("","CREATE INDEX prefix_user_city_idx ON prefix_user (city)");
  579. modify_database("","CREATE INDEX prefix_user_country_idx ON prefix_user (country)");
  580. modify_database("","CREATE INDEX prefix_user_lastaccess_idx ON prefix_user (lastaccess)");
  581. }
  582. if ($oldversion < 2004111700) { // one more index for email (for sorting)
  583. execute_sql("DROP INDEX {$CFG->prefix}user_email_idx;",false);
  584. modify_database('','CREATE INDEX prefix_user_email_idx ON prefix_user (email);');
  585. }
  586. if ($oldversion < 2004112200) { // new 'enrol' field for enrolment tables
  587. table_column('user_students', '', 'enrol', 'varchar', '20', '', '', 'not null');
  588. table_column('user_teachers', '', 'enrol', 'varchar', '20', '', '', 'not null');
  589. modify_database("","CREATE INDEX {$CFG->prefix}user_students_enrol_idx ON {$CFG->prefix}user_students (enrol);");
  590. modify_database("","CREATE INDEX {$CFG->prefix}user_teachers_enrol_idx ON {$CFG->prefix}user_teachers (enrol);");
  591. }
  592. if ($oldversion < 2004112300) { // update log display to use correct postgres friendly sql
  593. execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='user' AND action='view' AND mtable='user'");
  594. execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='course' AND action='user report' AND mtable='user'");
  595. }
  596. if ($oldversion < 2004112400) {
  597. /// Delete duplicate enrolments
  598. /// and then tell the database course,userid is a unique combination
  599. if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
  600. "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
  601. foreach ($users as $user) {
  602. delete_records_select("user_students", "userid = '$user->userid' ".
  603. "AND course = '$user->course' AND id <> '$user->id'");
  604. }
  605. }
  606. flush();
  607. // drop some indexes quietly -- they may or may not exist depending on what version
  608. // the user upgrades from
  609. execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_idx ", false);
  610. execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_uk ", false);
  611. modify_database('','CREATE UNIQUE INDEX prefix_user_students_courseuserid_uk ON prefix_user_students (course,userid);');
  612. /// Delete duplicate teacher enrolments
  613. /// and then tell the database course,userid is a unique combination
  614. if ($users = get_records_select("user_teachers", "userid > 0 GROUP BY course, userid ".
  615. "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
  616. foreach ($users as $user) {
  617. delete_records_select("user_teachers", "userid = '$user->userid' ".
  618. "AND course = '$user->course' AND id <> '$user->id'");
  619. }
  620. }
  621. flush();
  622. // drop some indexes quietly -- they may or may not exist depending on what version
  623. // the user upgrades from
  624. execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_idx ", false);
  625. execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_uk ", false);
  626. modify_database('','CREATE UNIQUE INDEX prefix_user_teachers_courseuserid_uk ON prefix_user_teachers (course,userid);');
  627. }
  628. if ($oldversion < 2004112401) {
  629. // some postgres databases may have a non-unique index mislabeled unique.
  630. fix_course_sortorder(0,0,1);
  631. execute_sql("DROP INDEX {$CFG->prefix}course_category_sortorder_uk ", false);
  632. execute_sql("DROP INDEX {$CFG->prefix}course_category_idx ", false);
  633. modify_database('', "CREATE UNIQUE INDEX prefix_course_category_sortorder_uk ON prefix_course(category,sortorder);");
  634. // odd! username was missing its unique index!
  635. // first silently drop it just in case...
  636. execute_sql("ALTER TABLE {$CFG->prefix}user DROP CONSTRAINT {$CFG->prefix}user_username_uk;", false);
  637. execute_sql("DROP INDEX {$CFG->prefix}user_username_uk", false);
  638. modify_database('', "CREATE UNIQUE INDEX prefix_user_username_uk ON prefix_user (username);");
  639. }
  640. if ($oldversion < 2004112900) {
  641. table_column('user', '', 'policyagreed', 'integer', '1', 'unsigned', '0', 'not null', 'confirmed');
  642. }
  643. if ($oldversion < 2004121400) {
  644. table_column('groups', '', 'password', 'varchar', '50', '', '', 'not null', 'description');
  645. }
  646. if ($oldversion < 2004121600) {
  647. modify_database('',"CREATE TABLE prefix_dst_preset (
  648. id SERIAL PRIMARY KEY,
  649. name varchar(48) NOT NULL default '',
  650. apply_offset integer NOT NULL default '0',
  651. activate_index integer NOT NULL default '1',
  652. activate_day integer NOT NULL default '1',
  653. activate_month integer NOT NULL default '1',
  654. activate_time char(5) NOT NULL default '03:00',
  655. deactivate_index integer NOT NULL default '1',
  656. deactivate_day integer NOT NULL default '1',
  657. deactivate_month integer NOT NULL default '2',
  658. deactivate_time char(5) NOT NULL default '03:00',
  659. last_change integer NOT NULL default '0',
  660. next_change integer NOT NULL default '0',
  661. current_offset integer NOT NULL default '0'
  662. );");
  663. }
  664. if ($oldversion < 2004122800) {
  665. execute_sql("DROP TABLE {$CFG->prefix}message", false);
  666. execute_sql("DROP TABLE {$CFG->prefix}message_read", false);
  667. execute_sql("DROP TABLE {$CFG->prefix}message_contacts", false);
  668. execute_sql("DROP INDEX {$CFG->prefix}message_useridfrom_idx", false);
  669. execute_sql("DROP INDEX {$CFG->prefix}message_useridto_idx", false);
  670. execute_sql("DROP INDEX {$CFG->prefix}message_read_useridfrom_idx", false);
  671. execute_sql("DROP INDEX {$CFG->prefix}message_read_useridto_idx", false);
  672. execute_sql("DROP INDEX {$CFG->prefix}message_contacts_useridcontactid_idx", false);
  673. modify_database('',"CREATE TABLE prefix_message (
  674. id SERIAL PRIMARY KEY,
  675. useridfrom integer NOT NULL default '0',
  676. useridto integer NOT NULL default '0',
  677. message text,
  678. timecreated integer NOT NULL default '0',
  679. messagetype varchar(50) NOT NULL default ''
  680. );
  681. CREATE INDEX prefix_message_useridfrom_idx ON prefix_message (useridfrom);
  682. CREATE INDEX prefix_message_useridto_idx ON prefix_message (useridto);
  683. CREATE TABLE prefix_message_read (
  684. id SERIAL PRIMARY KEY,
  685. useridfrom integer NOT NULL default '0',
  686. useridto integer NOT NULL default '0',
  687. message text,
  688. timecreated integer NOT NULL default '0',
  689. timeread integer NOT NULL default '0',
  690. messagetype varchar(50) NOT NULL default '',
  691. mailed integer NOT NULL default '0'
  692. );
  693. CREATE INDEX prefix_message_read_useridfrom_idx ON prefix_message_read (useridfrom);
  694. CREATE INDEX prefix_message_read_useridto_idx ON prefix_message_read (useridto);
  695. ");
  696. modify_database('',"CREATE TABLE prefix_message_contacts (
  697. id SERIAL PRIMARY KEY,
  698. userid integer NOT NULL default '0',
  699. contactid integer NOT NULL default '0',
  700. blocked integer NOT NULL default '0'
  701. );
  702. CREATE INDEX prefix_message_contacts_useridcontactid_idx ON prefix_message_contacts (userid,contactid);
  703. ");
  704. modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'write', 'user', 'firstname||\' \'||lastname');
  705. INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'read', 'user', 'firstname||\' \'||lastname');
  706. ");
  707. }
  708. if ($oldversion < 2004122801) {
  709. table_column('message', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
  710. table_column('message_read', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
  711. }
  712. if ($oldversion < 2005010100) {
  713. modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'add contact', 'user', 'firstname||\' \'||lastname');
  714. INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'remove contact', 'user', 'firstname||\' \'||lastname');
  715. INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'block contact', 'user', 'firstname||\' \'||lastname');
  716. INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'unblock contact', 'user', 'firstname||\' \'||lastname');
  717. ");
  718. }
  719. if ($oldversion < 2005011000) { // Create a .htaccess file in dataroot, just in case
  720. if (!file_exists($CFG->dataroot.'/.htaccess')) {
  721. if ($handle = fopen($CFG->dataroot.'/.htaccess', 'w')) { // For safety
  722. @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
  723. @fclose($handle);
  724. notify("Created a default .htaccess file in $CFG->dataroot");
  725. }
  726. }
  727. }
  728. if ($oldversion < 2005012500) { // add new table for meta courses.
  729. /*
  730. modify_database("","CREATE TABLE prefix_meta_course (
  731. id SERIAL primary key,
  732. parent_course integer NOT NULL,
  733. child_course integer NOT NULL
  734. );");
  735. modify_database("","CREATE INDEX prefix_meta_course_parent_idx ON prefix_meta_course (parent_course);");
  736. modify_database("","CREATE INDEX prefix_meta_course_child_idx ON prefix_meta_course (child_course);");
  737. table_column('course','','meta_course','integer','1','','0','not null');
  738. */ // taking this OUT for upgrade from 1.4 to 1.5 (those tracking head will have already seen it)
  739. }
  740. if ($oldversion < 2005012501) { //fix table names for consistency
  741. execute_sql("DROP TABLE {$CFG->prefix}meta_course",false); // drop silently
  742. execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN meta_course",false); // drop silently
  743. modify_database("","CREATE TABLE prefix_course_meta (
  744. id SERIAL primary key,
  745. parent_course integer NOT NULL,
  746. child_course integer NOT NULL
  747. );");
  748. modify_database("","CREATE INDEX prefix_course_meta_parent_idx ON prefix_course_meta (parent_course);");
  749. modify_database("","CREATE INDEX prefix_course_meta_child_idx ON prefix_course_meta (child_course);");
  750. table_column('course','','metacourse','integer','1','','0','not null');
  751. }
  752. if ($oldversion < 2005020100) {
  753. fix_course_sortorder(0, 1, 1);
  754. }
  755. if ($oldversion < 2005021000) { // New fields for theme choices
  756. table_column('course', '', 'theme', 'varchar', '50', '', '', '', 'lang');
  757. table_column('groups', '', 'theme', 'varchar', '50', '', '', '', 'lang');
  758. table_column('user', '', 'theme', 'varchar', '50', '', '', '', 'lang');
  759. set_config('theme', 'standardwhite'); // Reset to a known good theme
  760. }
  761. if ($oldversion < 2005021700) {
  762. table_column('user', '', 'dstpreset', 'int', '10', '', '0', 'not null', 'timezone');
  763. }
  764. if ($oldversion < 2005021800) {
  765. modify_database("","CREATE TABLE adodb_logsql (
  766. created timestamp NOT NULL,
  767. sql0 varchar(250) NOT NULL,
  768. sql1 text NOT NULL,
  769. params text NOT NULL,
  770. tracer text NOT NULL,
  771. timer decimal(16,6) NOT NULL
  772. );");
  773. }
  774. if ($oldversion < 2005022400) {
  775. table_column('dst_preset', '', 'family', 'varchar', '100', '', '', 'not null', 'name');
  776. table_column('dst_preset', '', 'year', 'int', '10', '', '0', 'not null', 'family');
  777. }
  778. if ($oldversion < 2005030501) {
  779. table_column('user', '', 'msn', 'varchar', '50', '', '', '', 'icq');
  780. table_column('user', '', 'aim', 'varchar', '50', '', '', '', 'icq');
  781. table_column('user', '', 'yahoo', 'varchar', '50', '', '', '', 'icq');
  782. table_column('user', '', 'skype', 'varchar', '50', '', '', '', 'icq');
  783. }
  784. if ($oldversion < 2005032300) {
  785. table_column('user', 'dstpreset', 'timezonename', 'varchar', '100');
  786. execute_sql('UPDATE '.$CFG->prefix.'user SET timezonename = \'\'');
  787. }
  788. if ($oldversion < 2005032600) {
  789. execute_sql('DROP TABLE '.$CFG->prefix.'dst_preset', false);
  790. modify_database('',"CREATE TABLE prefix_timezone (
  791. id SERIAL PRIMARY KEY,
  792. name varchar(100) NOT NULL default '',
  793. year integer NOT NULL default '0',
  794. rule varchar(20) NOT NULL default '',
  795. gmtoff integer NOT NULL default '0',
  796. dstoff integer NOT NULL default '0',
  797. dst_month integer NOT NULL default '0',
  798. dst_startday integer NOT NULL default '0',
  799. dst_weekday integer NOT NULL default '0',
  800. dst_skipweeks integer NOT NULL default '0',
  801. dst_time varchar(5) NOT NULL default '00:00',
  802. std_month integer NOT NULL default '0',
  803. std_startday integer NOT NULL default '0',
  804. std_weekday integer NOT NULL default '0',
  805. std_skipweeks integer NOT NULL default '0',
  806. std_time varchar(5) NOT NULL default '00:00'
  807. );");
  808. }
  809. if ($oldversion < 2005032800) {
  810. modify_database('',"CREATE TABLE prefix_grade_category (
  811. id SERIAL PRIMARY KEY,
  812. name varchar(64) default NULL,
  813. courseid integer NOT NULL default '0',
  814. drop_x_lowest integer NOT NULL default '0',
  815. bonus_points integer NOT NULL default '0',
  816. hidden integer NOT NULL default '0',
  817. weight decimal(4,2) default '0.00'
  818. );");
  819. modify_database('',"CREATE INDEX prefix_grade_category_courseid_idx ON prefix_grade_category (courseid);");
  820. modify_database('',"CREATE TABLE prefix_grade_exceptions (
  821. id SERIAL PRIMARY KEY,
  822. courseid integer NOT NULL default '0',
  823. grade_itemid integer NOT NULL default '0',
  824. userid integer NOT NULL default '0'
  825. );");
  826. modify_database('',"CREATE INDEX prefix_grade_exceptions_courseid_idx ON prefix_grade_exceptions (courseid);");
  827. modify_database('',"CREATE TABLE prefix_grade_item (
  828. id SERIAL PRIMARY KEY,
  829. courseid integer default NULL,
  830. category integer default NULL,
  831. modid integer default NULL,
  832. cminstance integer default NULL,
  833. scale_grade float(11) default '1.0000000000',
  834. extra_credit integer NOT NULL default '0',
  835. sort_order integer NOT NULL default '0'
  836. );");
  837. modify_database('',"CREATE INDEX prefix_grade_item_courseid_idx ON prefix_grade_item (courseid);");
  838. modify_database('',"CREATE TABLE prefix_grade_letter (
  839. id SERIAL PRIMARY KEY,
  840. courseid integer NOT NULL default '0',
  841. letter varchar(8) NOT NULL default 'NA',
  842. grade_high decimal(6,2) NOT NULL default '100.00',
  843. grade_low decimal(6,2) NOT NULL default '0.00'
  844. );");
  845. modify_database('',"CREATE INDEX prefix_grade_letter_courseid_idx ON prefix_grade_letter (courseid);");
  846. modify_database('',"CREATE TABLE prefix_grade_preferences (
  847. id SERIAL PRIMARY KEY,
  848. courseid integer default NULL,
  849. preference integer NOT NULL default '0',
  850. value integer NOT NULL default '0'
  851. );");
  852. modify_database('',"CREATE UNIQUE INDEX prefix_grade_prefs_courseidpref_uk ON prefix_grade_preferences (courseid,preference);");
  853. }
  854. if ($oldversion < 2005033100) { // Get rid of defunct field from course modules table
  855. delete_records('course_modules', 'deleted', 1); // Delete old records we don't need any more
  856. execute_sql('DROP INDEX '.$CFG->prefix.'course_modules_deleted_idx;'); // Old index
  857. execute_sql('ALTER TABLE '.$CFG->prefix.'course_modules DROP deleted;'); // Old field
  858. }
  859. if ($oldversion < 2005040800) {
  860. table_column('user', 'timezone', 'timezone', 'varchar', '100', '', '99');
  861. execute_sql(" ALTER TABLE {$CFG->prefix}user DROP timezonename ");
  862. }
  863. if ($oldversion < 2005041101) {
  864. require_once($CFG->libdir.'/filelib.php');
  865. if (is_readable($CFG->dirroot.'/lib/timezones.txt')) { // Distribution file
  866. if ($timezones = get_records_csv($CFG->dirroot.'/lib/timezones.txt', 'timezone')) {
  867. $db->debug = false;
  868. update_timezone_records($timezones);
  869. notify(count($timezones).' timezones installed');
  870. $db->debug = true;
  871. }
  872. }
  873. }
  874. if ($oldversion < 2005041900) { // Copy all Dialogue entries into Messages, and hide Dialogue module
  875. if ($entries = get_records_sql('SELECT e.id, e.userid, c.recipientid, e.text, e.timecreated
  876. FROM '.$CFG->prefix.'dialogue_conversations c,
  877. '.$CFG->prefix.'dialogue_entries e
  878. WHERE e.conversationid = c.id')) {
  879. foreach ($entries as $entry) {
  880. $message = NULL;
  881. $message->useridfrom = $entry->userid;
  882. $message->useridto = $entry->recipientid;
  883. $message->message = addslashes($entry->text);
  884. $message->format = FORMAT_HTML;
  885. $message->timecreated = $entry->timecreated;
  886. $message->messagetype = 'direct';
  887. insert_record('message_read', $message);
  888. }
  889. }
  890. set_field('modules', 'visible', 0, 'name', 'dialogue');
  891. notify('The Dialogue module has been disabled, and all the old Messages from it copied into the new standard Message feature. If you really want Dialogue back, you can enable it using the "eye" icon here: Admin >> Modules >> Dialogue');
  892. }
  893. if ($oldversion < 2005042100) {
  894. $result = table_column('event', '', 'repeatid', 'int', '10', 'unsigned', '0', 'not null', 'userid') && $result;
  895. }
  896. if ($oldversion < 2005042400) { // Add user tracking prefs field.
  897. table_column('user', '', 'trackforums', 'int', '4', 'unsigned', '0', 'not null', 'autosubscribe');
  898. }
  899. if ($oldversion < 2005051500) { // Add user tracking prefs field.
  900. table_column('grade_category', 'weight', 'weight', 'numeric(5,2)', '', '', '0.00', '', '');
  901. }
  902. if ($oldversion < 2005053000 ) { // Add config_plugins table
  903. // this table was created on the MOODLE_15_STABLE branch
  904. // so it may already exist. Therefore we hide potential errors
  905. // (Postgres doesn't support CREATE TABLE IF NOT EXISTS)
  906. execute_sql("CREATE TABLE {$CFG->prefix}config_plugins (
  907. id SERIAL PRIMARY KEY,
  908. plugin varchar(100) NOT NULL default 'core',
  909. name varchar(100) NOT NULL default '',
  910. value text NOT NULL default '',
  911. CONSTRAINT {$CFG->prefix}config_plugins_plugin_name_uk UNIQUE (plugin, name)
  912. );", false);
  913. }
  914. if ($oldversion < 2005060200) { // migrate some config items to config_plugins table
  915. // NOTE: this block is in both postgres AND mysql upgrade
  916. // files. If you edit either, update the otherone.
  917. $user_fields = array("firstname", "lastname", "email",
  918. "phone1", "phone2", "department",
  919. "address", "city", "country",
  920. "description", "idnumber", "lang");
  921. if (!empty($CFG->auth)) { // if we have no auth, just pass
  922. foreach ($user_fields as $field) {
  923. $suffixes = array('', '_editlock', '_updateremote', '_updatelocal');
  924. foreach ($suffixes as $suffix) {
  925. $key = 'auth_user_' . $field . $suffix;
  926. if (isset($CFG->$key)) {
  927. // translate keys & values
  928. // to the new convention
  929. // this should support upgrading
  930. // even 1.5dev installs
  931. $newkey = $key;
  932. $newval = $CFG->$key;
  933. if ($suffix === '') {
  934. $newkey = 'field_map_' . $field;
  935. } elseif ($suffix === '_editlock') {
  936. $newkey = 'field_lock_' . $field;
  937. $newval = ($newval==1) ? 'locked' : 'unlocked'; // translate 0/1 to locked/unlocked
  938. } elseif ($suffix === '_updateremote') {
  939. $newkey = 'field_updateremote_' . $field;
  940. } elseif ($suffix === '_updatelocal') {
  941. $newkey = 'field_updatelocal_' . $field;
  942. $newval = ($newval==1) ? 'onlogin' : 'oncreate'; // translate 0/1 to locked/unlocked
  943. }
  944. if (!(set_config($newkey, addslashes($newval), 'auth/'.$CFG->auth)
  945. && delete_records('config', 'name', $key))) {
  946. notify("Error updating Auth configuration $key to {$CFG->auth} $newkey .");
  947. $result = false;
  948. }
  949. } // end if isset key
  950. } // end foreach suffix
  951. } // end foreach field
  952. }
  953. }
  954. if ($oldversion < 2005060201) { // Close down the Attendance module, we are removing it from CVS.
  955. if (!file_exists($CFG->dirroot.'/mod/attendance/lib.php')) {
  956. if (count_records('attendance')) { // We have some data, so should keep it
  957. set_field('modules', 'visible', 0, 'name', 'attendance');
  958. notify('The Attendance module has been discontinued. If you really want to
  959. continue using it, you should download it individually from
  960. http://download.moodle.org/modules and install it, then
  961. reactivate it from Admin >> Configuration >> Modules.
  962. None of your existing data has been deleted, so all existing
  963. Attendance activities should re-appear.');
  964. } else { // No data, so do a complete delete
  965. execute_sql('DROP TABLE '.$CFG->prefix.'attendance', false);
  966. delete_records('modules', 'name', 'attendance');
  967. notify("The Attendance module has been discontinued and removed from your site.
  968. You weren't using it anyway. ;-)");
  969. }
  970. }
  971. }
  972. if ($oldversion < 2005060223) { // Mass cleanup of bad postgres upgrade scripts
  973. execute_sql("DROP TABLE {$CFG->prefix}attendance_roll", false); // There are no attendance module anymore
  974. modify_database('','ALTER TABLE prefix_config ALTER value SET NOT NULL');
  975. modify_database('','ALTER TABLE prefix_course ALTER metacourse SET NOT NULL');
  976. modify_database('','ALTER TABLE prefix_course ALTER theme SET NOT NULL');
  977. modify_database('','ALTER TABLE prefix_event ALTER repeatid SET NOT NULL');
  978. modify_database('','ALTER TABLE prefix_groups ALTER password SET NOT NULL');
  979. modify_database('','ALTER TABLE prefix_groups ALTER theme SET NOT NULL');
  980. modify_database('','ALTER TABLE prefix_message ALTER format SET NOT NULL');
  981. modify_database('','ALTER TABLE prefix_message_read ALTER format SET NOT NULL');
  982. modify_database('','ALTER TABLE prefix_groups ALTER theme SET NOT NULL');
  983. modify_database('','ALTER TABLE prefix_user ALTER aim DROP DEFAULT');
  984. modify_database('','ALTER TABLE prefix_user ALTER idnumber DROP DEFAULT');
  985. modify_database('','ALTER TABLE prefix_user ALTER msn DROP DEFAULT');
  986. modify_database('','ALTER TABLE prefix_user ALTER policyagreed SET NOT NULL');
  987. modify_database('','ALTER TABLE prefix_user ALTER skype DROP DEFAULT');
  988. modify_database('','ALTER TABLE prefix_user ALTER theme SET NOT NULL');
  989. modify_database('','ALTER TABLE prefix_user ALTER timezone SET NOT NULL');
  990. modify_database('','ALTER TABLE prefix_user ALTER trackforums SET NOT NULL');
  991. modify_database('','ALTER TABLE prefix_user ALTER yahoo DROP DEFAULT');
  992. modify_database('','ALTER TABLE prefix_user_students ALTER enrol SET NOT NULL');
  993. modify_database('','ALTER TABLE prefix_user_teachers ALTER enrol SET NOT NULL');
  994. }
  995. if ($oldversion < 2005071700) { // Close down the Dialogue module, we are removing it from CVS.
  996. if (!file_exists($CFG->dirroot.'/mod/dialogue/lib.php')) {
  997. if (count_records('dialogue')) { // We have some data, so should keep it
  998. set_field('modules', 'visible', 0, 'name', 'dialogue');
  999. notify('The Dialogue module has been discontinued. If you really want to
  1000. continue using it, you should download it individually from
  1001. http://download.moodle.org/modules and install it, then
  1002. reactivate it from Admin >> Configuration >> Modules.
  1003. None of your existing data has been deleted, so all existing
  1004. Dialogue activities should re-appear.');
  1005. } else { // No data, so do a complete delete
  1006. execute_sql('DROP TABLE '.$CFG->prefix.'dialogue', false);
  1007. delete_records('modules', 'name', 'dialogue');
  1008. notify("The Dialogue module has been discontinued and removed from your site.
  1009. You weren't using it anyway. ;-)");
  1010. }
  1011. }
  1012. }
  1013. if ($oldversion < 2005072000) { // Add a couple fields to mdl_event to work towards iCal import/export
  1014. table_column('event', '', 'uuid', 'char', '36', '', '', 'not null', 'visible');
  1015. table_column('event', '', 'sequence', 'integer', '10', 'unsigned', '1', 'not null', 'uuid');
  1016. }
  1017. if ($oldversion < 2005072100) { // run the online assignment cleanup code
  1018. include($CFG->dirroot.'/'.$CFG->admin.'/oacleanup.php');
  1019. if (function_exists('online_assignment_cleanup')) {
  1020. online_assignment_cleanup();
  1021. }
  1022. }
  1023. if ($oldversion < 2005072200) { // fix the mistakenly-added currency stuff from enrol/authorize
  1024. execute_sql("DROP TABLE {$CFG->prefix}currencies", false); // drop silently
  1025. execute_sql("ALTER TABLE {$CFG->prefix}course DROP currency", false);
  1026. $defaultcurrency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency;
  1027. table_column('course', '', 'currency', 'char', '3', '', $defaultcurrency, 'not null', 'cost');
  1028. }
  1029. if ($oldversion < 2005081600) { //set up the course requests table
  1030. modify_database('',"CREATE TABLE prefix_course_request (
  1031. id SERIAL PRIMARY KEY,
  1032. fullname varchar(254) NOT NULL default '',
  1033. shortname varchar(15) NOT NULL default '',
  1034. summary text NOT NULL default '',
  1035. reason text NOT NULL default '',
  1036. requester INTEGER NOT NULL default 0
  1037. );");
  1038. modify_database('','CREATE INDEX prefix_course_request_shortname_idx ON prefix_course_request (shortname);');
  1039. table_column('course','','requested');
  1040. }
  1041. if ($oldversion < 2005081601) {
  1042. modify_database('','CREATE TABLE prefix_course_allowed_modules (
  1043. id SERIAL PRIMARY KEY,
  1044. course INTEGER NOT NULL default 0,
  1045. module INTEGER NOT NULL default 0
  1046. );');
  1047. modify_database('','CREATE INDEX prefix_course_allowed_modules_course_idx ON prefix_course_allowed_modules (course);');
  1048. modify_database('','CREATE INDEX prefix_course_allowed_modules_module_idx ON prefix_course_allowed_modules (module);');
  1049. table_column('course','','restrictmodules','int','1','','0','not null');
  1050. }
  1051. if ($oldversion < 2005081700) {
  1052. table_column('course_categories','','depth','integer');
  1053. table_column('course_categories','','path','varchar','255');
  1054. }
  1055. if ($oldversion < 2005090100) { // stats!
  1056. modify_database('','CREATE TABLE prefix_stats_daily (
  1057. id SERIAL PRIMARY KEY,
  1058. courseid INTEGER NOT NULL default 0,
  1059. timeend INTEGER NOT NULL default 0,
  1060. students INTEGER NOT NULL default 0,
  1061. teachers INTEGER NOT NULL default 0,
  1062. activestudents INTEGER NOT NULL default 0,
  1063. activeteachers INTEGER NOT NULL default 0,
  1064. studentreads INTEGER NOT NULL default 0,
  1065. studentwrites INTEGER NOT NULL default 0,
  1066. teacherreads INTEGER NOT NULL default 0,
  1067. teacherwrites INTEGER NOT NULL default 0,
  1068. logins INTEGER NOT NULL default 0,
  1069. uniquelogins INTEGER NOT NULL default 0
  1070. );');
  1071. modify_database('','CREATE INDEX prefix_stats_daily_courseid_idx ON prefix_stats_daily (courseid);');
  1072. modify_database('','CREATE INDEX prefix_stats_daily_timeend_idx ON prefix_stats_daily (timeend);');
  1073. modify_database('','CREATE TABLE prefix_stats_weekly (
  1074. id SERIAL PRIMARY KEY,
  1075. courseid INTEGER NOT NULL default 0,
  1076. timeend INTEGER NOT NULL default 0,
  1077. students INTEGER NOT NULL default 0,
  1078. teachers INTEGER NOT NULL default 0,
  1079. activestudents INTEGER NOT NULL default 0,
  1080. activeteachers INTEGER NOT NULL default 0,
  1081. studentreads INTEGER NOT NULL default 0,
  1082. studentwrites INTEGER NOT NULL default 0,
  1083. teacherreads INTEGER NOT NULL default 0,
  1084. teacherwrites INTEGER NOT NULL default 0,
  1085. logins INTEGER NOT NULL default 0,
  1086. uniquelogins INTEGER NOT NULL default 0
  1087. );');
  1088. modify_database('','CREATE INDEX prefix_stats_weekly_courseid_idx ON prefix_stats_weekly (courseid);');
  1089. modify_database('','CREATE INDEX prefix_stats_weekly_timeend_idx ON prefix_stats_weekly (timeend);');
  1090. modify_database('','CREATE TABLE prefix_stats_monthly (
  1091. id SERIAL PRIMARY KEY,
  1092. courseid INTEGER NOT NULL default 0,
  1093. timeend INTEGER NOT NULL default 0,
  1094. students INTEGER NOT NULL default 0,
  1095. teachers INTEGER NOT NULL default 0,
  1096. activestudents INTEGER NOT NULL default 0,
  1097. activeteachers INTEGER NOT NULL default 0,
  1098. studentreads INTEGER NOT NULL default 0,
  1099. studentwrites INTEGER NOT NULL default 0,
  1100. teacherreads INTEGER NOT NULL default 0,
  1101. teacherwrites INTEGER NOT NULL default 0,
  1102. logins INTEGER NOT NULL default 0,
  1103. uniquelogins INTEGER NOT NULL default 0
  1104. );');
  1105. modify_database('','CREATE INDEX prefix_stats_monthly_courseid_idx ON prefix_stats_monthly (courseid);');
  1106. modify_database('','CREATE INDEX prefix_stats_monthly_timeend_idx ON prefix_stats_monthly (timeend);');
  1107. modify_database("","CREATE TABLE prefix_stats_user_daily (
  1108. id SERIAL PRIMARY KEY,
  1109. courseid INTEGER NOT NULL default 0,
  1110. userid INTEGER NOT NULL default 0,
  1111. roleid INTEGER NOT NULL default 0,
  1112. timeend INTEGER NOT NULL default 0,
  1113. statsreads INTEGER NOT NULL default 0,
  1114. statswrites INTEGER NOT NULL default 0,
  1115. stattype varchar(30) NOT NULL default ''
  1116. );");
  1117. modify_database("","CREATE INDEX prefix_stats_user_daily_courseid_idx ON prefix_stats_user_daily (courseid);");
  1118. modify_database("","CREATE INDEX prefix_stats_user_daily_userid_idx ON prefix_stats_user_daily (userid);");
  1119. modify_database("","CREATE INDEX prefix_stats_user_daily_roleid_idx ON prefix_stats_user_daily (roleid);");
  1120. modify_database("","CREATE INDEX prefix_stats_user_daily_timeend_idx ON prefix_stats_user_daily (timeend);");
  1121. modify_database("","CREATE TABLE prefix_stats_user_weekly (
  1122. id SERIAL PRIMARY KEY,
  1123. courseid INTEGER NOT NULL default 0,
  1124. userid INTEGER NOT NULL default 0,
  1125. roleid INTEGER NOT NULL default 0,
  1126. timeend INTEGER NOT NULL default 0,
  1127. statsreads INTEGER NOT NULL default 0,
  1128. statswrites INTEGER NOT NULL default 0,
  1129. stattype varchar(30) NOT NULL default ''
  1130. );");
  1131. modify_database("","CREATE INDEX prefix_stats_user_weekly_courseid_idx ON prefix_stats_user_weekly (courseid);");
  1132. modify_database("","CREATE INDEX prefix_stats_user_weekly_userid_idx ON prefix_stats_user_weekly (userid);");
  1133. modify_database("","CREATE INDEX prefix_stats_user_weekly_roleid_idx ON prefix_stats_user_weekly (roleid);");
  1134. modify_database("","CREATE INDEX prefix_stats_user_weekly_timeend_idx ON prefix_stats_user_weekly (timeend);");
  1135. modify_database("","CREATE TABLE prefix_stats_user_monthly (
  1136. id SERIAL PRIMARY KEY,
  1137. courseid INTEGER NOT NULL default 0,
  1138. userid INTEGER NOT NULL default 0,
  1139. roleid INTEGER NOT NULL default 0,
  1140. timeend INTEGER NOT NULL default 0,
  1141. statsreads INTEGER NOT NULL default 0,
  1142. statswrites INTEGER NOT NULL default 0,
  1143. stattype varchar(30) NOT NULL default ''
  1144. );");
  1145. modify_database("","CREATE INDEX prefix_stats_user_monthly_courseid_idx ON prefix_stats_user_monthly (courseid);");
  1146. modify_database("","CREATE INDEX prefix_stats_user_monthly_userid_idx ON prefix_stats_user_monthly (userid);");
  1147. modify_database("","CREATE INDEX prefix_stats_user_monthly_roleid_idx ON prefix_stats_user_monthly (roleid);");
  1148. modify_database("","CREATE INDEX prefix_stats_user_monthly_timeend_idx ON prefix_stats_user_monthly (timeend);");
  1149. }
  1150. if ($oldversion < 2005100300) {
  1151. table_column('course','','expirynotify','integer','1');
  1152. table_column('course','','expirythreshold','integer');
  1153. table_column('course','','notifystudents','integer','1');
  1154. $new = new stdClass();
  1155. $new->name = 'lastexpirynotify';
  1156. $new->value = 0;
  1157. insert_record('config', $new);
  1158. }
  1159. if ($oldversion < 2005100400) {
  1160. table_column('course','','enrollable','integer','1','unsigned','1');
  1161. table_column('course','','enrolstartdate','integer');
  1162. table_column('course','','enrolenddate','integer');
  1163. }
  1164. if ($oldversion < 2005101200) { // add enrolment key to course_request.
  1165. table_column('course_request','','password','text');
  1166. }
  1167. if ($oldversion < 2006030800) { # add extra indexes to log (see bug #4112)
  1168. modify_database('',"CREATE INDEX prefix_log_userid_idx ON prefix_log (userid);");
  1169. modify_database('',"CREATE INDEX prefix_log_info_idx ON prefix_log (info);");
  1170. }
  1171. if ($oldversion < 2006030900) {
  1172. table_column('course','','enrol','varchar','20','','');
  1173. if ($CFG->enrol == 'internal' || $CFG->enrol == 'manual') {
  1174. set_config('enrol_plugins_enabled', 'manual');
  1175. set_config('enrol', 'manual');
  1176. } else {
  1177. set_config('enrol_plugins_enabled', 'manual,'.$CFG->enrol);
  1178. }
  1179. require_once("$CFG->dirroot/enrol/enrol.class.php");
  1180. $defaultenrol = enrolment_factory::factory($CFG->enrol);
  1181. if (!method_exists($defaultenrol, 'print_entry')) { // switch enrollable to off for all courses in this case
  1182. modify_database('', 'UPDATE prefix_course SET enrollable = 0');
  1183. }
  1184. execute_sql("UPDATE {$CFG->prefix}user_students SET enrol='manual' WHERE enrol='' OR enrol='internal'");
  1185. execute_sql("UPDATE {$CFG->prefix}user_teachers SET enrol='manual' WHERE enrol=''");
  1186. }
  1187. if ($oldversion < 2006031000) {
  1188. modify_database("","CREATE TABLE prefix_post (
  1189. id SERIAL PRIMARY KEY,
  1190. userid INTEGER NOT NULL default 0,
  1191. courseid INTEGER NOT NULL default 0,
  1192. groupid INTEGER NOT NULL default 0,
  1193. moduleid INTEGER NOT NULL default 0,
  1194. coursemoduleid INTEGER NOT NULL default 0,
  1195. subject varchar(128) NOT NULL default '',
  1196. summary text,
  1197. content text,
  1198. uniquehash varchar(128) NOT NULL default '',
  1199. rating INTEGER NOT NULL default 0,
  1200. format INTEGER NOT NULL default 0,
  1201. publishstate varchar(10) CHECK (publishstate IN ('draft','site','public')) NOT NULL default 'draft',
  1202. lastmodified INTEGER NOT NULL default '0',
  1203. created INTEGER NOT NULL default '0'
  1204. );");
  1205. modify_database("","CREATE INDEX id_user_idx ON prefix_post (id, courseid);");
  1206. modify_database("","CREATE INDEX post_lastmodified_idx ON prefix_post (lastmodified);");
  1207. modify_database("","CREATE INDEX post_subject_idx ON prefix_post (subject);");
  1208. modify_database("","CREATE TABLE prefix_tags (
  1209. id SERIAL PRIMARY KEY,
  1210. type varchar(255) NOT NULL default 'official',
  1211. userid INTEGER NOT NULL default 0,
  1212. text varchar(255) NOT NULL default ''
  1213. );");
  1214. modify_database("","CREATE TABLE prefix_blog_tag_instance (
  1215. id SERIAL PRIMARY KEY,
  1216. entryid integer NOT NULL default 0,
  1217. tagid integer NOT NULL default 0,
  1218. groupid integer NOT NULL default 0,
  1219. courseid integer NOT NULL default 0,
  1220. userid integer NOT NULL default 0
  1221. );");
  1222. }
  1223. if ($oldversion < 2006031400) {
  1224. require_once("$CFG->dirroot/enrol/enrol.class.php");
  1225. $defaultenrol = enrolment_factory::factory($CFG->enrol);
  1226. if (!method_exists($defaultenrol, 'print_entry')) {
  1227. set_config('enrol', 'manual');
  1228. }
  1229. }
  1230. if ($oldversion < 2006032000) {
  1231. table_column('post','','module','varchar','20','','','not null', 'id');
  1232. modify_database('',"CREATE INDEX post_module_idx ON prefix_post (module);");
  1233. modify_database('',"UPDATE prefix_post SET module = 'blog';");
  1234. }
  1235. if ($oldversion < 2006032001) {
  1236. table_column('blog_tag_instance','','timemodified','integer','10','unsigned','0','not null', 'userid');
  1237. modify_database('',"CREATE INDEX bti_entryid_idx ON prefix_blog_tag_instance (entryid);");
  1238. modify_database('',"CREATE INDEX bti_tagid_idx ON prefix_blog_tag_instance (tagid);");
  1239. modify_database('',"UPDATE prefix_blog_tag_instance SET timemodified = '".time()."';");
  1240. }
  1241. if ($oldversion < 2006040500) { // Add an index to course_sections that was never upgraded (bug 5100)
  1242. execute_sql(" CREATE INDEX {$CFG->prefix}course_sections_coursesection_idx ON {$CFG->prefix}course_sections (course,section) ", false);
  1243. }
  1244. if ($oldversion < 2006041100) {
  1245. table_column('course_modules','','visibleold','integer','1','unsigned','1','not null', 'visible');
  1246. }
  1247. if ($oldversion < 2006042400) {
  1248. // Look through table log_display and get rid of duplicates.
  1249. $rs = get_recordset_sql('SELECT DISTINCT * FROM '.$CFG->prefix.'log_display');
  1250. // Drop the log_display table and create it back with an id field.
  1251. execute_sql("DROP TABLE {$CFG->prefix}log_display", false);
  1252. modify_database('', "CREATE TABLE prefix_log_display (
  1253. id SERIAL PRIMARY KEY,
  1254. module varchar(30) NOT NULL default '',
  1255. action varchar(40) NOT NULL default '',
  1256. mtable varchar(30) NOT NULL default '',
  1257. field varchar(50) NOT NULL default '')");
  1258. // Add index to ensure that module and action combination is unique.
  1259. modify_database('', 'CREATE INDEX prefix_log_display_moduleaction ON prefix_log_display (module,action)');
  1260. // Insert the records back in, sans duplicates.
  1261. if ($rs) {
  1262. while (!$rs->EOF) {
  1263. $sql = "INSERT INTO {$CFG->prefix}log_display ".
  1264. "VALUES('', '".$rs->fields['module']."', ".
  1265. "'".$rs->fields['action']."', ".
  1266. "'".$rs->fields['mtable']."', ".
  1267. "'".$rs->fields['field']."')";
  1268. execute_sql($sql, false);
  1269. $rs->MoveNext();
  1270. }
  1271. rs_close($rs);
  1272. }
  1273. }
  1274. // add 2 indexes to tags table
  1275. if ($oldversion < 2006042401) {
  1276. modify_database('',"CREATE INDEX tags_typeuserid_idx ON prefix_tags (type, userid);");
  1277. modify_database('',"CREATE INDEX tags_text_idx ON prefix_tags (text);");
  1278. }
  1279. if ($oldversion < 2006050500) {
  1280. table_column('log', 'action', 'action', 'varchar', '40', '', '', 'not null');
  1281. }
  1282. if ($oldversion < 2006050502) { // Close down the Dialogue module, we are removing it from CVS.
  1283. if (!file_exists($CFG->dirroot.'/mod/dialogue/lib.php')) {
  1284. if (!count_records('dialogue_conversations')) { // no data, drop the extra tables
  1285. execute_sql('DROP TABLE '.$CFG->prefix.'dialogue_conversations', false);
  1286. execute_sql('DROP TABLE '.$CFG->prefix.'dialogue_entries', false);
  1287. notify("The Dialogue module has been discontinued and removed from your site.
  1288. You weren't using it anyway. ;-)");
  1289. }
  1290. }
  1291. table_column('course_request', 'password', 'password', 'varchar', '50', '', '');
  1292. table_column('course', 'currency', 'currency', 'varchar', '3');
  1293. modify_database('', 'ALTER TABLE prefix_course_categories
  1294. ALTER COLUMN path SET DEFAULT \'\'');
  1295. table_column('log_display', 'module', 'module', 'varchar', '20');
  1296. modify_database("","DROP INDEX id_user_idx");
  1297. modify_database("","DROP INDEX post_lastmodified_idx");
  1298. modify_database("","DROP INDEX post_subject_idx");
  1299. modify_database('',"DROP INDEX bti_entryid_idx");
  1300. modify_database('',"DROP INDEX bti_tagid_idx");
  1301. modify_database('',"DROP INDEX post_module_idx");
  1302. modify_database('',"DROP INDEX tags_typeuserid_idx");
  1303. modify_database('',"DROP INDEX tags_text_idx");
  1304. modify_database("","CREATE INDEX {$CFG->prefix}id_user_idx ON prefix_post (id, courseid);");
  1305. modify_database("","CREATE INDEX {$CFG->prefix}post_lastmodified_idx ON prefix_post (lastmodified);");
  1306. modify_database("","CREATE INDEX {$CFG->prefix}post_subject_idx ON prefix_post (subject);");
  1307. modify_database('',"CREATE INDEX {$CFG->prefix}bti_entryid_idx ON prefix_blog_tag_instance (entryid);");
  1308. modify_database('',"CREATE INDEX {$CFG->prefix}bti_tagid_idx ON prefix_blog_tag_instance (tagid);");
  1309. modify_database('',"CREATE INDEX {$CFG->prefix}post_module_idx ON prefix_post (moduleid);");
  1310. modify_database('',"CREATE INDEX {$CFG->prefix}tags_typeuserid_idx ON prefix_tags (type, userid);");
  1311. modify_database('',"CREATE INDEX {$CFG->prefix}tags_text_idx ON prefix_tags (text);");
  1312. }
  1313. // renaming of reads and writes for stats_user_xyz
  1314. if ($oldversion < 2006052400) { // change this later
  1315. // we are using this because we want silent updates
  1316. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_daily RENAME COLUMN reads TO statsreads", false);
  1317. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_daily RENAME COLUMN writes TO statswrites", false);
  1318. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_weekly RENAME COLUMN reads TO statsreads", false);
  1319. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_weekly RENAME COLUMN writes TO statswrites", false);
  1320. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_monthly RENAME COLUMN reads TO statsreads", false);
  1321. execute_sql("ALTER TABLE {$CFG->prefix}stats_user_monthly RENAME COLUMN writes TO statswrites", false);
  1322. }
  1323. // Adding some missing log actions
  1324. if ($oldversion < 2006060400) {
  1325. // But only if they doesn't exist (because this was introduced after branch and we could be duplicating!)
  1326. if (!record_exists('log_display', 'module', 'course', 'action', 'report log')) {
  1327. execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report log', 'course', 'fullname')");
  1328. }
  1329. if (!record_exists('log_display', 'module', 'course', 'action', 'report live')) {
  1330. execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report live', 'course', 'fullname')");
  1331. }
  1332. if (!record_exists('log_display', 'module', 'course', 'action', 'report outline')) {
  1333. execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report outline', 'course', 'fullname')");
  1334. }
  1335. if (!record_exists('log_display', 'module', 'course', 'action', 'report participation')) {
  1336. execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report participation', 'course', 'fullname')");
  1337. }
  1338. if (!record_exists('log_display', 'module', 'course', 'action', 'report stats')) {
  1339. execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report stats', 'course', 'fullname')");
  1340. }
  1341. }
  1342. //Renaming lastIP to lastip (all fields lowercase)
  1343. if ($oldversion < 2006060900) {
  1344. //Not needed unded PG because it stores fieldnames lowecase by default
  1345. //Only if it exists (because MOODLE_16_STABLE could have done this work before. Bug 5763)
  1346. //$fields = $db->MetaColumnNames($CFG->prefix.'user');
  1347. //if (in_array('lastIP',$fields)) {
  1348. // table_column("user", "lastIP", "lastip", "varchar", "15", "", "", "", "currentlogin");
  1349. //}
  1350. }
  1351. if ($oldversion < 2006080400) {
  1352. modify_database('', "CREATE TABLE prefix_role (
  1353. id SERIAL PRIMARY KEY,
  1354. name varchar(255) NOT NULL default '',
  1355. shortname varchar(100) NOT NULL default '',
  1356. description text NOT NULL default '',
  1357. sortorder integer NOT NULL default '0'
  1358. );");
  1359. modify_database('', "CREATE TABLE prefix_context (
  1360. id SERIAL PRIMARY KEY,
  1361. level integer NOT NULL default 0,
  1362. instanceid integer NOT NULL default 0
  1363. );");
  1364. modify_database('', "CREATE TABLE prefix_role_assignments (
  1365. id SERIAL PRIMARY KEY,
  1366. roleid integer NOT NULL default 0,
  1367. contextid integer NOT NULL default 0,
  1368. userid integer NOT NULL default 0,
  1369. hidden integer NOT NULL default 0,
  1370. timestart integer NOT NULL default 0,
  1371. timeend integer NOT NULL default 0,
  1372. timemodified integer NOT NULL default 0,
  1373. modifierid integer NOT NULL default 0,
  1374. enrol varchar(20) NOT NULL default '',
  1375. sortorder integer NOT NULL default '0'
  1376. );");
  1377. modify_database('', "CREATE TABLE prefix_role_capabilities (
  1378. id SERIAL PRIMARY KEY,
  1379. contextid integer NOT NULL default 0,
  1380. roleid integer NOT NULL default 0,
  1381. capability varchar(255) NOT NULL default '',
  1382. permission integer NOT NULL default 0,
  1383. timemodified integer NOT NULL default 0,
  1384. modifierid integer NOT NULL default 0
  1385. );");
  1386. modify_database('', "CREATE TABLE prefix_role_deny_grant (
  1387. id SERIAL PRIMARY KEY,
  1388. roleid integer NOT NULL default '0',
  1389. unviewableroleid integer NOT NULL default '0'
  1390. );");
  1391. modify_database('', "CREATE TABLE prefix_capabilities (
  1392. id SERIAL PRIMARY KEY,
  1393. name varchar(255) NOT NULL default '',
  1394. captype varchar(50) NOT NULL default '',
  1395. contextlevel integer NOT NULL default 0,
  1396. component varchar(100) NOT NULL default ''
  1397. );");
  1398. modify_database('', "CREATE TABLE prefix_role_names (
  1399. id SERIAL PRIMARY KEY,
  1400. roleid integer NOT NULL default 0,
  1401. contextid integer NOT NULL default 0,
  1402. text text NOT NULL default ''
  1403. );");
  1404. }
  1405. if ($oldversion < 2006081000) {
  1406. modify_database('',"CREATE INDEX prefix_role_sortorder_idx ON prefix_role (sortorder);");
  1407. modify_database('',"CREATE INDEX prefix_context_instanceid_idx ON prefix_context (instanceid);");
  1408. modify_database('',"CREATE UNIQUE INDEX prefix_context_levelinstanceid_idx ON prefix_context (level, instanceid);");
  1409. modify_database('',"CREATE INDEX prefix_role_assignments_roleid_idx ON prefix_role_assignments (roleid);");
  1410. modify_database('',"CREATE INDEX prefix_role_assignments_contextidid_idx ON prefix_role_assignments (contextid);");
  1411. modify_database('',"CREATE INDEX prefix_role_assignments_userid_idx ON prefix_role_assignments (userid);");
  1412. modify_database('',"CREATE UNIQUE INDEX prefix_role_assignments_contextidroleiduserid_idx ON prefix_role_assignments (contextid, roleid, userid);");
  1413. modify_database('',"CREATE INDEX prefix_role_assignments_sortorder_idx ON prefix_role_assignments (sortorder);");
  1414. modify_database('',"CREATE INDEX prefix_role_capabilities_roleid_idx ON prefix_role_capabilities (roleid);");
  1415. modify_database('',"CREATE INDEX prefix_role_capabilities_contextid_idx ON prefix_role_capabilities (contextid);");
  1416. modify_database('',"CREATE INDEX prefix_role_capabilities_modifierid_idx ON prefix_role_capabilities (modifierid);");
  1417. // MDL-10640 adding missing index from upgrade
  1418. modify_database('',"CREATE INDEX prefix_role_capabilities_capability_idx ON prefix_role_capabilities (capability);");
  1419. modify_database('',"CREATE UNIQUE INDEX prefix_role_capabilities_roleidcontextidcapability_idx ON prefix_role_capabilities (roleid, contextid, capability);");
  1420. modify_database('',"CREATE INDEX prefix_role_deny_grant_roleid_idx ON prefix_role_deny_grant (roleid);");
  1421. modify_database('',"CREATE INDEX prefix_role_deny_grant_unviewableroleid_idx ON prefix_role_deny_grant (unviewableroleid);");
  1422. modify_database('',"CREATE UNIQUE INDEX prefix_role_deny_grant_roleidunviewableroleid_idx ON prefix_role_deny_grant (roleid, unviewableroleid);");
  1423. modify_database('',"CREATE UNIQUE INDEX prefix_capabilities_name_idx ON prefix_capabilities (name);");
  1424. modify_database('',"CREATE INDEX prefix_role_names_roleid_idx ON prefix_role_names (roleid);");
  1425. modify_database('',"CREATE INDEX prefix_role_names_contextid_idx ON prefix_role_names (contextid);");
  1426. modify_database('',"CREATE UNIQUE INDEX prefix_role_names_roleidcontextid_idx ON prefix_role_names (roleid, contextid);");
  1427. }
  1428. if ($oldversion < 2006081700) {
  1429. modify_database('',"DROP TABLE prefix_role_deny_grant");
  1430. modify_database('',"CREATE TABLE prefix_role_allow_assign (
  1431. id SERIAL PRIMARY KEY,
  1432. roleid integer NOT NULL default '0',
  1433. allowassign integer NOT NULL default '0'
  1434. );");
  1435. modify_database('',"CREATE INDEX prefix_role_allow_assign_roleid_idx ON prefix_role_allow_assign (roleid);");
  1436. modify_database('',"CREATE INDEX prefix_role_allow_assign_allowassign_idx ON prefix_role_allow_assign (allowassign);");
  1437. modify_database('',"CREATE UNIQUE INDEX prefix_role_allow_assign_roleidallowassign_idx ON prefix_role_allow_assign (roleid, allowassign);");
  1438. modify_database('',"CREATE TABLE prefix_role_allow_override (
  1439. id SERIAL PRIMARY KEY,
  1440. roleid integer NOT NULL default '0',
  1441. allowoverride integer NOT NULL default '0'
  1442. );");
  1443. modify_database('',"CREATE INDEX prefix_role_allow_override_roleid_idx ON prefix_role_allow_override (roleid);");
  1444. modify_database('',"CREATE INDEX prefix_role_allow_override_allowoverride_idx ON prefix_role_allow_override (allowoverride);");
  1445. modify_database('',"CREATE UNIQUE INDEX prefix_role_allow_override_roleidallowoverride_idx ON prefix_role_allow_override (roleid, allowoverride);");
  1446. }
  1447. if ($oldversion < 2006082100) {
  1448. execute_sql("DROP INDEX {$CFG->prefix}context_levelinstanceid_idx;",false);
  1449. table_column('context', 'level', 'aggregatelevel', 'integer', '10', 'unsigned', '0', 'not null', '');
  1450. modify_database('',"CREATE UNIQUE INDEX prefix_context_aggregatelevelinstanceid_idx ON prefix_context (aggregatelevel, instanceid);");
  1451. }
  1452. if ($oldversion < 2006082200) {
  1453. table_column('timezone', 'rule', 'tzrule', 'varchar', '20', '', '', 'not null', '');
  1454. }
  1455. if ($oldversion < 2006082800) {
  1456. table_column('user', '', 'ajax', 'integer', '1', 'unsigned', '1', 'not null', 'htmleditor');
  1457. }
  1458. if ($oldversion < 2006082900) {
  1459. execute_sql("DROP TABLE {$CFG->prefix}sessions", true);
  1460. execute_sql("
  1461. CREATE TABLE {$CFG->prefix}sessions2 (
  1462. sesskey VARCHAR(255) NOT NULL default '',
  1463. expiry TIMESTAMP NOT NULL,
  1464. expireref VARCHAR(255),
  1465. created TIMESTAMP NOT NULL,
  1466. modified TIMESTAMP NOT NULL,
  1467. sessdata TEXT,
  1468. CONSTRAINT {$CFG->prefix}sess_ses_pk PRIMARY KEY (sesskey)
  1469. );", true);
  1470. execute_sql("
  1471. CREATE INDEX {$CFG->prefix}sess_exp_ix ON {$CFG->prefix}sessions2 (expiry);", true);
  1472. execute_sql("
  1473. CREATE INDEX {$CFG->prefix}sess_exp2_ix ON {$CFG->prefix}sessions2 (expireref);", true);
  1474. }
  1475. if ($oldversion < 2006083002) {
  1476. table_column('capabilities', '', 'riskbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
  1477. }
  1478. if ($oldversion < 2006083100) {
  1479. execute_sql("ALTER TABLE {$CFG->prefix}course ALTER COLUMN modinfo DROP NOT NULL");
  1480. execute_sql("ALTER TABLE {$CFG->prefix}course ALTER COLUMN modinfo DROP DEFAULT");
  1481. }
  1482. if ($oldversion < 2006083101) {
  1483. execute_sql("ALTER TABLE {$CFG->prefix}course_categories ALTER COLUMN description DROP NOT NULL");
  1484. execute_sql("ALTER TABLE {$CFG->prefix}course_categories ALTER COLUMN description DROP DEFAULT");
  1485. }
  1486. if ($oldversion < 2006083102) {
  1487. execute_sql("ALTER TABLE {$CFG->prefix}user ALTER COLUMN description DROP NOT NULL");
  1488. execute_sql("ALTER TABLE {$CFG->prefix}user ALTER COLUMN description DROP DEFAULT");
  1489. }
  1490. if ($oldversion < 2006090200) {
  1491. execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN summary DROP NOT NULL");
  1492. execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN summary DROP DEFAULT");
  1493. execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN sequence DROP NOT NULL");
  1494. execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN sequence DROP DEFAULT");
  1495. }
  1496. // table to keep track of course page access times, used in online participants block, and participants list
  1497. if ($oldversion < 2006091200) {
  1498. execute_sql("CREATE TABLE {$CFG->prefix}user_lastaccess (
  1499. id SERIAL PRIMARY KEY,
  1500. userid integer NOT NULL default 0,
  1501. courseid integer NOT NULL default 0,
  1502. timeaccess integer NOT NULL default 0
  1503. );", true);
  1504. execute_sql("CREATE INDEX {$CFG->prefix}user_lastaccess_userid_idx ON {$CFG->prefix}user_lastaccess (userid);", true);
  1505. execute_sql("CREATE INDEX {$CFG->prefix}user_lastaccess_courseid_idx ON {$CFG->prefix}user_lastaccess (courseid);", true);
  1506. execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}user_lastaccess_useridcourseid_idx ON {$CFG->prefix}user_lastaccess (userid, courseid);", true);
  1507. }
  1508. if (!empty($CFG->rolesactive) and $oldversion < 2006091212) { // Reload the guest roles completely with new defaults
  1509. if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
  1510. delete_records('capabilities');
  1511. $sitecontext = get_context_instance(CONTEXT_SYSTEM);
  1512. foreach ($guestroles as $guestrole) {
  1513. delete_records('role_capabilities', 'roleid', $guestrole->id);
  1514. assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id);
  1515. }
  1516. }
  1517. }
  1518. if ($oldversion < 2006091700) {
  1519. table_column('course','','defaultrole','integer','10', 'unsigned', '0', 'not null');
  1520. }
  1521. if ($oldversion < 2006091800) {
  1522. delete_records('config', 'name', 'showsiteparticipantslist');
  1523. delete_records('config', 'name', 'requestedteachername');
  1524. delete_records('config', 'name', 'requestedteachersname');
  1525. delete_records('config', 'name', 'requestedstudentname');
  1526. delete_records('config', 'name', 'requestedstudentsname');
  1527. }
  1528. if (!empty($CFG->rolesactive) and $oldversion < 2006091901) {
  1529. if ($roles = get_records('role')) {
  1530. $first = array_shift($roles);
  1531. if (!empty($first->shortname)) {
  1532. // shortnames already exist
  1533. } else {
  1534. table_column('role', '', 'shortname', 'varchar', '100', '', '', 'not null', 'name');
  1535. $legacy_names = array('admin', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest');
  1536. foreach ($legacy_names as $name) {
  1537. if ($roles = get_roles_with_capability('moodle/legacy:'.$name, CAP_ALLOW)) {
  1538. $i = '';
  1539. foreach ($roles as $role) {
  1540. if (empty($role->shortname)) {
  1541. $updated = new object();
  1542. $updated->id = $role->id;
  1543. $updated->shortname = $name.$i;
  1544. update_record('role', $updated);
  1545. $i++;
  1546. }
  1547. }
  1548. }
  1549. }
  1550. }
  1551. }
  1552. }
  1553. /// Tables for customisable user profile fields
  1554. if ($oldversion < 2006092000) {
  1555. execute_sql("CREATE TABLE {$CFG->prefix}user_info_field (
  1556. id BIGSERIAL,
  1557. name VARCHAR(255) NOT NULL default '',
  1558. datatype VARCHAR(255) NOT NULL default '',
  1559. categoryid BIGINT NOT NULL default 0,
  1560. sortorder BIGINT NOT NULL default 0,
  1561. required SMALLINT NOT NULL default 0,
  1562. locked SMALLINT NOT NULL default 0,
  1563. visible SMALLINT NOT NULL default 0,
  1564. defaultdata TEXT,
  1565. CONSTRAINT {$CFG->prefix}userinfofiel_id_pk PRIMARY KEY (id));", true);
  1566. execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_field IS 'Customisable user profile fields';", true);
  1567. execute_sql("CREATE TABLE {$CFG->prefix}user_info_category (
  1568. id BIGSERIAL,
  1569. name VARCHAR(255) NOT NULL default '',
  1570. sortorder BIGINT NOT NULL default 0,
  1571. CONSTRAINT {$CFG->prefix}userinfocate_id_pk PRIMARY KEY (id));", true);
  1572. execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_category IS 'Customisable fields categories';", true);
  1573. execute_sql("CREATE TABLE {$CFG->prefix}user_info_data (
  1574. id BIGSERIAL,
  1575. userid BIGINT NOT NULL default 0,
  1576. fieldid BIGINT NOT NULL default 0,
  1577. data TEXT NOT NULL,
  1578. CONSTRAINT {$CFG->prefix}userinfodata_id_pk PRIMARY KEY (id));", true);
  1579. execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_data IS 'Data for the customisable user fields';", true);
  1580. }
  1581. if ($oldversion < 2006092200) {
  1582. table_column('context', 'aggregatelevel', 'contextlevel', 'int', '10', 'unsigned', '0', 'not null', '');
  1583. /* execute_sql("ALTER TABLE `{$CFG->prefix}context` DROP INDEX `aggregatelevel-instanceid`;",false);
  1584. execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `contextlevel-instanceid` (`contextlevel`, `instanceid`)",false); // see 2006092409 below */
  1585. }
  1586. if ($oldversion < 2006092302) {
  1587. // fix sortorder first if needed
  1588. if ($roles = get_all_roles()) {
  1589. $i = 0;
  1590. foreach ($roles as $rolex) {
  1591. if ($rolex->sortorder != $i) {
  1592. $r = new object();
  1593. $r->id = $rolex->id;
  1594. $r->sortorder = $i;
  1595. update_record('role', $r);
  1596. }
  1597. $i++;
  1598. }
  1599. }
  1600. /* execute_sql("ALTER TABLE {$CFG->prefix}role DROP INDEX {$CFG->prefix}role_sor_ix;");
  1601. execute_sql("ALTER TABLE {$CFG->prefix}role ADD UNIQUE INDEX {$CFG->prefix}role_sor_uix (sortorder)");*/
  1602. }
  1603. if ($oldversion < 2006092400) {
  1604. table_column('user', '', 'trustbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
  1605. }
  1606. if ($oldversion < 2006092409) {
  1607. // ok, once more and now correctly!
  1608. execute_sql("DROP INDEX \"aggregatelevel-instanceid\";", false);
  1609. execute_sql("DROP INDEX \"contextlevel-instanceid\";", false);
  1610. execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}cont_conins_uix ON {$CFG->prefix}context (contextlevel, instanceid);", false);
  1611. execute_sql("DROP INDEX {$CFG->prefix}role_sor_ix;", false);
  1612. execute_sql("DROP INDEX {$CFG->prefix}role_sor_uix;", false);
  1613. execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}role_sor_uix ON {$CFG->prefix}role (sortorder);", false);
  1614. }
  1615. if ($oldversion < 2006092410) {
  1616. /// Convert all the PG unique keys into their corresponding unique indexes
  1617. /// we don't want such keys inside Moodle 1.7 and above
  1618. /// Look for all the UNIQUE CONSTRAINSTS existing in DB
  1619. $uniquecons = get_records_sql ("SELECT conname, relname, conkey, clas.oid AS tableoid
  1620. FROM pg_constraint cons,
  1621. pg_class clas
  1622. WHERE cons.contype='u'
  1623. AND cons.conrelid = clas.oid");
  1624. /// Iterate over every unique constraint, calculating its fields
  1625. if ($uniquecons) {
  1626. foreach ($uniquecons as $uniquecon) {
  1627. $conscols = trim(trim($uniquecon->conkey, '}'), '{');
  1628. $conscols = explode(',', $conscols);
  1629. /// Iterate over each column to fetch its name
  1630. $indexcols = array();
  1631. foreach ($conscols as $conscol) {
  1632. $column = get_record_sql ("SELECT attname, attname
  1633. FROM pg_attribute
  1634. WHERE attrelid = $uniquecon->tableoid
  1635. AND attnum = $conscol");
  1636. $indexcols[] = $column->attname;
  1637. }
  1638. /// Drop the old UNIQUE CONSTRAINT
  1639. execute_sql ("ALTER TABLE $uniquecon->relname DROP CONSTRAINT $uniquecon->conname", false);
  1640. /// Create the new UNIQUE INDEX
  1641. execute_sql ("CREATE UNIQUE INDEX {$uniquecon->relname}_".implode('_', $indexcols)."_uix ON $uniquecon->relname (".implode(', ', $indexcols).')', false);
  1642. }
  1643. }
  1644. }
  1645. if ($oldversion < 2006092601) {
  1646. table_column('log_display', 'field', 'field', 'varchar', '200', '', '', 'not null', '');
  1647. }
  1648. ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
  1649. return $result;
  1650. }
  1651. ?>