diff --git a/app/client/views/studies/edit_study_designs.coffee b/app/client/views/studies/edit_study_designs.coffee index 23f683c..e143b06 100644 --- a/app/client/views/studies/edit_study_designs.coffee +++ b/app/client/views/studies/edit_study_designs.coffee @@ -1,128 +1,123 @@ +listQuestionnaireIds = new ReactiveVar([]) +listRecordPhysicalData = new ReactiveVar(false) + +remainingQuestionnaires = (design) -> + qIds = design.questionnaireIds or [] + qIds = _.union(qIds, (listQuestionnaireIds.get() or []) ) + Questionnaires.find + _id: {$nin: qIds} + + Template.editStudyDesigns.helpers + designs: -> + StudyDesigns.find studyId: @_id, + sort: {createdAt: 1} + + #this design=design titleEO: -> - self = @ - value: @title + design = @design + value: design.title emptytext: "no title" success: (response, newVal) -> - StudyDesigns.update self._id, + StudyDesigns.update design._id, $set: {title: newVal} return - return - designs: -> - StudyDesigns.find studyId: @_id, - sort: {createdAt: 1} + #this design=design + hasRemainingQuestionnaires: -> + remainingQuestionnaires(@design).count() + + #this design=design + remainingQuestionnaires: -> + remainingQuestionnaires(@design) - #this: design + #this design=design + questionnaires: -> + qIds = @design.questionnaireIds or [] + qIds = _.union(qIds, (listQuestionnaireIds.get() or []) ) + Questionnaires.find + _id: {$in: qIds} + + listRecordPhysicalData: -> + @design.recordPhysicalData || listRecordPhysicalData.get() + + #this design=design visits: -> - @visits.sort (a, b)-> + @design.visits.sort (a, b)-> a.day - b.day prevDay = 0 #augment visits #http://stackoverflow.com/questions/13789622/accessing-parent-context-in-meteor-templates-and-template-helpers - self = @ - @visits.map (v)-> + @design.visits.map (v)-> daysBetween = v.day-prevDay _.extend v, date: moment().add(v.day, 'days').toDate() daysBetween: daysBetween prevDay = v.day if daysBetween is 0 delete v.daysBetween - #questionnaireIds - v.designQuestionnaireIds = self.questionnaireIds v - #this design - hasRemainingQuestionnaires: -> - qIds = @questionnaireIds or [] - qIds = _.union(qIds, (Session.get('editStudyDesignsQuestionnaireIds') or []) ) - Questionnaires.find( - _id: {$nin: qIds} - ).count() - - remainingQuestionnaires: -> - qIds = @questionnaireIds or [] - qIds = _.union(qIds, (Session.get('editStudyDesignsQuestionnaireIds') or []) ) - Questionnaires.find - _id: {$nin: qIds} - - #this design - questionnaires: -> - qIds = @questionnaireIds or [] - qIds = _.union(qIds, (Session.get('editStudyDesignsQuestionnaireIds') or []) ) - Questionnaires.find - _id: {$in: qIds} - - #this visit - designQuestionnaires: -> - qIds = @designQuestionnaireIds or [] - qIds = _.union(qIds, (Session.get('editStudyDesignsQuestionnaireIds') or []) ) - Questionnaires.find - _id: {$in: qIds} - -#this: { studyDesign:StudyDesign visit:StudyDesign.visit questionnaire:Questionnaire } -Template.editVisitQuestionnaireTd.helpers - iconClass: -> - self = @ + #this design:StudyDesign visit:StudyDesign.visit questionnaire:Questionnaire + questionnaireIconClass: -> + questionnaire = @questionnaire found = false if @visit.questionnaireIds _.some @visit.questionnaireIds, (qId)-> - found = qId is self.questionnaire._id + found = qId is questionnaire._id found if found return "fa-check-square-o brand-primary" else return "fa-square-o hoverOpaqueExtreme" -#this: { studyDesign:StudyDesign visit:StudyDesign.visit } -Template.editVisitPhysTd.helpers - iconClass: -> - if @visit.recordPhysicalData? + #this design:StudyDesign visit:StudyDesign.visit + physicalIconClass: -> + if @visit.recordPhysicalData? and @visit.recordPhysicalData return "fa-check-square-o brand-primary" else return "fa-square-o hoverOpaqueExtreme" Template.editStudyDesigns.events "click #createStudyDesign": (evt) -> Meteor.call "createStudyDesign", @_id, (error, studyDesignId) -> throwError error if error? "submit #addVisit": (evt) -> evt.preventDefault() offset = evt.target.offset.value if offset? and offset.length > 0 console.log "offset #{offset}" evt.target.offset.value = "" evt.target.offset.blur() Meteor.call "addStudyDesignVisit", @_id, offset, (error) -> throwError error if error? - "click .addQuestionnaire": (evt) -> + "click .listQuestionnaire": (evt) -> evt.preventDefault() questionnaireId = $(evt.target).data("id") - qIds = Session.get("editStudyDesignsQuestionnaireIds") or [] + qIds = listQuestionnaireIds.get() or [] qIds.push questionnaireId - Session.set "editStudyDesignsQuestionnaireIds", qIds + listQuestionnaireIds.set qIds + + "click .listRecordPhysicalData": (evt) -> + evt.preventDefault() + listRecordPhysicalData.set !listRecordPhysicalData.get() "click .toggleQuestionnaireAtVisit": (evt) -> evt.preventDefault() - self = @ + questionnaire = @questionnaire found = false if @visit.questionnaireIds _.some @visit.questionnaireIds, (qId)-> - found = qId is self.questionnaire._id + found = qId is questionnaire._id found doSchedule = !found - doAllOfGroup = false - #if @visit.groupId - # #TODO description - # doAllOfGroup = confirm('Do you want to apply this to all visits of the same group?') - Meteor.call "scheduleQuestionnaireAtVisit", @studyDesign._id, @visit._id, @questionnaire._id, doSchedule, doAllOfGroup, (error) -> + Meteor.call "scheduleQuestionnaireAtVisit", @design._id, @visit._id, @questionnaire._id, doSchedule, (error) -> throwError error if error? "click .toggleRecordPhysicalDataAtVisit": (evt) -> evt.preventDefault() - Meteor.call "scheduleRecordPhysicalDataAtVisit", @studyDesign._id, @visit._id, !@visit.recordPhysicalData, (error) -> + Meteor.call "scheduleRecordPhysicalDataAtVisit", @design._id, @visit._id, !@visit.recordPhysicalData, (error) -> throwError error if error? diff --git a/app/client/views/studies/edit_study_designs.html b/app/client/views/studies/edit_study_designs.html index 269f91d..ba9b777 100644 --- a/app/client/views/studies/edit_study_designs.html +++ b/app/client/views/studies/edit_study_designs.html @@ -1,101 +1,106 @@ diff --git a/app/lib/collections/studie_designs.coffee b/app/lib/collections/studie_designs.coffee index 8bba7f4..3d4236c 100644 --- a/app/lib/collections/studie_designs.coffee +++ b/app/lib/collections/studie_designs.coffee @@ -1,126 +1,145 @@ class @StudyDesign constructor: (doc) -> _.extend this, doc @StudyDesigns = new Meteor.Collection("study_designs", transform: (doc) -> new StudyDesign(doc) ) StudyDesigns.before.insert BeforeInsertTimestampHook StudyDesigns.before.update BeforeUpdateTimestampHook schema = 'title': type: String 'studyId': type: String 'creatorId': type: String 'visits': type: [Object] optional: true 'visits._id': type: String 'visits.title': type: String 'visits.index': type: Number #TODO: attach schema #StudyDesigns.attachSchema new SimpleSchema(schema) StudyDesigns.allow update: (userId, doc, fieldNames, modifier) -> #TODO check if allowed notAllowedFields = _.without fieldNames, 'title', 'updatedAt' return false if notAllowedFields.length > 0 true #TODO secure methods Meteor.methods "createStudyDesign": (studyId, title) -> count = StudyDesigns.find( studyId: studyId ).count() _id = StudyDesigns.insert title: "design #{count+1}" studyId: studyId creatorId: Meteor.userId() visits: [ _id: new Meteor.Collection.ObjectID()._str day: 0 index: 0 title: "baseline" ] _id "removeStudyDesign": (_id) -> #TODO: check if allowed StudyDesigns.remove _id: _id "addStudyDesignVisit": (studyDesignId, days) -> check studyDesignId, String days = parseInt(days) check days, Number design = StudyDesigns.findOne _id: studyDesignId throw new Meteor.Error(500, "StudyDesign #{studyDesignId} not found!") unless design? preVisit = design.visits[design.visits.length-1] day = preVisit.day+days if preVisit.day is day throw new Meteor.Error(500, "A visit on this day already exists") title = "visit #{design.visits.length}" if design.visits.length is 0 title = "baseline" visit = _id: new Meteor.Collection.ObjectID()._str day: day title: title StudyDesigns.update _id: studyDesignId , $push: visits: visit - "scheduleQuestionnaireAtVisit": (studyDesignId, visitId, questionnaireId, doSchedule, doAllOfGroup) -> + "scheduleQuestionnaireAtVisit": (studyDesignId, visitId, questionnaireId, doSchedule) -> + check studyDesignId, String check visitId, String check questionnaireId, String - if doSchedule - n = 0 - n = StudyDesigns.update - _id: studyDesignId - , - $push: - questionnaireIds: questionnaireId - throw new Meteor.Error(500, "mapQuestionnaireToVisit: no StudyDesign found") unless n > 0 - n = 0 + find = _id: studyDesignId 'visits._id': visitId if doSchedule n = StudyDesigns.update find, $push: 'visits.$.questionnaireIds': questionnaireId else n = StudyDesigns.update find, $pull: 'visits.$.questionnaireIds': questionnaireId - throw new Meteor.Error(500, "mapQuestionnaireToVisit: no StudyDesign with that visit found") unless n > 0 + throw new Meteor.Error(500, "scheduleQuestionnaireAtVisit: no StudyDesign with that visit found") unless n > 0 + + #update used questionnaire ids in design + design = StudyDesigns.findOne studyDesignId + throw new Meteor.Error(500, "scheduleQuestionnaireAtVisit: studyDesign not found") unless n > 0 + questionnaireIds = [] + design.visits.forEach (visit) -> + questionnaireIds = _.union questionnaireIds, visit.questionnaireIds + StudyDesigns.update + _id: studyDesignId + , + $set: + questionnaireIds: questionnaireIds + "scheduleRecordPhysicalDataAtVisit": (studyDesignId, visitId, doSchedule) -> check visitId, String check studyDesignId, String n = StudyDesigns.update _id: studyDesignId 'visits._id': visitId , $set: 'visits.$.recordPhysicalData': doSchedule - throw new Meteor.Error(500, "recordPhysicalDataAtVisit: no StudyDesign with that visit found") unless n > 0 + throw new Meteor.Error(500, "scheduleRecordPhysicalDataAtVisit: no StudyDesign with that visit found") unless n > 0 + + #update recordPhysicalData in design + design = StudyDesigns.findOne studyDesignId + throw new Meteor.Error(500, "scheduleRecordPhysicalDataAtVisit: studyDesign not found") unless n > 0 + recordPhysicalData = false + _.some design.visits, (visit) -> + recordPhysicalData = visit.recordPhysicalData + recordPhysicalData + StudyDesigns.update + _id: studyDesignId + , + $set: + recordPhysicalData: recordPhysicalData