diff --git a/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterialAssignmentTableGUI.php b/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterialAssignmentTableGUI.php index 50bd54d9b64f..dac711fcd14a 100644 --- a/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterialAssignmentTableGUI.php +++ b/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterialAssignmentTableGUI.php @@ -1,6 +1,5 @@ @@ -36,6 +37,7 @@ public function __construct(object $a_parent_obj, ilObject $a_course_obj, int $a $this->objectDefinition = $DIC['objDefinition']; $this->objective_id = $a_objective_id; + $this->setId('tbl_crs_obj_mat_assignment'); parent::__construct($a_parent_obj, 'materialAssignment'); $this->lng->loadLanguageModule('crs'); @@ -46,7 +48,7 @@ public function __construct(object $a_parent_obj, ilObject $a_course_obj, int $a $this->setFormAction($this->ctrl->getFormAction($a_parent_obj)); $this->setRowTemplate("tpl.crs_objective_list_materials_row.html", "Modules/Course"); $this->setDefaultOrderField('title'); - $this->setLimit(200); + $this->setShowRowsSelector(true); $this->setNoEntriesText($this->lng->txt('crs_no_objective_lms_found')); $this->addCommandButton('updateMaterialAssignment', $this->lng->txt('crs_wiz_next')); $this->initObjectiveAssignments(); diff --git a/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterials.php b/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterials.php index fbea60a3480f..b4ceb26ac6cd 100644 --- a/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterials.php +++ b/Modules/Course/classes/Objectives/class.ilCourseObjectiveMaterials.php @@ -1,7 +1,5 @@ @@ -293,6 +293,26 @@ public function add(): int return $next_id; } + public function deleteMaterial(int $ref_id, int $obj_id): bool + { + $query = "DELETE FROM crs_objective_lm " . + "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " . + "AND ref_id = " . $this->db->quote($ref_id, 'integer') . " " . + "AND obj_id = " . $this->db->quote($obj_id, 'integer'); + $this->db->manipulate($query); + return true; + } + + public function isMaterialAssigned(int $ref_id, int $obj_id): bool + { + $query = "SELECT * FROM crs_objective_lm " . + "WHERE ref_id = " . $this->db->quote($ref_id, 'integer') . " " . + "AND obj_id = " . $this->db->quote($obj_id, 'integer') . " " . + "AND objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " "; + $res = $this->db->query($query); + return (bool) $res->numRows(); + } + public function delete(int $lm_id): bool { if (!$lm_id) { diff --git a/Modules/Course/classes/Objectives/class.ilCourseObjectivesGUI.php b/Modules/Course/classes/Objectives/class.ilCourseObjectivesGUI.php index ee1573530b03..53afb77b5599 100644 --- a/Modules/Course/classes/Objectives/class.ilCourseObjectivesGUI.php +++ b/Modules/Course/classes/Objectives/class.ilCourseObjectivesGUI.php @@ -1,7 +1,5 @@ tpl->setContent($table->getHTML()); } + /** + * @return int[] + */ + private function getIntArrayFromPost(string $key): array + { + if ($this->http->wrapper()->post()->has($key)) { + return $this->http->wrapper()->post()->retrieve( + $key, + $this->refinery->kindlyTo()->listOf( + $this->refinery->kindlyTo()->int() + ) + ); + } + return []; + } + + /** + * @return string[] + */ + private function getStringArrayFromPost(string $key): array + { + if ($this->http->wrapper()->post()->has($key)) { + return $this->http->wrapper()->post()->retrieve( + $key, + $this->refinery->kindlyTo()->listOf( + $this->refinery->kindlyTo()->string() + ) + ); + } + return []; + } + protected function updateMaterialAssignment(): void { if (!$this->access->checkAccess('write', '', $this->course_obj->getRefId())) { @@ -410,38 +442,36 @@ protected function updateMaterialAssignment(): void } $this->__initLMObject($this->initObjectiveIdFromQuery()); - $this->objectives_lm_obj->deleteAll(); - $materials = []; - if ($this->http->wrapper()->post()->has('materials')) { - $materials = $this->http->wrapper()->post()->retrieve( - 'materials', - $this->refinery->kindlyTo()->listOf( - $this->refinery->kindlyTo()->int() - ) - ); - } - foreach ($materials as $node_id) { - $obj_id = $this->objectDataCache->lookupObjId((int) $node_id); - $type = $this->objectDataCache->lookupType($obj_id); + $visibleMaterials = $this->getIntArrayFromPost('visible_materials'); + $visibleChapters = $this->getStringArrayFromPost('visible_chapters'); + $materials = $this->getIntArrayFromPost('materials'); + $chapters = $this->getStringArrayFromPost('chapters'); + foreach ($visibleMaterials as $node_id) { + $obj_id = $this->objectDataCache->lookupObjId((int) $node_id); + if (!in_array($node_id, $materials)) { + $this->objectives_lm_obj->deleteMaterial($node_id, $obj_id); + continue; + } + if ($this->objectives_lm_obj->isMaterialAssigned($node_id, $obj_id)) { + continue; + } $this->objectives_lm_obj->setLMRefId($node_id); $this->objectives_lm_obj->setLMObjId($obj_id); - $this->objectives_lm_obj->setType($type); + $this->objectives_lm_obj->setType($this->objectDataCache->lookupType($obj_id)); $this->objectives_lm_obj->add(); } - $chapters = []; - if ($this->http->wrapper()->post()->has('chapters')) { - $chapters = $this->http->wrapper()->post()->retrieve( - 'chapters', - $this->refinery->kindlyTo()->listOf( - $this->refinery->kindlyTo()->string() - ) - ); - } - foreach ($chapters as $chapter) { - list($ref_id, $chapter_id) = explode('_', $chapter); + foreach ($visibleChapters as $chapter) { + list($ref_id, $chapter_id) = explode('_', $chapter); + if (!in_array($chapter, $chapters)) { + $this->objectives_lm_obj->deleteMaterial($ref_id, $chapter_id); + continue; + } + if ($this->objectives_lm_obj->isMaterialAssigned($ref_id, $chapter_id)) { + continue; + } $this->objectives_lm_obj->setLMRefId($ref_id); $this->objectives_lm_obj->setLMObjId($chapter_id); $this->objectives_lm_obj->setType(ilLMObject::_lookupType($chapter_id)); diff --git a/Modules/Course/templates/default/tpl.crs_objective_list_materials_row.html b/Modules/Course/templates/default/tpl.crs_objective_list_materials_row.html index 135ca7d2c6ec..433676ac2288 100644 --- a/Modules/Course/templates/default/tpl.crs_objective_list_materials_row.html +++ b/Modules/Course/templates/default/tpl.crs_objective_list_materials_row.html @@ -1,7 +1,8 @@ - + +