if ($oldversion < 2019030700.01) {
- upgrade_main_savepoint(true, 2019030700.01);
+ // Define field evaluationmode to be added to analytics_models_log.
+ $table = new xmldb_table('analytics_models_log');
+ $field = new xmldb_field('evaluationmode', XMLDB_TYPE_CHAR, '50', null, null, null,
+ null, 'version');
+
+ // Conditionally launch add field evaluationmode.
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+
+ $updatesql = "UPDATE {analytics_models_log}
+ SET evaluationmode = 'configuration'";
+ $DB->execute($updatesql, []);
+
+ // Changing nullability of field evaluationmode on table block_instances to not null.
+ $field = new xmldb_field('evaluationmode', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL,
+ null, null, 'version');
+
+ // Launch change of nullability for field evaluationmode.
+ $dbman->change_field_notnull($table, $field);
+ }
+
+ // Main savepoint reached.
+ upgrade_main_savepoint(true, 2019030700.01);
+ }
+
+ if ($oldversion < 2019030800.00) {
+ // Define table 'message_conversation_actions' to be created.
+ // Note - I would have preferred 'message_conversation_user_actions' but due to Oracle we can't. Boo.
+ $table = new xmldb_table('message_conversation_actions');
+
+ // Adding fields to table 'message_conversation_actions'.
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('conversationid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+
+ // Adding keys to table 'message_conversation_actions'.
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
+ $table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
+ $table->add_key('conversationid', XMLDB_KEY_FOREIGN, ['conversationid'], 'message_conversations', ['id']);
+
+ // Conditionally launch create table for 'message_conversation_actions'.
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Main savepoint reached.
+ upgrade_main_savepoint(true, 2019030800.00);
+ }
+
+ if ($oldversion < 2019030800.02) {
+ // Remove any conversations and their members associated with non-existent groups.
+ $sql = "SELECT mc.id
+ FROM {message_conversations} mc
+ LEFT JOIN {groups} g
+ ON mc.itemid = g.id
+ WHERE mc.component = :component
+ AND mc.itemtype = :itemtype
+ AND g.id is NULL";
+ $conversations = $DB->get_records_sql($sql, ['component' => 'core_group', 'itemtype' => 'groups']);
+
+ if ($conversations) {
+ $conversationids = array_keys($conversations);
+
+ $DB->delete_records_list('message_conversations', 'id', $conversationids);
+ $DB->delete_records_list('message_conversation_members', 'conversationid', $conversationids);
+ $DB->delete_records_list('message_conversation_actions', 'conversationid', $conversationids);
+
+ // Now, go through each conversation and delete any messages and related message actions.
+ foreach ($conversationids as $conversationid) {
+ if ($messages = $DB->get_records('messages', ['conversationid' => $conversationid])) {
+ $messageids = array_keys($messages);
+
+ // Delete the actions.
+ list($insql, $inparams) = $DB->get_in_or_equal($messageids);
+ $DB->delete_records_select('message_user_actions', "messageid $insql", $inparams);
+
+ // Delete the messages.
+ $DB->delete_records('messages', ['conversationid' => $conversationid]);
+ }
+ }
+ }
+
+ // Main savepoint reached.
+ upgrade_main_savepoint(true, 2019030800.02);
+ }
+
++ if ($oldversion < 2019030800.03) {
++
+ // Add missing indicators to course_dropout.
+ $params = [
+ 'target' => '\core\analytics\target\course_dropout',
+ 'trained' => 0,
+ 'enabled' => 0,
+ ];
+ $models = $DB->get_records('analytics_models', $params);
+ foreach ($models as $model) {
+ $indicators = json_decode($model->indicators);
+
+ $potentiallymissingindicators = [
+ '\core_course\analytics\indicator\completion_enabled',
+ '\core_course\analytics\indicator\potential_cognitive_depth',
+ '\core_course\analytics\indicator\potential_social_breadth',
+ '\core\analytics\indicator\any_access_after_end',
+ '\core\analytics\indicator\any_access_before_start',
+ '\core\analytics\indicator\any_write_action_in_course',
+ '\core\analytics\indicator\read_actions'
+ ];
+
+ $missing = false;
+ foreach ($potentiallymissingindicators as $potentiallymissingindicator) {
+ if (!in_array($potentiallymissingindicator, $indicators)) {
+ // Add the missing indicator to sites upgraded before 2017072000.02.
+ $indicators[] = $potentiallymissingindicator;
+ $missing = true;
+ }
+ }
+
+ if ($missing) {
+ $model->indicators = json_encode($indicators);
+ $model->version = time();
+ $model->timemodified = time();
+ $DB->update_record('analytics_models', $model);
+ }
+ }
+
+ // Add missing indicators to no_teaching.
+ $params = [
+ 'target' => '\core\analytics\target\no_teaching',
+ ];
+ $models = $DB->get_records('analytics_models', $params);
+ foreach ($models as $model) {
+ $indicators = json_decode($model->indicators);
+ if (!in_array('\core_course\analytics\indicator\no_student', $indicators)) {
+ // Add the missing indicator to sites upgraded before 2017072000.02.
+
+ $indicators[] = '\core_course\analytics\indicator\no_student';
+
+ $model->indicators = json_encode($indicators);
+ $model->version = time();
+ $model->timemodified = time();
+ $DB->update_record('analytics_models', $model);
+ }
+ }
+
+ // Main savepoint reached.
++ upgrade_main_savepoint(true, 2019030800.03);
+ }
+
return true;
}