Ignore:
Timestamp:
12/16/12 20:07:35 (12 years ago)
Author:
hendrikvanantwerpen
Message:

We can store answers for surveys now!

Introduces SurveyRun?, which can be edited. Workflow not quite clear yet. A
running survey can be accessed to leave a response. When the response
has an ID, it is loaded (used for closed surveys and continuations). A
researcher cannot create responses yet. He should also be able to add
comments to responses (that he creates).

Introduced caching of store requests.

Factored out path matching and formatting.

Put object creation in separate classes, to localize model/storage
dependency. Not consistent at the moment.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/client/qed/main-response.js

    r419 r420  
    11require([
     2    'dojo/date',
     3    'dojo/date/locale',
    24    'dojo/hash',
    35    'dojo/parser',
     6    'qed/store',
    47    'qed/app/Content',
    58    'qed/app/Page',
    6     'qed/pages/viewSurvey',
     9    'qed/app/Path',
     10    'qed/lib/async',
     11    'qed/model/classes/Response',
     12    'qed/model/classes/SurveyRun',
     13    'qed/pages/response',
    714    'dojo/domReady!',
    815    'qed/stddeps'
    9 ],function(hash,parser,Content,Page,viewSurvey) {
     16],function(date,locale,hash,parser,store,Content,Page,Path,async,Response,SurveyRun,response) {
    1017    parser.parse();
    1118    Content.startup();
    1219
    13     var match = /^!\/(\w+)$/g.exec(hash());
    14     if ( !match ) {
     20    function error(msg) {
    1521        Content.set(new Page({
    16             templateString: "<div>Something is wrong with the URL, don't know what survey to show you. Sorry.</div>"
     22            templateString: "<div>"+msg+"</div>"
    1723        }));
     24    }
     25
     26    var path = new Path('/:surveyRunId');
     27    var params = path.match(hash());
     28    params.options = params.options || {};
     29
     30    if ( !params || !params.surveyRunId ) {
     31        error("Something is wrong with the URL, don't know what survey to show you. Sorry.");
    1832        return;
    1933    }
    20     var surveyId = match[1];
    2134
    22     // read options from hash/url
    23     //
    24     // authenticate
     35    var surveyRunId = params.surveyRunId;
    2536
    26     Content.set(new viewSurvey({
    27         surveyId: surveyId
    28     }));
     37    function checkDates(surveyRun) {
     38        var now = new Date();
     39        var startDate = SurveyRun.StartDate.get(surveyRun);
     40        var endDate = SurveyRun.EndDate.get(surveyRun);
     41        if ( startDate && date.compare(startDate,now) > 0 ) {
     42            error("This survey will start on "+locale.format(startDate,'date'));
     43            throw false;
     44        }
     45        if ( endDate && date.compare(now,endDate) > 0 ) {
     46            error("This survey ended on "+locale.format(endDate,'date'));
     47            throw false;
     48        }
     49    }
     50
     51    store.get(surveyRunId)
     52    .then(function(surveyRun){
     53        checkDates(surveyRun);
     54        if ( params.options.id ) {
     55            return params.options.id;
     56        } else {
     57            if ( surveyRun.mode === "open") {
     58                var response = Response.create();
     59                response.surveyRunId = surveyRunId;
     60                return store.put(response).then(function(res){
     61                    return store.getIdentity(res);
     62                });
     63            } else {
     64                error("Cannot respond to closed survey without response id. Sorry.");
     65                throw false;
     66            }
     67        }
     68        return surveyRun;
     69    },function(){
     70        error("No running survey found for the given id. Sorry.");
     71        throw false;
     72    })
     73    .then(function(responseId){
     74        hash(Path.format("/"+surveyRunId,{ id: responseId }));
     75        Content.set(new response({
     76            surveyRunId: surveyRunId,
     77            options: {
     78                id: responseId
     79            }
     80        }));
     81    });
    2982});
Note: See TracChangeset for help on using the changeset viewer.