--- /dev/null
+<?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();
+ }
+ }
+}
->where('hash', '[0-9a-f]{32}')
->where('path', '.*')
->whereNumber('time')
- ->whereAlpha('version')
->withoutMiddleware([CheckIfAdmin::class])
->name('quiz_preview_with_time');
}
--- /dev/null
+<?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';
+ }
+}
$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();
}
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="" />');
+ }
+
+
}
--- /dev/null
+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();
+})();