Import/export course progress (thematic) see #3858

skala
Julio Montoya 13 years ago
parent 3e1521982d
commit f3f7cfdfcd
  1. 13
      main/course_progress/index.php
  2. 20
      main/course_progress/thematic.php
  3. 68
      main/course_progress/thematic_controller.php
  4. 36
      main/inc/lib/import.lib.php

@ -18,7 +18,9 @@ require_once api_get_path(LIBRARY_PATH).'attendance.lib.php';
require_once api_get_path(LIBRARY_PATH).'thematic.lib.php';
require_once api_get_path(LIBRARY_PATH).'app_view.php';
require_once 'thematic_controller.php';
//require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
require_once api_get_path(LIBRARY_PATH).'export.lib.inc.php';
require_once api_get_path(LIBRARY_PATH).'import.lib.php';
// current section
$this_section = SECTION_COURSES;
@ -33,6 +35,7 @@ define('ADD_THEMATIC_PLAN', 6);
// get actions
$actions = array('thematic_details', 'thematic_list', 'thematic_add', 'thematic_edit', 'thematic_copy', 'thematic_delete', 'moveup', 'movedown',
'thematic_import_select', 'thematic_import', 'thematic_export',
'thematic_plan_list', 'thematic_plan_add', 'thematic_plan_edit', 'thematic_plan_delete',
'thematic_advance_list', 'thematic_advance_add', 'thematic_advance_edit', 'thematic_advance_delete');
@ -298,13 +301,16 @@ switch ($action) {
case 'thematic_edit' :
case 'thematic_delete' :
case 'thematic_delete_select' :
case 'thematic_copy' :
case 'thematic_copy' :
case 'thematic_import_select' :
case 'thematic_import' :
case 'moveup' :
case 'movedown' :
if (!api_is_allowed_to_edit(null,true)) {
api_not_allowed();
}
case 'thematic_list' :
case 'thematic_export' :
case 'thematic_details' :
$thematic_controller->thematic($action);
break;
@ -316,8 +322,7 @@ switch ($action) {
}
case 'thematic_plan_list' :
$thematic_controller->thematic_plan($action);
break;
break;
case 'thematic_advance_add' :
case 'thematic_advance_edit' :
case 'thematic_advance_delete' :

@ -14,11 +14,12 @@ api_protect_course_script(true);
$token = Security::get_token();
$url_token = "&sec_token=".$token;
if (api_is_allowed_to_edit(null, true)) {
if (api_is_allowed_to_edit(null, true)) {
echo '<div class="actions">';
switch ($action) {
case 'thematic_add' :
case 'thematic_import_select' :
echo '<a href="index.php?'.api_get_cidreq().'">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ThematicDetails'),'',ICON_SIZE_MEDIUM).'</a>';
break;
case 'thematic_list' :
@ -26,9 +27,12 @@ if (api_is_allowed_to_edit(null, true)) {
break;
case 'thematic_details' :
echo '<a href="index.php?'.api_get_cidreq().'&action=thematic_add'.$url_token.'">'.Display::return_icon('new_course_progress.png',get_lang('NewThematicSection'),'',ICON_SIZE_MEDIUM).'</a>';
echo '<a href="index.php?'.api_get_cidreq().'&action=thematic_import_select'.$url_token.'">'.Display::return_icon('import_csv.png',get_lang('ImportThematic'),'',ICON_SIZE_MEDIUM).'</a>';
echo '<a href="index.php?'.api_get_cidreq().'&action=thematic_export'.$url_token.'">'.Display::return_icon('export_csv.png',get_lang('ExportThematic'),'', ICON_SIZE_MEDIUM).'</a>';
break;
default :
echo '<a href="index.php?'.api_get_cidreq().'&action=thematic_add'.$url_token.'">'.Display::return_icon('new_course_progress.png',get_lang('NewThematicSection'),'',ICON_SIZE_MEDIUM).'</a>';
echo '<a href="index.php?'.api_get_cidreq().'&action=thematic_add'.$url_token.'">'.Display::return_icon('new_course_progress.png',get_lang('NewThematicSection'),'',ICON_SIZE_MEDIUM).'</a>';
}
echo '</div>';
}
@ -250,5 +254,13 @@ if ($action == 'thematic_list') {
Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'),false);
}
if ($show_form)
$form->display();
$form->display();
} else if ($action == 'thematic_import_select') {
// Create form to upload csv file.
$form = new FormValidator('thematic_import','POST','index.php?action=thematic_import&'.api_get_cidreq().$url_token);
$form->addElement('header', get_lang('ImportThematic'));
$form->addElement('file', 'file');
$form->addElement('checkbox', 'replace', null, get_lang('DeleteAllThematic'));
$form->addElement('style_submit_button', 'SubmitImport', get_lang('Import'), 'class="save"');
$form->display();
}

@ -106,7 +106,65 @@ class ThematicController
$thematic_id = null;
$action = 'thematic_details';
}
break;
break;
case 'thematic_import_select':
break;
case 'thematic_import':
$csv_import_array = Import::csv_to_array($_FILES['file']['tmp_name'], 'horizontal');
if (isset($_POST['replace']) && $_POST['replace']) {
// Remove current thematic.
$list = $thematic->get_thematic_list();
foreach ($list as $i) {
$thematic->thematic_destroy($i);
}
}
// Import the progress.
$current_thematic = null;
foreach ($csv_import_array as $data) {
foreach ($data as $key => $item) {
if ($key == 'title') {
$thematic->set_thematic_attributes(null, $item[0], $item[1], api_get_session_id ());
$current_thematic = $thematic->thematic_save ();
$description_type = 0;
}
if ($key == 'plan') {
$thematic->set_thematic_plan_attributes($current_thematic, $item[0], $item[1], $description_type);
$thematic->thematic_plan_save();
$description_type++;
}
if ($key == 'progress') {
$thematic->set_thematic_advance_attributes (null, $current_thematic, 0, $item[2], $item[0], $item[1]);
$thematic->thematic_advance_save ();
}
}
}
$action = 'thematic_details';
break;
case 'thematic_export':
$list = $thematic->get_thematic_list();
$csv = array();
foreach ($list as $theme) {
$csv[] = array ('title', $theme['title'], $theme['content']);
$data = $thematic->get_thematic_plan_data ($theme['id']);
if (!empty($data)) {
foreach ($data as $plan) {
$csv[] = array ('plan', $plan['title'], $plan['description']);
}
}
$data = $thematic->get_thematic_advance_by_thematic_id ($theme['id']);
if (!empty($data)) {
foreach ($data as $advance) {
$csv[] = array('progress', $advance['start_date'], $advance['duration'], $advance['content']);
}
}
}
Export::export_table_csv($csv);
exit;
// Don't continue building a normal page.
return;
case 'moveup':
$thematic->move_thematic('up', $thematic_id);
$action = 'thematic_details';
@ -181,7 +239,7 @@ class ThematicController
if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
if (trim($_POST['title']) !== '') {
if (isset($_POST['title'])) {
if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
if (api_is_allowed_to_edit(null, true)) {
@ -347,9 +405,9 @@ class ThematicController
}
}
break;
default:
$thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
break;
default:
$thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
break;
}

@ -36,7 +36,7 @@ class Import {
*
* @deprecated use cvs_reader instead
*/
function csv_to_array($filename) {
function csv_to_array($filename, $csv_order = 'vertical') {
$result = array();
// Encoding detection.
@ -63,22 +63,40 @@ class Import {
if ($handle === false) {
return $result;
}
$keys = api_fgetcsv($handle, null, ';');
foreach ($keys as $key => &$key_value) {
$key_value = api_to_system_encoding($key_value, $from_encoding);
}
if ($csv_order == 'vertical') {
$keys = api_fgetcsv($handle, null, ';');
foreach ($keys as $key => &$key_value) {
$key_value = api_to_system_encoding($key_value, $from_encoding);
}
}
while (($row_tmp = api_fgetcsv($handle, null, ';')) !== false) {
$row = array();
// Avoid empty lines in csv.
// Avoid empty lines in csv
if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
if (!is_null($row_tmp[0])) {
foreach ($row_tmp as $index => $value) {
$row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
}
if ($csv_order == 'vertical') {
foreach ($row_tmp as $index => $value) {
$row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
}
} else {
$first = null;
$count = 1;
foreach ($row_tmp as $index => $value) {
if ($count == 1) {
$first = $value;
} else {
$row[$first][] = api_to_system_encoding($value, $from_encoding);
}
$count++;
}
}
$result[] = $row;
}
}
}
fclose($handle);
return $result;
}

Loading…
Cancel
Save