Skip to content

Commit

Permalink
add edit function for peml_parsons problem
Browse files Browse the repository at this point in the history
  • Loading branch information
keweizhan committed Apr 17, 2024
1 parent 90ca67c commit 27e902f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 79 deletions.
97 changes: 69 additions & 28 deletions app/controllers/exercises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,46 +283,87 @@ def edit
end
# -------------------------------------------------------------
def edit_parsons
step = params[:step]
step = params[:step].to_i
full_name = "parsons_s#{step}"
puts "full_name = #{full_name}"
@exercise = Exercise.where('name LIKE ?', "%#{full_name}%").first
json_file_path = Rails.root.join('public', 'data', 'simple_code.json')
if File.exist?(json_file_path)
file_content = File.read(json_file_path)
json_content = JSON.parse(file_content)
@step_data = json_content["s#{step}"]

if step >= 8
html_file_path = Rails.root.join('app', 'views', 'exercises', 'Jsparson', 'exercise', 'simple', "s#{step}.html.erb")
if File.exist?(html_file_path)
file_content = File.read(html_file_path)
peml_content_match = file_content.match(/pemlContent\s*=\s*`(.*?)`/m)
if peml_content_match
@peml_content = peml_content_match[1].strip
else
@peml_content = ''
end
else
@peml_content = ''
end
else
@step_data = {}
end
json_file_path = Rails.root.join('public', 'data', 'simple_code.json')
if File.exist?(json_file_path)
file_content = File.read(json_file_path)
json_content = JSON.parse(file_content)
@step_data = json_content["s#{step}"]
else
@step_data = {}
end
end
end

# -------------------------------------------------------------
def update_parsons
step = params[:step]
json_data = params[:step_json_data] # Ensure this matches the name attribute from the form
Rails.logger.debug "Received JSON data: #{json_data}"
step = params[:step].to_i

json_file_path = Rails.root.join('public', 'data', 'simple_code.json')

unless File.exist?(json_file_path)
redirect_to edit_parsons_exercise_path(step: step), alert: 'JSON file not found.'
return
end
if step >= 8
peml_content = params[:peml_content]
if peml_content.present?
html_file_path = Rails.root.join('app', 'views', 'exercises', 'Jsparson', 'exercise', 'simple', "s#{step}.html.erb")
unless File.exist?(html_file_path)
redirect_to edit_parsons_exercise_path(step: step), alert: 'HTML file not found.'
return
end

begin
content = JSON.parse(File.read(json_file_path))
updated_data = JSON.parse(json_data)
if content["s#{step}"].present?
content["s#{step}"] = updated_data
File.write(json_file_path, JSON.pretty_generate(content))
redirect_to edit_parsons_exercise_path(step: step), notice: 'Parsons updated successfully.'
begin
file_content = File.read(html_file_path)
updated_content = file_content.gsub(/pemlContent\s*=\s*`.*?`/m, "pemlContent = `#{peml_content}`")
File.write(html_file_path, updated_content)
redirect_to edit_parsons_exercise_path(step: step), notice: 'Parsons updated successfully.'
rescue => e
Rails.logger.debug e.inspect # Log the error for inspection
redirect_to edit_parsons_exercise_path(step: step), alert: "Failed to update Parsons: #{e.message}"
end
else
redirect_to edit_parsons_exercise_path(step: step), alert: 'PEML content is missing.'
end
else
updated_content = params[:step_content]
if updated_content.present?
json_file_path = Rails.root.join('public', 'data', 'simple_code.json')
unless File.exist?(json_file_path)
redirect_to edit_parsons_exercise_path(step: step), alert: 'JSON file not found.'
return
end

begin
content = JSON.parse(File.read(json_file_path))
updated_data = JSON.parse(updated_content)
if content["s#{step}"].present?
content["s#{step}"] = updated_data
File.write(json_file_path, JSON.pretty_generate(content))
redirect_to edit_parsons_exercise_path(step: step), notice: 'Parsons updated successfully.'
else
redirect_to edit_parsons_exercise_path(step: step), alert: "Step not found in JSON."
end
rescue JSON::ParserError => e
Rails.logger.debug e.inspect # Log the error for inspection
redirect_to edit_parsons_exercise_path(step: step), alert: "Failed to parse JSON: #{e.message}"
end
else
redirect_to edit_parsons_exercise_path(step: step), alert: "Step not found in JSON."
redirect_to edit_parsons_exercise_path(step: step), alert: 'Step content is missing.'
end
rescue JSON::ParserError => e
Rails.logger.debug e.inspect # Log the error for inspection
redirect_to edit_parsons_exercise_path(step: step), alert: "Failed to parse JSON: #{e.message}"
end
end

Expand Down
68 changes: 33 additions & 35 deletions app/views/exercises/Jsparson/exercise/simple/s8.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,39 @@
<%= javascript_include_tag 'skulpt-stdlib' %>

<script>
const pemlContent = `
# Simple example of a problem that is assessed by the order of the
# blocks.
# Derived from jsParsons hello-parson.html.
exercise_id: https://github.com/CSSPLICE/peml-feasibility-examples/blob/main/parsons/hello-parson-noindent-order.peml
title: Your First Parsons Problem -- Ordered
author: Cliff Shaffer (from js-Parsons, converted to PEML)
license.id: MIT
license.owner: Cliff Shaffer
tags.topics: PEML Demo Parsons Problem
tags.style: parsons, order
instructions:----------
**Your task**: Construct a Python program that prints strings "Hello", "Parsons", and "Problems" on their own lines. You can get feedback on your current solution with the feedback button. You should construct your program by dragging and dropping the lines to the solution area on the right.
----------
[assets.code.starter.files]
[.content]
tag: one
display: print('Hello')
tag: two
display: print('Parsons')
tag: three
display: print('Problems!')
[]
# A (simple) DAG -- there is only one acceptable order
[assets.test.files]
content:----------
one:
two: one
three: two
----------
`;
const pemlContent = `# Simple example of a problem that is assessed by the order of the
# blocks.
# Derived from jsParsons hello-parson.html.
exercise_id: https://github.com/CSSPLICE/peml-feasibility-examples/blob/main/parsons/hello-parson-noindent-order.peml
title: Your First Parsons Problem -- Ordered
author: Cliff Shaffer (from js-Parsons, converted to PEML)
license.id: MIT
license.owner: Cliff Shaffer
tags.topics: PEML Demo Parsons Problem
tags.style: parsons, order
instructions:----------
**Your task**: Construct a Python program that prints strings "Hello", "Parsons", and "Problems" on their own lines. You can get feedback on your current solution with the feedback button. You should construct your program by dragging and dropping the lines to the solution area on the right.
----------
[assets.code.starter.files]
[.content]
tag: one
display: print('Hello')
tag: two
display: print('Parsons')
tag: three
display: print('Problems!')
[]
# A (simple) DAG -- there is only one acceptable order
[assets.test.files]
content:----------
one:
two: one
three: two
----------`;

const title = pemlContent.match(/title:\s*(.*)/)[1].trim();
const instructions = pemlContent.match(/instructions:----------\s*([\s\S]*?)\s*----------/)[1].trim();
Expand Down
47 changes: 31 additions & 16 deletions app/views/exercises/edit_parsons.html.haml
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
- if !@lti_launch
= render partial: 'layouts/breadcrumb'

%h1
Edit Parsons:
- if @exercise
= link_to @exercise.display_name, exercise_practice_path(@exercise)
- else
%span Error: Exercise not found.

%h1 Edit Parsons:
- if @exercise
= link_to @exercise.display_name, exercise_practice_path(@exercise)
- else
%span Error: Exercise not found.

%h2
Edit JSON Data for Step: #{params[:step]}
= form_tag update_parsons_exercise_path(step: params[:step]), method: :post do
%fieldset
.field
= label_tag :step_json_data, "Step JSON Data"
= text_area_tag :step_json_data, JSON.pretty_generate(@step_data), class: 'json-editor', rows: 20, cols: 80
.actions
= submit_tag "Save JSON Changes", class: 'btn btn-primary'
%h2 Edit JSON Data for Step: #{params[:step]}

- if params[:step].to_i >= 8
= form_tag update_parsons_exercise_path(step: params[:step]), method: :post do
%fieldset
.field
= label_tag :peml_content, "PEML Content"
= text_area_tag :peml_content, @peml_content, class: 'peml-editor', rows: 20, cols: 80
.actions
= submit_tag "Save PEML Changes", class: 'btn btn-primary'
- else
= form_tag update_parsons_exercise_path(step: params[:step]), method: :post do
%fieldset
.field
= label_tag :step_json_data, "Step JSON Data"
= text_area_tag :step_json_data, JSON.pretty_generate(@step_data), class: 'json-editor', rows: 20, cols: 80
.actions
= submit_tag "Save JSON Changes", class: 'btn btn-primary'

:css
.json-editor {
Expand All @@ -26,4 +33,12 @@
border: 1px solid #ccc;
width: 100%;
margin-top: 5px;
}

.peml-editor {
font-family: monospace;
background-color: #f9f9f9;
border: 1px solid #ccc;
width: 100%;
margin-top: 5px;
}

0 comments on commit 27e902f

Please sign in to comment.