Skip to content

Commit 892c7c3

Browse files
authored
Migrate depending on a subset of content-types (#20)
A new option, '--contenttype' or '-t', has been added to CLI to let admins migrate mod_hvp activities based on their main library id. In order to achieve this, a new parameter, '$libraryids', has been added to get_sql_hvp_to_migrate API method, to get SQL for contents using these libraries. For now, no changes have been done in GUI because admins can order by the "Content type" column if they want to migrate contents based on the main library id.
1 parent 731d954 commit 892c7c3

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

classes/api.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,16 @@ public static function migrate_hvp2h5p(int $hvpid, int $keeporiginal = self::KEE
161161
* is a good idea.
162162
*
163163
* @param bool $count when true, returns the count SQL.
164+
* @param string $sort sorting criteria.
165+
* @param array $libraryids List of the library ids for the mod_hvp contents to migrate. Only contents with these
166+
* main libraries will be returned.
164167
* @return array containing sql to use and an array of params.
165168
*/
166-
public static function get_sql_hvp_to_migrate(bool $count = false, ?string $sort = null): array {
169+
public static function get_sql_hvp_to_migrate(bool $count = false, ?string $sort = null, ?array $libraryids = null): array {
167170

168171
self::fix_duplicated_hvp();
169172

173+
$params = [];
170174
if ($count) {
171175
$select = "COUNT(1)";
172176
$groupby = '';
@@ -176,6 +180,27 @@ public static function get_sql_hvp_to_migrate(bool $count = false, ?string $sort
176180
$groupby = 'GROUP BY h.id, h.course, c.fullname, h.name, hl.machine_name, cm.id';
177181
}
178182

183+
// Get only mod_hvp contents with main library in libraryids (if defined).
184+
$where = 'mgr.id IS NULL';
185+
if (!empty($libraryids)) {
186+
$i = 1;
187+
foreach ($libraryids as $libraryid) {
188+
if (is_numeric($libraryid)) {
189+
if ($i > 1) {
190+
$where .= ' OR ';
191+
} else {
192+
$where .= ' AND (';
193+
}
194+
$where .= 'h.main_library_id = :libraryid' . $i;
195+
$params['libraryid' . $i] = $libraryid;
196+
$i++;
197+
}
198+
}
199+
if ($i > 1) {
200+
$where .= ')';
201+
}
202+
}
203+
179204
// We need to select the hvp activities which are not migrated but ignoring the activities in the recycle bin.
180205
// The most efficient way seems to have a subtable with all non-delete h5p activities.
181206
$sql = "SELECT $select
@@ -194,14 +219,14 @@ public static function get_sql_hvp_to_migrate(bool $count = false, ?string $sort
194219
AND i.id = mgrcm.instance AND mgrcm.deletioninprogress = 0
195220
) mgr
196221
ON mgr.name = h.name AND mgr.timecreated = h.timecreated AND mgr.course = h.course
197-
WHERE mgr.id IS NULL
222+
WHERE $where
198223
$groupby";
199224

200225
if (!empty($sort)) {
201226
$sql .= " ORDER BY " . $sort;
202227
}
203228

204-
return [$sql, []];
229+
return [$sql, $params];
205230
}
206231

207232
/**

cli/migrate.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
'limit' => 100,
3838
'keeporiginal' => 1,
3939
'copy2cb' => api::COPY2CBYESWITHLINK,
40+
'contenttypes' => [],
4041
], [
4142
'e' => 'execute',
4243
'h' => 'help',
4344
'l' => 'limit',
4445
'k' => 'keeporiginal',
4546
'c' => 'copy2cb',
47+
't' => 'contenttypes',
4648
]
4749
);
4850

@@ -60,8 +62,10 @@
6062
-e, --execute Run the migration tool
6163
-k, --keeporiginal=N After migration 0 will remove the original activity, 1 will keep it and 2 will hide it
6264
-c, --copy2cb=N Whether H5P files should be added to the content bank with a link (1), as a copy (2) or not added (0)
65+
-t, --contenttypes=N The library ids, separated by commas, for the mod_hvp contents to migrate.
66+
Only contents having these libraries defined as main library will be migrated.
6367
-l --limit=N The maximmum number of activities per execution (default 100).
64-
Already migrated activities will be ignored.
68+
Already migrated activities will be ignored.
6569
6670
Example:
6771
\$sudo -u www-data /usr/bin/php admin/tool/migratehvp2h5p/cli/migrate.php --execute
@@ -90,6 +94,12 @@
9094
$options['copy2cb'] = api::COPY2CBYESWITHLINK;
9195
}
9296

97+
if (!empty($options['contenttypes'])) {
98+
$ctparam = explode(',', $options['contenttypes']);
99+
} else {
100+
$ctparam = [];
101+
}
102+
93103
$keeporiginal = $options['keeporiginal'];
94104
$copy2cb = $options['copy2cb'];
95105
$limit = $options['limit'] ?? 100;
@@ -110,6 +120,18 @@
110120
exit(1);
111121
}
112122

123+
$contenttypes = [];
124+
if (!empty($ctparam)) {
125+
foreach ($ctparam as $contenttype) {
126+
if (!is_numeric($contenttype)) {
127+
echo "contenttypes must be a list of library ids separated by commas.\n";
128+
exit(1);
129+
} else {
130+
$contenttypes[] = intval($contenttype);
131+
}
132+
}
133+
}
134+
113135
core_php_time_limit::raise();
114136

115137
// Increase memory limit.
@@ -124,7 +146,7 @@
124146

125147
mtrace("Search for $limit non migrated hvp activites\n");
126148

127-
list($sql, $params) = api::get_sql_hvp_to_migrate();
149+
list($sql, $params) = api::get_sql_hvp_to_migrate(false, null, $contenttypes);
128150
if (!empty($limit)) {
129151
$sql .= " LIMIT " . $limit;
130152
}

0 commit comments

Comments
 (0)