Home > database > check_completion.m

check_completion

PURPOSE ^

Returns a list of subject and session IDs that responded to a given form or question.

SYNOPSIS ^

function [subids, sessids, form_id, question_id, timestamps] = check_completion(tbl_name,varargin)

DESCRIPTION ^

 Returns a list of subject and session IDs that responded to a given form or question.

 [subids, sessids, form_id, question_id, timestamp] = check_completion(tbl_name,varargin);

 If no output arguments are specified, the results are
 printed to standard output.

 The form ID and/or question ID are specified by tag/value pairs, e.g.
 
 'form_id', 123
 'question_id', 456

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [subids, sessids, form_id, question_id, timestamps] = check_completion(tbl_name,varargin)
0002 % Returns a list of subject and session IDs that responded to a given form or question.
0003 %
0004 % [subids, sessids, form_id, question_id, timestamp] = check_completion(tbl_name,varargin);
0005 %
0006 % If no output arguments are specified, the results are
0007 % printed to standard output.
0008 %
0009 % The form ID and/or question ID are specified by tag/value pairs, e.g.
0010 %
0011 % 'form_id', 123
0012 % 'question_id', 456
0013 
0014 % 07/02/06 Petr Janata
0015 % 06/15/10 PJ - eliminated mysql_make_conn
0016 
0017 % Parse input arguments
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 % if isstruct(curr_arg)
0059 end % for iarg=
0060 
0061 % Connect to host with a temporary connection if necessary
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 % If we are searching by ticket codes, get a listing of the appropriate sessions
0068 % based on the list of ticket codes
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 % If we are searching by subject, return the form and question ID last completed
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 % Close the mysql connection if this was a temporary opening of the database
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

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003