1 |
efrain |
1 |
<?php
|
|
|
2 |
require(__DIR__. '/../constants.php');
|
|
|
3 |
|
|
|
4 |
function xmldb_report_coursestats_upgrade($oldversion) {
|
|
|
5 |
global $DB;
|
|
|
6 |
$dbman = $DB->get_manager();
|
|
|
7 |
|
|
|
8 |
if ($oldversion < 2017061737) {
|
|
|
9 |
|
|
|
10 |
// Define field categoryid to be added to report_coursestats.
|
|
|
11 |
$table = new xmldb_table(PLUGIN_TABLE_NAME);
|
|
|
12 |
$field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'last_update');
|
|
|
13 |
|
|
|
14 |
// Conditionally launch add field categoryid.
|
|
|
15 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
16 |
$dbman->add_field($table, $field);
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
$key = new xmldb_key('categoryid_fk', XMLDB_KEY_FOREIGN, array('categoryid'), 'course_categories', array('id'));
|
|
|
20 |
|
|
|
21 |
// Launch add key categoryid_fk.
|
|
|
22 |
$dbman->add_key($table, $key);
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
// Coursestats savepoint reached.
|
|
|
26 |
upgrade_plugin_savepoint(true, 2017061737, 'report', 'coursestats');
|
|
|
27 |
|
|
|
28 |
$result = $DB->get_records(PLUGIN_TABLE_NAME);
|
|
|
29 |
foreach ($result as $cs) {
|
|
|
30 |
$course = $DB->get_record(COURSE_TABLE_NAME, array('id'=>$cs->courseid));
|
|
|
31 |
$cs->categoryid = $course->category;
|
|
|
32 |
$DB->update_record(PLUGIN_TABLE_NAME, $cs);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
return true;
|
|
|
39 |
}
|