Skip to content
Snippets Groups Projects
Commit 594fd748 authored by jtimotei's avatar jtimotei
Browse files

Bugfix for changing labeltype to T/F

parent c3c3a587
No related branches found
No related tags found
1 merge request!31Layout side panel
...@@ -10,7 +10,7 @@ class PanelMCQ extends React.Component { ...@@ -10,7 +10,7 @@ class PanelMCQ extends React.Component {
this.onChangeNPA = this.onChangeNPA.bind(this) this.onChangeNPA = this.onChangeNPA.bind(this)
this.onChangeLabelType = this.onChangeLabelType.bind(this) this.onChangeLabelType = this.onChangeLabelType.bind(this)
this.generateLabels = this.generateLabels.bind(this) this.generateLabels = this.generateLabels.bind(this)
this.updateMCO = this.updateMCO.bind(this) this.updateNumberOptions = this.updateNumberOptions.bind(this)
this.state = { this.state = {
isMCQ: false, isMCQ: false,
...@@ -27,7 +27,7 @@ class PanelMCQ extends React.Component { ...@@ -27,7 +27,7 @@ class PanelMCQ extends React.Component {
problemId: prob.id, problemId: prob.id,
isMCQ: prob.mc_options.length > 0, isMCQ: prob.mc_options.length > 0,
nrPossibleAnswers: prob.mc_options.length || 2, nrPossibleAnswers: prob.mc_options.length || 2,
chosenLabelType: prob.labelType || 2, chosenLabelType: PanelMCQ.deriveLabelType(prob.mc_options),
} }
} }
...@@ -36,11 +36,11 @@ class PanelMCQ extends React.Component { ...@@ -36,11 +36,11 @@ class PanelMCQ extends React.Component {
static deriveLabelType (options) { static deriveLabelType (options) {
if (options.length === 0) { if (options.length === 0) {
return 0 return 2
} else if (options.length === 2 && ((options[0].label === 'T' && options[1].label === 'F') } else if (options.length === 2 && ((options[0].label === 'T' && options[1].label === 'F')
|| (options[0].label === 'F' && options[1].label === 'T'))) { || (options[0].label === 'F' && options[1].label === 'T'))) {
return 1 return 1
} else if (options[0].label.match(/[a-z]/)) { } else if (options[0].label.match(/[A-Z]/)) {
return 2 return 2
} else if (parseInt(options[0].label)) { } else if (parseInt(options[0].label)) {
return 3 return 3
...@@ -49,19 +49,14 @@ class PanelMCQ extends React.Component { ...@@ -49,19 +49,14 @@ class PanelMCQ extends React.Component {
} }
} }
updateMCO() { updateNumberOptions() {
let difference = this.state.nrPossibleAnswers - this.props.problem.mc_options.length let difference = this.state.nrPossibleAnswers - this.props.problem.mc_options.length
if (difference > 0) { if (difference > 0) {
let startingAt = this.props.problem.mc_options.length let startingAt = this.props.problem.mc_options.length
let labels = this.generateLabels(difference, startingAt) let labels = this.generateLabels(difference, startingAt)
this.props.generateMCOs(labels) return this.props.generateMCOs(labels)
} else if (difference < 0) { } else if (difference < 0) {
this.props.deleteMCOs(-difference) return this.props.deleteMCOs(-difference)
}
if (this.state.chosenLabelType !== this.props.problem.labelType) {
let labels = this.generateLabels(this.state.nrPossibleAnswers, 0)
this.props.updateMCOs(labels)
} }
} }
...@@ -74,7 +69,7 @@ class PanelMCQ extends React.Component { ...@@ -74,7 +69,7 @@ class PanelMCQ extends React.Component {
} }
this.setState({ this.setState({
nrPossibleAnswers: value nrPossibleAnswers: value
}, this.updateMCO) }, this.updateNumberOptions)
} }
} }
...@@ -82,13 +77,22 @@ class PanelMCQ extends React.Component { ...@@ -82,13 +77,22 @@ class PanelMCQ extends React.Component {
onChangeLabelType (e) { onChangeLabelType (e) {
let value = parseInt(e.target.value) let value = parseInt(e.target.value)
if (!isNaN(value)) { if (!isNaN(value)) {
this.setState({
chosenLabelType: value
}, this.updateMCO)
if (parseInt(value) === 1) { if (parseInt(value) === 1) {
this.setState({ this.setState({
nrPossibleAnswers: 2 nrPossibleAnswers: 2,
}, this.updateMCO) chosenLabelType: value
}, () => {
this.updateNumberOptions()
let labels = this.generateLabels(this.state.nrPossibleAnswers, 0)
this.props.updateLabels(labels)
})
} else {
this.setState({
chosenLabelType: value
}, () => {
let labels = this.generateLabels(this.state.nrPossibleAnswers, 0)
this.props.updateLabels(labels)
})
} }
} }
} }
......
...@@ -59,8 +59,7 @@ class Exams extends React.Component { ...@@ -59,8 +59,7 @@ class Exams extends React.Component {
}), }),
widthMCO: 24, widthMCO: 24,
heightMCO: 38, heightMCO: 38,
isMCQ: problem.mc_options && problem.mc_options.length !== 0, // is the problem a mc question - used to display PanelMCQ isMCQ: problem.mc_options && problem.mc_options.length !== 0 // is the problem a mc question - used to display PanelMCQ
labelType: PanelMCQ.deriveLabelType(problem.mc_options)
} }
} }
}) })
...@@ -125,14 +124,26 @@ class Exams extends React.Component { ...@@ -125,14 +124,26 @@ class Exams extends React.Component {
} }
updateFeedbackAtIndex = (feedback, problemWidget, idx) => { updateFeedbackAtIndex = (feedback, problemWidget, idx) => {
let fb = [...problemWidget.problem.feedback]
if (idx === -1) { if (idx === -1) {
problemWidget.problem.feedback.push(feedback) fb.push(feedback)
} else { } else {
if (feedback.deleted) problemWidget.problem.feedback.splice(idx, 1) if (feedback.deleted) fb.splice(idx, 1)
else problemWidget.problem.feedback[idx] = feedback else fb[idx] = feedback
} }
this.setState({
widgets: this.state.widgets this.setState(prevState => {
return {
widgets: update(prevState.widgets, {
[problemWidget.id]: {
'problem': {
'feedback': {
$set: fb
}
}
}
})
}
}) })
} }
...@@ -319,15 +330,15 @@ class Exams extends React.Component { ...@@ -319,15 +330,15 @@ class Exams extends React.Component {
} }
deleteMCO = (widget, index) => { deleteMCO = (widgetId, index, nrMCOs) => {
let widget = this.state.widgets[widgetId]
if (nrMCOs <= 0 || !widget.problem.mc_options.length) return;
let option = widget.problem.mc_options[index] let option = widget.problem.mc_options[index]
api.del('mult-choice/' + option.id) return api.del('mult-choice/' + option.id)
.catch(err => { .catch(err => {
console.log(err) console.log(err)
err.json().then(res => { err.json().then(res => {
this.setState({
deletingMCWidget: false
})
Notification.error('Could not delete multiple choice option' + Notification.error('Could not delete multiple choice option' +
(res.message ? ': ' + res.message : '')) (res.message ? ': ' + res.message : ''))
// update to try and get a consistent state // update to try and get a consistent state
...@@ -335,32 +346,33 @@ class Exams extends React.Component { ...@@ -335,32 +346,33 @@ class Exams extends React.Component {
}) })
}) })
.then(res => { .then(res => {
let index = widget.problem.feedback.findIndex(e => { return e.id === res.feedback_id })
let feedback = widget.problem.feedback[index] let feedback = widget.problem.feedback[index]
feedback.deleted = true feedback.deleted = true
this.updateFeedbackAtIndex(feedback, widget, index) this.updateFeedback(feedback)
} this.setState((prevState) => {
) return {
widgets: update(prevState.widgets, {
[widget.id]: {
problem: {
mc_options: {
$splice: [[index, 1]]
}
}
}
})
}
}, () => {
this.deleteMCO(widgetId, index, nrMCOs-1)
})
})
}
deleteMCOs = (widget, startIndex, nrMCOs) => {
let options = [...widget.problem.mc_options] let options = [...widget.problem.mc_options]
options.splice(index, 1) options.splice(startIndex, nrMCOs)
// remove the mc options from the state this.deleteMCO(widget.id, startIndex, nrMCOs)
// note that this can happen before they are removed in the DB due to async calls
this.setState((prevState) => {
return {
widgets: update(prevState.widgets, {
[widget.id]: {
problem: {
mc_options: {
$set: options
}
}
}
}),
deletingMCWidget: false
}
})
} }
/** /**
...@@ -591,14 +603,13 @@ class Exams extends React.Component { ...@@ -591,14 +603,13 @@ class Exams extends React.Component {
let len = problem.mc_options.length let len = problem.mc_options.length
if (nrMCOs >= len) { if (nrMCOs >= len) {
this.setState({deletingMCWidget: true}) this.setState({deletingMCWidget: true})
} else { } else if (nrMCOs > 0) {
for (let i = 0; i < nrMCOs; i++) { this.deleteMCOs(selectedWidget, len - nrMCOs, nrMCOs)
this.deleteMCO(selectedWidget, len - 1 - i)
}
} }
}} }}
updateMCOs={(labels) => { updateLabels={(labels) => {
problem.mc_options.map((option, index) => { labels.map((label, index) => {
let option = problem.mc_options[index]
const formData = new window.FormData() const formData = new window.FormData()
formData.append('name', option.widget.name) formData.append('name', option.widget.name)
formData.append('x', option.widget.x + option.cbOffsetX) formData.append('x', option.widget.x + option.cbOffsetX)
...@@ -635,21 +646,6 @@ class Exams extends React.Component { ...@@ -635,21 +646,6 @@ class Exams extends React.Component {
}) })
}) })
}) })
// this.setState(prevState => ({
// widgets: update(prevState.widgets, {
// [selectedWidget.id]: {
// 'problem': {
// 'mc_options': {
// $set : options
// },
// 'labelType': {
// $set: PanelMCQ.deriveLabelType(options)
// }
// }
// }
// })
// }))
}} }}
/> ) : null} /> ) : null}
<this.PanelExamActions /> <this.PanelExamActions />
...@@ -874,7 +870,7 @@ class Exams extends React.Component { ...@@ -874,7 +870,7 @@ class Exams extends React.Component {
this.state.widgets[this.state.selectedWidgetId].problem.name}"`} this.state.widgets[this.state.selectedWidgetId].problem.name}"`}
confirmText='Delete multiple choice options' confirmText='Delete multiple choice options'
onCancel={() => this.setState({deletingMCWidget: false})} onCancel={() => this.setState({deletingMCWidget: false})}
onConfirm={() => this.deleteMCWidget(this.state.selectedWidgetId)} onConfirm={() => this.deleteMCO(this.state.selectedWidgetId, 0)}
/> />
</div> </div>
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment