Home > database > mysql_is_correct.m

mysql_is_correct

PURPOSE ^

Checks to see whether the response to a trial is correct

SYNOPSIS ^

function outstr = mysql_is_correct(varargin)

DESCRIPTION ^

 Checks to see whether the response to a trial is correct

 Input arguments are passed in as parameter/value pairs.  
 They must include:

 'trial_id'
 'session_id'

 By default, or if 'return_true', is set to 1, the string 'TRUE' will be
 returned if the response is correct.  If return_true=0 then 'FALSE' will be
 returned if the response is correct.  This behavior is implemented so that
 this function can service condition_matlab evaluation for contingent display
 of forms in Ensemble.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function outstr = mysql_is_correct(varargin)
0002 % Checks to see whether the response to a trial is correct
0003 %
0004 % Input arguments are passed in as parameter/value pairs.
0005 % They must include:
0006 %
0007 % 'trial_id'
0008 % 'session_id'
0009 %
0010 % By default, or if 'return_true', is set to 1, the string 'TRUE' will be
0011 % returned if the response is correct.  If return_true=0 then 'FALSE' will be
0012 % returned if the response is correct.  This behavior is implemented so that
0013 % this function can service condition_matlab evaluation for contingent display
0014 % of forms in Ensemble.
0015 
0016 % 08/31/2009 Petr Janata
0017 % 06/15/10 PJ - adapted mysql_make_conn handling
0018 
0019 global mysql_conn_id
0020 
0021 outstr = '';
0022 
0023 % Parse input arguments
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 % Check to see if we're debugging
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 % Check for valid connection to database
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 % We need to get the response table name, based on the session ID
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 % Get the trial information from the trial table
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 % Get the response to this session and trial from the response table. Make sure
0092 % we get the most recent trial in the event that multiple iterations of this
0093 % trial were presented in this session
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 % Flip the direction of the response string if necessary
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

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