0001 function [subids, sessids, form_id, question_id, timestamps] = check_completion(tbl_name,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 narg = length(varargin);
0019
0020 cond_str = '';
0021 FIND_BY_SUBJECT = 0;
0022 FIND_BY_TICKET = 0;
0023
0024 conn_id = [];
0025 for iarg = 1:2:narg
0026 curr_arg = varargin{iarg};
0027 if isempty(cond_str)
0028 prefix_str = '';
0029 else
0030 prefix_str = 'AND';
0031 end
0032
0033 if isstruct(curr_arg)
0034 ap = curr_arg;
0035 if isfield(ap,'ticket_code') && ~isempty(ap.ticket_code)
0036 ticket_code = ap.ticket_code;
0037 if ~iscell(ticket_code)
0038 ticket_code = {ticket_code};
0039 end
0040 ntickets = length(ticket_code);
0041 FIND_BY_TICKET = 1;
0042 end
0043 else
0044 switch curr_arg
0045 case 'form_id'
0046 form_id = varargin{iarg+1};
0047 cond_str = sprintf('%s %s form_id=%d ', cond_str, prefix_str, form_id);
0048 case 'question_id'
0049 question_id = varargin{iarg+1}
0050 cond_str = sprintf('%s %s question_id=%d ', cond_str, prefix_str, question_id);
0051 case 'subject_id'
0052 subids = varargin{iarg+1};
0053 cond_str = sprintf('%s %s subject_id="%s" ', cond_str, prefix_str, subids);
0054 FIND_BY_SUBJECT = 1;
0055 case 'conn_id'
0056 conn_id = varargin{iarg+1};
0057 end
0058 end
0059 end
0060
0061
0062 if isempty(conn_id) || mysql(conn_id,'status')
0063 error('%s: No connection ID specifed or connection not open')
0064 end
0065
0066
0067
0068
0069
0070 if FIND_BY_TICKET
0071 ticket_str = sprintf('"%s"', cell2str(ticket_code,','));
0072 mysql_str = sprintf(['SELECT session.session_id FROM session, ticket WHERE' ...
0073 ' session.ticket_id = ticket.ticket_id AND ticket.ticket_code IN (%s);'], ticket_str);
0074 [sessids] = mysql(conn_id,mysql_str);
0075 end
0076
0077
0078
0079
0080 if FIND_BY_SUBJECT
0081 var_list = 'session_id, form_id, question_id, date_time';
0082 else
0083 var_list = 'session_id, subject_id, date_time';
0084 end
0085 mysql_str = sprintf('SELECT %s FROM %s WHERE %s;', var_list, tbl_name, cond_str);
0086
0087 if FIND_BY_SUBJECT
0088 [sessids, formids, qids, timestamps] = mysql(conn_id, mysql_str);
0089 form_id = formids(end);
0090 question_id = qids(end);
0091 timestamps = timestamps(end);
0092 sessids = sessids(end);
0093 else
0094 [sessids, subids, timestamps] = mysql(conn_id, mysql_str);
0095 end
0096
0097
0098
0099
0100
0101 if ~conn_id
0102 mysql(conn_id,'close');
0103 end
0104
0105 if ~nargout
0106 fprintf('SubjectID\tSession\tFormID\tQuestionID\tTimestamp\n');
0107 for is = 1:length(subids)
0108 sub = subids{is};
0109 sess = sessids(is);
0110 tstmp = timestamps(is);
0111 fprintf('%s\t%d\t%d\t%d\t%s\n', sub, sess, form_id, question_id, datestr(tstmp));
0112 end
0113 end