Skip to content

Commit fee398f

Browse files
committed
add import xlsx file
1 parent 986c737 commit fee398f

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# moodle2xlsx
1+
# moodle-qformat_xlsx
22

3-
`moodle2xlsx` is a plugin for Moodle that provides seamless integration for importing and exporting XLSX files. This plugin enhances the functionality of Moodle by allowing users to easily upload and download XLSX files, which can be particularly useful for managing course data, student information, grades, and more.
3+
`moodle-qformat_xlsx` is a plugin for Moodle that provides seamless integration for importing and exporting XLSX files. This plugin enhances the functionality of Moodle by allowing users to easily upload and download XLSX files, which can be particularly useful for managing course data, student information, grades, and more.
44

55
## Features
66

@@ -13,21 +13,21 @@
1313

1414
1. **Download the Plugin**: Clone this repository or download the ZIP file.
1515
```
16-
git clone https://github.com/michael2to3/moodle2xlsx.git
16+
git clone https://github.com/michael2to3/moodle-qformat_xlsx.git
1717
```
1818
2. **Install the Plugin on Moodle**:
1919
- Navigate to your Moodle installation's `admin/tool/installaddon/index.php` page.
2020
- Upload the downloaded plugin ZIP file.
2121
- Follow the on-screen instructions to complete the installation.
2222
2323
3. **Verify Installation**:
24-
- After installation, 'moodle2xlsx' should be listed under the Plugins section in Moodle.
24+
- After installation, 'moodle-qformat_xlsx' should be listed under the Plugins section in Moodle.
2525
2626
## Usage
2727
2828
### Importing XLSX Files
2929
30-
1. Navigate to the `moodle2xlsx` import section in Moodle.
30+
1. Navigate to the `moodle-qformat_xlsx` import section in Moodle.
3131
2. Choose the XLSX file you want to import.
3232
3. Map the columns in your XLSX file to the corresponding fields in Moodle.
3333
4. Click on 'Import' to complete the process.
@@ -45,7 +45,7 @@ No additional configuration is needed after installation. However, you can modif
4545
4646
## Support
4747
48-
For issues, feature requests, or contributions, please use the [Issues](https://github.com/michael2to3/moodle2xlsx/issues) section of this repository.
48+
For issues, feature requests, or contributions, please use the [Issues](https://github.com/michael2to3/moodle-qformat_xlsx/issues) section of this repository.
4949
5050
## License
5151

format.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@
66
require_once "$CFG->dirroot/lib/uploadlib.php";
77
require_once "$CFG->dirroot/question/format/xml/format.php";
88
require_once "$CFG->dirroot/lib/excellib.class.php";
9+
use moodle_exception;
910

10-
class qformat_xlsxtable extends qformat_xml
11+
class qformat_xlsxtable extends qformat_default
1112
{
1213
private $lessonquestions = [];
1314

1415

16+
public function provide_import()
17+
{
18+
return true;
19+
20+
}//end provide_import()
21+
22+
23+
public function provide_export()
24+
{
25+
return true;
26+
27+
}//end provide_export()
28+
29+
1530
public function mime_type()
1631
{
1732
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
@@ -30,10 +45,39 @@ public function validate_file(stored_file $file): string
3045
}//end validate_file()
3146

3247

33-
public function importpreprocess()
48+
public function readquestions($data)
3449
{
50+
$qa = [];
51+
foreach ($data as $i => $question) {
52+
$name = $question[0];
53+
$questiontext = $question[1];
54+
$answer = $question[2];
55+
if (empty($name) || empty($questiontext) || empty($answer)) {
56+
debugging('Skipping question '.$i, DEBUG_DEVELOPER);
57+
continue;
58+
}
59+
60+
$q = $this->defaultquestion();
61+
$q->id = $i;
62+
$q->name = $name;
63+
$q->questiontext = $questiontext;
64+
$q->qtype = 'shortanswer';
65+
$q->feedback = [
66+
0 => [
67+
'text' => ' ',
68+
'format' => FORMAT_HTML,
69+
]
70+
];
3571

36-
}//end importpreprocess()
72+
$q->fraction = [1];
73+
$q->answer = [$answer];
74+
$qa[] = $q;
75+
}//end foreach
76+
77+
debugging('qa: '.print_r($qa, true), DEBUG_DEVELOPER);
78+
return $qa;
79+
80+
}//end readquestions()
3781

3882

3983
public function export_file_extension()
@@ -59,11 +103,12 @@ public function presave_process($content)
59103

60104
$workbook = new MoodleExcelWorkbook($this->filename);
61105
$worksheet = $workbook->add_worksheet('Questions');
62-
$answers = $question->options->answers;
63106
foreach ($this->lessonquestions as $rowIndex => $question) {
64-
$worksheet->write($rowIndex, 0, $question->questiontext);
65-
foreach ($answers as $answer) {
66-
$worksheet->write($rowIndex, 1, $answer->answer);
107+
$worksheet->write($rowIndex, 0, $question->name);
108+
$worksheet->write($rowIndex, 1, $question->questiontext);
109+
$answers = $question->options->answers;
110+
foreach ($answers as $a) {
111+
$worksheet->write($rowIndex, 2, $a->answer);
67112
}
68113
}
69114

@@ -93,13 +138,21 @@ public function readdata($filename)
93138
$data = [];
94139
foreach ($worksheet->getRowIterator() as $row) {
95140
$cellIterator = $row->getCellIterator();
96-
$cellIterator->setIterateOnlyExistingCells(false);
97-
$rowData = [];
141+
$cellIterator->setIterateOnlyExistingCells(true);
142+
$rowData = [];
143+
$rowEmpty = true;
98144
foreach ($cellIterator as $cell) {
99-
$rowData[] = $cell->getValue();
145+
$value = $cell->getValue();
146+
if (!empty($value)) {
147+
$rowEmpty = false;
148+
}
149+
150+
$rowData[] = $value;
100151
}
101152

102-
$data[] = $rowData;
153+
if (!$rowEmpty) {
154+
$data[] = $rowData;
155+
}
103156
}
104157

105158
return $data;

lang/en/qformat_xlsxtable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
$string['htmldocnotsupported'] = 'Incorrect XLSX format: please use <i>File>Save As...</i> to save <b>{$a}</b> in native XLSX 2010 (.xlsx) format and import again';
88
$string['htmlnotsupported'] = 'Files in HTML format not supported: <b>{$a}</b>';
99
$string['noquestions'] = 'No questions to export';
10-
$string['pluginname'] = 'Microsoft XLSX 2008 table format (xlsxtable)';
11-
$string['pluginname_help'] = 'This is a front-end for converting Microsoft XLSX 2008 files into Moodle Question XML format for import, and converting Moodle Question XML format into a format suitable for editing in Microsoft XLSX.';
10+
$string['pluginname'] = 'Microsoft XLSX table format';
11+
$string['pluginname_help'] = 'This is a front-end for converting Microsoft XLSX files into Moodle Question XML format for import, and converting Moodle Question XML format into a format suitable for editing in Microsoft XLSX.';
1212
$string['pluginname_link'] = 'qformat/xlsxtable';
1313
$string['preview_question_not_found'] = 'Preview question not found, name / course ID: {$a}';
1414
$string['privacy:metadata'] = 'The XLSXTable question format plugin does not store any personal data.';
1515
$string['stylesheetunavailable'] = 'XSLT Stylesheet <b>{$a}</b> is not available';
1616
$string['transformationfailed'] = 'XSLT transformation failed (<b>{$a}</b>)';
17-
$string['xlsxtable'] = 'Microsoft XLSX 2008 table format (xlsxtable)';
18-
$string['xlsxtable_help'] = 'This is a front-end for converting Microsoft XLSX 2008 files into Moodle Question XML format for import, and converting Moodle Question XML format into an enhanced XHTML format for exporting into a format suitable for editing in Microsoft XLSX.';
17+
$string['xlsxtable'] = 'Microsoft XLSX table format';
18+
$string['xlsxtable_help'] = 'This is a front-end for converting Microsoft XLSX files into Moodle Question XML format for import, and converting Moodle Question XML format into an enhanced XHTML format for exporting into a format suitable for editing in Microsoft XLSX.';
1919
$string['xmlnotsupported'] = 'Files in XML format not supported: <b>{$a}</b>';
2020
$string['xsltunavailable'] = 'You need the XSLT library installed in PHP to save this XLSX file';
2121

@@ -41,7 +41,7 @@
4141
$string['xlsx_import'] = 'Import';
4242
$string['xlsx_multiple_answer'] = 'Multiple answer';
4343
$string['xlsx_new_question_file'] = 'New Question File';
44-
$string['xlsx_new_question_file_screentip'] = 'Questions must be saved in XLSX 2008 (.xlsx) format';
44+
$string['xlsx_new_question_file_screentip'] = 'Questions must be saved in XLSX (.xlsx) format';
4545
$string['xlsx_new_question_file_supertip'] = 'Each XLSX file may contain multiple categories';
4646
$string['xlsx_setunset_assessment_view'] = 'Set/Unset Assessment View';
4747
$string['xlsx_showhide_assessment_screentip'] = 'Show question metadata to edit, hide to preview printed assessment';

lang/ru/qformat_xlsxtable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
$string['htmldocnotsupported'] = 'Неверный формат XLSX: пожалуйста, используйте <i>Файл>Сохранить как...</i> для сохранения <b>{$a}</b> в нативном формате XLSX 2010 (.xlsx) и повторно импортируйте';
77
$string['htmlnotsupported'] = 'Файлы в формате HTML не поддерживаются: <b>{$a}</b>';
88
$string['noquestions'] = 'Нет вопросов для экспорта';
9-
$string['pluginname'] = 'Формат таблицы Microsoft XLSX 2008 (xlsxtable)';
10-
$string['pluginname_help'] = 'Это интерфейс для конвертации файлов Microsoft XLSX 2008 в формат XML вопросов Moodle для импорта, а также для конвертации формата XML вопросов Moodle в формат, подходящий для редактирования в Microsoft XLSX.';
9+
$string['pluginname'] = 'Формат таблицы Microsoft XLSX 2008';
10+
$string['pluginname_help'] = 'Это интерфейс для конвертации файлов Microsoft XLSX в формат XML вопросов Moodle для импорта, а также для конвертации формата XML вопросов Moodle в формат, подходящий для редактирования в Microsoft XLSX.';
1111
$string['pluginname_link'] = 'qformat/xlsxtable';
1212
$string['preview_question_not_found'] = 'Предварительный просмотр вопроса не найден, имя / ID курса: {$a}';
1313
$string['privacy:metadata'] = 'Плагин формата вопросов XLSXTable не хранит личных данных.';
1414
$string['stylesheetunavailable'] = 'Таблица стилей XSLT <b>{$a}</b> недоступна';
1515
$string['transformationfailed'] = 'Преобразование XSLT не удалось (<b>{$a}</b>)';
16-
$string['xlsxtable'] = 'Формат таблицы Microsoft XLSX 2008 (xlsxtable)';
17-
$string['xlsxtable_help'] = 'Это интерфейс для конвертации файлов Microsoft XLSX 2008 в формат XML вопросов Moodle для импорта, а также для конвертации формата XML вопросов Moodle в улучшенный формат XHTML для экспорта в формат, подходящий для редактирования в Microsoft XLSX.';
16+
$string['xlsxtable'] = 'Формат таблицы Microsoft XLSX 2008';
17+
$string['xlsxtable_help'] = 'Это интерфейс для конвертации файлов Microsoft XLSX в формат XML вопросов Moodle для импорта, а также для конвертации формата XML вопросов Moodle в улучшенный формат XHTML для экспорта в формат, подходящий для редактирования в Microsoft XLSX.';
1818
$string['xmlnotsupported'] = 'Файлы в формате XML не поддерживаются: <b>{$a}</b>';
1919
$string['xsltunavailable'] = 'Вам нужна библиотека XSLT, установленная в PHP, чтобы сохранить этот файл XLSX';
2020

@@ -37,7 +37,7 @@
3737
$string['xlsx_import'] = 'Импорт';
3838
$string['xlsx_multiple_answer'] = 'Несколько ответов';
3939
$string['xlsx_new_question_file'] = 'Новый файл вопроса';
40-
$string['xlsx_new_question_file_screentip'] = 'Вопросы должны быть сохранены в формате XLSX 2008 (.xlsx)';
40+
$string['xlsx_new_question_file_screentip'] = 'Вопросы должны быть сохранены в формате XLSX (.xlsx)';
4141
$string['xlsx_new_question_file_supertip'] = 'Каждый файл XLSX может содержать несколько категорий';
4242
$string['xlsx_setunset_assessment_view'] = 'Установить/Снять режим оценки';
4343
$string['xlsx_showhide_assessment_screentip'] = 'Показать метаданные вопроса для редактирования, скрыть для предварительного просмотра напечатанной оценки';

0 commit comments

Comments
 (0)