Skip to content

Commit e67f6fe

Browse files
committed
Added explanation to questions
1 parent 228fcfe commit e67f6fe

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed

dataBaseSchema.sql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ ALTER TABLE post OWNER TO postgres;
104104

105105
CREATE TABLE question (
106106
id uuid DEFAULT public.gen_random_uuid() NOT NULL,
107-
key character varying(255),
107+
key character varying(255) NOT NULL,
108108
text text,
109109
certification_id uuid,
110-
"position" integer
110+
"position" integer,
111+
explanation text
111112
);
112113

113114

@@ -172,6 +173,14 @@ ALTER TABLE ONLY post
172173
ADD CONSTRAINT post_pkey PRIMARY KEY (id);
173174

174175

176+
--
177+
-- Name: question question_key_certification_id_unique; Type: CONSTRAINT; Schema: open_certification_trainer; Owner: postgres
178+
--
179+
180+
ALTER TABLE ONLY question
181+
ADD CONSTRAINT question_key_certification_id_unique UNIQUE (key, certification_id);
182+
183+
175184
--
176185
-- Name: question question_pkey; Type: CONSTRAINT; Schema: open_certification_trainer; Owner: postgres
177186
--

src/components/PostEditView.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default class PostEditView extends React.PureComponent<IBaseProps, PostEd
2626
this.markdownChanged = this.markdownChanged.bind(this);
2727
this.save = this.save.bind(this);
2828
this.delete = this.delete.bind(this);
29+
this.openHelp = this.openHelp.bind(this);
2930
}
3031

3132
componentDidMount() {
@@ -107,6 +108,10 @@ export default class PostEditView extends React.PureComponent<IBaseProps, PostEd
107108
});
108109
}
109110

