/**
  * @return bool
  */
 public function executeCommand()
 {
     if ($_GET['mode']) {
         $this->ctrl->saveParameter($this, 'mode');
         $this->ctrl->setParameterByClass("ildatacollectionrecordlistgui", "mode", $_GET['mode']);
     }
     $this->ctrl->saveParameter($this, 'redirect');
     if ($this->record_id) {
         $this->record = ilDataCollectionCache::getRecordCache($this->record_id);
         if (!$this->record->hasPermissionToEdit($this->parent_obj->ref_id) or !$this->record->hasPermissionToView($this->parent_obj->ref_id)) {
             $this->accessDenied();
         }
         $this->table = $this->record->getTable();
         $this->table_id = $this->table->getId();
     } else {
         $this->table = ilDataCollectionCache::getTableCache($this->table_id);
         if (!ilObjDataCollectionAccess::hasAddRecordAccess($_GET['ref_id'])) {
             $this->accessDenied();
         }
     }
     $cmd = $this->ctrl->getCmd();
     switch ($cmd) {
         default:
             $this->{$cmd}();
             break;
     }
     return true;
 }
 public function delete()
 {
     $mainTableId = $this->table->getCollectionObject()->getMainTableId();
     if ($mainTableId == $this->table->getId()) {
         ilUtil::sendFailure($this->lng->txt("dcl_cant_delete_main_table"), true);
     } else {
         $this->ctrl->setParameterByClass("ildatacollectionfieldlistgui", "table_id", $mainTableId);
     }
     $this->table->doDelete();
     $this->ctrl->redirectByClass("ildatacollectionfieldlistgui", "listFields");
 }
 /**
  * Import records from Excel file
  *
  * @param      $file
  * @param bool $simulate
  */
 private function importRecords($file, $simulate = false)
 {
     global $ilUser, $lng;
     include_once "./Modules/DataCollection/libs/ExcelReader/excel_reader2.php";
     $warnings = array();
     try {
         $excel = new Spreadsheet_Excel_Reader($file);
     } catch (Exception $e) {
         $warnings[] = $lng->txt("dcl_file_not_readable");
     }
     if (count($warnings)) {
         $this->endImport(0, $warnings);
         return;
     }
     $field_names = array();
     for ($i = 1; $i <= $excel->colcount(); $i++) {
         $field_names[$i] = $excel->val(1, $i);
     }
     $fields = $this->getImportFieldsFromTitles($field_names, $warnings);
     for ($i = 2; $i <= $excel->rowcount(); $i++) {
         $record = new ilDataCollectionRecord();
         $record->setTableId($this->table_obj->getId());
         $record->setOwner($ilUser->getId());
         $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
         $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
         $record->setTableId($this->table_id);
         if (!$simulate) {
             $record->doCreate();
         }
         foreach ($fields as $col => $field) {
             $value = $excel->val($i, $col);
             $value = utf8_encode($value);
             try {
                 if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
                     $old = $value;
                     $value = $this->getReferenceFromValue($field, $value);
                     if (!$value) {
                         $warnings[] = "(" . $i . ", " . $this->getExcelCharForInteger($col) . ") " . $lng->txt("dcl_no_such_reference") . " " . $old;
                     }
                 } else {
                     if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) {
                         $value = array('date' => date('Y-m-d', strtotime($value)), 'time' => '00:00:00');
                     }
                 }
                 $field->checkValidity($value, $record->getId());
                 if (!$simulate) {
                     $record->setRecordFieldValue($field->getId(), $value);
                 }
             } catch (ilDataCollectionInputException $e) {
                 $warnings[] = "(" . $i . ", " . $this->getExcelCharForInteger($col) . ") " . $e;
             }
         }
         if (!$simulate) {
             $record->doUpdate();
         }
         if ($i - 1 > $this->max_imports) {
             $warnings[] = $lng->txt("dcl_max_import") . ($excel->rowcount() - 1) . " > " . $this->max_imports;
             break;
         }
     }
     $this->endImport($i - 2, $warnings);
 }
 /**
  * @param bool $editComments
  */
 public function renderRecord($editComments = false)
 {
     global $ilTabs, $tpl, $ilCtrl, $lng;
     $rctpl = new ilTemplate("tpl.record_view.html", false, true, "Modules/DataCollection");
     $ilTabs->setTabActive("id_content");
     $view_id = self::_getViewDefinitionId($this->record_obj);
     if (!$view_id) {
         $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
     }
     // see ilObjDataCollectionGUI->executeCommand about instantiation
     include_once "./Modules/DataCollection/classes/class.ilDataCollectionRecordViewViewdefinitionGUI.php";
     $pageObj = new ilDataCollectionRecordViewViewdefinitionGUI($this->record_obj->getTableId(), $view_id);
     include_once "./Services/Style/classes/class.ilObjStyleSheet.php";
     $pageObj->setStyleId(ilObjStyleSheet::getEffectiveContentStyleId(0, "dcl"));
     $html = $pageObj->getHTML();
     $rctpl->addCss("./Services/COPage/css/content.css");
     $rctpl->fillCssFiles();
     $table = ilDataCollectionCache::getTableCache($this->record_obj->getTableId());
     foreach ($table->getRecordFields() as $field) {
         //ILIAS_Ref_Links
         $pattern = '/\\[dcliln field="' . preg_quote($field->getTitle(), "/") . '"\\](.*?)\\[\\/dcliln\\]/';
         if (preg_match($pattern, $html)) {
             $html = preg_replace($pattern, $this->record_obj->getRecordFieldSingleHTML($field->getId(), $this->setOptions("\$1")), $html);
         }
         //DataCollection Ref Links
         $pattern = '/\\[dclrefln field="' . preg_quote($field->getTitle(), "/") . '"\\](.*?)\\[\\/dclrefln\\]/';
         if (preg_match($pattern, $html)) {
             $this->currentField = $field;
             $html = preg_replace_callback($pattern, array($this, "doReplace"), $html);
         }
         $pattern = '/\\[ext tableOf="' . preg_quote($field->getTitle(), "/") . '" field="(.*?)"\\]/';
         if (preg_match($pattern, $html)) {
             $this->currentField = $field;
             $html = preg_replace_callback($pattern, array($this, "doExtReplace"), $html);
         }
         $html = str_ireplace("[" . $field->getTitle() . "]", $this->record_obj->getRecordFieldSingleHTML($field->getId()), $html);
     }
     foreach ($table->getStandardFields() as $field) {
         $html = str_ireplace("[" . $field->getId() . "]", $this->record_obj->getRecordFieldSingleHTML($field->getId()), $html);
     }
     $rctpl->setVariable("CONTENT", $html);
     //Permanent Link
     $perma_link = new ilPermanentLinkGUI("dcl", $_GET["ref_id"], "_" . $_GET['record_id']);
     $tpl->setVariable('PRMLINK', $perma_link->getHTML());
     // Buttons for previous/next records
     if ($this->is_enabled_paging) {
         $prevNextLinks = $this->renderPrevNextLinks();
         $rctpl->setVariable('PREV_NEXT_RECORD_LINKS', $prevNextLinks);
         $ilCtrl->clearParameters($this);
         // #14083
         $rctpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this));
         $rctpl->setVariable('RECORD', $lng->txt('dcl_record'));
         $rctpl->setVariable('RECORD_FROM_TOTAL', sprintf($lng->txt('dcl_record_from_total'), $this->current_record_position, count($this->record_ids)));
         $rctpl->setVariable('SELECT_OPTIONS', $this->renderSelectOptions());
     }
     // Edit Button
     if ($this->record_obj->hasPermissionToEdit((int) $_GET['ref_id'])) {
         $button = ilLinkButton::getInstance();
         $ilCtrl->setParameterByClass('ildatacollectionrecordeditgui', 'table_id', $this->table->getId());
         $ilCtrl->setParameterByClass('ildatacollectionrecordeditgui', 'redirect', ilDataCollectionRecordEditGUI::REDIRECT_DETAIL);
         $ilCtrl->saveParameterByClass('ildatacollectionrecordeditgui', 'record_id');
         $button->setUrl($ilCtrl->getLinkTargetByClass('ildatacollectionrecordeditgui', 'edit'));
         $button->setCaption($lng->txt('dcl_edit_record'), false);
         $rctpl->setVariable('EDIT_RECORD_BUTTON', $button->render());
     }
     // Comments
     if ($this->table->getPublicCommentsEnabled()) {
         $rctpl->setVariable('COMMENTS', $this->renderComments($editComments));
     }
     $tpl->setContent($rctpl->get());
 }
 /**
  * Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollection; $x->cloneStructure($id))
  *
  * @param $original_id The original ID of the dataselection you want to clone it's structure
  */
 public function cloneStructure($original_id)
 {
     $original = new ilObjDataCollection($original_id);
     $this->setApproval($original->getApproval());
     $this->setNotification($original->getNotification());
     $this->setPublicNotes($original->getPublicNotes());
     $this->setRating($original->getRating());
     // delete old tables.
     foreach ($this->getTables() as $table) {
         $table->doDelete(true);
     }
     // add new tables.
     foreach ($original->getTables() as $table) {
         $new_table = new ilDataCollectionTable();
         $new_table->setObjId($this->getId());
         $new_table->cloneStructure($table);
         if ($table->getId() == $original->getMainTableId()) {
             $this->setMainTableId($new_table->getId());
         }
     }
     // update because maintable id is now set.
     $this->doUpdate();
     // Set new field-ID of referenced fields
     foreach ($original->getTables() as $origTable) {
         foreach ($origTable->getRecordFields() as $origField) {
             if ($origField->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
                 $newRefId = NULL;
                 $origFieldRefObj = ilDataCollectionCache::getFieldCache($origField->getFieldRef());
                 $origRefTable = ilDataCollectionCache::getTableCache($origFieldRefObj->getTableId());
                 // Lookup the new ID of the referenced field in the actual DC
                 $tableId = ilDataCollectionTable::_getTableIdByTitle($origRefTable->getTitle(), $this->getId());
                 $fieldId = ilDataCollectionField::_getFieldIdByTitle($origFieldRefObj->getTitle(), $tableId);
                 $field = ilDataCollectionCache::getFieldCache($fieldId);
                 $newRefId = $field->getId();
                 // Set the new refID in the actual DC
                 $tableId = ilDataCollectionTable::_getTableIdByTitle($origTable->getTitle(), $this->getId());
                 $fieldId = ilDataCollectionField::_getFieldIdByTitle($origField->getTitle(), $tableId);
                 $field = ilDataCollectionCache::getFieldCache($fieldId);
                 $field->setPropertyvalue($newRefId, ilDataCollectionField::PROPERTYID_REFERENCE);
                 $field->doUpdate();
             }
         }
     }
 }
 /**
  * @param ilDataCollectionRecordListGUI $a_parent_obj
  * @param string                        $a_parent_cmd
  * @param ilDataCollectionTable         $table
  * @param int                           $mode
  */
 public function __construct(ilDataCollectionRecordListGUI $a_parent_obj, $a_parent_cmd, ilDataCollectionTable $table, $mode = ilDataCollectionRecordListGUI::MODE_VIEW)
 {
     global $lng, $ilCtrl;
     $identifier = 'dcl_rl' . $table->getId();
     $this->setPrefix($identifier);
     $this->setFormName($identifier);
     $this->setId($identifier);
     parent::__construct($a_parent_obj, $a_parent_cmd);
     $this->table = $table;
     $this->parent_obj = $a_parent_obj;
     $this->setRowTemplate("tpl.record_list_row.html", "Modules/DataCollection");
     $this->mode = $mode;
     // Setup columns and sorting columns
     if ($this->mode == ilDataCollectionRecordListGUI::MODE_MANAGE) {
         // Add checkbox columns
         $this->addColumn("", "", "1", true);
         $this->setSelectAllCheckbox("record_ids[]");
         $this->addMultiCommand("confirmDeleteRecords", $lng->txt('dcl_delete_records'));
     }
     if (ilDataCollectionRecordViewGUI::hasTableValidViewDefinition($this->table)) {
         $this->addColumn("", "_front", '15px');
     }
     $this->numeric_fields = array();
     foreach ($this->table->getVisibleFields() as $field) {
         $title = $field->getTitle();
         $sort_field = $title;
         if ($field->getId() == 'comments') {
             $sort_field = 'n_comments';
             $this->numeric_fields[] = $title;
         }
         $this->addColumn($title, $sort_field);
         if ($field->getLearningProgress()) {
             $this->addColumn($lng->txt("dcl_status"), "_status_" . $field->getTitle());
         }
         if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_NUMBER) {
             $this->numeric_fields[] = $title;
         }
     }
     $this->addColumn($lng->txt("actions"), "", "30px");
     $this->setTopCommands(true);
     $this->setEnableHeader(true);
     $this->setShowRowsSelector(true);
     $this->setShowTemplates(true);
     $this->setEnableHeader(true);
     $this->setEnableTitle(true);
     $this->setDefaultOrderDirection($this->table->getDefaultSortFieldOrder());
     // Set a default sorting?
     $default_sort_title = 'id';
     if ($fieldId = $this->table->getDefaultSortField()) {
         if (ilDataCollectionStandardField::_isStandardField($fieldId)) {
             /** @var $stdField ilDataCollectionStandardField */
             foreach (ilDataCollectionStandardField::_getStandardFields($this->table->getId()) as $stdField) {
                 if ($stdField->getId() == $fieldId) {
                     $default_sort_title = $stdField->getTitle();
                     break;
                 }
             }
         } else {
             $default_sort_title = ilDataCollectionCache::getFieldCache($fieldId)->getTitle();
         }
         $this->setDefaultOrderField($default_sort_title);
     }
     $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, "applyFilter"));
     $this->initFilter();
     $this->setStyle('table', $this->getStyle('table') . ' ' . 'dcl_record_list');
 }
 /**
  * @param ilDataCollectionTable $original
  */
 public function cloneStructure(ilDataCollectionTable $original)
 {
     $this->setTitle($original->getTitle());
     $this->setDescription($original->getDescription());
     $this->setIsVisible($original->getIsVisible());
     $this->setEditByOwner($original->getEditByOwner());
     $this->setAddPerm($original->getAddPerm());
     $this->setEditPerm($original->getEditPerm());
     $this->setDeletePerm($original->getDeletePerm());
     $this->setLimited($original->getLimited());
     $this->setLimitStart($original->getLimitStart());
     $this->setLimitEnd($original->getLimitEnd());
     $this->setViewOwnRecordsPerm($original->getViewOwnRecordsPerm());
     $this->setExportEnabled($original->getExportEnabled());
     $this->setPublicCommentsEnabled($original->getPublicCommentsEnabled());
     $this->setDefaultSortFieldOrder($original->getDefaultSortFieldOrder());
     $this->doCreate();
     // reset stdFields to get new for the created object
     $default_sort_field = 0;
     // Clone standard-fields
     $org_std_fields = $original->getStandardFields();
     foreach ($this->getStandardFields() as $element_key => $std_field) {
         $std_field->cloneStructure($org_std_fields[$element_key]);
         if ($std_field->getId() == $original->getDefaultSortField()) {
             $default_sort_field = $std_field->getId();
         }
     }
     // Clone fields
     $new_fields = array();
     foreach ($original->getFields() as $orig_field) {
         if (!$orig_field->isStandardField()) {
             $new_field = new ilDataCollectionField();
             $new_field->setTableId($this->getId());
             $new_field->cloneStructure($orig_field->getId());
             $new_fields[$orig_field->getId()] = $new_field;
             if ($orig_field->getId() == $original->getDefaultSortField()) {
                 $default_sort_field = $new_field->getId();
             }
         }
     }
     $this->setDefaultSortField($default_sort_field);
     $this->doUpdate();
     //TODO: Find better way to copy data (include referenced data)
     // Clone Records with recordfields
     /*foreach($original->getRecords() as $orig_record){
     			$new_record = new ilDataCollectionRecord();
     			$new_record->setTableId($this->getId());
     			$new_record->cloneStructure($orig_record->getId(), $new_fields);
     		}*/
     if ($old_view_id = ilDataCollectionRecordViewViewdefinition::getIdByTableId($original->getId())) {
         $old_view = new ilDataCollectionRecordViewViewdefinition($old_view_id);
         $old_view->setTableId($original->getId());
         $viewdef = new ilDataCollectionRecordViewViewdefinition();
         $viewdef->setTableId($this->id);
         $viewdef->setXMLContent($old_view->getXMLContent(false));
         $viewdef->create();
     }
 }