Home > Uncategorized > create_moodle_numerical_quiz_questions.m

create_moodle_numerical_quiz_questions.m

% This function processes the structured variable passed and uses
% the data contained within this variable to create a moodle XML quiz import file (Moodle XML format) which
% can then be imported into the moodle learning environment.
%
% The function relies on the presence of a couple of template files (question_template.xml and question_category_template.xml) which should be stored in a folder called 'moodle xml import templates'.
% These files can be downloaded from https://dadorran.wordpress.com/
%
% The quizzes are only of numerical type i.e. the answers to the quizzes
% must be numerical answers. Each set of questions must have an associated
% category name.
%
% The question_bank structured variable has the following structure for N questions:
%   question_bank.category_name                         (string)
%   question_bank.question(1).question_name             (string)
%   question_bank.question(1).question_text             (string)
%   question_bank.question(1).question_answer           (numeric)
%   question_bank.question(1).question_tolerance        (numeric)
%   question_bank.question(2).question_name             (string)
%   question_bank.question(2).question_text             (string)
%   question_bank.question(2).question_answer           (numeric)
%   question_bank.question(2).question_tolerance        (numeric)
%     --            --            --
%     --            --            --
%   question_bank.question(N).question_name             (string)
%   question_bank.question(N).question_text             (string)
%   question_bank.question(N).question_answer           (numeric)
%   question_bank.question(N).question_tolerance        (numeric)
%
% Created by David Dorran (david.dorran@gmail.com)  Jan 2013

function create_moodle_numerical_quiz_questions(question_bank, xml_filename)
% ----------------------------------------------------------
% first check that the template files are available and load them in. The script populates
% the templates and creates an xml file which can then be uploaded to moodle
[pathstr, name, ext] = fileparts(mfilename('fullpath')) ; % get the details of this m-file
template_dir = [pathstr '/moodle xml import templates/'];
question_template_file = [template_dir 'question_template.xml'];
question_category_template_file = [template_dir 'question_category_template.xml'];

if(~exist(question_template_file) || ~exist(question_category_template_file))
    error(['Missing moodle xml templates required to create the import file. Download question_category_template.xml and question_template.xml from https://dadorran.wordpress.com/ and store them in ' template_dir ])
end

question_template = fileread(question_template_file);
question_category_template = fileread(question_category_template_file);

% ----------------------------------------------------------
% Check the data structure
if(~isfield(question_bank,'category_name'))
    data_structure_error('Missing category_name field');
end
if(~isfield(question_bank, 'question'))
    data_structure_error('Missing question field');
end

if(length(question_bank.question) < 1)
    data_structure_error('No Questions Added');
end

question_fields(1).name = 'name';
question_fields(2).name = 'text';
question_fields(3).name = 'answer';
question_fields(4).name = 'tolerance';

for k = 1 : length(question_fields)
    field_name = ['question_' question_fields(k).name];

    if(~isfield(question_bank.question(1),field_name))
        data_structure_error(['Question ' field_name ' field missing']);
    end
end

% ----------------------------------------------------------
% Check the data is present (empty fields not allowed) and formatted correctly. If so then build the
% XML file
all_questions = ''; % this variable will contain all the xml details of each question
for k = 1 : length(question_bank.question)
    question_details = question_template;
    for n = 1 : length(question_fields)
        field_name = question_fields(n).name;
        if(eval(['length(question_bank.question(k).question_' field_name ')']))
            replacement = eval(['question_bank.question(k).question_' field_name]);
            if(strcmp(field_name, 'answer') || strcmp(field_name, 'tolerance'))
                if(isnumeric(replacement))
                    replacement = num2str(replacement);
                else
                    data_structure_error(['The format of Question ' num2str(k) ' is incorrect - the question_' field_name ' field must be numeric' ]);
                end
            end
            question_details = make_template_replacements(question_details, ['question_' field_name],replacement );
        else
            data_structure_error(['Question ' num2str(k) ' is missing its question_' field_name ' field' ]);
        end
    end
    all_questions = [all_questions question_details];
end

disp(['Found ' num2str(k) ' questions under category of ' question_bank.category_name '.']);

% ----------------------------------------------------------
% Question data has now been created. Time to populate the final template
xml_content = make_template_replacements(question_category_template, 'category_name', question_bank.category_name);
xml_content = make_template_replacements(xml_content, 'quiz_questions', all_questions);

[pathstr, name, ext] = fileparts(xml_filename);
if (~strcmp(ext,'.xml'))
    xml_filename = [xml_filename '.xml'];
end

disp(['Writing to XML file ' xml_filename ' ...'])

fp = fopen(xml_filename,'w');
fprintf(fp,'%s', xml_content);
fclose(fp);

end

% ----------------------------------------------------------
function updated_template =  make_template_replacements(template, field, replacement)
updated_template = regexprep(template ,['==' field '=='], replacement);
end

% ----------------------------------------------------------
function data_structure_error(msg)
error(['       Data is not structured correctly - ' msg '.  See help on how to structure data correctly.']);

end
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: