WIP: Remove use of "PHPExcel" use Export class (export.lib.php)

- Update composer dependencies
pull/2744/head
Julio Montoya 7 years ago
parent 76abd292f1
commit 76d6e9d06f
  1. 5
      composer.json
  2. 2
      main/exercise/exercise.php
  3. 262
      main/exercise/exercise_result.class.php
  4. 219
      main/exercise/hotpotatoes_exercise_result.class.php
  5. 35
      main/gradebook/lib/gradebook_result.class.php
  6. 4
      main/inc/lib/export.lib.inc.php
  7. 38
      main/mySpace/current_courses.php

@ -129,7 +129,10 @@
"studio-42/elfinder": "2.1.*", "studio-42/elfinder": "2.1.*",
"jbroadway/urlify": "1.1.0-stable", "jbroadway/urlify": "1.1.0-stable",
"patchwork/utf8": "~1.3", "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/phpspreadsheet": "~1.5",
"phpoffice/phpword": "^0.14.0", "phpoffice/phpword": "^0.14.0",
"webit/eval-math": "1.0.1", "webit/eval-math": "1.0.1",

@ -19,6 +19,8 @@ use ChamiloSession as Session;
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
$current_course_tool = TOOL_QUIZ; $current_course_tool = TOOL_QUIZ;
$charset = 'UTF-8';
// Setting the tabs // Setting the tabs
$this_section = SECTION_COURSES; $this_section = SECTION_COURSES;

@ -468,53 +468,40 @@ class ExerciseResult
$hotpotato_name $hotpotato_name
); );
$now = api_get_local_time(); $now = api_get_local_time();
$filename = 'exercise_results_'.$now.'.xlsx'; $filename = 'exercise_results_'.$now;
if (!empty($user_id)) { 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' // check if exists column 'user'
$with_column_user = false; $withColumnUser = false;
foreach ($this->results as $result) { foreach ($this->results as $result) {
if (!empty($result['last_name']) && !empty($result['first_name'])) { if (!empty($result['last_name']) && !empty($result['first_name'])) {
$with_column_user = true; $withColumnUser = true;
break; break;
} }
} }
$officialCodeInList = api_get_setting('show_official_code_exercise_result_list'); $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
$list = [];
if ($with_column_user) { if ($withColumnUser) {
if (api_is_western_name_order()) { if (api_is_western_name_order()) {
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName')); $list[0][] = get_lang('FirstName');
$column++; $list[0][] = get_lang('LastName');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
$column++;
} else { } else {
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName')); $list[0][] = get_lang('LastName');
$column++; $list[0][] = get_lang('FirstName');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
$column++;
} }
if ($officialCodeInList === 'true') { if ($officialCodeInList === 'true') {
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode')); $list[0][] = get_lang('OfficialCode');
$column++;
} }
$worksheet->setCellValueByColumnAndRow($column++, $line, get_lang('LoginName')); $list[0][] = get_lang('LoginName');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email')); $list[0][] = get_lang('Email');
$column++;
} }
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups'));
$column++; $list[0][] = get_lang('Groups');
if ($export_user_fields) { if ($export_user_fields) {
//show user fields section with a big th colspan that spans over all 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 //show the fields names for user fields
foreach ($extra_user_fields as $field) { foreach ($extra_user_fields as $field) {
$worksheet->setCellValueByColumnAndRow( $list[0][] = api_html_entity_decode(
$column, strip_tags($field[3]),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($field[3]),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
} }
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title')); $list[0][] = get_lang('Title');
$column++; $list[0][] = get_lang('StartDate');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate')); $list[0][] = get_lang('EndDate');
$column++; $list[0][] = get_lang('Duration').' ('.get_lang('MinMinutes').')';
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate')); $list[0][] = get_lang('Score');
$column++; $list[0][] = get_lang('Total');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')'); $list[0][] = get_lang('Status');
$column++; $list[0][] = get_lang('ToolLearnpath');
$worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score')); $list[0][] = get_lang('UserIsCurrentlySubscribed');
$column++; $column = 1;
$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++;
foreach ($this->results as $row) { foreach ($this->results as $row) {
$column = 0; if ($withColumnUser) {
if ($with_column_user) {
if (api_is_western_name_order()) { if (api_is_western_name_order()) {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['first_name']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['first_name']),
ENT_QUOTES,
$charset
)
); );
$column++; $list[$column][] = api_html_entity_decode(
$worksheet->setCellValueByColumnAndRow( strip_tags($row['last_name']),
$column, ENT_QUOTES,
$line, $charset
api_html_entity_decode(
strip_tags($row['last_name']),
ENT_QUOTES,
$charset
)
); );
$column++;
} else { } else {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['last_name']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['last_name']),
ENT_QUOTES,
$charset
)
); );
$column++; $list[$column][] = api_html_entity_decode(
$worksheet->setCellValueByColumnAndRow( strip_tags($row['first_name']),
$column, ENT_QUOTES,
$line, $charset
api_html_entity_decode(
strip_tags($row['first_name']),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
if ($officialCodeInList === 'true') { if ($officialCodeInList === 'true') {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['official_code']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['official_code']),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column++, strip_tags($row['username']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['username']),
ENT_QUOTES,
$charset
)
); );
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['email']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['email']),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags(
$line, implode(
api_html_entity_decode( ', ',
strip_tags( GroupManager:: get_user_group_name($row['user_id'])
implode( )
", ", ),
GroupManager:: get_user_group_name($row['user_id']) ENT_QUOTES,
) $charset
),
ENT_QUOTES,
$charset
)
); );
$column++;
if ($export_user_fields) { if ($export_user_fields) {
//show user fields data, if any, for this user // 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'], $row['user_id'],
false, false,
false, false,
false, false,
true true
); );
foreach ($user_fields_values as $value) { foreach ($values as $value) {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($value),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($value),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
} }
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['title']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['title']),
ENT_QUOTES,
$charset
)
); );
$column++; $list[$column][] = $row['start_date'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); $list[$column][] = $row['end_date'];
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']);
$column++;
$duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0; $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0;
$worksheet->setCellValueByColumnAndRow($column, $line, $duration); $list[$column][] = $duration;
$column++; $list[$column][] = $row['result'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['result']); $list[$column][] = $row['max'];
$column++; $list[$column][] = $row['status'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['max']); $list[$column][] = $row['lp_name'];
$list[$column][] = $row['is_user_subscribed'];
$column++; $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++;
} }
Export::arrayToXls($list, $filename);
$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);
return true; return true;
} }

