Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save user code on Run button click when in Artist Freeplay #19102

Merged
merged 2 commits into from
Nov 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 53 additions & 31 deletions apps/src/turtle/turtle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,14 @@ Artist.prototype.execute = function () {

// api.log now contains a transcript of all the user's actions.

// If this is a free play level, save the code every time the run button is
// clicked rather than only on finish
if (this.level.freePlay) {
this.levelComplete = true;
this.testResults = TestResults.FREE_PLAY;
this.report(false);
}

if (this.shouldSupportNormalization()) {
// First, draw a normalized version of the user's actions (ie, one which
// doesn't vary patterns or stickers) to a dedicated context. Note that we
Expand Down Expand Up @@ -1765,23 +1773,19 @@ Artist.prototype.checkAnswer = function () {
// Test whether the student can progress to the next level. There can be no
// errors, and this either needs to be a free play/preidction level, or they
// need to have met success conditions.
var levelComplete = (!level.editCode || !this.executionError) &&
this.levelComplete = (!level.editCode || !this.executionError) &&
(level.freePlay ||
this.studioApp_.hasContainedLevels ||
this.isCorrect_(delta, permittedErrors));
this.testResults = this.studioApp_.getTestResults(levelComplete);
this.testResults = this.studioApp_.getTestResults(this.levelComplete);

var program;
if (this.studioApp_.isUsingBlockly()) {
var xml = Blockly.Xml.blockSpaceToDom(Blockly.mainBlockSpace);
program = Blockly.Xml.domToText(xml);
}
var program = this.getUserCode();

// Make sure we don't reuse an old message, since not all paths set one.
this.message = undefined;

// In level K1, check if only lengths differ.
if (level.isK1 && !levelComplete && !this.studioApp_.editCode &&
if (level.isK1 && !this.levelComplete && !this.studioApp_.editCode &&
level.solutionBlocks &&
removeK1Lengths(program) === removeK1Lengths(level.solutionBlocks)) {
this.testResults = TestResults.APP_SPECIFIC_ERROR;
Expand Down Expand Up @@ -1810,16 +1814,6 @@ Artist.prototype.checkAnswer = function () {
}
}

if (level.editCode) {
// If we want to "normalize" the JavaScript to avoid proliferation of nearly
// identical versions of the code on the service, we could do either of these:

// do an acorn.parse and then use escodegen to generate back a "clean" version
// or minify (uglifyjs) and that or js-beautify to restore a "clean" version

program = this.studioApp_.editor.getValue();
}

// If the current level is a free play, always return the free play
// result type
if (level.freePlay) {
Expand All @@ -1841,19 +1835,7 @@ Artist.prototype.checkAnswer = function () {
this.onReportComplete();
});
} else {
var reportData = {
app: 'turtle',
level: level.id,
result: levelComplete,
testResult: this.testResults,
program: encodeURIComponent(program),
onComplete: _.bind(this.onReportComplete, this),
save_to_gallery: level.impressive
};

reportData = this.setReportDataImage_(level, reportData);

this.studioApp_.report(reportData);
this.report();
}

if (this.studioApp_.isUsingBlockly()) {
Expand All @@ -1864,6 +1846,46 @@ Artist.prototype.checkAnswer = function () {
// The call to displayFeedback() will happen later in onReportComplete()
};

Artist.prototype.getUserCode = function () {
if (this.studioApp_.isUsingBlockly()) {
var xml = Blockly.Xml.blockSpaceToDom(Blockly.mainBlockSpace);
return Blockly.Xml.domToText(xml);
} else if (this.level.editCode) {
// If we want to "normalize" the JavaScript to avoid proliferation of nearly
// identical versions of the code on the service, we could do either of these:

// do an acorn.parse and then use escodegen to generate back a "clean" version
// or minify (uglifyjs) and that or js-beautify to restore a "clean" version
return this.studioApp_.editor.getValue();
}
};

/**
* Send the milestone post, including level progress (result and testResults)
* and saved user code.
*
* @param {boolean} [enableOnComplete=true] whether or not to attach the
* onComplete handler to the StudioApp.report call
*/
Artist.prototype.report = function (enableOnComplete = true) {
let reportData = {
app: 'turtle',
level: this.level.id,
result: this.levelComplete,
testResult: this.testResults,
program: encodeURIComponent(this.getUserCode()),
save_to_gallery: this.level.impressive
};

if (enableOnComplete) {
reportData.onComplete = this.onReportComplete.bind(this);
}

reportData = this.setReportDataImage_(this.level, reportData);

this.studioApp_.report(reportData);
};

/**
* Adds the feedback image to the report data if indicated by the level config.
* @param {Object} level Level config.
Expand Down