Skip to content

Commit

Permalink
add command to update LTI 1.3 URLs in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispittman committed Dec 14, 2023
1 parent d5e43ec commit 1db4bae
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Commands/UpdateConsumerSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace LonghornOpen\LaravelCelticLTI\Commands;

use ceLTIc\LTI;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use LonghornOpen\LaravelCelticLTI\PlatformCreator;

class UpdateConsumerSetting extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lti:update-consumer-setting {setting : the setting to update} {old-value : the old value of the setting} {new-value : the new value of the setting}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update a consumer setting for all consumers with a given value to a new value.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

protected function promptForMissingArgumentsUsing()
{
return [
'setting' => 'Which LTI consumer setting should I update?',
'old-value' => 'What value of that setting should I replace?',
'new-value' => 'What value should I replace it with?',
];
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$total_processed = 0;
DB::table('lti2_consumer')->orderBy('consumer_pk')->chunk(100, function ($consumers) use (&$total_processed) {
foreach ($consumers as $consumer) {
$settings = json_decode($consumer->settings, true);
if (!array_key_exists($this->argument('setting'), $settings)) {
continue;
}
if ($settings[$this->argument('setting')] !== $this->argument('old-value')) {
continue;
}
$settings[$this->argument('setting')] = $this->argument('new-value');
$consumer->settings = json_encode($settings);
DB::update('UPDATE lti2_consumer SET settings=? WHERE consumer_pk=?', [$consumer->settings, $consumer->consumer_pk]);
$total_processed++;
}
});

$this->info("Successfully updated ".$total_processed." consumer settings.");
return 0;
}

}
2 changes: 2 additions & 0 deletions src/LtiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\ServiceProvider;
use LonghornOpen\LaravelCelticLTI\Commands\AddLti1p2Platform;
use LonghornOpen\LaravelCelticLTI\Commands\AddLti1p3Platform;
use LonghornOpen\LaravelCelticLTI\Commands\UpdateConsumerSetting;

class LtiServiceProvider extends ServiceProvider
{
Expand All @@ -17,6 +18,7 @@ public function boot() : void
$this->commands([
AddLti1p2Platform::class,
AddLti1p3Platform::class,
UpdateConsumerSetting::class,
]);
}

Expand Down

0 comments on commit 1db4bae

Please sign in to comment.