0001 function rp = report_completion_stats(einfo, rp)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 try rp.conn_id(1);
0026 conn_id = rp.conn_id;
0027 catch
0028 conn_id = 0;
0029 end
0030 mysql_check_conn(conn_id,'open');
0031
0032 nexp = length(einfo);
0033 if nexp > 1
0034 error(sprintf('Too many (%d) experiments\n', nexp))
0035 end
0036
0037
0038 if isfield(rp,'ticket_code') && ~isempty(rp.ticket_code)
0039 ticket_code = rp.ticket_code;
0040 if ~iscell(ticket_code)
0041 ticket_code = {ticket_code};
0042 end
0043 ntickets = length(ticket_code);
0044
0045 ticket_str = sprintf('"%s"', cell2str(ticket_code,'","'));
0046 mysql_str = sprintf(['SELECT session.session_id FROM session, ticket WHERE' ...
0047 ' session.ticket_id = ticket.ticket_id AND ticket.ticket_code IN (%s);'], ticket_str);
0048 [ticket_sessids] = mysql(conn_id,mysql_str);
0049 else
0050 ticket_sessids = [];
0051 end
0052
0053 if ~isfield(rp,'report_str')
0054 rp.report_str = {};
0055 end
0056
0057 fid = 1;
0058 if isfield(rp,'report_fname')
0059 fid = fopen(rp.report_fname,'wt');
0060 if fid == -1
0061 fprintf('Problem opening reporting file: %s\n', rp.report_fname);
0062 fid = 1;
0063 end
0064 end
0065
0066
0067 if isfield(rp,'print_report') && rp.print_report
0068 PRINT_REPORT = 1;
0069
0070 fprintf(fid,['Subject Name\tSubject ID\tSession ID\tStart Time\tElapsed time' ...
0071 ' (h)\tLast Form ID\tLast Form Name\n']);
0072 else
0073 PRINT_REPORT = 0;
0074 end
0075
0076 cexp = einfo(1);
0077
0078
0079 exp_formid_list = cexp.forms.ids;
0080 exp_formname_list = cexp.forms.names;
0081
0082
0083 terminal_form_count = zeros(size(exp_formid_list));
0084
0085 nsubs = length(cexp.subs.names);
0086 for isub = 1:nsubs
0087
0088 nsess = cexp.subs.nsess(isub);
0089
0090 for isess = 1:nsess
0091 sess_id = cexp.subs.sess{isub}(isess);
0092
0093 if ~isempty(ticket_sessids) && ~any(ticket_sessids == sess_id)
0094 continue
0095 end
0096
0097
0098 mysql_str = sprintf(['SELECT date_time,end_datetime FROM session ' ...
0099 'WHERE session_id=%d;'], sess_id);
0100 [start_datenum, end_datenum] = mysql(conn_id, mysql_str);
0101
0102
0103 mysql_str = sprintf(['SELECT DISTINCT form_id FROM %s '...
0104 'WHERE session_id=%d;'], cexp.resp_table, sess_id);
0105 completed_ids = mysql(conn_id,mysql_str);
0106
0107
0108 last_form_idx = max(find(ismember(exp_formid_list,completed_ids)));
0109
0110 rp.subs.last_form_idx{isub}(isess) = last_form_idx;
0111 rp.subs.last_form_id{isub}(isess) = exp_formid_list(last_form_idx);
0112 rp.subs.last_form_name{isub}{isess} = exp_formname_list{last_form_idx};
0113 rp.subs.starttime{isub}(isess) = start_datenum;
0114 if ~isempty(end_datenum)
0115 elapsed_time = etime(datevec(end_datenum),datevec(start_datenum));
0116 rp.subs.elapsedtime{isub}(isess) = elapsed_time;
0117 else
0118 elapsed_time = -1;
0119 end
0120
0121
0122 terminal_form_count(last_form_idx) = terminal_form_count(last_form_idx)+1;
0123
0124 curr_str = sprintf('%20s\t%s\t%d\t%s\t%1.2f\t%d\t%s\n', ...
0125 cexp.subs.names{isub}, ...
0126 cexp.subs.ids{isub}, ...
0127 sess_id, ...
0128 datestr(start_datenum), ...
0129 elapsed_time/3600, ...
0130 exp_formid_list(last_form_idx), ...
0131 exp_formname_list{last_form_idx});
0132 rp.report_str{end+1} = curr_str;
0133
0134 if PRINT_REPORT
0135 fprintf(fid,'%s', curr_str);
0136 end
0137 end
0138 end
0139
0140 rp.terminal_form_count = terminal_form_count;
0141
0142 if ~conn_id
0143 mysql(conn_id,'close')
0144 end
0145
0146