Skip to content

Commit

Permalink
Merge pull request #1660 from dmetzner2/patch-5
Browse files Browse the repository at this point in the history
SHARE-487 Achievement events + admin
  • Loading branch information
dmetzner authored Jun 4, 2021
2 parents 6dc5659 + da724ef commit 791e2fa
Show file tree
Hide file tree
Showing 74 changed files with 2,916 additions and 53 deletions.
10 changes: 10 additions & 0 deletions assets/css/custom/achievements.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
.achievement__badge__image {
z-index: 1;
width: 10rem;
height: 10rem;
margin: auto;

&.achievement__badge__image--small {
Expand All @@ -42,6 +43,11 @@
}
}

.achievement-top__badge__image {
width: 7rem;
height: 7rem;
}

.achievement-top__badge__banner,
.achievement__badge__banner {
position: absolute;
Expand Down Expand Up @@ -76,3 +82,7 @@
text-align: left;
}

.achievement__badge__text {
margin-top: 0.25rem;
}

18 changes: 11 additions & 7 deletions config/packages/sonata_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ sonata_admin:
items:
- catrowebadmin.block.survey

sonata.admin.group.db_updater:
label: DB Updater
label_catalogue: Catrobat
icon: '<i class="fa fa-cogs"></i>'
items:
- catrowebadmin.block.cron_jobs
- catrowebadmin.block.special_updater
- catrowebadmin.block.achievements
- catrowebadmin.block.extensions
- catrowebadmin.block.tags

sonata.admin.group.tools:
label: Tools
label_catalogue: Catrobat
Expand All @@ -148,13 +159,6 @@ sonata_admin:
- catrowebadmin.block.tools.broadcast
- catrowebadmin.block.tools.mail

sonata.admin.group.extensions:
label: Extensions
label_catalogue: Catrobat
icon: '<i class="fa fa-external-link"></i>'
items:
- catrowebadmin.block.extensions.all

sonata.admin.group.statistics:
label: Statistics
label_catalogue: Catrobat
Expand Down
78 changes: 65 additions & 13 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ services:
tags: ['controller.service_arguments']
public: true

App\Admin\:
resource: '../src/Admin/**/Controller'
tags: [ 'controller.service_arguments' ]
public: true

App\Api_deprecated\Controller\:
resource: '../src/Api_deprecated/Controller'
tags: ['controller.service_arguments']
Expand Down Expand Up @@ -308,6 +313,13 @@ services:
public: true
App\Catrobat\EventListener\ResettingSendEmailInitializeSubscriber: "@resetting_send_email_initialize"

App\EventListener\UserPostPersistNotifier:
tags:
- { name: 'doctrine.orm.entity_listener', event: 'postPersist', entity: 'App\Entity\User' }

App\EventListener\UserPostUpdateNotifier:
tags:
- { name: 'doctrine.orm.entity_listener', event: 'postUpdate', entity: 'App\Entity\User' }

# ====================================================================================================================
# Listeners:
Expand Down Expand Up @@ -713,6 +725,58 @@ services:
- ~
public: true

# ===== DB Updater

catrowebadmin.block.special_updater:
class: App\Admin\DB_Updater\SpecialUpdaterAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: "Special Updater", icon: '<i class="fa fa-cogs"></i>' }
arguments:
- ~
- ~
- App\Admin\DB_Updater\Controller\SpecialUpdaterAdminController
public: true

catrowebadmin.block.cron_jobs:
class: App\Admin\DB_Updater\CronJobsAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: "Cron Jobs", icon: '<i class="fa fa-hourglass"></i>' }
arguments:
- ~
- App\Entity\CronJob
- App\Admin\DB_Updater\Controller\CronJobsAdminController
public: true

catrowebadmin.block.achievements:
class: App\Admin\DB_Updater\AchievementsAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: "Achievements" }
arguments:
- ~
- App\Entity\Achievements\Achievement
- App\Admin\DB_Updater\Controller\AchievementsAdminController
public: true


catrowebadmin.block.extensions:
class: App\Admin\AllExtensionsAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: Extensions (WIP) }
arguments:
- ~
- App\Entity\Extension
- App\Catrobat\Controller\Admin\ExtensionController
public: true

catrowebadmin.block.tags:
class: App\Admin\AllExtensionsAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: "Tags (WIP)" }
arguments:
- ~
- App\Entity\Extension
- App\Catrobat\Controller\Admin\ExtensionController
public: true

# ===== Tools

Expand Down Expand Up @@ -788,22 +852,10 @@ services:
- App\Catrobat\Controller\Admin\EmailUserMessageController
public: true

# ===== Extensions

catrowebadmin.block.extensions.all:
class: App\Admin\AllExtensionsAdmin
tags:
- { name: sonata.admin, manager_type: orm, label: "All Extensions" }
arguments:
- ~
- App\Entity\Extension
- App\Catrobat\Controller\Admin\ExtensionController
public: true

