Archive

Archive for January, 2013

question_template.xml

January 15, 2013 Leave a comment
  <question type="numerical">
    <name>
      <text><![CDATA[==question_name== ]]></text>
    </name>
    <questiontext format="html">
      <text><![CDATA[==question_text==]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1.0000000</defaultgrade>
    <penalty>0.0000000</penalty>
    <hidden>0</hidden>
    <answer fraction="100" format="moodle_auto_format">
      <text>==question_answer==</text>
      <feedback format="html">
        <text></text>
      </feedback>
      <tolerance>==question_tolerance==</tolerance>
    </answer>
    <unitgradingtype>0</unitgradingtype>
    <unitpenalty>0.1000000</unitpenalty>
    <showunits>3</showunits>
    <unitsleft>0</unitsleft>
  </question>
Categories: Uncategorized

question_category_template.xml

January 15, 2013 Leave a comment
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 0  -->
  <question type="category">
    <category>
        <text><![CDATA[==category_name==]]></text>

    </category>
  </question>

	==quiz_questions==

</quiz>
Categories: Uncategorized

moodle_numerical_quiz_examples.m

January 15, 2013 Leave a comment
% This script shows two examples of how to create numerical questions for the moodle
% learning environment.
%
% The script makes use of a function called create_moodle_numerical_quiz_questions (download from https://dadorran.wordpress.com) to parse a structured variable.
% These examples essentially show you how to to create the data structures
% required by the function.
%
% The first example is the most important one to understand. The other
% example really just demonstrate how to create more
% complicated problems.
%
%Created by David Dorran (david.dorran@gmail.com) Jan 2013

% Eg 1 ------------------------------------------------------------------------
% Basic Addition Example with two questions
data1.category_name = 'Basic Addition';

data1.question(1).question_name = 'Basic Addition';
data1.question(1).question_text = 'What is 2 + 3';
data1.question(1).question_answer = 5;
data1.question(1).question_tolerance = 0;

data1.question(2).question_name = 'Basic Addition'; %call this Basic Addition 2 if you like but it might be useful to use the same name to reduce possibility of answers being posted on the web
data1.question(2).question_text = 'What is 9.2 + 3.333';
data1.question(2).question_answer = 12.533;
data1.question(2).question_tolerance = 0.05;

create_moodle_numerical_quiz_questions(data1,'addition_example.xml')

% Eg 2------------------------------------------------------------------------
% Determine the maximum sample in a segment of a signal

% data is on the web - next four lines download the data and import into
% matlab (more recent versions of matlab ( > 7.1 ) allow you to do this on one line)
url = 'http://eleceng.dit.ie/dorran/quiz_data/examples/'; % change this to a location that you have access to
data_url = [url 'signal.zip'];
help_url = [url 'help.html'];

urlwrite(data_url,'url_data_temp.mat');
load('url_data_temp.mat');
delete('url_data_temp.mat');

% create the data structure
data2.category_name = 'Signal Maximum';

data2.question(1).question_name = 'Signal Maximum Question';
data2.question(1).question_text = ['Download and import the signal in the zip file located at   ' data_url '. <p>Determine the value of maximum sample in the signal from the ' num2str(seg_start) 'th to the ' num2str(seg_end) 'th sample, inclusive. <br> Note that that matlab''s indexing system is ''incorrect'' in that the 0th sample is indexed as the the 1st element in the data sequence i.e. signal(1) in matlab is the 0th sample of the discrete signal. <p>See ' help_url ' for a demonstration.'];
data2.question(1).question_answer = sum(signal(999:1999).^2);
data2.question(1).question_tolerance = (sum(signal(999:1999).^2))*.02; % 2 percent tolerance

% create another 100 questions like this automatically
min_seg_len = 100;
max_seg_len = 1000;
max_start = length(signal) - max_seg_len -1;
for k = 1: 100
    seg_len = round(min_seg_len + (max_seg_len - min_seg_len)*rand);
    seg_start = round(max_start*rand);
    seg_end = seg_start + seg_len; 
    data2.question(k+1).question_name = 'Signal Maximum Question';
    data2.question(k+1).question_text = ['Download and import the signal in the zip file located at   ' data_url '. <p>Determine the value of maximum sample in the signal from the ' num2str(seg_start) 'th to the ' num2str(seg_end) 'th sample, inclusive. <br> Note that that matlab''s indexing system is ''incorrect'' in that the 0th sample is indexed as the the 1st element in the data sequence i.e. signal(1) in matlab is the 0th sample of the discrete signal. <p>See ' help_url ' for a demonstration.'];
    data2.question(k+1).question_answer = sum(signal(seg_start-1:seg_end-1).^2);
    data2.question(k+1).question_tolerance = (sum(signal(seg_start-1:seg_end-1).^2))*.02; %2 percent tolerance
end


create_moodle_numerical_quiz_questions(data2,'signal_maximum_example.xml')
Categories: Uncategorized

create_moodle_numerical_quiz_questions.m

January 15, 2013 Leave a comment
% 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