]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6170 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 28 Sep 2023 08:05:01 +0000 (10:05 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 28 Sep 2023 08:05:01 +0000 (10:05 +0200)
app/Console/Commands/QuizThemePreview.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/Quiz/PreviewOperation.php
app/Jobs/GenerateQuizThemePreview.php [new file with mode: 0644]
app/Models/QuizTheme.php
resources/quiztheme/theme_preview/theme_preview.js [new file with mode: 0644]

diff --git a/app/Console/Commands/QuizThemePreview.php b/app/Console/Commands/QuizThemePreview.php
new file mode 100644 (file)
index 0000000..ea9d2ca
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Console\Commands\Base\ToolboxCommand;
+use App\Jobs\GenerateQuizThemePreview;
+use App\Models\QuizTheme;
+use Cubist\Util\ArrayUtil;
+use Illuminate\Database\Query\Builder;
+
+class QuizThemePreview extends ToolboxCommand
+{
+    protected $signature = 'quiz:theme:preview {id?}';
+    protected $description = 'Generate a quiz theme preview';
+
+    public function handle()
+    {
+        $themeId = $this->argument('id');
+        /** @var Builder $q */
+        $q = QuizTheme::withoutGlobalScopes()->where('created_ok', '1')->orderBy('id', 'desc');
+        if (null !== $themeId) {
+            $themeIds = ArrayUtil::parseRange($themeId);
+            $q->whereIn('id', $themeIds);
+        }
+        $themes = $q->get();
+        $this->progressBar = $this->output->createProgressBar(count($themes));
+        $this->progressBar->start();
+
+        foreach ($themes as $theme) {
+            dispatch_sync(new GenerateQuizThemePreview($theme));
+            $this->progressBar->advance();
+        }
+    }
+}
index 4d42a4fc19c6c9a591a9d44a8421a8ae0c0f1a40..7d2348b4b77bd6150f30e88a1e3a5617c617558e 100644 (file)
@@ -32,7 +32,6 @@ trait PreviewOperation
             ->where('hash', '[0-9a-f]{32}')
             ->where('path', '.*')
             ->whereNumber('time')
-            ->whereAlpha('version')
             ->withoutMiddleware([CheckIfAdmin::class])
             ->name('quiz_preview_with_time');
     }
diff --git a/app/Jobs/GenerateQuizThemePreview.php b/app/Jobs/GenerateQuizThemePreview.php
new file mode 100644 (file)
index 0000000..ef40c18
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\QuizTheme;
+use Cubist\Util\CommandLine;
+use Cubist\Util\Files\Files;
+
+class GenerateQuizThemePreview extends Base
+{
+    /**
+     * @var QuizTheme
+     */
+    public $theme;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(QuizTheme $theme)
+    {
+        parent::__construct();
+        $this->theme = $theme;
+    }
+
+    public function uniqueId()
+    {
+        return $this->theme->id;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $cl = new CommandLine('node');
+        $cl->setArg(null, resource_path('quiztheme/theme_preview/theme_preview.js'));
+        $cl->setArg('width', 1200);
+        $cl->setArg('height', 680);
+        $cl->setArg('delay', 2);
+        $cl->setArg('scale', 1);
+        $cl->setArg('intro', self::getPreviewPath($this->theme->getIdValue(), 'intro'));
+        $cl->setArg('standard', self::getPreviewPath($this->theme->getIdValue(), 'standard'));
+        $cl->setArg('draganddrop', self::getPreviewPath($this->theme->getIdValue(), 'draganddrop'));
+        $cl->setArg('outro', self::getPreviewPath($this->theme->getIdValue(), 'outro'));
+        $url = $this->theme->getPreviewURL(['shortLoading' => 1, 'transition' => 1, 'puppeteer' => 1]);
+        $cl->setArg('url', $url);
+        $cl->execute();
+        $cl->debug();
+    }
+
+    public static function getPreviewPath($themeId, $variant = '')
+    {
+        return Files::mkdir(storage_path('quizthemes') . '/' . $themeId) . $variant . '.jpg';
+    }
+}
index ba3cd72ed449d767a4657853fa016de169cb2516..0a1e932d027c50ddc59de9d527609211256909c3 100644 (file)
@@ -41,7 +41,7 @@ class QuizTheme extends ToolboxModel
 
         $this->addField('hash', Hidden::class);
         $this->addField('name', 'Text', __('Nom'), ['column' => true]);