# ===== Statistics

catrowebadmin.block.tools.statistic:
class: App\Admin\Blocks\StatisticBlockService
class: App\Admin\StatisticBlockService
tags:
- { name: sonata.block, label: Cleanup }
arguments:
Expand Down
12 changes: 12 additions & 0 deletions deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@
run('sh docker/app/init-jwt-config.sh');
});

task('update:achievements', function () {
cd('{{release_path}}');
run('bin/console catrobat:update:achievements');
});

task('update:special', function () {
cd('{{release_path}}');
run('bin/console catrobat:update:special');
});

/**
* Main task
*/
Expand Down Expand Up @@ -158,6 +168,8 @@
'deploy:jwt',
'restart:nginx',
'restart:php-fpm',
'update:achievements',
'update:special',
'deploy:unlock',
'slack:notify:success',
'cleanup',
Expand Down
31 changes: 31 additions & 0 deletions migrations/2021/Version20210531200855.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210531200855 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE cronjob (name VARCHAR(255) NOT NULL, state VARCHAR(255) DEFAULT \'idle\' NOT NULL, cron_interval VARCHAR(255) DEFAULT \'1 days\' NOT NULL, priority INT DEFAULT 0 NOT NULL, start_at DATETIME DEFAULT NULL, end_at DATETIME DEFAULT NULL, result_code INT DEFAULT NULL, PRIMARY KEY(name)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE cronjob');
}
}
Empty file removed src/Admin/.gitignore
Empty file.
70 changes: 70 additions & 0 deletions src/Admin/DB_Updater/AchievementsAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Admin\DB_Updater;

use App\Manager\AchievementManager;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Route\RouteCollection;

class AchievementsAdmin extends AbstractAdmin
{
/**
* @var string
*/
protected $baseRouteName = 'admin_catrobat_adminbundle_achievementsadmin';

/**
* @var string
*/
protected $baseRoutePattern = 'achievements';

protected AchievementManager $achievement_manager;

public function __construct($code, $class, $baseControllerName, AchievementManager $achievement_manager)
{
parent::__construct($code, $class, $baseControllerName);

$this->achievement_manager = $achievement_manager;
}

protected function configureRoutes(RouteCollection $collection): void
{
$collection
->remove('export')
->remove('acl')
->remove('delete')
->remove('create')
->add('update_achievements')
;
}

/**
* @param mixed $object
*/
public function getUnlockedByCount($object): int
{
$id = $object->getId();

return $this->achievement_manager->countUserAchievementsOfAchievement($id);
}

/**
* @param ListMapper $list
*
* Fields to be shown on lists
*/
protected function configureListFields(ListMapper $list): void
{
$list
->add('priority')
->add('internal_title')
->add('internal_description')
->add('badge', null, ['template' => 'Admin/achievement_badge_image.html.twig'])
->add('badge_locked', null, ['template' => 'Admin/achievement_badge_locked_image.html.twig'])
->add('banner_color')
->add('enabled')
->add('unlocked_by', 'string', ['template' => 'Admin/achievement_unlocked_by.html.twig'])
;
}
}
64 changes: 64 additions & 0 deletions src/Admin/DB_Updater/Controller/AchievementsAdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Admin\DB_Updater\Controller;

use App\Commands\Helpers\CommandHelper;
use App\Manager\AchievementManager;
use Exception;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class AchievementsAdminController extends CRUDController
{
protected AchievementManager $achievement_manager;

public function __construct(AchievementManager $achievement_manager)
{
$this->achievement_manager = $achievement_manager;
}

public function listAction(Request $request = null): Response
{
$achievements = $this->achievement_manager->findAllAchievements();

$numberOfUserAchievements = [];
foreach ($achievements as $achievement) {
$id = $achievement->getId();
$numberOfUserAchievements[$id] = $this->achievement_manager->countUserAchievementsOfAchievement($id);
}

return $this->renderWithExtraParams('Admin/DB_Updater/admin_achievements.html.twig', [
'action' => 'update_achievements',
'numberOfUserAchievements' => $numberOfUserAchievements,
'updateAchievementsUrl' => $this->admin->generateUrl('update_achievements'),
]);
}

/**
* @throws Exception
*/
public function updateAchievementsAction(KernelInterface $kernel): RedirectResponse
{
if (!$this->admin->isGranted('ACHIEVEMENTS')) {
throw new AccessDeniedException();
}

$output = new BufferedOutput();
$result = CommandHelper::executeShellCommand(
['bin/console', 'catrobat:update:achievements'], ['timeout' => 86400], '', $output, $kernel
);

if (0 === $result) {
$this->addFlash('sonata_flash_success', 'Achievements have been successfully updated');
} else {
$this->addFlash('sonata_flash_error', "Updating achievements failed!\n".$output->fetch());
}

return new RedirectResponse($this->admin->generateUrl('list'));
}
}
Loading

0 comments on commit 791e2fa

Please sign in to comment.