refs #7010 Inclusión formato Aiken

1.9.x
Cesar Perales 11 years ago
parent 4554068b07
commit f3ca712c67
  1. 101
      main/exercice/aiken.php
  2. 142
      main/exercice/export/aiken/aiken_classes.php
  3. 157
      main/exercice/export/aiken/aiken_import.inc.php

@ -0,0 +1,101 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Code for Aiken import integration.
* @package chamilo.exercise
* @author Ronny Velasquez
* @author César Perales <cesar.perales@gmail.com> Updated function names and import files for Aiken format support
* @version $Id: Aiken.php 2010-03-12 12:14:25Z $
*/
/**
* Code
*/
// name of the language file that needs to be included
$language_file = 'exercice';
// including the global Chamilo file
require_once '../inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH).'document.lib.php';
// including additional libraries
require_once 'exercise.class.php';
require_once 'question.class.php';
require_once 'answer.class.php';
require_once 'unique_answer.class.php';
// section (for the tabs)
$this_section = SECTION_COURSES;
// access restriction: only teachers are allowed here
if (!api_is_allowed_to_edit(null, true)) {
api_not_allowed();
}
// the breadcrumbs
$interbreadcrumb[]= array ("url"=>"exercice.php", "name"=> get_lang('Exercices'));
$is_allowedToEdit = api_is_allowed_to_edit(null, true);
/**
* This function displays the form for import of the zip file with qti2
*/
function aiken_display_form() {
$name_tools = get_lang('ImportAikenQuiz');
$form = '<div class="actions">';
$form .= '<a href="exercice.php?show=test">' . Display :: return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
$form .= '</div>';
$form_validator = new FormValidator('aiken_upload', 'post',api_get_self()."?".api_get_cidreq(), null, array('enctype' => 'multipart/form-data') );
$form_validator->addElement('header', $name_tools);
$form_validator->addElement('file', 'userFile', get_lang('DownloadFile'));
$form_validator->addElement('style_submit_button', 'submit', get_lang('Send'), 'class="upload"');
$form .= $form_validator->return_form();
echo $form;
}
/**
* This function will import the zip file with the respective qti2
* @param array $uploaded_file ($_FILES)
*/
function aiken_import_file($array_file) {
$unzip = 0;
$lib_path = api_get_path(LIBRARY_PATH);
require_once $lib_path.'fileUpload.lib.php';
require_once $lib_path.'fileManage.lib.php';
$process = process_uploaded_file($array_file);
if (preg_match('/\.zip$/i', $array_file['name'])) {
// if it's a zip, allow zip upload
$unzip = 1;
}
if ($process && $unzip == 1) {
$main_path = api_get_path(SYS_CODE_PATH);
require_once $main_path.'exercice/export/aiken/aiken_import.inc.php';
require_once $main_path.'exercice/export/aiken/aiken_classes.php';
$imported = import_exercise($array_file['name']);
if ($imported) {
header('Location: exercice.php?'.api_get_cidreq());
} else {
Display::display_error_message(get_lang('UplNoFileUploaded'));
return false;
}
}
}
// display header
Display::display_header(get_lang('ImportAikenQuiz'), 'Exercises');
// import file
if ((api_is_allowed_to_edit(null, true))) {
if (isset($_POST['submit'])) {
aiken_import_file($_FILES['userFile']);
}
}
// display Aiken form
aiken_display_form();
// display the footer
Display::display_footer();

