],
'contentbank content' => [
'datagenerator' => 'contentbank_content',
- 'required' => array('contenttype', 'user', 'contentname'),
- 'switchids' => array('course' => 'courseid', 'user' => 'userid')
+ 'required' => array('contextlevel', 'reference', 'contenttype', 'user', 'contentname'),
+ 'switchids' => array('user' => 'userid')
],
+ 'badge external backpack' => [
+ 'datagenerator' => 'badge_external_backpack',
+ 'required' => ['backpackapiurl', 'backpackweburl', 'apiversion']
+ ],
+ 'setup backpack connected' => [
+ 'datagenerator' => 'setup_backpack_connected',
+ 'required' => ['user', 'externalbackpack'],
+ 'switchids' => ['user' => 'userid', 'externalbackpack' => 'externalbackpackid']
+ ]
];
}
$record->usercreated = $data['userid'];
$record->name = $data['contentname'];
$content = $contenttype->create_content($record);
+
+ if (!empty($data['filepath'])) {
+ $fs = get_file_storage();
+ $filerecord = array(
+ 'component' => 'contentbank',
+ 'filearea' => 'public',
+ 'contextid' => $context->id,
+ 'userid' => $data['userid'],
+ 'itemid' => $content->get_id(),
+ 'filename' => $data['contentname'],
+ 'filepath' => '/'
+ );
+ $fs->create_file_from_pathname($filerecord, $CFG->dirroot . $data['filepath']);
+ }
+ } else {
+ throw new Exception('The specified "' . $data['contenttype'] . '" contenttype does not exist');
}
}
+
+ /**
+ * Create a exetrnal backpack.
+ *
+ * @param array $data
+ */
+ protected function process_badge_external_backpack(array $data) {
+ global $DB;
+ $DB->insert_record('badge_external_backpack', $data, true);
+ }
+
+ /**
+ * Setup a backpack connected for user.
+ *
+ * @param array $data
+ * @throws dml_exception
+ */
+ protected function process_setup_backpack_connected(array $data) {
+ global $DB;
+
+ if (empty($data['userid'])) {
+ throw new Exception('\'setup backpack connected\' requires the field \'user\' to be specified');
+ }
+ if (empty($data['externalbackpackid'])) {
+ throw new Exception('\'setup backpack connected\' requires the field \'externalbackpack\' to be specified');
+ }
+ // Dummy badge_backpack_oauth2 data.
+ $timenow = time();
+ $backpackoauth2 = new stdClass();
+ $backpackoauth2->usermodified = $data['userid'];
+ $backpackoauth2->timecreated = $timenow;
+ $backpackoauth2->timemodified = $timenow;
+ $backpackoauth2->userid = $data['userid'];
+ $backpackoauth2->issuerid = 1;
+ $backpackoauth2->externalbackpackid = $data['externalbackpackid'];
+ $backpackoauth2->token = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ $backpackoauth2->refreshtoken = '0123456789abcdefghijk';
+ $backpackoauth2->expires = $timenow + 3600;
+ $backpackoauth2->scope = 'https://purl.imsglobal.org/spec/ob/v2p1/scope/assertion.create';
+ $backpackoauth2->scope .= ' https://purl.imsglobal.org/spec/ob/v2p1/scope/assertion.readonly offline_access';
+ $DB->insert_record('badge_backpack_oauth2', $backpackoauth2);
+
+ // Dummy badge_backpack data.
+ $backpack = new stdClass();
+ $backpack->userid = $data['userid'];
+ $backpack->email = 'student@behat.moodle';
+ $backpack->backpackuid = 0;
+ $backpack->autosync = 0;
+ $backpack->password = '';
+ $backpack->externalbackpackid = $data['externalbackpackid'];
+ $DB->insert_record('badge_backpack', $backpack);
+ }
}
upgrade_main_savepoint(true, 2020042800.01);
}
- if ($oldversion < 2020051500.01) {
+ if ($oldversion < 2020051900.01) {
+ // Define field component to be added to event.
+ $table = new xmldb_table('event');
+ $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'repeatid');
+
+ // Conditionally launch add field component.
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ // Define index component (not unique) to be added to event.
+ $table = new xmldb_table('event');
+ $index = new xmldb_index('component', XMLDB_INDEX_NOTUNIQUE, ['component', 'eventtype', 'instance']);
+
+ // Conditionally launch add index component.
+ if (!$dbman->index_exists($table, $index)) {
+ $dbman->add_index($table, $index);
+ }
+
+ // Main savepoint reached.
+ upgrade_main_savepoint(true, 2020051900.01);
+ }
+
++ if ($oldversion < 2020052000.00) {
+ // Define table badge_backpack_oauth2 to be created.
+ $table = new xmldb_table('badge_backpack_oauth2');
+
+ // Adding fields to table badge_backpack_oauth2.
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('externalbackpackid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('token', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
+ $table->add_field('refreshtoken', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
+ $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
+ $table->add_field('scope', XMLDB_TYPE_TEXT, null, null, null, null, null);
+
+ // Adding keys to table badge_backpack_oauth2.
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
+ $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']);
+ $table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
+ $table->add_key('issuerid', XMLDB_KEY_FOREIGN, ['issuerid'], 'oauth2_issuer', ['id']);
+ $table->add_key('externalbackpackid', XMLDB_KEY_FOREIGN, ['externalbackpackid'], 'badge_external_backpack', ['id']);
+ // Conditionally launch create table for badge_backpack_oauth2.
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Define field oauth2_issuerid to be added to badge_external_backpack.
+ $tablebadgeexternalbackpack = new xmldb_table('badge_external_backpack');
+ $fieldoauth2issuerid = new xmldb_field('oauth2_issuerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'password');
+ $keybackpackoauth2key = new xmldb_key('backpackoauth2key', XMLDB_KEY_FOREIGN, ['oauth2_issuerid'], 'oauth2_issuer', ['id']);
+
+ // Conditionally launch add field oauth2_issuerid.
+ if (!$dbman->field_exists($tablebadgeexternalbackpack, $fieldoauth2issuerid)) {
+ $dbman->add_field($tablebadgeexternalbackpack, $fieldoauth2issuerid);
+
+ // Launch add key backpackoauth2key.
+ $dbman->add_key($tablebadgeexternalbackpack, $keybackpackoauth2key);
+ }
+
+ // Define field assertion to be added to badge_external.
+ $tablebadgeexternal = new xmldb_table('badge_external');
+ $fieldassertion = new xmldb_field('assertion', XMLDB_TYPE_TEXT, null, null, null, null, null, 'entityid');
+
+ // Conditionally launch add field assertion.
+ if (!$dbman->field_exists($tablebadgeexternal, $fieldassertion)) {
+ $dbman->add_field($tablebadgeexternal, $fieldassertion);
+ }
+
+ // Main savepoint reached.
- upgrade_main_savepoint(true, 2020051500.01);
++ upgrade_main_savepoint(true, 2020052000.00);
+ }
+
return true;
}