Minor - format code.

1.9.x
Julio Montoya 12 years ago
parent 7b42bfe403
commit d66f76ea28
  1. 69
      main/exercice/export/exercise_import.inc.php
  2. 39
      main/exercice/qti2.php
  3. 24
      main/inc/lib/fileUpload.lib.php

@ -31,35 +31,39 @@ function tempdir($dir, $prefix = 'tmp', $mode = 0777) {
}
/**
* @return the path of the temporary directory where the exercise was uploaded and unzipped
* the path of the temporary directory where the exercise was uploaded and unzipped
* @param string
* @param string
* @return bool
*/
function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath) {
global $_course, $_user;
function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)
{
$_course = api_get_course_info();
$_user = api_get_user_info();
//Check if the file is valid (not to big and exists)
if (!isset ($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
if (!isset($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
// upload failed
return false;
}
if (preg_match('/.zip$/i', $_FILES['userFile']['name']) && handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 1)) {
if (!function_exists('gzopen')) {
//claro_delete_file($uploadPath);
return false;
}
// upload successfull
}
if (preg_match('/.zip$/i', $_FILES['userFile']['name']) &&
handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 1)
) {
return true;
} else {
//claro_delete_file($uploadPath);
return false;
}
return false;
}
/**
* main function to import an exercise,
*
* @param array $file
* @return an array as a backlog of what was really imported, and error or debug messages to display
*/
function import_exercise($file) {
function import_exercise($file)
{
global $exercise_info;
global $element_pile;
global $non_HTML_tag_to_avoid;
@ -99,15 +103,17 @@ function import_exercise($file) {
}
// find the different manifests for each question and parse them.
$exerciseHandle = opendir($baseWorkDir);
//$question_number = 0;
$file_found = false;
$operation = false;
$result = false;
// parse every subdirectory to search xml question files
while (false !== ($file = readdir($exerciseHandle))) {
if (is_dir($baseWorkDir . '/' . $file) && $file != "." && $file != "..") {
//find each manifest for each question repository found
// Find each manifest for each question repository found
$questionHandle = opendir($baseWorkDir . '/' . $file);
while (false !== ($questionFile = readdir($questionHandle))) {
if (preg_match('/.xml$/i', $questionFile)) {
@ -120,11 +126,12 @@ function import_exercise($file) {
$file_found = true;
} // 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 XML file found in the zip'));
return false;
}
if ($result == false ) {
if ($result == false ) {
return false;
}
@ -133,7 +140,7 @@ function import_exercise($file) {
//1.create exercise
$exercise = new Exercise();
$exercise->exercise = $exercise_info['name'];
$exercise->save();
$last_exercise_id = $exercise->selectId();
if (!empty($last_exercise_id)) {
@ -170,11 +177,12 @@ function import_exercise($file) {
// delete the temp dir where the exercise was unzipped
my_delete($baseWorkDir . $uploadPath);
$operation = true;
}
}
return $operation;
}
function parse_file($exercisePath, $file, $questionFile) {
function parse_file($exercisePath, $file, $questionFile)
{
global $exercise_info;
global $element_pile;
global $non_HTML_tag_to_avoid;
@ -183,8 +191,7 @@ function parse_file($exercisePath, $file, $questionFile) {
$questionTempDir = $exercisePath . '/' . $file . '/';
$questionFilePath = $questionTempDir . $questionFile;
if (!($fp = @ fopen($questionFilePath, 'r'))) {
if (!($fp = fopen($questionFilePath, 'r'))) {
Display :: display_error_message(get_lang('Error opening question\'s XML file'));
return false;
} else {
@ -193,7 +200,7 @@ function parse_file($exercisePath, $file, $questionFile) {
//parse XML question file
$data = str_replace(array('<p>', '</p>','<front>','</front>'), '', $data);
//used global variable start values declaration :
$record_item_body = false;
@ -412,12 +419,12 @@ function startElement($parser, $name, $attributes) {
//retrieve answers id for MCUA and MCMA questions
case 'SIMPLECHOICE' :
$current_answer_id = $attributes['IDENTIFIER'];
if (!isset($exercise_info['question'][$current_question_ident]['answer'][$current_answer_id])) {
$exercise_info['question'][$current_question_ident]['answer'][$current_answer_id] = array ();
}
break;
case 'MAPENTRY' :
@ -524,15 +531,15 @@ function elementData($parser, $data) {
if ($record_item_body && (!in_array($current_element, $non_HTML_tag_to_avoid))) {
$current_question_item_body .= $data;
}
switch ($current_element) {
case 'SIMPLECHOICE' :
if (!isset ($exercise_info['question'][$current_question_ident]['answer'][$current_answer_id]['value'])) {
$exercise_info['question'][$current_question_ident]['answer'][$current_answer_id]['value'] = trim($data);
} else {
$exercise_info['question'][$current_question_ident]['answer'][$current_answer_id]['value'] .= ''.trim($data);
}
}
break;
case 'FEEDBACKINLINE' :
@ -590,4 +597,4 @@ function elementData($parser, $data) {
}
break;
}
}
}

@ -30,22 +30,33 @@ if (!api_is_allowed_to_edit(null, true)) {
}
// the breadcrumbs
$interbreadcrumb[]= array ("url"=>"exercice.php", "name"=> get_lang('Exercices'));
$interbreadcrumb[]= array (
"url" => api_get_path(WEB_CODE_PATH)."exercice/exercice.php?".api_get_cidreq(),
"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 ch_qti2_display_form() {
function ch_qti2_display_form()
{
$name_tools = get_lang('ImportQtiQuiz');
$form = '<div class="actions">';
$form .= '<a href="exercice.php?show=test">' . Display :: return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
$form .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/exercice.php?show=test&'.api_get_cidreq().'">'.
Display :: return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
$form .= '</div>';
$form_validator = new FormValidator('qti_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();
$formValidator = new FormValidator(
'qti_upload',
'post',
api_get_self()."?".api_get_cidreq(),
null,
array('enctype' => 'multipart/form-data')
);
$formValidator->addElement('header', $name_tools);
$formValidator->addElement('file', 'userFile', get_lang('DownloadFile'));
$formValidator->addElement('style_submit_button', 'submit', get_lang('Send'), 'class="upload"');
$form .= $formValidator->return_form();
echo $form;
}
@ -54,7 +65,8 @@ function ch_qti2_display_form() {
* This function will import the zip file with the respective qti2
* @param array $uploaded_file ($_FILES)
*/
function ch_qti2_import_file($array_file) {
function ch_qti2_import_file($array_file)
{
$unzip = 0;
$lib_path = api_get_path(LIBRARY_PATH);
require_once $lib_path.'fileUpload.lib.php';
@ -64,15 +76,20 @@ function ch_qti2_import_file($array_file) {
// 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/exercise_import.inc.php';
require_once $main_path.'exercice/export/qti2/qti2_classes.php';
$imported = import_exercise($array_file['name']);
if ($imported) {
header('Location: exercice.php?'.api_get_cidreq());
header('Location: '.api_get_path(WEB_CODE_PATH).'exercice/exercice.php?'.api_get_cidreq());
exit;
} else {
Display::display_error_message(get_lang('UplNoFileUploaded'));
return false;
}
}
@ -92,4 +109,4 @@ if ((api_is_allowed_to_edit(null, true))) {
ch_qti2_display_form();
// display the footer
Display::display_footer();
Display::display_footer();

@ -211,7 +211,14 @@ function handle_uploaded_document(
// If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
return unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $max_filled_space, $output, $to_group_id);
return unzip_uploaded_document(
$uploaded_file,
$upload_path,
$base_work_dir,
$max_filled_space,
$output,
$to_group_id
);
//display_message('Unzipping file');
} elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploaded_file['name']))) { // We can only unzip ZIP files (no gz, tar,...)
if ($output) {
@ -751,7 +758,14 @@ function unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_
*
* @return boolean true if it succeeds false otherwise
*/
function unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $max_filled_space, $output = true, $to_group_id = 0) {
function unzip_uploaded_document(
$uploaded_file,
$upload_path,
$base_work_dir,
$max_filled_space,
$output = true,
$to_group_id = 0
) {
global $_course;
global $_user;
global $to_user_id;
@ -760,11 +774,10 @@ function unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $
$zip_file = new PclZip($uploaded_file['tmp_name']);
// Check the zip content (real size and file extension)
$zip_content_array = (array)$zip_file->listContent();
$real_filesize = 0;
foreach($zip_content_array as & $this_content) {
foreach ($zip_content_array as & $this_content) {
$real_filesize += $this_content['size'];
}
@ -782,12 +795,13 @@ function unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $
// Get into the right directory
$save_dir = getcwd();
chdir($base_work_dir.$upload_path);
// We extract using a callback function that "cleans" the path
$unzipping_state = $zip_file->extract(PCLZIP_CB_PRE_EXTRACT, 'clean_up_files_in_zip', PCLZIP_OPT_REPLACE_NEWER);
// Add all documents in the unzipped folder to the database
add_all_documents_in_folder_to_database($_course, $_user['user_id'], $base_work_dir ,$upload_path == '/' ? '' : $upload_path, $to_group_id);
//Display::display_normal_message(get_lang('UplZipExtractSuccess'));
return true;
}

Loading…
Cancel
Save