Add import/export of questions see BT#10891

ofaj
jmontoya 10 years ago
parent 885bb43eab
commit 1af145f353
  1. 4
      main/exercice/TestCategory.php
  2. 129
      main/exercice/tests_category.php

@ -759,9 +759,9 @@ class TestCategory
$content .= $category['description']; $content .= $category['description'];
$content .= '</div>'; $content .= '</div>';
$links = '<a href="' . api_get_self() . '?action=editcategory&category_id=' . $category['id'] . '">' . $links = '<a href="' . api_get_self() . '?action=editcategory&category_id=' . $category['id'] . '&'.api_get_cidreq().'">' .
Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>'; Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>';
$links .= ' <a href="' . api_get_self() . '?action=deletecategory&category_id=' . $category['id'] . '" '; $links .= ' <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=deletecategory&category_id=' . $category['id'] . '" ';
$links .= 'onclick="return confirmDelete(\'' . self::protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure') . '[' . $rowname) . '] ?\', \'id_cat' . $category['id'] . '\');">'; $links .= 'onclick="return confirmDelete(\'' . self::protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure') . '[' . $rowname) . '] ?\', \'id_cat' . $category['id'] . '\');">';
$links .= Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>'; $links .= Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
$html .= Display::panel($content, $category['title'].$links); $html .= Display::panel($content, $category['title'].$links);

