namespace App\Elearning;
+use App\Fields\FluidbookFont;
use App\Jobs\Base;
use App\Models\Quiz;
+use App\Models\QuizTheme;
use Cubist\Scorm\Manifest;
use Cubist\Scorm\Version;
use Cubist\Util\CommandLine\Npx;
use Cubist\Util\Files\VirtualDirectory;
+use Cubist\Util\Graphics\Color;
class QuizCompiler extends Base
{
*/
protected $quiz;
+
+ /**
+ * @var QuizTheme
+ */
+ protected $theme;
+
protected $dest;
protected $forceScorm = false;
protected $sassVariables = [];
+ protected $mixDirectories = [];
public function __construct($quiz, $dest, $forceScorm = false)
{
$this->quiz = Quiz::withoutGlobalScopes()->find($quiz);
+ $this->theme = QuizTheme::withoutGlobalScopes()->find($this->quiz->theme ?: 3);
$this->compilePath = protected_path('quiz/compile/' . $this->quiz->id);
$this->dest = $dest;
$this->forceScorm = $forceScorm;
$vdir->file_put_contents('index.html', $html);
-
if ($this->forceScorm || $this->quiz->getAttribute('scorm')) {
$scormVersion = $this->quiz->getAttribute('scorm_version') ?: Version::SCORM_1_2;
$manifest = new Manifest($this->quiz->getAttribute('title'), $scormVersion, $this->quiz->getAttribute('client') ?: backpack_user()->getCompanyNameAttribute(), $this->quiz->getAttribute('project') ?: 'Quiz');
protected function writeStyles()
{
+ // Main texts color
+ $this->sassVariables['texts-color'] = $this->theme->textsColor;
+ if ($this->theme->textsColor == 'auto' || !$this->theme->textsColor) {
+ // IF auto mode, check between black and white which have the biggest distance (i.e. most contrast) with neutralColor
+ $neutral = new Color($this->theme->neutralColor);
+ $black = new Color('#000000');
+ $white = new Color('#ffffff');
+ $distBlack = $neutral->distance($black);
+ $distWhite = $neutral->distance($white);
+ if ($distWhite > $distBlack) {
+ $this->sassVariables['texts-color'] = '#ffffff';
+ } else {
+ $this->sassVariables['texts-color'] = '#000000';
+ }
+ }
+ // Font
+ $this->sassVariables['font'] = $this->_font($this->theme->font);
+ // Colors
+ $this->sassVariables['neutral-color'] = $this->theme->neutralColor;
$this->writeSass();
}
+ protected function _font($f)
+ {
+ $font = FluidbookFont::getAvailableFonts()[$f];
+ if ($font['font_kit']) {
+ $this->addFontKit($font['font_kit']);
+ }
+ return $font['font_family'];
+ }
+
+ public function addFontKit($kit)
+ {
+ if (!$kit) {
+ return;
+ }
+
+ $kitPath = resource_path('fluidbookpublication/fonts/' . $kit);
+ `mkdir -p $this->compilePath/style/font;cp -r $kitPath/* $this->compilePath/style/font/`;
+ $this->mixDirectories["style/font"] = "dist/css";
+ file_put_contents($this->compilePath . '/style/style.sass', '@import font/font.css' . "\n" . file_get_contents($this->compilePath . '/style/style.sass'));
+ }
+
protected function writeData(&$data)
{
$data = $this->quiz->getData();
$compile = new VirtualDirectory($this->compilePath);
$compile->copyDirectory(resource_path('quizv2/js'), 'js');
$compile->copyDirectory(resource_path('quizv2/style'), 'style');
- $mix = 'const mix = require("laravel-mix");
-mix.setPublicPath("dist").js("js/quiz.js", "js")
- .sass("style/style.sass", "css").options({processCssUrls: false}).version();
-';
- $compile->file_put_contents('webpack.mix.js', $mix);
$compile->sync(true);
}
protected function runWebpack()
{
+ $mix = 'const mix = require("laravel-mix");';
+ foreach ($this->mixDirectories as $from => $to) {
+ $mix .= 'mix.copyDirectory("' . $from . '","' . $to . '");';
+ }
+ $mix .= 'mix.setPublicPath("dist").js("js/quiz.js", "js")
+ .sass("style/style.sass", "css").options({processCssUrls: false}).version();';
+ file_put_contents($this->compilePath . '/webpack.mix.js', $mix);
+
$cli = new Npx();
$cli->cd($this->compilePath);
$cli->setModule('cross-env process.env.local=1 mix --production');