111+
openHelp() {
112+
window.open("https://github.github.com/gfm/", "about:blank");
113+
}
114+
110115
render() {
111116
if (!this.state.post) {
112117
return <p>Loading</p>;
@@ -117,6 +122,7 @@ export default class PostEditView extends React.PureComponent<IBaseProps, PostEd
117122
<MessageBar message= { this.state.message } errors={ this.state.errors } />
118123
<ButtonToolbar>
119124
<ButtonGroup>
125+
<Button bsStyle="default" onClick={ this.openHelp }>Help</Button>
120126
<Button bsStyle="default" onClick={ this.save }>Save</Button>
121127
</ButtonGroup>
122128
<ButtonGroup>

src/components/PostView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface PostViewProps {
1010

1111
const PostView = ( props: PostViewProps ) => (
1212
<Panel>
13-
<p style={ { "text-align": "right" } }>{ new Date(props.post.created_on).toTimeString() }</p>
13+
<p style={ { "text-align": "right" } }>{ props.post.created_on }</p>
1414

1515
<ReactMarkdown
1616
key={ props.post.id }

src/components/QuestionEditView.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class QuestionEditView extends React.PureComponent<QuestionEditVi
2323
this.addAnswer = this.addAnswer.bind(this);
2424
this.deleteAnswer = this.deleteAnswer.bind(this);
2525
this.onQuestionTextChange = this.onQuestionTextChange.bind(this);
26+
this.onQuestionExplanationChange = this.onQuestionExplanationChange.bind(this);
2627
this.onQuestionKeyChange = this.onQuestionKeyChange.bind(this);
2728
}
2829

@@ -38,6 +39,10 @@ export default class QuestionEditView extends React.PureComponent<QuestionEditVi
3839
return true;
3940
}
4041

42+
if (q.explanation !== nq.explanation) {
43+
return true;
44+
}
45+
4146
if (!q.answers && nq.answers) {
4247
return true;
4348
}
@@ -57,7 +62,6 @@ export default class QuestionEditView extends React.PureComponent<QuestionEditVi
5762
return false;
5863
}
5964

60-
6165
onAnswerChange(index: number, answer: Answer) {
6266
const question = this.props.question;
6367
const answers = (question.answers || []).map((value, i) => i != index ? value : answer);
@@ -83,6 +87,14 @@ export default class QuestionEditView extends React.PureComponent<QuestionEditVi
8387
this.props.onQuestionChange(update);
8488
}
8589

90+
onQuestionExplanationChange (e: any) {
91+
const value = e.target.value;
92+
const question = this.props.question;
93+
const update = { ...question, explanation: new Text({ value: value }) };
94+
95+
this.props.onQuestionChange(update);
96+
}
97+
8698
addAnswer() {
8799
const question = this.props.question;
88100
const update = { ...question, answers: (question.answers || []).concat(new Answer({ id: uuid(), isCorrect: false })) };
@@ -111,6 +123,11 @@ export default class QuestionEditView extends React.PureComponent<QuestionEditVi
111123
control={ { componentClass: "textarea", rows: 3, value: this.props.question.text ? this.props.question.text.value : "", onChange: this.onQuestionTextChange } }
112124
label="Question"
113125
/>
126+
<FieldGroup
127+
id={ this.props.question.id + "_qExplanation" }
128+
control={ { componentClass: "textarea", rows: 3, value: this.props.question.explanation ? this.props.question.explanation.value : "", onChange: this.onQuestionExplanationChange } }
129+
label="Explanation"
130+
/>
114131
<Button bsStyle="success" onClick={ this.addAnswer }>Add Answer</Button>
115132
<ButtonGroup vertical block type="checkbox">
116133
{ this.props.question.answers && (this.props.question.answers.map((a, index) => {

src/components/QuestionView.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Question from "../model/Question";
44
import Answer from "../model/Answer";
55
import AnswerView from "./AnswerView";
66
import IAssociativeArray from "../domain/IAssociativeArray";
7+
import * as ReactMarkdown from "react-markdown";
78

89
export interface QuestionViewProps {
910
question: Question;
@@ -33,12 +34,24 @@ export default class QuestionView extends React.PureComponent<QuestionViewProps,
3334
const content =
3435
<div style={ { whiteSpace: "pre-wrap" } }>
3536
<h3>{ this.props.question.key }</h3>
36-
<p>{ this.props.question.text.value }</p>
37+
<p>{ this.props.question.text ? this.props.question.text.value : "" }</p>
3738
<ButtonGroup vertical block>
3839
{ this.props.question.answers ? (this.props.question.answers.map(a => (
3940
<AnswerView onAnswerChange={ this.props.onAnswerChange } disabled={ this.props.answersDisabled } checked={ this.props.checkedAnswers ? (this.props.checkedAnswers[a.id] || false) : false } answer={ a } key={ a.id } highlightIfCorrect={ this.props.highlightCorrectAnswers } highlightIfIncorrect={ this.props.highlightIncorrectAnswers } />
4041
))) : <span>No answers found</span> }
4142
</ButtonGroup>
43+
{ this.props.answersDisabled && this.props.question.explanation && this.props.question.explanation.value &&
44+
(<div>
45+
<h4>Explanation</h4>
46+
<ReactMarkdown
47+
key={ this.props.question.id }
48+
className="result"
49+
source={ this.props.question.explanation ? this.props.question.explanation.value : "" }
50+
skipHtml={ true }
51+
escapeHtml={ true }
52+
/>
53+
</div>)
54+
}
4255
</div>;
4356

4457
return (

src/controllers/CourseController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const retrieveCourse = (courseName: string) => {
6767
for (let i = 0; dbQuestions && i < dbQuestions.length; i++) {
6868
const dbQuestion = dbQuestions[i];
6969

70-
const question = new Question({ id: dbQuestion.id, key: dbQuestion.key, text: new Text({ value: dbQuestion.text }), position: dbQuestion.position, answers: [] });
70+
const question = new Question({ id: dbQuestion.id, key: dbQuestion.key, text: new Text({ value: dbQuestion.text }), explanation: new Text({ value: dbQuestion.explanation }), position: dbQuestion.position, answers: [] });
7171

7272
questions.push(
7373
pool.query("SELECT * from open_certification_trainer.answer AS answer WHERE answer.question_id = $1 ORDER BY answer.key", [question.id])
@@ -139,9 +139,9 @@ export const postUpload = (req: Request, res: Response) => {
139139

140140
// Upsert question one by one
141141
query.push(
142-
`INSERT INTO open_certification_trainer.question (id, key, text, certification_id, position) VALUES ('${ question.id }', '${ escapeSpecialCharacters(question.key) }', '${ question.text ? escapeSpecialCharacters(question.text.value) : "" }', '${ data.id }', '${ question.position }')
142+
`INSERT INTO open_certification_trainer.question (id, key, text, explanation, certification_id, position) VALUES ('${ question.id }', '${ escapeSpecialCharacters(question.key) }', '${ question.text ? escapeSpecialCharacters(question.text.value) : "" }', '${ question.explanation ? escapeSpecialCharacters(question.explanation.value) : "" }', '${ data.id }', '${ question.position }')
143143
ON CONFLICT(id) DO
144-
UPDATE SET (key, text, certification_id, position) = ('${ escapeSpecialCharacters(question.key) }', '${ question.text ? escapeSpecialCharacters(question.text.value) : "" }', '${ data.id }', '${ question.position }')
144+
UPDATE SET (key, text, explanation, certification_id, position) = ('${ escapeSpecialCharacters(question.key) }', '${ question.text ? escapeSpecialCharacters(question.text.value) : "" }', '${ question.explanation ? escapeSpecialCharacters(question.explanation.value) : "" }', '${ data.id }', '${ question.position }')
145145
WHERE open_certification_trainer.question.id = '${ question.id }';`
146146
);
147147

src/model/Question.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Text from "./Text";
44
export interface QuestionProps {
55
key?: string;
66
text?: Text;
7+
explanation?: Text;
78
position?: number;
89
answers?: Array<Answer>;
910
id: string;
@@ -12,13 +13,15 @@ export interface QuestionProps {
1213
export default class Question {
1314
key: string;
1415
text: Text;
16+
explanation: Text;
1517
position: number;
1618
answers: Array<Answer>;
1719
id: string;
1820

1921
constructor(props: QuestionProps) {
2022
this.key = props.key;
2123
this.text = props.text;
24+
this.explanation = props.explanation;
2225
this.position = props.position;
2326
this.answers = props.answers;
2427
this.id = props.id;

0 commit comments

Comments
 (0)