@ -0,0 +1,142 @@
<?php // $Id: $
/* For licensing terms, see /license.txt */
/**
* @author Claro Team <cvs@claroline.net>
* @author Yannick Warnier <yannick.warnier@beeznest.com> - updated ImsAnswerHotspot to match QTI norms
* @author César Perales <cesar.perales@gmail.com> Updated function names and import files for Aiken format support
* @package chamilo.exercise
*/
/**
* Code
*/
if ( count( get_included_files() ) == 1 ) die( '---' );
if (!function_exists('mime_content_type')) {
require_once api_get_path(LIBRARY_PATH).'document.lib.php';
function mime_content_type($filename) {
return DocumentManager::file_get_mime_type((string)$filename);
}
}
require_once(api_get_path(SYS_CODE_PATH).'/exercice/answer.class.php');
require_once(api_get_path(SYS_CODE_PATH).'/exercice/exercise.class.php');
require_once(api_get_path(SYS_CODE_PATH).'/exercice/question.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/hotspot.class.php');
require_once(api_get_path(SYS_CODE_PATH).'/exercice/unique_answer.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/multiple_answer.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/multiple_answer_combination.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/matching.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/freeanswer.class.php');
//require_once(api_get_path(SYS_CODE_PATH).'/exercice/fill_blanks.class.php');
//include_once $path . '/../../lib/answer_multiplechoice.class.php';
//include_once $path . '/../../lib/answer_truefalse.class.php';
//include_once $path . '/../../lib/answer_fib.class.php';
//include_once $path . '/../../lib/answer_matching.class.php';
/**
*
* @package chamilo.exercise
*/
class Aiken2Question extends Question
{
/**
* Include the correct answer class and create answer
*/
function setAnswer()
{
switch($this->type)
{
case MCUA :
$answer = new AikenAnswerMultipleChoice($this->id);
return $answer;
default :
$answer = null;
break;
}
return $answer;
}
function createAnswersForm($form)
{
return true;
}
function processAnswersCreation($form)
{
return true;
}
}
/**
* Class
* @package chamilo.exercise
*/
class AikenAnswerMultipleChoice extends Answer
{
/**
* Return the XML flow for the possible answers.
*
*/
function imsExportResponses($questionIdent, $questionStatment)
{
$this->answerList = $this->getAnswersList(true);
$out = ' <choiceInteraction responseIdentifier="' . $questionIdent . '" >' . "\n";
$out .= ' <prompt> ' . $questionStatment . ' </prompt>'. "\n";
if (is_array($this->answerList)) {
foreach ($this->answerList as $current_answer) {
$out .= ' <simpleChoice identifier="answer_' . $current_answer['id'] . '" fixed="false">' . $current_answer['answer'];
if (isset($current_answer['comment']) && $current_answer['comment'] != '')
{
$out .= '<feedbackInline identifier="answer_' . $current_answer['id'] . '">' . $current_answer['comment'] . '</feedbackInline>';
}
$out .= '</simpleChoice>'. "\n";
}
}
$out .= ' </choiceInteraction>'. "\n";
return $out;
}
/**
* Return the XML flow of answer ResponsesDeclaration
*
*/
function imsExportResponsesDeclaration($questionIdent)
{
$this->answerList = $this->getAnswersList(true);
$type = $this->getQuestionType();
if ($type == MCMA) $cardinality = 'multiple'; else $cardinality = 'single';
$out = ' <responseDeclaration identifier="' . $questionIdent . '" cardinality="' . $cardinality . '" baseType="identifier">' . "\n";
//Match the correct answers
$out .= ' <correctResponse>'. "\n";
if (is_array($this->answerList)) {
foreach($this->answerList as $current_answer) {
if ($current_answer['correct'])
{
$out .= ' <value>answer_'. $current_answer['id'] .'</value>'. "\n";
}
}
}
$out .= ' </correctResponse>'. "\n";
//Add the grading
$out .= ' <mapping>'. "\n";
if (is_array($this->answerList)) {
foreach($this->answerList as $current_answer)
{
if (isset($current_answer['grade']))
{
$out .= ' <mapEntry mapKey="answer_'. $current_answer['id'] .'" mappedValue="'.$current_answer['grade'].'" />'. "\n";
}
}
}
$out .= ' </mapping>'. "\n";
$out .= ' </responseDeclaration>'. "\n";
return $out;
}
}