@ -208,60 +208,28 @@ class HotpotatoesExerciseResult
$exercise_id, $exercise_id,
$hotpotato_name $hotpotato_name
); );
$filename = 'exercise_results_'.api_get_local_time().'.xls'; $filename = 'exercise_results_'.api_get_local_time();
if (!empty($user_id)) { 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' // check if exists column 'user'
$with_column_user = false; $withColumnUser = false;
foreach ($this->results as $result) { foreach ($this->results as $result) {
if (!empty($result['last_name']) && !empty($result['first_name'])) { if (!empty($result['last_name']) && !empty($result['first_name'])) {
$with_column_user = true; $withColumnUser = true;
break; break;
} }
} }
$list = [];
if ($with_column_user) { if ($withColumnUser) {
$worksheet->setCellValueByColumnAndRow( $list[0][] = get_lang('Email');
$column,
$line,
get_lang('Email')
);
$column++;
if (api_is_western_name_order()) { if (api_is_western_name_order()) {
$worksheet->setCellValueByColumnAndRow( $list[0][] = get_lang('FirstName');
$column, $list[0][] = get_lang('LastName');
$line,
get_lang('FirstName')
);
$column++;
$worksheet->setCellValueByColumnAndRow(
$column,
$line,
get_lang('LastName')
);
$column++;
} else { } else {
$worksheet->setCellValueByColumnAndRow( $list[0][] = get_lang('LastName');
$column, $list[0][] = get_lang('FirstName');
$line,
get_lang('LastName')
);
$column++;
$worksheet->setCellValueByColumnAndRow(
$column,
$line,
get_lang('FirstName')
);
$column++;
} }
} }
@ -278,157 +246,80 @@ class HotpotatoesExerciseResult
//show the fields names for user fields //show the fields names for user fields
foreach ($extra_user_fields as $field) { foreach ($extra_user_fields as $field) {
$worksheet->setCellValueByColumnAndRow( $list[0][] = api_html_entity_decode(
$column, strip_tags($field[3]),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($field[3]),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
} }
$worksheet->setCellValueByColumnAndRow( $list[0][] = get_lang('Title');
$column, $list[0][] = get_lang('StartDate');
$line, $list[0][] = get_lang('EndDate');
get_lang('Title') $list[0][] = get_lang('Duration').' ('.get_lang('MinMinutes').')';
); $list[0][] = get_lang('Score');
$column++; $list[0][] = get_lang('Total');
$worksheet->setCellValueByColumnAndRow( $list[0][] = get_lang('Status');
$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++;
$column = 1;
foreach ($this->results as $row) { foreach ($this->results as $row) {
$column = 0; if ($withColumnUser) {
$list[$column][] = api_html_entity_decode(
if ($with_column_user) { strip_tags($row['email']),
$worksheet->setCellValueByColumnAndRow( ENT_QUOTES,
$column, $charset
$line,
api_html_entity_decode(
strip_tags($row['email']),
ENT_QUOTES,
$charset
)
); );
$column++;
if (api_is_western_name_order()) { if (api_is_western_name_order()) {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['first_name']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['first_name']),
ENT_QUOTES,
$charset
)
); );
$column++; $list[$column][] = api_html_entity_decode(
$worksheet->setCellValueByColumnAndRow( strip_tags($row['last_name']),
$column, ENT_QUOTES,
$line, $charset
api_html_entity_decode(
strip_tags($row['last_name']),
ENT_QUOTES,
$charset
)
); );
$column++;
} else { } else {
$worksheet->setCellValueByColumnAndRow( $list[$column][] = api_html_entity_decode(
$column, strip_tags($row['last_name']),
$line, ENT_QUOTES,
api_html_entity_decode( $charset
strip_tags($row['last_name']),
ENT_QUOTES,
$charset
)
); );
$column++; $list[$column][] = api_html_entity_decode(
$worksheet->setCellValueByColumnAndRow( strip_tags($row['first_name']),
$column, ENT_QUOTES,
$line, $charset
api_html_entity_decode(
strip_tags($row['first_name']),
ENT_QUOTES,
$charset
)
); );
$column++;
} }
} }
if ($export_user_fields) { if ($export_user_fields) {
//show user fields data, if any, for this user //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'], $row['user_id'],
false, false,
false, false,
false, false,
true true
); );
foreach ($user_fields_values as $value) { foreach ($values as $value) {
$worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)); $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++; $list[$column][] = $row['start_date'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); $list[$column][] = $row['end_date'];
$column++; $list[$column][] = $row['duration'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']); $list[$column][] = $row['result'];
$column++; $list[$column][] = $row['max'];
$worksheet->setCellValueByColumnAndRow($column, $line, $row['duration']); $list[$column][] = $row['status'];
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, $row['result']);
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, $row['max']);
$column++;
$worksheet->setCellValueByColumnAndRow($column, $line, $row['status']);
$line++;
} }
$file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); Export::arrayToXls($list, $filename);
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
$writer->save($file);
DocumentManager::file_send_for_download($file, true, $filename);
return true; return true;
} }