@ -37,40 +37,88 @@ $interbreadcrumb[] = array(
"url" => "exercise.php?".api_get_cidreq(), "url" => "exercise.php?".api_get_cidreq(),
"name" => get_lang('Exercises'), "name" => get_lang('Exercises'),
); );
Display::display_header(get_lang('Category'));
// Action handling: add, edit and remove $action = isset($_GET['action']) ? $_GET['action'] : '';
if (isset($_GET['action']) && $_GET['action'] == 'addcategory') { $content = '';
add_category_form($_GET['action']);
display_add_category(); switch ($action) {
} else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') { case 'addcategory':
edit_category_form($_GET['action']); $content = add_category_form('addcategory');
} else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') { break;
delete_category_form($_GET['action']); case 'editcategory':
display_add_category(); $content = edit_category_form('editcategory');
} else { break;
display_add_category(); case 'deletecategory':
delete_category_form('deletecategory');
break;
case 'export_category':
$archiveFile = 'export_exercise_categoroes_'.api_get_course_id().'_'.api_get_local_time();
$categories = $category->getCategories($courseId, $sessionId);
$export = [];
$export[] = ['title', 'description'];
if (!empty($categories)) {
foreach ($categories as $category) {
$export[] = [$category['title'], $category['description']];
}
}
Export::arrayToCsv($export, $archiveFile);
exit;
break;
case 'import_category':
$form = importCategoryForm();
if ($form->validate()) {
$categories = Import::csv_reader($_FILES['file']['tmp_name']);
if (!empty($categories)) {
foreach ($categories as $item) {
$cat = new TestCategory(0, $item['title'], $item['description']);
$cat->addCategoryInBDD();
}
Display::addFlash(Display::return_message(get_lang('Imported')));
}
}
$content = $form->returnForm();
break;
} }
echo $category->displayCategories($courseId, $sessionId);
Display::display_header(get_lang('Category'));
displayActionBar();
echo $content;
echo $category->displayCategories($courseId, $sessionId);
Display::display_footer(); Display::display_footer();
/**
* @return FormValidator
*/
function importCategoryForm()
{
$form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq());
//$form->addElement('header', get_lang('ImportGroups'));
$form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
$form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
//$form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv'));
$form->addButtonImport(get_lang('Import'));
return $form;
}
/** /**
* Form to edit a category * Form to edit a category
* @todo move to TestCategory.class.php * @todo move to TestCategory.class.php
* @param string $action * @param string $action
*/ */
function edit_category_form($action) { function edit_category_form($action)
{
$action = Security::remove_XSS($action); $action = Security::remove_XSS($action);
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) { if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category_id = Security::remove_XSS($_GET['category_id']); $category_id = intval($_GET['category_id']);
$objcat = new TestCategory($category_id); $objcat = new TestCategory($category_id);
$form = new FormValidator( $form = new FormValidator(
'note', 'note',
'post', 'post',
api_get_self().'?action='.$action.'&category_id='.$category_id api_get_self().'?action='.$action.'&category_id='.$category_id.'&'.api_get_cidreq()
); );
// Setting the form elements // Setting the form elements
@ -106,9 +154,9 @@ function edit_category_form($action) {
$v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
$objcat = new TestCategory($v_id, $v_name, $v_description); $objcat = new TestCategory($v_id, $v_name, $v_description);
if ($objcat->modifyCategory()) { if ($objcat->modifyCategory()) {
Display::display_confirmation_message(get_lang('MofidfyCategoryDone')); Display::addFlash(Display::return_message(get_lang('MofidfyCategoryDone')));
} else { } else {
Display::display_confirmation_message(get_lang('ModifyCategoryError')); Display::addFlash(Display::return_message(get_lang('ModifyCategoryError')));
} }
} }
Security::clear_token(); Security::clear_token();
@ -117,15 +165,19 @@ function edit_category_form($action) {
$token = Security::get_token(); $token = Security::get_token();
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token)); $form->setConstants(array('sec_token' => $token));
$form->display();
return $form->returnForm();
} }
} else { } else {
Display::display_error_message(get_lang('CannotEditCategory')); Display::addFlash(
Display::return_message(get_lang('CannotEditCategory'), 'error')
);
} }
} }
// process to delete a category // process to delete a category
function delete_category_form($action) { function delete_category_form($action)
{
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) { if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category_id = Security::remove_XSS($_GET['category_id']); $category_id = Security::remove_XSS($_GET['category_id']);
$catobject = new TestCategory($category_id); $catobject = new TestCategory($category_id);
@ -144,10 +196,11 @@ function delete_category_form($action) {
* @todo move to TestCategory.class.php * @todo move to TestCategory.class.php
* @param string $action * @param string $action
*/ */
function add_category_form($action) { function add_category_form($action)
{
$action = Security::remove_XSS($action); $action = Security::remove_XSS($action);
// initiate the object // initiate the object
$form = new FormValidator('note', 'post', api_get_self() . '?action=' . $action); $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $action.'&'.api_get_cidreq());
// Setting the form elements // Setting the form elements
$form->addElement('header', get_lang('AddACategory')); $form->addElement('header', get_lang('AddACategory'));
$form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95')); $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
@ -177,31 +230,45 @@ function add_category_form($action) {
} }
Security::clear_token(); Security::clear_token();
} else { } else {
display_goback(); //display_goback();
$token = Security::get_token(); $token = Security::get_token();
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token)); $form->setConstants(array('sec_token' => $token));
$form->display();
return $form->returnForm();
} }
} }
// Display add category button // Display add category button
function displayActionBar()
function display_add_category() { {
echo '<div class="actions">'; echo '<div class="actions">';
echo '<a href="exercise.php?' . api_get_cidreq() . '">' . echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?' . api_get_cidreq() . '">' .
Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>'; Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
echo '<a href="' . api_get_self() . '?action=addcategory">' .
echo '<a href="' . api_get_self() . '?action=addcategory&'.api_get_cidreq().'">' .
Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>'; Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>';
echo Display::url(
Display::return_icon('export_csv.png', get_lang('ExportAsCSV')),
api_get_self() . '?action=export_category&'.api_get_cidreq()
);
echo Display::url(
Display::return_icon('import_csv.png', get_lang('ImportAsCSV')),
api_get_self() . '?action=import_category&'.api_get_cidreq()
);
echo '</div>'; echo '</div>';
echo "<br/>"; echo "<br/>";
echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>"; echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
} }
// display goback to category list page link // display goback to category list page link
function display_goback() { function display_goback()
{
echo '<div class="actions">'; echo '<div class="actions">';
echo '<a href="' . api_get_self() . '">' . echo '<a href="' . api_get_self() . '?'.api_get_cidreq().'">' .
Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>'; Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>';
echo '</div>'; echo '</div>';
} }

Loading…
Cancel
Save