Skip to content

Commit

Permalink
Version 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fneumann committed Feb 29, 2024
1 parent ab3fd81 commit 5032bd4
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 176 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change log
# Change Log

## Version 1.3 (upcoming)
## Version 1.4 (2024-02-29)
- cleanup cross-task corrector assignments created by wrong imports
- improve corrector assignments export and import
- only login is used to identify writers and correctors
- create writer or corrector on the fly if login exists

## Version 1.3 (2024-02-24)
- unnumbered headlines schemes with one or three levels
- switch to allow browser spellcheck for writing a task
- fixed 0040291: Imagemagick error in edutiek on ubuntu 22.04
Expand Down
181 changes: 21 additions & 160 deletions classes/CorrectorAdmin/class.CorrectorAdminGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@
*/
class CorrectorAdminGUI extends BaseGUI
{

/** @var CorrectorAdminService */
protected $service;

/** @var CorrectionSettings */
protected $settings;
protected CorrectorAdminService $service;
protected CorrectorAssignmentsService $assignment_service;
protected CorrectionSettings $settings;

public function __construct(\ilObjLongEssayAssessmentGUI $objectGUI)
{
parent::__construct($objectGUI);
$this->service = $this->localDI->getCorrectorAdminService($this->object->getId());
$this->assignment_service = $this->localDI->getCorrectorAssignmentService($this->object->getId());
$this->settings = $this->localDI->getTaskRepo()->getCorrectionSettingsById($this->object->getId());
}

Expand Down Expand Up @@ -696,8 +694,6 @@ protected function confirmRemoveAuthorizationsAsync()

public function correctorAssignmentSpreadsheetImport()
{
$corrector_repo = $this->localDI->getCorrectorRepo();
$task_repo = $this->localDI->getTaskRepo();
$tempfile = new ilLongEssayAssessmentUploadTempFile($this->storage, $this->dic->filesystem(), $this->dic->upload());

$form = $this->uiFactory->input()->container()->form()->standard(
Expand All @@ -707,7 +703,8 @@ public function correctorAssignmentSpreadsheetImport()
$this->storage,
$tempfile
),
$this->plugin->txt("assignment_excel_import")
$this->plugin->txt("assignment_excel_import"),
$this->plugin->txt("assignment_excel_import_info")
)]
);

Expand All @@ -716,172 +713,36 @@ public function correctorAssignmentSpreadsheetImport()

if($data = $form->getData()) {
$filename = $data['excel'][0];
$needed_corr = $task_repo->getCorrectionSettingsById($this->object->getId())->getRequiredCorrectors();

try {
$spreadsheet = new CorrectorAssignmentExcel();
$spreadsheet->loadFromFile(\ilUtil::getDataDir() . '/temp/' . $filename);

$spreadsheet->setActiveSheet(1);
$corrector = [];
$r = 2;
while (($id = $spreadsheet->getCell($r, 0)) != null) {
$login = $spreadsheet->getCell($r, 1);
$corrector[$login] = $id;
$r++;
}
$spreadsheet->setActiveSheet(0);
$corrector_assignments = [];
$r = 2;
while (($id = $spreadsheet->getCell($r, 0)) != null) {
$assigned = [];
foreach(range(0, $needed_corr-1) as $pos) {
$login = $spreadsheet->getCell($r, 8+$pos);
$assigned[$pos] = (int) $corrector[$login] ?? CorrectorAdminService::BLANK_CORRECTOR_ASSIGNMENT;
}

$corrector_assignments[$id] = $assigned;
$r++;
}

foreach($corrector_assignments as $writer_id => $as) {
$fa = $as[0];
$sa = $needed_corr == 2 ? $as[1] : CorrectorAdminService::UNCHANGED_CORRECTOR_ASSIGNMENT;

$this->service->assignMultipleCorrector($fa, $sa, [$writer_id]);
}
$this->assignment_service->importAssignments(\ilUtil::getDataDir() . '/temp/' . $filename);
ilUtil::sendSuccess($this->plugin->txt("corrector_assignment_change_file_success"), true);
$tempfile->removeTempFile($filename);
ilUtil::sendSuccess($this->plugin->txt("corrector_assignment_changed"), true);
$this->ctrl->redirect($this);
} catch (\Exception $exception) {
}
catch (CorrectorAssignmentsException $exception) {
$tempfile->removeTempFile($filename);
ilUtil::sendFailure($this->plugin->txt("corrector_assignment_change_file_failure")
. '<p class="small">' . nl2br($exception->getMessage()), true) . '</p>';
}
catch (\Exception $exception) {
$tempfile->removeTempFile($filename);
ilUtil::sendFailure($this->plugin->txt("corrector_assignment_change_file_failure"), true);
ilUtil::sendFailure($this->plugin->txt("corrector_assignment_change_file_failure"));
}
}
}
ilUtil::sendInfo($this->plugin->txt("change_corrector_info"));