-        //$this->addField('preview', 'NoValue', __('Preview'), ['column_escape' => false, 'column' => true, 'column_type' => 'model_function', 'column_function_name' => 'getPreviewImage', 'column_limit' => -1]);
+        $this->addField('preview', 'NoValue', __('Preview'), ['column_escape' => false, 'column' => true, 'column_type' => 'model_function', 'column_function_name' => 'getPreviewImage', 'column_limit' => -1]);
         $this->setSettingsFields();
     }
 
@@ -255,4 +255,23 @@ class QuizTheme extends ToolboxModel
         unlink($tmp);
         return $blur;
     }
+
+    public function getPreviewURL($params = [], $page = 2)
+    {
+        return self::getThemePreviewURL($this->getIdValue(), $params, $page);
+    }
+
+    public static function getThemePreviewURL($id, $params = [])
+    {
+        $defaultParams = ['id' => '1061-' . $id, 'hash' => 'a549a6f6da4fba85cd47e2871e0153a8', 'time' => time(), 'path' => 'index.html', 'force' => 0, 'shortLoading' => 1, 'puppeteer' => 0];
+        return route('quiz_preview_with_time', array_merge($defaultParams, $params));
+    }
+
+
+    public function getPreviewImage()
+    {
+        return clean('<img src="' . backpack_url('quizthemepreview/' . $this->getIdValue() . '-intro.jpg') . '" width="200" height="140" alt="" />');
+    }
+
+
 }
diff --git a/resources/quiztheme/theme_preview/theme_preview.js b/resources/quiztheme/theme_preview/theme_preview.js
new file mode 100644 (file)
index 0000000..fd54013
--- /dev/null
@@ -0,0 +1,61 @@
+const puppeteer = require('puppeteer');
+const commandLineArgs = require('command-line-args');
+const optionDefinitions = [
+    {name: 'url', type: String},
+    {name: 'intro', type: String},
+    {name: 'standard', type: String},
+    {name: 'draganddrop', type: String},
+    {name: 'outro', type: String},
+    {name: 'delay', type: Number, defaultOption: 10},
+    {name: 'scale', type: Number, defaultOption: 1},
+    {name: 'width', type: Number, defaultOption: 1200},
+    {name: 'height', type: Number, defaultOption: 680},
+    {name: 'page', type: Number, defaultOption: 2}
+];
+
+(async () => {
+    const {installMouseHelper} = require('./install-mouse-helper');
+    const options = commandLineArgs(optionDefinitions);
+    const browser = await puppeteer.launch({
+        headless: 'new',
+        args: [
+            "--disable-setuid-sandbox",
+            "--no-sandbox",
+        ],
+        executablePath: 'google-chrome-stable',
+    });
+
+    const page = await browser.newPage();
+    page.on('console', async (msg) => {
+        const msgArgs = msg.args();
+        for (let i = 0; i < msgArgs.length; ++i) {
+            console.log(await msgArgs[i].jsonValue());
+        }
+    });
+    await page.setViewport({
+        width: options.width / options.scale, height: options.height / options.scale, deviceScaleFactor: options.scale,
+    });
+
+    // Intro screen
+    await page.goto(options.url, {waitUntil: 'networkidle2'});
+    await page.screenshot({path: options.intro, type: 'jpeg', quality: 95});
+
+    // Question 1
+    await page.evaluate(() => window.quiz.screens.showScreen('q-0'));
+    await new Promise(r => setTimeout(r, 1000 * (options.delay + 3)));
+    await page.screenshot({path: options.standard, type: 'jpeg', quality: 95});
+    await page.evaluate(() => window.quiz.score.setAnswer(0, [0]));
+
+    // Question 2
+    await  page.evaluate(() => window.quiz.screens.showScreen('q-1'));
+    await new Promise(r => setTimeout(r, 1000 * (options.delay + 3)));
+    await page.screenshot({path: options.draganddrop, type: 'jpeg', quality: 95});
+    await  page.evaluate(() => window.quiz.score.setAnswer(1, [1, 1, 2, 1, 2]));
+
+    // Outro
+    await  page.evaluate(() => window.quiz.screens.showScreen('outro'));
+    await new Promise(r => setTimeout(r, 1000 * (options.delay + 3)));
+    await page.screenshot({path: options.outro, type: 'jpeg', quality: 95});
+
+    await browser.close();
+})();