Index: lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/dbupdates/patch20200218.sql =================================================================== diff -u --- lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/dbupdates/patch20200218.sql (revision 0) +++ lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/dbupdates/patch20200218.sql (revision 0a5a11351daa8e308190921254096c712ce88837) @@ -0,0 +1,21 @@ +-- Turn off autocommit, so nothing is committed if there is an error +SET AUTOCOMMIT = 0; +SET FOREIGN_KEY_CHECKS=0; +-- Put all sql statements below here + +--LDEV-4976 Introduce an extra column which holds the same information as parent table, then add unique index to prevent duplicates + +ALTER TABLE tl_lamc11_usr_attempt ADD COLUMN qb_tool_question_uid BIGINT AFTER uid; + +UPDATE tl_lamc11_usr_attempt AS a JOIN lams_qb_tool_answer AS q ON a.uid = q.answer_uid +SET a.qb_tool_question_uid = q.tool_question_uid; + +ALTER TABLE tl_lamc11_usr_attempt MODIFY COLUMN qb_tool_question_uid BIGINT NOT NULL, + ADD UNIQUE INDEX IDX_attempt_duplicate_prevent (qb_tool_question_uid, que_usr_id); + +-- Put all sql statements above here + +-- If there were no errors, commit and restore autocommit to on +COMMIT; +SET AUTOCOMMIT = 1; +SET FOREIGN_KEY_CHECKS=1; Index: lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/model/McUsrAttempt.java =================================================================== diff -u -rf49dd9f6b076828f464fc954bb9c3dc0c87e8b37 -r0a5a11351daa8e308190921254096c712ce88837 --- lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/model/McUsrAttempt.java (.../McUsrAttempt.java) (revision f49dd9f6b076828f464fc954bb9c3dc0c87e8b37) +++ lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/model/McUsrAttempt.java (.../McUsrAttempt.java) (revision 0a5a11351daa8e308190921254096c712ce88837) @@ -66,6 +66,11 @@ @Column private boolean passed; + // this is a copy of tool_question_uid from lams_qb_tool_answer, so we can create an unique index on it and que_usr_id + // there are no getters or setter for this column as they are not needed + @Column(name = "qb_tool_question_uid") + private Long qbToolQuestionUid; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "que_usr_id") private McQueUsr mcQueUsr; @@ -79,7 +84,7 @@ public McUsrAttempt(Date attemptTime, McQueContent mcQueContent, McQueUsr mcQueUsr, QbOption qbOption, Integer mark, boolean passed, boolean attemptCorrect, int confidenceLevel) { this.attemptTime = attemptTime; - this.qbToolQuestion = mcQueContent; + setMcQueContent(mcQueContent); this.mcQueUsr = mcQueUsr; this.qbOption = qbOption; this.mark = mark; @@ -105,6 +110,7 @@ public void setMcQueContent(McQueContent mcQueContent) { this.qbToolQuestion = mcQueContent; + this.qbToolQuestionUid = this.qbToolQuestion.getUid(); } public McQueUsr getMcQueUsr() { @@ -160,6 +166,10 @@ this.confidenceLevel = confidenceLevel; } + public Long getQbToolQuestionUid() { + return qbToolQuestionUid; + } + /** * Get the mark for displaying to the user. If retries or passmark is off, then just check whether or not answer is * correct If retries and passmark is on, then we only want the marks if the user has passed!