$this->tpl->setContent($this->renderer->render($form));
}

public function correctorAssignmentSpreadsheetExport()
{
$corrector_repo = $this->localDI->getCorrectorRepo();
$writer_repo = $this->localDI->getWriterRepo();
$task_repo = $this->localDI->getTaskRepo();
$essay_repo = $this->localDI->getEssayRepo();
$needed_corr = $task_repo->getCorrectionSettingsById($this->object->getId())->getRequiredCorrectors();
$participant_title = "Writer";
$corrector_title = "Corrector";
$locations = [];

foreach($task_repo->getLocationsByTaskId($this->object->getId()) as $location) {
$locations[$location->getId()] = $location;
try {
$this->assignment_service->exportAssignments($this->spreadsheetAssignmentToggle());
}

$essays = [];

foreach($essay_repo->getEssaysByTaskId($this->object->getId()) as $essay) {
$essays[$essay->getWriterId()] = $essay;
catch (Exception $e) {
// maybe logging
}
$corrector = [];
foreach($corrector_repo->getCorrectorsByTaskId($this->object->getId()) as $r) {
$corrector[$r->getId()] = $r;
}

$writer = $writer_repo->getWritersByTaskId($this->object->getId());

if($this->spreadsheetAssignmentToggle()) {

$authorized_user = [];
foreach($essays as $writer_id => $essay) {
if($essay->getWritingAuthorized() !== null) {
$authorized_user[] = $essay->getWriterId();
}
}
$writer = array_filter($writer, fn ($x) => in_array($x->getId(), $authorized_user));
}

$users = [];

foreach(\ilObjUser::_getUserData(array_merge(
array_map(fn (Corrector $x) => $x->getUserId(), $corrector),
array_map(fn (Writer $x) => $x->getUserId(), $writer),
)) as $u) {
$users[(int)$u['usr_id']] = $u;
}

$assignments = [];

foreach($corrector_repo->getAssignmentsByTaskId($this->object->getId()) as $assignment) {
$assignments[$assignment->getWriterId()][$assignment->getPosition()] = $assignment;
}
$r = 2;
$spreadsheet = new CorrectorAssignmentExcel();
$writer_sheet = $spreadsheet->addSheet($participant_title, true);
$corrector_sheet = $spreadsheet->addSheet($corrector_title, false);
$spreadsheet->setCell(1, 0, 'Id');
$spreadsheet->setCell(1, 1, 'Login');
$spreadsheet->setCell(1, 2, 'Firstname');
$spreadsheet->setCell(1, 3, 'Lastname');
$spreadsheet->setCell(1, 4, 'Email');
$spreadsheet->setCell(1, 5, 'Pseudonym');
$spreadsheet->setCell(1, 6, 'Location');
$spreadsheet->setCell(1, 7, 'Words');
foreach(range(0, $needed_corr-1) as $pos) {
$spreadsheet->setCell(1, 8+$pos, 'Corrector ' . ($pos+1));
}

foreach($writer as $w) {
$data = $users[$w->getUserId()] ?? [];
$ass = $assignments[$w->getId()] ?? [];
ksort($ass);
$essay = $essays[$w->getId()] ?? null;
$location = $essay !== null ? $locations[$essay->getLocation()] ?? null : null;
$written_text = $essay !== null ? $essay->getWrittenText() : "";
$location_text = $location !== null ? $location->getTitle() : "";

$spreadsheet->setCell($r, 0, $w->getId());
$spreadsheet->setCell($r, 1, $data['login'] ?? "");
$spreadsheet->setCell($r, 2, $data['firstname'] ?? "");
$spreadsheet->setCell($r, 3, $data['lastname'] ?? "");
$spreadsheet->setCell($r, 4, $data['email'] ?? "");
$spreadsheet->setCell($r, 5, $w->getPseudonym());
$spreadsheet->setCell($r, 6, $location_text);
$spreadsheet->setCell($r, 7, str_word_count($written_text));

foreach(range(0, $needed_corr-1) as $pos) {
$spreadsheet->addDropdownCol($r, 8+$pos, '=\''.$corrector_title.'\'!$B$2:$B$'.(count($corrector)+1));
}

foreach ($ass as $a) {
$c = $corrector[$a->getCorrectorId()] ?? null;
$login = $c !== null && isset($users[$c->getUserId()]) ? $users[$c->getUserId()]['login'] ?? '' : '';
$spreadsheet->setCell($r, 8+$a->getPosition(), $login);
}
$r++;
}
$r = 2;
$spreadsheet->setActiveSheet($corrector_sheet);
$spreadsheet->setCell(1, 0, "Id");
$spreadsheet->setCell(1, 1, 'Login');
$spreadsheet->setCell(1, 2, 'Firstname');
$spreadsheet->setCell(1, 3, 'Lastname');
$spreadsheet->setCell(1, 4, 'Email');

foreach($corrector as $c) {
$data = $users[$c->getUserId()] ?? [];
$ass = $assignments[$c->getId()] ?? [];
ksort($ass);

$spreadsheet->setCell($r, 0, $c->getId());
$spreadsheet->setCell($r, 1, $data['login'] ?? "");
$spreadsheet->setCell($r, 2, $data['firstname'] ?? "");
$spreadsheet->setCell($r, 3, $data['lastname'] ?? "");
$spreadsheet->setCell($r, 4, $data['email'] ?? "");
$r++;
}
$spreadsheet->setActiveSheet($corrector_sheet);
$spreadsheet->sendToClient("corrector_assignment.xlsx");
$this->ctrl->redirect($this);
}

Expand Down
17 changes: 14 additions & 3 deletions classes/CorrectorAdmin/class.CorrectorAdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,27 @@ public function getSettings() : CorrectionSettings

/**
* Get or create a writer object for an ILIAS user
* @param int $user_id
* @return Corrector
* A new corrector object is not yet saved
*/
public function getOrCreateCorrectorFromUserId(int $user_id) : Corrector
public function getCorrectorFromUserId(int $user_id) : Corrector
{
$corrector = $this->correctorRepo->getCorrectorByUserId($user_id, $this->settings->getTaskId());
if (!isset($corrector)) {
$corrector = Corrector::model();
$corrector->setUserId($user_id);
$corrector->setTaskId($this->settings->getTaskId());
}
return $corrector;
}

/**
* Get or create a writer object for an ILIAS user
* A new corrector object is already saved
*/
public function getOrCreateCorrectorFromUserId(int $user_id) : Corrector
{
$corrector = $this->getCorrectorFromUserId($user_id);
if (empty($corrector->getId())) {
$this->correctorRepo->save($corrector);
}
return $corrector;
Expand Down
35 changes: 35 additions & 0 deletions classes/CorrectorAdmin/class.CorrectorAssignmentExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,39 @@ public function addDropdownCol($a_row, $a_col, $a_target_formula)
$objValidation->setFormula1($a_target_formula);
$this->workbook->getActiveSheet()->getCellByColumnAndRow($a_row, $a_col)->setDataValidation(clone $objValidation);
}

/**
* Get the column titles in the forts row of the sheet
*/
public function getColumnTitlesFromActiveSheet() : array
{
$data = $this->getSheetAsArray();
return $data[0] ?? [];
}

/**
* Get an array of records below the first line
* Each record is an associative array with column title as key
* @return array[][]
*/
public function getAssocDataFromActiveSheet() : array
{
$data = $this->getSheetAsArray();
$columns = $this->getColumnTitlesFromActiveSheet();
$assoc = [];

for ($row = 1; $row <= count($data); $row++) {
$record = [];
foreach ($columns as $col => $label) {
if (!empty($label)) {
$record[$label] = $data[$row][$col] ?? '';
}
}
if (!empty($record)) {
$assoc[] = $record;
}
}
return $assoc;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ILIAS\Plugin\LongEssayAssessment\CorrectorAdmin;

use ilException;

class CorrectorAssignmentsException extends ilException
{
}
Loading

0 comments on commit 5032bd4

Please sign in to comment.