|
| 1 | +<?php |
| 2 | +/* For licensing terms, see /license.txt */ |
| 3 | + |
| 4 | +/** |
| 5 | + * @package chamilo.admin |
| 6 | + */ |
| 7 | + |
| 8 | +// Language files that need to be included. |
| 9 | +$language_file = array('admin'); |
| 10 | + |
| 11 | +$cidReset = true; |
| 12 | +require_once '../inc/global.inc.php'; |
| 13 | + |
| 14 | +$this_section = SECTION_PLATFORM_ADMIN; |
| 15 | + |
| 16 | +$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; |
| 17 | + |
| 18 | +api_protect_admin_script(); |
| 19 | + |
| 20 | +// setting breadcrumbs |
| 21 | +$interbreadcrumb[] = array('url' => 'index.php','name' => get_lang('PlatformAdmin')); |
| 22 | + |
| 23 | +$tool_name = null; |
| 24 | + |
| 25 | +$action = isset($_GET['action']) ? $_GET['action'] : null; |
| 26 | +$field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null; |
| 27 | + |
| 28 | +if (empty($field_id)) { |
| 29 | + api_not_allowed(); |
| 30 | +} |
| 31 | +if (!in_array($type, ExtraField::getValidExtraFieldTypes())) { |
| 32 | + api_not_allowed(); |
| 33 | +} |
| 34 | + |
| 35 | +$extraField = new ExtraField($type); |
| 36 | +$extraFieldInfo = $extraField->get($field_id); |
| 37 | + |
| 38 | +$check = Security::check_token('request'); |
| 39 | +$token = Security::get_token(); |
| 40 | + |
| 41 | +if ($action == 'add') { |
| 42 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
| 43 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
| 44 | + $interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); |
| 45 | + $interbreadcrumb[]=array('url' => '#','name' => get_lang('Add')); |
| 46 | +} elseif ($action == 'edit') { |
| 47 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
| 48 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
| 49 | + $interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); |
| 50 | + |
| 51 | + $interbreadcrumb[]=array('url' => '#','name' => get_lang('Edit')); |
| 52 | +} else { |
| 53 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
| 54 | + $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
| 55 | + $interbreadcrumb[]=array('url' => '#','name' => get_lang('EditExtraFieldOptions')); |
| 56 | +} |
| 57 | + |
| 58 | +$roleId = isset($_REQUEST['roleId']) ? $_REQUEST['roleId'] : null; |
| 59 | + |
| 60 | +//jqgrid will use this URL to do the selects |
| 61 | +$params = 'field_id='.$field_id.'&type='.$extraField->type.'&roleId='.$roleId; |
| 62 | +$paramsNoRole = 'field_id='.$field_id.'&type='.$extraField->type; |
| 63 | + |
| 64 | +//The order is important you need to check the the $column variable in the model.ajax.php file |
| 65 | +$columns = array(get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions')); |
| 66 | + |
| 67 | +$htmlHeadXtra[]='<script> |
| 68 | +
|
| 69 | + function setHidden(obj) { |
| 70 | + var name = $(obj).attr("name"); |
| 71 | + var hiddenName = "hidden_" + name; |
| 72 | + if ($("#" + hiddenName).attr("value") == 1) { |
| 73 | + $("#" + hiddenName).attr("value", 0); |
| 74 | + } else { |
| 75 | + $("#" + hiddenName).attr("value", 1); |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + function changeStatus(obj) { |
| 80 | + var roleId = $(obj).find(":selected").val(); |
| 81 | + if (roleId != 0) { |
| 82 | + window.location.replace("'.api_get_self().'?'.$paramsNoRole.'&roleId="+roleId); |
| 83 | + } |
| 84 | + } |
| 85 | + $().ready( function() { |
| 86 | + $(".select_all").on("click", function() { |
| 87 | + $("#workflow :checkbox").prop("checked", 1); |
| 88 | + $("#workflow :hidden").prop("value", 1); |
| 89 | + return false; |
| 90 | + }); |
| 91 | + $(".unselect_all").on("click", function() { |
| 92 | + $("#workflow :checkbox").prop("checked", 0); |
| 93 | + $("#workflow :hidden").prop("value", 0); |
| 94 | + return false; |
| 95 | + }); |
| 96 | + }); |
| 97 | +</script>'; |
| 98 | + |
| 99 | +// The header. |
| 100 | +Display::display_header($tool_name); |
| 101 | + |
| 102 | +echo Display::page_header($extraFieldInfo['field_display_text']); |
| 103 | + |
| 104 | +$obj = new ExtraFieldOption($type); |
| 105 | +$columns = array('option_display_text', 'option_value', 'option_order'); |
| 106 | +$result = Database::select('*', $obj->table, array('where' => array("field_id = ? " => $field_id), 'order'=>"option_order ASC")); |
| 107 | + |
| 108 | +$table = new HTML_Table(array('class' => 'data_table')); |
| 109 | +$column = 0; |
| 110 | +$row = 0; |
| 111 | +$table->setHeaderContents($row, $column, get_lang('CurrentStatus')); |
| 112 | +$column++; |
| 113 | +foreach ($result as $item) { |
| 114 | + $table->setHeaderContents($row, $column, $item['option_display_text']); |
| 115 | + $column++; |
| 116 | +} |
| 117 | +$row++; |
| 118 | + |
| 119 | +$form = new FormValidator('workflow', 'post', api_get_self().'?'.$params); |
| 120 | +$options = api_get_user_roles(); |
| 121 | +$options[0] = get_lang('SelectAnOption'); |
| 122 | +ksort($options); |
| 123 | +$form->addElement('select', 'status', get_lang('SelectRole'), $options, array('onclick' => 'changeStatus(this)')); |
| 124 | + |
| 125 | +$checks = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')->findBy(array('fieldId' => $field_id, 'roleId' => $roleId)); |
| 126 | +$includedFields = array(); |
| 127 | +if (!empty($checks)) { |
| 128 | + foreach ($checks as $availableField) { |
| 129 | + $includedFields[$availableField->getFieldOptionId()][] = $availableField->getRelatedFieldOptionId(); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +foreach ($result as $item) { |
| 134 | + $column = 0; |
| 135 | + $table->setCellContents($row, $column, $item['option_display_text']); |
| 136 | + $column++; |
| 137 | + $value = null; |
| 138 | + |
| 139 | + foreach ($result as $itemCol) { |
| 140 | + $id = 'extra_field_status_'.$item['id'].'_'.$itemCol['id']; |
| 141 | + $idForm = 'extra_field_status['.$item['id'].']['.$itemCol['id'].']'; |
| 142 | + $attributes = array('onclick' => 'setHidden(this)'); |
| 143 | + $value = 0; |
| 144 | + |
| 145 | + if (isset($includedFields[$itemCol['id']]) && in_array($item['id'], $includedFields[$itemCol['id']])) { |
| 146 | + $value = 1; |
| 147 | + $attributes['checked'] = 'checked'; |
| 148 | + } |
| 149 | + |
| 150 | + $element = Display::input('checkbox', $id, null, $attributes); |
| 151 | + $table->setCellContents($row, $column, $element); |
| 152 | + $form->addElement('hidden', 'hidden_'.$idForm, $value, array('id' => 'hidden_'.$id)); |
| 153 | + $column++; |
| 154 | + } |
| 155 | + $row++; |
| 156 | +} |
| 157 | + |
| 158 | +if (!empty($roleId)) { |
| 159 | + $form->addElement('html', $table->toHtml()); |
| 160 | + $group = array(); |
| 161 | + $group[]= $form->createElement('button', 'submit', get_lang('Save')); |
| 162 | + $group[]= $form->createElement('button', 'select_all', get_lang('SelectAll'), array('class' => 'btn select_all')); |
| 163 | + $group[]= $form->createElement('button', 'unselect_all', get_lang('UnSelectAll'), array('class' => 'btn unselect_all')); |
| 164 | + $form->addGroup($group, '', null, ' '); |
| 165 | + |
| 166 | + $form->setDefaults(array('status' => $roleId)); |
| 167 | +} else { |
| 168 | + $form->addElement('button', 'submit', get_lang('Edit')); |
| 169 | +} |
| 170 | + |
| 171 | +$form->display(); |
| 172 | + |
| 173 | +if ($form->validate()) { |
| 174 | + $values = $form->getSubmitValues(); |
| 175 | + $result = $values['hidden_extra_field_status']; |
| 176 | + if (!empty($result)) { |
| 177 | + foreach ($result as $id => $items) { |
| 178 | + foreach ($items as $subItemId => $value) { |
| 179 | + $extraFieldOptionRelFieldOption = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')->findOneBy( |
| 180 | + array( |
| 181 | + 'fieldId' => $field_id, |
| 182 | + 'fieldOptionId' => $subItemId, |
| 183 | + 'roleId' => $roleId, |
| 184 | + 'relatedFieldOptionId' => $id |
| 185 | + ) |
| 186 | + ); |
| 187 | + |
| 188 | + if ($value == 1) { |
| 189 | + if (empty($extraFieldOptionRelFieldOption)) { |
| 190 | + $extraFieldOptionRelFieldOption = new \ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption(); |
| 191 | + $extraFieldOptionRelFieldOption->setFieldId($field_id); |
| 192 | + $extraFieldOptionRelFieldOption->setFieldOptionId($subItemId); |
| 193 | + $extraFieldOptionRelFieldOption->setRelatedFieldOptionId($id); |
| 194 | + $extraFieldOptionRelFieldOption->setRoleId($roleId); |
| 195 | + $app['orm.ems']['db_write']->persist($extraFieldOptionRelFieldOption); |
| 196 | + } |
| 197 | + } else { |
| 198 | + |
| 199 | + if ($extraFieldOptionRelFieldOption) { |
| 200 | + $app['orm.ems']['db_write']->remove($extraFieldOptionRelFieldOption); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | + } |
| 206 | + $app['orm.ems']['db_write']->flush(); |
| 207 | + header('Location:'.api_get_self().'?'.$params); |
| 208 | + exit; |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +Display :: display_footer(); |
0 commit comments