@ -1,5 +1,4 @@
<?php
error_log(__LINE__);
/**
* @copyright (c) 2001-2006 Universite catholique de Louvain (UCL)
*
@ -9,6 +8,7 @@ error_log(__LINE__);
*
* @author claro team <cvs@claroline.net>
* @author Guillaume Lederer <guillaume@claroline.net>
* @author César Perales <cesar.perales@gmail.com> Parse function for Aiken format
*/
/**
* Security check
@ -61,7 +61,6 @@ function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath) {
*/
function import_exercise($file) {
error_log(__LINE__);
global $exercise_info;
global $element_pile;
global $non_HTML_tag_to_avoid;
@ -99,7 +98,6 @@ function import_exercise($file) {
Display :: display_error_message(get_lang('You must upload a zip file'));
return false;
}
error_log(__LINE__);
// find the different manifests for each question and parse them.
$exerciseHandle = opendir($baseWorkDir);
@ -124,7 +122,7 @@ error_log(__LINE__);
} // else ignore file
}
if (!$file_found) {
Display :: display_error_message(get_lang('No XML file found in the zip'));
Display :: display_error_message(get_lang('No TXT file found in the zip'));
return false;
}
if ($result == false ) {
@ -143,7 +141,7 @@ error_log(__LINE__);
//For each question found...
foreach ($exercise_info['question'] as $key => $question_array) {
//2.create question
$question = new AikenQuestion();
$question = new Aiken2Question();
$question->type = $question_array['type'];
$question->setAnswer();
$question->updateTitle($question_array['title']); // question ...
@ -155,133 +153,68 @@ error_log(__LINE__);
$answer = new Answer($last_question_id);
$answer->new_nbrAnswers = count($question_array['answer']);
foreach ($question_array['answer'] as $key => $answers) {
$split = explode('_', $key);
$i = $split[1];
$answer->new_answer[$i] = $answers['value']; // answer ...
$answer->new_comment[$i] = $answers['feedback']; // comment ...
$answer->new_position[$i] = $i; // position ...
$key++;
$answer->new_answer[$key] = $answers['value']; // answer ...
$answer->new_comment[$key] = $answers['feedback']; // comment ...
$answer->new_position[$key] = $key; // position ...
// correct answers ...
if (in_array($key, $question_array['correct_answers'])) {
$answer->new_correct[$i] = 1;
$answer->new_correct[$key] = 1;
} else {
$answer->new_correct[$i] = 0;
$answer->new_correct[$key] = 0;
}
$answer->new_weighting[$i] = $question_array['weighting'][$key];
error_log($question_array['weighting']);
$answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
}
$answer->save();
}
// delete the temp dir where the exercise was unzipped
my_delete($baseWorkDir . $uploadPath);
$operation = true;
}
}
return $operation;
}
function parse_file($exercisePath, $file, $questionFile) {
global $exercise_info;
global $element_pile;
global $non_HTML_tag_to_avoid;
global $record_item_body;
global $questionTempDir;
$questionTempDir = $exercisePath . '/' . $file . '/';
$questionFilePath = $questionTempDir . $questionFile;
if (!($fp = @ fopen($questionFilePath, 'r'))) {
Display :: display_error_message(get_lang('Error opening question\'s TXT file'));
return false;
} else {
$data = fread($fp, filesize($questionFilePath));
}
/*while(!feof($fp)) {
$data = fread($fp, filesize($questionFilePath));
error_log(print_r($data,1));
}*/
error_log('test');
$data = file($questionFilePath);
$question_index = 0;
foreach($data as $linea => $info) {
//$preg = preg_match('/^[A-Z](\)|\.)\s(.*)/g', $linea, $matches);
#$preg = preg_match('/(^ANSWER:\sq([A-Z])\s?)|(.*))/i', $info, $matches);
if (preg_match('/^([A-Z])(\)|\.)\s(.*)/', $info)) {
$questions[$question_index]['option'][] = $info;
} elseif (preg_match('/^ANSWER:\s([A-Z])\s?/', $info)) {
$questions[$question_index]['answer'] = $info;
} elseif (preg_match('/^ANSWER_EXPLANATION:\s([A-Z])\s?/', $info)) {
$questions[$question_index]['answer_explanation'] = $info;
} elseif (preg_match('/^TAGS:\s([A-Z])\s?/', $info)) {
$questions[$question_index]['answer_tags'] = explode(',', $info);
} elseif (preg_match('/^\n/',$info)) {
$question_index++;
} else {
$questions[$question_index]['title'] = $info;
}
}
error_log(print_r($questions,1));
/*
//used global variable start values declaration :
$record_item_body = false;
$non_HTML_tag_to_avoid = array (
"SIMPLECHOICE",
"CHOICEINTERACTION",
"INLINECHOICEINTERACTION",
"INLINECHOICE",
"SIMPLEMATCHSET",
"SIMPLEASSOCIABLECHOICE",
"TEXTENTRYINTERACTION",
"FEEDBACKINLINE",
"MATCHINTERACTION",
"ITEMBODY",
"BR",
"IMG"
);
//this array to detect tag not supported by claroline import in the xml file to warn the user.
$non_supported_content_in_question = array (
"GAPMATCHINTERACTION",
"EXTENDEDTEXTINTERACTION",
"HOTTEXTINTERACTION",
"HOTSPOTINTERACTION",
"SELECTPOINTINTERACTION",
"GRAPHICORDERINTERACTION",
"GRAPHICASSOCIATIONINTERACTION",
"GRAPHICGAPMATCHINTERACTION",
"POSITIONOBJECTINTERACTION",
"SLIDERINTERACTION",
"DRAWINGINTERACTION",
"UPLOADINTERACTION",
"RESPONSECONDITION",
"RESPONSEIF"
);
$question_format_supported = true;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, false);
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
xml_set_character_data_handler($xml_parser, 'elementData');
if (!xml_parse($xml_parser, $data, feof($fp))) {
// if reading of the xml file in not successfull :
// set errorFound, set error msg, break while statement
Display :: display_error_message(get_lang('Error reading XML file'));
return false;
}
//close file
fclose($fp);
if (!$question_format_supported) {
Display :: display_error_message(get_lang('Unknown question format in file %file', array (
'%file' => $questionFile
)));
return false;
$data = file($questionFilePath);
$question_index = 0;
$correct_answer = '';
$answers_array = array();
foreach ($data as $linea => $info) {
$exercise_info['question'][$question_index]['type'] = 'MCUA';
if (preg_match('/^([A-Z])(\)|\.)\s(.*)/', $info, $matches)) {
//adding one of the posible answers
$exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
$answers_array[] = $matches[1];
} elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
//the correct answers
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
//weight for correct answer
$exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
} elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)\s?/', $info, $matches)) {
//Comment of correct answer
$exercise_info['question'][$question_index]['answer'][$correct_answer_index]['feedback'] = $matches[1];
} elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
//TAGS for chamilo >= 1.10
$exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
} elseif (preg_match('/^\n/',$info)) {
//moving to next question
$question_index++;
//emptying answers array when moving to next question
$answers_array = array();
} else {
//Question itself
$exercise_info['question'][$question_index]['title'] = $info;
}
}
*/
return true;
}
}
Loading…
Cancel
Save