0001 function outstr = mysql_is_correct(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 global mysql_conn_id
0020
0021 outstr = '';
0022
0023
0024 trial_id = [];
0025 session_id = [];
0026 debug_fname = '';
0027 return_true = true;
0028
0029 narg = length(varargin);
0030 for iarg = 1:2:narg
0031 switch varargin{iarg}
0032 case {'trial','trial_id'}
0033 trial_id = varargin{iarg+1};
0034 case {'session','session_id'}
0035 session_id = varargin{iarg+1};
0036 case {'debug_file'}
0037 debug_fname = varargin{iarg+1};
0038 case {'return_true'}
0039 return_true = varargin{iarg+1};
0040 otherwise
0041 fprintf('%s: Unknown input argument: %s\n', mfilename, varargin{iarg});
0042 end
0043 end
0044
0045
0046 VERBOSE = false;
0047 if ~isempty(debug_fname)
0048 VERBOSE = true;
0049 fid = fopen(debug_fname,'wt');
0050 if fid < 3
0051 VERBOSE = false;
0052 end
0053 end
0054
0055 if isempty(trial_id)
0056 error_str = sprintf('%s: Trial ID not specified', mfilename);
0057 if VERBOSE, fprintf(fid,error_str); end
0058 error(error_str);
0059 end
0060 if isempty(session_id)
0061 error_str = sprintf('%s: Session ID not specified', mfilename);
0062 if VERBOSE, fprintf(fid, error_str); end
0063 error(error_str);
0064 end
0065
0066
0067 if ~exist('mysql_conn_id','var') || isempty(mysql_conn_id) || mysql(mysql_conn_id,'status')
0068 error('%s: Do not have a valid connection ID', mfilename);
0069 end
0070
0071
0072 if VERBOSE, fprintf(fid,'Figuring out response table ... '); end
0073 mysql_str = sprintf(['SELECT response_table FROM experiment WHERE ' ...
0074 'experiment_id = (SELECT experiment_id FROM session WHERE session_id = %d);'], session_id);
0075 if VERBOSE, fprintf(fid,mysql_str); end
0076 response_table = mysql(mysql_conn_id, mysql_str);
0077 response_table = response_table{1};
0078 if VERBOSE, fprintf(fid,'using %s\n', response_table); end
0079
0080
0081 mysql_str = sprintf(['SELECT correct_response_enum, correct_response_text FROM trial ', ...
0082 'WHERE trial_id = %d;'], trial_id);
0083 [correct_response_enum, correct_response_text] = mysql(mysql_conn_id, mysql_str);
0084 correct_response_text = correct_response_text{1};
0085 if isempty(correct_response_text) && isnumeric(correct_response_enum);
0086 use_enum = true;
0087 else
0088 use_enum = false;
0089 end
0090
0091
0092
0093
0094 mysql_str = sprintf(['SELECT response_enum, response_text, response_order FROM %s ' ...
0095 'WHERE session_id = %d AND trial_id = %d;'], response_table, session_id, trial_id);
0096 [response_enum, response_text, response_order] = mysql(mysql_conn_id, mysql_str);
0097
0098 [dummy, resp_idx] = max(response_order);
0099 response_text = response_text{resp_idx};
0100
0101 if use_enum
0102 if response_enum(resp_idx) == correct_response_enum
0103 outstr = 'TRUE';
0104 else
0105 outstr = 'FALSE';
0106 end
0107 else
0108 if strcmp(response_text, correct_response_text)
0109 outstr = 'TRUE';
0110 else
0111 outstr = 'FALSE';
0112 end
0113 end
0114
0115
0116 if ~return_true
0117 if strcmp(outstr,'TRUE')
0118 outstr = 'FALSE';
0119 else
0120 outstr = 'TRUE';
0121 end
0122 end
0123
0124 if VERBOSE, fclose(fid); end
0125
0126 return
0127
0128