Skip to content

Commit

Permalink
Merge branch 'evaluate-correct-submissions' into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienTainon committed Jun 5, 2024
2 parents 05b3d00 + 54837c5 commit a6a98da
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
4 changes: 2 additions & 2 deletions frontend/common/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ export default function(bundle: Bundle) {
if (false !== reloadTask) {
yield* put({type: StepperActionTypes.StepperExit});

if (!state.options.tabsEnabled) {
const activeBufferName = state.buffers.activeBufferName;
const activeBufferName = state.buffers.activeBufferName;
if (!state.options.tabsEnabled && null !== activeBufferName) {
yield* put(bufferChangePlatform(activeBufferName, newPlatform));
}
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/submission/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ export default function (bundle: Bundle) {

// Refresh test visualization
const currentTestId = yield* appSelect(state => state.task.currentTestId);
yield* put(updateCurrentTestId({testId: currentTestId, record: false}));
if (null !== currentTestId) {
yield* put(updateCurrentTestId({testId: currentTestId, record: false}));
}
});

yield* takeEvery(submissionChangeDisplayedError, function* ({payload}) {
Expand Down
6 changes: 3 additions & 3 deletions frontend/task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function* taskLoadSaga(app: App, action) {

const currentTask = yield* appSelect(state => state.task.currentTask);
if (!isServerTask(currentTask)) {
yield* call(subscribePlatformHelper);
yield* fork(subscribePlatformHelper);
}

if (currentTask) {
Expand Down Expand Up @@ -985,10 +985,10 @@ export default function (bundle: Bundle) {
return;
}
const state = yield* appSelect();
const currentBuffer = state.buffers.activeBufferName;
if (state.options.tabsEnabled || !state.buffers.activeBufferName) {
yield* call(createSourceBufferFromDocument, answer.document, answer.platform);
} else {
const currentBuffer = state.buffers.activeBufferName;
} else if (null !== currentBuffer) {
if (state.buffers.buffers[currentBuffer].platform !== answer.platform) {
yield* put(bufferChangePlatform(currentBuffer, answer.platform, answer.document));
} else {
Expand Down
8 changes: 6 additions & 2 deletions frontend/task/libs/quickalgo_library_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {selectActiveBufferPlatform} from '../../buffers/buffer_selectors';
import {selectAvailableExecutionModes} from '../../submission/submission_selectors';
import {submissionChangeExecutionMode} from '../../submission/submission_slice';

export function* createQuickalgoLibrary() {
export function* createQuickalgoLibrary(platformAlreadyChanged: boolean = false) {
let state = yield* appSelect();
let oldContext = quickAlgoLibraries.getContext(null, state.environment);
log.getLogger('libraries').debug('Create a context', state.environment);
Expand Down Expand Up @@ -124,9 +124,13 @@ export function* createQuickalgoLibrary() {
availablePlatforms = availablePlatforms.filter(platform => -1 !== taskAvailablePlatforms.indexOf(platform));
}
if (-1 === availablePlatforms.indexOf(state.options.platform) && availablePlatforms.length) {
if (platformAlreadyChanged) {
throw new Error("Platform has already changed once, cannot converge to a valid platform");
}

yield* put({type: CommonActionTypes.PlatformChanged, payload: {platform: availablePlatforms[0], reloadTask: true}});

return false;
return yield* call(createQuickalgoLibrary, true);
}

yield* put(taskSetAvailablePlatforms(availablePlatforms));
Expand Down
35 changes: 34 additions & 1 deletion frontend/task/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ import {Codecast} from '../../app_types';
import {Document} from '../../buffers/buffer_types';
import {quickAlgoLibraries} from '../libs/quick_algo_libraries_model';
import {ActionTypes} from '../../common/actionTypes';
import {TaskAnswer} from '../task_types';
import {isServerTask, TaskAnswer} from '../task_types';
import {RECORDING_FORMAT_VERSION} from '../../version';
import {BlockBufferHandler, uncompressIntoDocument} from '../../buffers/document';
import {CodecastPlatform} from '../../stepper/codecast_platform';
import {hasBlockPlatform} from '../../stepper/platforms';
import {callPlatformValidate} from '../../submission/submission_actions';
import {loadOptionsFromQuery} from '../../common/options';
import {CodecastOptions} from '../../store';
import {asyncGetFile} from '../../utils/api';

let getTaskAnswer: () => Generator<unknown, TaskAnswer>;
let getTaskState: () => Generator;
Expand Down Expand Up @@ -365,6 +366,31 @@ function* taskGetStateEventSaga ({payload: {success}}: ReturnType<typeof taskGet
yield* call(success, strDump);
}

function* taskGetResourcesImportCorrectSolutions(resources) {
let taskSettingsParsed = getTaskMetadata();
const task = yield* appSelect(state => state.task.currentTask);
if (isServerTask(task)) {
const taskSettings = yield* call(asyncGetFile, 'taskSettings.json');
if (!taskSettings) {
return;
}
taskSettingsParsed = JSON.parse(taskSettings);
}

if (!taskSettingsParsed?.correctSolutions) {
return;
}

const {correctSolutions} = taskSettingsParsed;
resources.correct_solutions = [];
for (let correctSolution of correctSolutions) {
const {path, language, grade} = correctSolution;
const correctedPath = path.replace(/\$TASK_PATH\/?/, '');
const solution = yield* call(asyncGetFile, correctedPath);
resources.correct_solutions.push({type: 'solution', solution, id: path, language, grade});
}
}

function* taskGetResourcesPostSaga ({payload: {resources, callback}}: ReturnType<typeof taskGetResourcesPost>) {
const options = yield* appSelect(state => state.options);
let optionsToPreload = {};
Expand All @@ -388,6 +414,13 @@ function* taskGetResourcesPostSaga ({payload: {resources, callback}}: ReturnType
}
});

try {
yield* call(taskGetResourcesImportCorrectSolutions, resources);
} catch (e) {
// Avoid blocking errors here
console.error(e);
}

// For Castor platform, we need to add custom scripts that will be added to the assets during the generation of the task
const castorScriptInject = `window.codecastPreload = JSON.parse('${JSON.stringify(optionsToPreload)}');
document.body.setAttribute('id', 'app');
Expand Down
2 changes: 1 addition & 1 deletion frontend/task/task_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export interface TaskServer extends TaskNormalized {
export type Task = QuickalgoTask & Partial<TaskServer>;

export function isServerTask(object: Task|null): boolean {
return (object && null !== object.id && undefined !== object.id) || window.PEMTaskMetaData;
return !!((object && null !== object.id && undefined !== object.id) || window.PEMTaskMetaData);
}

export function isServerTest(object: TaskTest): boolean {
Expand Down
2 changes: 1 addition & 1 deletion frontend/task/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function checkCompilingCode(answer: TaskAnswer|null, state: AppStore, dis

export function getBlocksUsage(answer: TaskAnswer|null) {
const context = quickAlgoLibraries.getContext(null, 'main');
if (!context) {
if (!context || !answer) {
return null;
}

Expand Down
21 changes: 21 additions & 0 deletions frontend/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ export const asyncGetJson = function (path, withToken: boolean = false) {

return promise;
};

export const asyncGetFile = function(path) {
let req;
const promise = new Promise<string>(function(resolve, reject) {
req = request.get(path);

req.end(function(err, res) {
if (err || !res.ok) {
return reject({err, res});
}

resolve(res.text);
});
});

promise[CANCEL] = () => {
req.abort();
};

return promise;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,6 @@
"webpack-dev-middleware": "^6.1.1",
"webpack-hot-middleware": "^2.25.4",
"worker-loader": "^3.0.8"
}
},
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}

0 comments on commit a6a98da

Please sign in to comment.