@ -86,46 +86,31 @@ class GradeBookResult
*/ */
public function exportCompleteReportXLS($data) public function exportCompleteReportXLS($data)
{ {
$filename = 'gradebook-results-'.api_get_local_time().'.xlsx'; $filename = 'gradebook-results-'.api_get_local_time();
$list = [];
$spreadsheet = new PHPExcel();
$spreadsheet->setActiveSheetIndex(0);
$worksheet = $spreadsheet->getActiveSheet();
$line = 1;
$column = 0;
//headers //headers
foreach ($data[0] as $header_col) { foreach ($data[0] as $header_col) {
$worksheet->SetCellValueByColumnAndRow( $list[0][] = html_entity_decode(strip_tags($header_col));
$column,
$line,
html_entity_decode(strip_tags($header_col))
);
$column++;
} }
$line++;
$cant_students = count($data[1]); $cant_students = count($data[1]);
$line = 0;
for ($i = 0; $i < $cant_students; $i++) { for ($i = 0; $i < $cant_students; $i++) {
$column = 0; $column = 0;
foreach ($data[1][$i] as $col_name) { foreach ($data[1][$i] as $col_name) {
$worksheet->SetCellValueByColumnAndRow( $list[$column][$line] = html_entity_decode(strip_tags($col_name));
/*$worksheet->SetCellValueByColumnAndRow(
$column, $column,
$line, $line,
html_entity_decode(strip_tags($col_name)) html_entity_decode(strip_tags($col_name))
); );*/
$column++; $column++;
} }
$line++; $line++;
} }
$file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); Export::arrayToXls($list, $filename);
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
$writer->save($file); return true;
DocumentManager::file_send_for_download($file, true, $filename);
exit;
} }
/** /**

@ -3,8 +3,8 @@
use Chamilo\CoreBundle\Component\Editor\Connector; use Chamilo\CoreBundle\Component\Editor\Connector;
use Chamilo\CoreBundle\Component\Filesystem\Data; use Chamilo\CoreBundle\Component\Filesystem\Data;
use Ddeboer\DataImport\Writer\CsvWriter; use Port\Csv\CsvWriter;
use Ddeboer\DataImport\Writer\ExcelWriter; use Port\Excel\ExcelWriter;
use MediaAlchemyst\Alchemyst; use MediaAlchemyst\Alchemyst;
use MediaAlchemyst\DriversContainer; use MediaAlchemyst\DriversContainer;
use Neutron\TemporaryFilesystem\Manager; use Neutron\TemporaryFilesystem\Manager;

@ -9,7 +9,7 @@ $cidReset = true;
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
$this_section = SECTION_TRACKING; $this_section = SECTION_TRACKING;
$filename = 'reporting.xlsx'; $filename = 'reporting';
if (!api_is_allowed_to_create_course()) { if (!api_is_allowed_to_create_course()) {
api_not_allowed(true); api_not_allowed(true);
@ -212,37 +212,11 @@ $headers = [
]; ];
if (isset($_GET['export'])) { if (isset($_GET['export'])) {
global $charset; $list = [
$spreadsheet = new PHPExcel(); 0 => $headers,
$spreadsheet->setActiveSheetIndex(0); 1 => $array[0],
$worksheet = $spreadsheet->getActiveSheet(); ];
Export::arrayToXls($list, $filename);
$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);
exit; exit;
} }

Loading…
Cancel
Save