Skip to content
Snippets Groups Projects
Commit a8298bed authored by Elmar Ludwig's avatar Elmar Ludwig
Browse files

add migration to move admin tool to first position, fixes #4417

Closes #4417

Merge request !3790
parent 27104518
No related branches found
No related tags found
No related merge requests found
<?php
class FixToolPositions extends Migration
{
public function description()
{
return 'move admin tool to first position in course';
}
public function up()
{
$db = DBManager::get();
// update default order in sem_classes
$result = $db->query('SELECT * FROM sem_classes');
$update = $db->prepare('UPDATE sem_classes SET modules = ? WHERE id = ?');
foreach ($result as $row) {
$modules = json_decode($row['modules'], true);
$admin = $modules['CoreAdmin'] ?? $modules['CoreStudygroupAdmin'];
unset($modules['CoreAdmin'], $modules['CoreStudygroupAdmin']);
if ($row['studygroup_mode']) {
$modules = ['CoreStudygroupAdmin' => $admin] + $modules;
} else {
$modules = ['CoreAdmin' => $admin] + $modules;
}
$update->execute([json_encode($modules), $row['id']]);
}
// update individual order in tools_activated
$stmt = $db->query('SELECT pluginclassname, pluginid FROM plugins');
$plugins = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP | PDO::FETCH_UNIQUE);
$admin_ids = [$plugins['CoreAdmin'], $plugins['CoreStudygroupAdmin']];
$result = $db->query('SELECT * FROM tools_activated ORDER BY range_id, position');
$update = $db->prepare('UPDATE tools_activated SET position = ? WHERE range_id = ? AND plugin_id = ?');
$range_id = null;
foreach ($result as $row) {
if ($range_id !== $row['range_id']) {
$range_id = $row['range_id'];
$position = 0;
}
if (in_array($row['plugin_id'], $admin_ids)) {
$new_position = 0;
} else {
$new_position = ++$position;
}
if ($row['position'] != $new_position) {
$update->execute([$new_position, $range_id, $row['plugin_id']]);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment