From 76d6e9d06f4b4af6261a754d00e6e85fde2748db Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 14 Nov 2018 15:27:18 +0100 Subject: [PATCH] WIP: Remove use of "PHPExcel" use Export class (export.lib.php) - Update composer dependencies --- composer.json | 5 +- main/exercise/exercise.php | 2 + main/exercise/exercise_result.class.php | 262 ++++++------------ .../hotpotatoes_exercise_result.class.php | 219 ++++----------- main/gradebook/lib/gradebook_result.class.php | 35 +-- main/inc/lib/export.lib.inc.php | 4 +- main/mySpace/current_courses.php | 38 +-- 7 files changed, 166 insertions(+), 399 deletions(-) diff --git a/composer.json b/composer.json index c8aaef8b39..175c5eaa24 100755 --- a/composer.json +++ b/composer.json @@ -129,7 +129,10 @@ "studio-42/elfinder": "2.1.*", "jbroadway/urlify": "1.1.0-stable", "patchwork/utf8": "~1.3", - "ddeboer/data-import": "@stable", + "portphp/portphp": "~1.3", + "portphp/excel": "^1.1", + "portphp/doctrine": "^1.0", + "portphp/csv": "^1.1", "phpoffice/phpspreadsheet": "~1.5", "phpoffice/phpword": "^0.14.0", "webit/eval-math": "1.0.1", diff --git a/main/exercise/exercise.php b/main/exercise/exercise.php index d918710dc5..1e3e8e956e 100644 --- a/main/exercise/exercise.php +++ b/main/exercise/exercise.php @@ -19,6 +19,8 @@ use ChamiloSession as Session; require_once __DIR__.'/../inc/global.inc.php'; $current_course_tool = TOOL_QUIZ; +$charset = 'UTF-8'; + // Setting the tabs $this_section = SECTION_COURSES; diff --git a/main/exercise/exercise_result.class.php b/main/exercise/exercise_result.class.php index 15e62937d2..f504d3354e 100755 --- a/main/exercise/exercise_result.class.php +++ b/main/exercise/exercise_result.class.php @@ -468,53 +468,40 @@ class ExerciseResult $hotpotato_name ); $now = api_get_local_time(); - $filename = 'exercise_results_'.$now.'.xlsx'; + $filename = 'exercise_results_'.$now; if (!empty($user_id)) { - $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.xlsx'; + $filename = 'exercise_results_user_'.$user_id.'_'.$now; } - $spreadsheet = new PHPExcel(); - $spreadsheet->setActiveSheetIndex(0); - $worksheet = $spreadsheet->getActiveSheet(); - - $line = 1; // Skip first line - $column = 0; //skip the first column (row titles) - // check if exists column 'user' - $with_column_user = false; + $withColumnUser = false; foreach ($this->results as $result) { if (!empty($result['last_name']) && !empty($result['first_name'])) { - $with_column_user = true; + $withColumnUser = true; break; } } $officialCodeInList = api_get_setting('show_official_code_exercise_result_list'); - - if ($with_column_user) { + $list = []; + if ($withColumnUser) { if (api_is_western_name_order()) { - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName')); - $column++; + $list[0][] = get_lang('FirstName'); + $list[0][] = get_lang('LastName'); } else { - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName')); - $column++; + $list[0][] = get_lang('LastName'); + $list[0][] = get_lang('FirstName'); } if ($officialCodeInList === 'true') { - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode')); - $column++; + $list[0][] = get_lang('OfficialCode'); } - $worksheet->setCellValueByColumnAndRow($column++, $line, get_lang('LoginName')); - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email')); - $column++; + $list[0][] = get_lang('LoginName'); + $list[0][] = get_lang('Email'); } - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups')); - $column++; + + $list[0][] = get_lang('Groups'); if ($export_user_fields) { //show user fields section with a big th colspan that spans over all fields @@ -529,192 +516,117 @@ class ExerciseResult //show the fields names for user fields foreach ($extra_user_fields as $field) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($field[3]), - ENT_QUOTES, - $charset - ) + $list[0][] = api_html_entity_decode( + strip_tags($field[3]), + ENT_QUOTES, + $charset ); - $column++; } } - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')'); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Status')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('ToolLearnpath')); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('UserIsCurrentlySubscribed')); - $line++; - + $list[0][] = get_lang('Title'); + $list[0][] = get_lang('StartDate'); + $list[0][] = get_lang('EndDate'); + $list[0][] = get_lang('Duration').' ('.get_lang('MinMinutes').')'; + $list[0][] = get_lang('Score'); + $list[0][] = get_lang('Total'); + $list[0][] = get_lang('Status'); + $list[0][] = get_lang('ToolLearnpath'); + $list[0][] = get_lang('UserIsCurrentlySubscribed'); + $column = 1; foreach ($this->results as $row) { - $column = 0; - if ($with_column_user) { + if ($withColumnUser) { if (api_is_western_name_order()) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['first_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['first_name']), + ENT_QUOTES, + $charset ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['last_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['last_name']), + ENT_QUOTES, + $charset ); - $column++; } else { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['last_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['last_name']), + ENT_QUOTES, + $charset ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['first_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['first_name']), + ENT_QUOTES, + $charset ); - $column++; } if ($officialCodeInList === 'true') { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['official_code']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['official_code']), + ENT_QUOTES, + $charset ); - $column++; } - $worksheet->setCellValueByColumnAndRow( - $column++, - $line, - api_html_entity_decode( - strip_tags($row['username']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['username']), + ENT_QUOTES, + $charset ); - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['email']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['email']), + ENT_QUOTES, + $charset ); - $column++; } - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags( - implode( - ", ", - GroupManager:: get_user_group_name($row['user_id']) - ) - ), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags( + implode( + ', ', + GroupManager:: get_user_group_name($row['user_id']) + ) + ), + ENT_QUOTES, + $charset ); - $column++; if ($export_user_fields) { - //show user fields data, if any, for this user - $user_fields_values = UserManager::get_extra_user_data( + // show user fields data, if any, for this user + $values = UserManager::get_extra_user_data( $row['user_id'], false, false, false, true ); - foreach ($user_fields_values as $value) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($value), - ENT_QUOTES, - $charset - ) + foreach ($values as $value) { + $list[$column][] = api_html_entity_decode( + strip_tags($value), + ENT_QUOTES, + $charset ); - $column++; } } - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['title']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['title']), + ENT_QUOTES, + $charset ); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']); - $column++; + $list[$column][] = $row['start_date']; + $list[$column][] = $row['end_date']; $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0; - $worksheet->setCellValueByColumnAndRow($column, $line, $duration); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']); + $list[$column][] = $duration; + $list[$column][] = $row['result']; + $list[$column][] = $row['max']; + $list[$column][] = $row['status']; + $list[$column][] = $row['lp_name']; + $list[$column][] = $row['is_user_subscribed']; $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['lp_name']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['is_user_subscribed']); - $line++; } - - $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); - $writer = new PHPExcel_Writer_Excel2007($spreadsheet); - $writer->save($file); - DocumentManager::file_send_for_download($file, true, $filename); + Export::arrayToXls($list, $filename); return true; } diff --git a/main/exercise/hotpotatoes_exercise_result.class.php b/main/exercise/hotpotatoes_exercise_result.class.php index 63f099f586..8abe0f63c4 100755 --- a/main/exercise/hotpotatoes_exercise_result.class.php +++ b/main/exercise/hotpotatoes_exercise_result.class.php @@ -208,60 +208,28 @@ class HotpotatoesExerciseResult $exercise_id, $hotpotato_name ); - $filename = 'exercise_results_'.api_get_local_time().'.xls'; + $filename = 'exercise_results_'.api_get_local_time(); if (!empty($user_id)) { - $filename = 'exercise_results_user_'.$user_id.'_'.api_get_local_time().'.xls'; + $filename = 'exercise_results_user_'.$user_id.'_'.api_get_local_time(); } - $spreadsheet = new PHPExcel(); - $spreadsheet->setActiveSheetIndex(0); - $worksheet = $spreadsheet->getActiveSheet(); - - $line = 0; - $column = 0; //skip the first column (row titles) - // check if exists column 'user' - $with_column_user = false; + $withColumnUser = false; foreach ($this->results as $result) { if (!empty($result['last_name']) && !empty($result['first_name'])) { - $with_column_user = true; + $withColumnUser = true; break; } } - - if ($with_column_user) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Email') - ); - $column++; + $list = []; + if ($withColumnUser) { + $list[0][] = get_lang('Email'); if (api_is_western_name_order()) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('FirstName') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('LastName') - ); - $column++; + $list[0][] = get_lang('FirstName'); + $list[0][] = get_lang('LastName'); } else { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('LastName') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('FirstName') - ); - $column++; + $list[0][] = get_lang('LastName'); + $list[0][] = get_lang('FirstName'); } } @@ -278,157 +246,80 @@ class HotpotatoesExerciseResult //show the fields names for user fields foreach ($extra_user_fields as $field) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($field[3]), - ENT_QUOTES, - $charset - ) + $list[0][] = api_html_entity_decode( + strip_tags($field[3]), + ENT_QUOTES, + $charset ); - $column++; } } - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Title') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('StartDate') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('EndDate') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Duration').' ('.get_lang('MinMinutes').')' - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Score') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Total') - ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - get_lang('Status') - ); - $line++; + $list[0][] = get_lang('Title'); + $list[0][] = get_lang('StartDate'); + $list[0][] = get_lang('EndDate'); + $list[0][] = get_lang('Duration').' ('.get_lang('MinMinutes').')'; + $list[0][] = get_lang('Score'); + $list[0][] = get_lang('Total'); + $list[0][] = get_lang('Status'); + $column = 1; foreach ($this->results as $row) { - $column = 0; - - if ($with_column_user) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['email']), - ENT_QUOTES, - $charset - ) + if ($withColumnUser) { + $list[$column][] = api_html_entity_decode( + strip_tags($row['email']), + ENT_QUOTES, + $charset ); - $column++; if (api_is_western_name_order()) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['first_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['first_name']), + ENT_QUOTES, + $charset ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['last_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['last_name']), + ENT_QUOTES, + $charset ); - $column++; } else { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['last_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['last_name']), + ENT_QUOTES, + $charset ); - $column++; - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['first_name']), - ENT_QUOTES, - $charset - ) + $list[$column][] = api_html_entity_decode( + strip_tags($row['first_name']), + ENT_QUOTES, + $charset ); - $column++; } } if ($export_user_fields) { //show user fields data, if any, for this user - $user_fields_values = UserManager::get_extra_user_data( + $values = UserManager::get_extra_user_data( $row['user_id'], false, false, false, true ); - foreach ($user_fields_values as $value) { - $worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)); - $column++; + foreach ($values as $value) { + $list[$column][] = api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset); } } - $worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['duration']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']); - $column++; - $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']); - $line++; + $list[$column][] = api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset); + $list[$column][] = $row['start_date']; + $list[$column][] = $row['end_date']; + $list[$column][] = $row['duration']; + $list[$column][] = $row['result']; + $list[$column][] = $row['max']; + $list[$column][] = $row['status']; } - $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); - $writer = new PHPExcel_Writer_Excel2007($spreadsheet); - $writer->save($file); - DocumentManager::file_send_for_download($file, true, $filename); + Export::arrayToXls($list, $filename); return true; } diff --git a/main/gradebook/lib/gradebook_result.class.php b/main/gradebook/lib/gradebook_result.class.php index 3f1fbc7879..8290f35a4b 100644 --- a/main/gradebook/lib/gradebook_result.class.php +++ b/main/gradebook/lib/gradebook_result.class.php @@ -86,46 +86,31 @@ class GradeBookResult */ public function exportCompleteReportXLS($data) { - $filename = 'gradebook-results-'.api_get_local_time().'.xlsx'; - - $spreadsheet = new PHPExcel(); - $spreadsheet->setActiveSheetIndex(0); - $worksheet = $spreadsheet->getActiveSheet(); - - $line = 1; - $column = 0; - + $filename = 'gradebook-results-'.api_get_local_time(); + $list = []; //headers foreach ($data[0] as $header_col) { - $worksheet->SetCellValueByColumnAndRow( - $column, - $line, - html_entity_decode(strip_tags($header_col)) - ); - $column++; + $list[0][] = html_entity_decode(strip_tags($header_col)); } - $line++; - $cant_students = count($data[1]); - + $line = 0; for ($i = 0; $i < $cant_students; $i++) { $column = 0; foreach ($data[1][$i] as $col_name) { - $worksheet->SetCellValueByColumnAndRow( + $list[$column][$line] = html_entity_decode(strip_tags($col_name)); + /*$worksheet->SetCellValueByColumnAndRow( $column, $line, html_entity_decode(strip_tags($col_name)) - ); + );*/ $column++; } $line++; } - $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); - $writer = new PHPExcel_Writer_Excel2007($spreadsheet); - $writer->save($file); - DocumentManager::file_send_for_download($file, true, $filename); - exit; + Export::arrayToXls($list, $filename); + + return true; } /** diff --git a/main/inc/lib/export.lib.inc.php b/main/inc/lib/export.lib.inc.php index f7c894dbc1..b9fa77a128 100755 --- a/main/inc/lib/export.lib.inc.php +++ b/main/inc/lib/export.lib.inc.php @@ -3,8 +3,8 @@ use Chamilo\CoreBundle\Component\Editor\Connector; use Chamilo\CoreBundle\Component\Filesystem\Data; -use Ddeboer\DataImport\Writer\CsvWriter; -use Ddeboer\DataImport\Writer\ExcelWriter; +use Port\Csv\CsvWriter; +use Port\Excel\ExcelWriter; use MediaAlchemyst\Alchemyst; use MediaAlchemyst\DriversContainer; use Neutron\TemporaryFilesystem\Manager; diff --git a/main/mySpace/current_courses.php b/main/mySpace/current_courses.php index 2ed05b6b43..c0a746ec1b 100755 --- a/main/mySpace/current_courses.php +++ b/main/mySpace/current_courses.php @@ -9,7 +9,7 @@ $cidReset = true; require_once __DIR__.'/../inc/global.inc.php'; $this_section = SECTION_TRACKING; -$filename = 'reporting.xlsx'; +$filename = 'reporting'; if (!api_is_allowed_to_create_course()) { api_not_allowed(true); @@ -212,37 +212,11 @@ $headers = [ ]; if (isset($_GET['export'])) { - global $charset; - $spreadsheet = new PHPExcel(); - $spreadsheet->setActiveSheetIndex(0); - $worksheet = $spreadsheet->getActiveSheet(); - - $line = 0; - $column = 0; //skip the first column (row titles) - - foreach ($headers as $header) { - $worksheet->setCellValueByColumnAndRow($column, $line, $header); - $column++; - } - $line++; - foreach ($array as $row) { - $column = 0; - foreach ($row as $item) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - html_entity_decode(strip_tags($item)) - ); - $column++; - } - $line++; - } - $line++; - - $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); - $writer = new PHPExcel_Writer_Excel2007($spreadsheet); - $writer->save($file); - DocumentManager::file_send_for_download($file, true, $filename); + $list = [ + 0 => $headers, + 1 => $array[0], + ]; + Export::arrayToXls($list, $filename); exit; }