function generateContest($tasks, $contestID, $contestFolder, $fullFeedback = false, $status = 'RunningContest') { global $mode, $contestLocalDir; if ($status == 'RunningContest' || $status == 'FutureContest') { $generateSolutions = false; } $strQuestions = ''; $strQuestionsArr = array(); $strSolutions = ''; $images = array(); $imagesSols = array(); $jsModulesRes = array(); $cssModulesRes = array(); $strGraders = ""; $tasks = json_decode($_REQUEST['tasks'], true); $numPart = 0; $nameParts = ""; $buffer = ""; foreach ($tasks as $curTask) { $strQuestion = ""; $jsQuestions = ''; $cssQuestions = ''; $cssSolutions = ''; $jsModules = array(); $jsCurrentModules = array(); $cssCurrentModules = array(); $cssModules = array(); list($curFolder, $curKey) = explode('/', $curTask['url']); $taskUrl = $curTask['url'] . '/'; $task = new PEMTaskCompiler($curTask['bebras'], __DIR__ . '/beaver_tasks/' . $curFolder . '/' . $curKey . '/', true); // Task directory if ($mode != "aws") { $dstDir = $contestLocalDir . $contestFolder . '/' . $curKey; if (!is_dir($dstDir)) { mkdir($dstDir); } } // Copy bebras.js $bebrasJsContent = 'var json = ' . $task->getBebrasJson() . '; function getTaskResources() { return json; }'; $bebrasJsDstFile = $contestFolder . '/' . $curKey . '/bebras.js'; contestPutContents($bebrasJsDstFile, $bebrasJsContent); $curImages = $task->copyImages(PEMTaskCompiler::TASK, $contestFolder . '/' . $curKey, 'contestCopyFile'); $images = array_merge($images, Bebras::addAbsoluteStaticPath($curImages, $contestFolder . '/' . $curKey)); $curImagesSols = $task->copyImages(PEMTaskCompiler::SOLUTION, $contestFolder . '/' . $curKey, 'contestCopyFileSols'); $imagesSols = array_merge($imagesSols, Bebras::addAbsoluteStaticPath($curImagesSols, $contestFolder . '/' . $curKey)); // Convert JS and CSS image path $questionJs = $task->getJavascript(PEMTaskCompiler::TASK | PEMTaskCompiler::SAT | PEMTaskCompiler::DISPLAY | PEMTaskCompiler::PROXY); $solutionJs = $task->getJavascript(PEMTaskCompiler::SOLUTION); $questionCss = $task->getCss(PEMTaskCompiler::TASK | PEMTaskCompiler::SAT | PEMTaskCompiler::DISPLAY | PEMTaskCompiler::PROXY); $solutionCss = $task->getCss(PEMTaskCompiler::SOLUTION); // Javascript & css modules $modules = $task->getModules(); $jsCurrentModules = $modules['jsModules']['ref']; $jsModulesRes = array_merge($jsModulesRes, $jsCurrentModules); $cssCurrentModules = $modules['cssModules']['ref']; $cssModulesRes = array_merge($cssModulesRes, $cssCurrentModules); // JS modules content foreach ($modules['jsModules']['inline'] as $curJsModuleContent) { $jsQuestions .= $curJsModuleContent; } // Css modules content foreach ($modules['cssModules']['inline'] as $curCssModuleContent) { $cssQuestions .= $curCssModuleContent; } // Javascript grader $strGraders .= '<div id="javascript-grader-' . $curKey . '" data-content="' . htmlspecialchars($task->getGrader(), ENT_COMPAT, 'UTF-8') . '"></div>' . "\r\n"; $questionRelatedJs = Bebras::moveQuestionImagesSrc($questionJs, $curKey, $contestFolder); $solutionRelatedJs = Bebras::moveQuestionImagesSrc($solutionJs, $curKey, $contestFolder); $cssQuestions .= Bebras::moveQuestionImagesSrc($questionCss, $curKey, $contestFolder); $cssSolutions .= Bebras::moveQuestionImagesSrc($solutionCss, $curKey, $contestFolder); // Content $questionBody = $task->getContent(PEMTaskCompiler::TASK); $questionSolution = $task->getContent(PEMTaskCompiler::SOLUTION); // Remove absolute images $questionBody = preg_replace('#http\\://.*\\.(png|jpg|gif|jpeg)#isU', '', $questionBody); $strQuestion .= '<div id="question-' . $curKey . '" class="question"><div id="task" class="taskView">' . "\r\n" . '<style>' . $cssQuestions . '</style>' . Bebras::moveQuestionImagesSrc($questionBody, $curKey, $contestFolder) . '</div></div>' . "\r\n"; $strQuestion .= '<div id="javascript-' . $curKey . '" data-content="' . htmlspecialchars($questionRelatedJs, ENT_COMPAT, 'UTF-8') . '"></div>' . "\r\n"; foreach ($jsCurrentModules as $name => $content) { $strQuestion .= '<div class="js-module-' . $curKey . '" data-content="' . $name . '"></div>' . "\n"; } foreach ($cssCurrentModules as $name => $content) { $strQuestion .= '<div class="css-module-' . $curKey . '" data-content="' . $name . '"></div>' . "\n"; } $strSolutions .= '<div id="solution-' . $curKey . '" class="solution">' . "\r\n" . '<style>' . $cssSolutions . '</style>' . Bebras::moveQuestionImagesSrc($questionSolution, $curKey, $contestFolder) . '</div>' . "\r\n" . '<div id="javascript-solution-' . $curKey . '" data-content="' . htmlspecialchars($solutionRelatedJs, ENT_COMPAT, 'UTF-8') . '"></div>' . "\r\n"; $strQuestions .= $strQuestion; contestAddContent($contestFolder, $strQuestion, $nameParts, $buffer, $numPart, false); } $jsPreload = "\r\n//ImagesLoader is injected by the platform just before the contest is loaded\r\n"; $jsPreload .= "ImagesLoader.setImagesToPreload([\n'" . implode("' ,\n'", $images) . "']);\r\n"; $jsPreload .= "function preloadSolImages() { var imagesToLoad = [\n'" . implode("' ,\n'", $imagesSols) . "'];\r\n ImagesLoader.addImagesToPreload(imagesToLoad);\r\n}\n"; $htAccessContent = '<Files "contest_' . $contestID . '_sols.html">' . "\n" . "\t" . 'Deny from all' . "\n" . '</Files>' . "\n" . '<Files "bebras.js">' . "\n" . "\t" . 'Deny from all' . "\n" . '</Files>' . "\n"; if (!$fullFeedback) { $htAccessContent .= '<Files "contest_' . $contestID . '_graders.html">' . "\n" . "\t" . 'Deny from all' . "\n" . '</Files>' . "\n"; } // Compile js modules $strQuestions .= "\n"; foreach ($jsModulesRes as $name => $content) { $strModule = '<div class="js-module" id="js-module-' . $name . '" data-content="' . htmlspecialchars($content, ENT_COMPAT, 'UTF-8') . '"></div>' . "\n"; $strQuestions .= $strModule; contestAddContent($contestFolder, $strModule, $nameParts, $buffer, $numPart, false); } // Compile css modules $strQuestions .= "\n"; foreach ($cssModulesRes as $name => $content) { $strModule .= '<div class="css-module" id="css-module-' . $name . '" data-content="' . htmlspecialchars($content, ENT_COMPAT, 'UTF-8') . '"></div>' . "\n"; $strQuestions .= $strModule; contestAddContent($contestFolder, $strModule, $nameParts, $buffer, $numPart, false); } contestAddContent($contestFolder, "", $nameParts, $buffer, $numPart, true); contestPutContents($contestFolder . '/index.txt', trim($nameParts)); // Preload //$strQuestions .= '<div id="preload-images-js" data-content="'.htmlspecialchars($jsPreload, ENT_COMPAT, 'UTF-8').'"></div>'."\n"; // Create files contestPutContents($contestFolder . '/contest_' . $contestID . '.html', $strQuestions); contestPutContents($contestFolder . '/contest_' . $contestID . '_sols.html', $strSolutions, true); contestPutContents($contestFolder . '/contest_' . $contestID . '.js', $jsPreload); contestPutContents($contestFolder . '/contest_' . $contestID . '_graders.html', $strGraders, !$fullFeedback); contestPutContents($contestFolder . '/.htaccess', $htAccessContent, true); }
/** * Generate the task file */ public function generateTaskFile() { $task = new PEMTaskCompiler($this->taskDirectory . '/bebras.json', $this->taskDirectory); //$bebrasJson = file_get_contents($this->taskDirectory.'/bebras.json'); $content = '<!doctype html> <html> <head> <meta charset="utf-8"> <title>' . $task->getTitle() . '</title> ' . $task->getStaticResourcesImportHtml(PEMTaskCompiler::INCLUDE_MODULES | PEMTaskCompiler::TASK | PEMTaskCompiler::SOLUTION | PEMTaskCompiler::GRADER) . ' <script class="remove" type="text/javascript">var json = ' . $task->getBebrasJsonForQuestion() . ';</script> </head> <body> <task>' . "\r\n" . $task->getContent(PEMTaskCompiler::TASK) . "\n" . self::getOtherImagesHtml($task->getContent(PEMTaskCompiler::Task)) . "\r\n" . '</task> <solution>' . "\r\n" . $task->getContent(PEMTaskCompiler::SOLUTION) . "\r\n" . '</solution> </body> </html>'; file_put_contents($this->taskDirectory . '/index.html', $content); }