- cleaning and update * @author Julio Montoya - Improving the list of templates */ $this_section = SECTION_COURSES; api_protect_course_script(); api_protect_admin_script(true); $isStudentView = isset($_REQUEST['isStudentView']) ? $_REQUEST['isStudentView'] : null; $lpId = isset($_REQUEST['lp_id']) ? (int) $_REQUEST['lp_id'] : 0; $submit = isset($_POST['submit_button']) ? $_POST['submit_button'] : null; $type = isset($_GET['type']) ? $_GET['type'] : null; $action = isset($_GET['action']) ? $_GET['action'] : null; $is_allowed_to_edit = api_is_allowed_to_edit(null, false); $listUrl = api_get_path(WEB_CODE_PATH). 'lp/lp_controller.php?action=view&lp_id='.$lpId.'&'.api_get_cidreq().'&isStudentView=true'; if (!$is_allowed_to_edit) { header("Location: $listUrl"); exit; } /** @var learnpath $learnPath */ $learnPath = Session::read('oLP'); if (empty($learnPath)) { api_not_allowed(); } if ($learnPath->get_lp_session_id() != api_get_session_id()) { // You cannot edit an LP from a base course. header("Location: $listUrl"); exit; } if (api_is_in_gradebook()) { $interbreadcrumb[] = [ 'url' => Category::getUrl(), 'name' => get_lang('ToolGradebook'), ]; } $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']); $interbreadcrumb[] = [ 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(), 'name' => get_lang('LearningPaths'), ]; $interbreadcrumb[] = [ 'url' => api_get_self()."?action=build&lp_id=$lpId&".api_get_cidreq(), 'name' => $learnPath->getNameNoTags(), ]; switch ($type) { case 'dir': $interbreadcrumb[] = [ 'url' => 'lp_controller.php?action=add_item&type=step&lp_id='.$learnPath->get_id().'&'.api_get_cidreq(), 'name' => get_lang('NewStep'), ]; $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('NewChapter')]; break; case 'document': $interbreadcrumb[] = [ 'url' => 'lp_controller.php?action=add_item&type=step&lp_id='.$learnPath->get_id().'&'.api_get_cidreq(), 'name' => get_lang('NewStep'), ]; break; default: $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('NewStep')]; break; } if ($action === 'add_item' && $type === 'document') { $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('NewDocumentCreated')]; } // Theme calls. $show_learn_path = true; $lp_theme_css = $learnPath->get_theme(); $suredel = trim(get_lang('AreYouSureToDeleteJS')); ?> 'margin: 0px;'] ); $extraField['backTo'] = api_get_self().'?action=add_item&type=step&lp_id='.$lpId.'&'.api_get_cidreq(); $form->addHtml('
'); $extraFieldValue = new ExtraFieldValue('lp_item'); $form->addHeader(get_lang('LpByAuthor')); $default = []; $form->addHtml('
'); $defaultAuthor = []; foreach ($_SESSION['oLP']->items as $item) { $itemName = $item->name; $itemId = $item->iId; $extraFieldValues = $extraFieldValue->get_values_by_handler_and_field_variable($itemId, 'authorlpitem'); $priceItem = $extraFieldValue->get_values_by_handler_and_field_variable($itemId, 'price'); $authorName = []; if (!empty($extraFieldValues)) { if ($extraFieldValues != false) { $authors = explode(';', $extraFieldValues['value']); if (!empty($authors)) { foreach ($authors as $author) { if ($author != 0) { $defaultAuthor[$author] = $author; $teacher = api_get_user_info($author); $authorName[] = $teacher['complete_name']; } } } } } if (count($authorName) != 0) { $authorName = " (".implode(', ', $authorName).")"; } else { $authorName = ''; } if (isset($priceItem['value']) && !empty($priceItem['value'])) { $authorName .= "
".get_lang('Price')." (".$priceItem['value'].")"; } $form->addCheckBox( "itemSelected[$itemId]", null, Display::return_icon('lp_document.png', $itemName).$itemName.$authorName ); $default['itemSelected'][$itemId] = false; } $options = [0 => get_lang('RemoveSelected')]; $default['authorItemSelect'] = []; $form->addHtml('
'); $teachers = []; $field = new ExtraField('user'); $authorLp = $field->get_handler_field_info_by_field_variable('authorlp'); $extraFieldId = isset($authorLp['id']) ? (int) $authorLp['id'] : 0; if ($extraFieldId != 0) { $extraFieldValueUser = new ExtraFieldValue('user'); $arrayExtraFieldValueUser = $extraFieldValueUser->get_item_id_from_field_variable_and_field_value( 'authorlp', 1, true, false, true ); if (!empty($arrayExtraFieldValueUser)) { foreach ($arrayExtraFieldValueUser as $item) { $teacher = api_get_user_info($item['item_id']); $teachers[] = $teacher; } } } foreach ($teachers as $key => $value) { $authorId = $value['id']; $authorName = $value['complete_name']; if (!empty($authorName)) { $options[$authorId] = $authorName; } } $form->addSelect('authorItemSelect', get_lang('Authors'), $options, ['multiple' => 'multiple']); $form->addFloat('price', get_lang('Price')); $form->addHtml('
'); $form->addButtonCreate(get_lang('Send')); $form->setDefaults($default); if ($form->validate()) { if (isset($_GET['sub_action']) && ($_GET['sub_action'] === 'author_view')) { $authors = isset($_POST['authorItemSelect']) ? $_POST['authorItemSelect'] : []; $items = isset($_POST['itemSelected']) ? $_POST['itemSelected'] : []; $price = api_float_val($_POST['price']); unset($author); $saveExtraFieldItem = []; $saveAuthor = []; $removeExist = 0; foreach ($_SESSION['oLP']->items as $item) { $itemName = $item->name; $itemId = $item->iId; if (isset($items[$itemId])) { foreach ($authors as $author) { if ($author == 0 || $removeExist == 1) { $saveExtraFieldItem[$itemId][0] = 0; $removeExist = 1; } else { $saveExtraFieldItem[$itemId][$author] = $author; } } if ($price > 0) { $extraFieldValues = $extraFieldValue->get_values_by_handler_and_field_variable( $itemId, 'price' ); $extraFieldValue->save([ 'variable' => 'price', 'value' => $price, 'item_id' => $itemId, ]); } } } if (count($saveExtraFieldItem) > 0 || $price > 0) { $lastEdited = []; foreach ($saveExtraFieldItem as $saveItemId => $values) { $extraFieldValues = $extraFieldValue->get_values_by_handler_and_field_variable( $saveItemId, 'authorlpitem' ); $extraFieldValue->save([ 'variable' => 'authorlpitem', 'value' => $values, 'item_id' => $saveItemId, ]); $lastEdited = $values; if (isset($options[$author])) { $saveAuthor[] = $options[$author]; } } $saveAuthor = array_unique($saveAuthor); $messages = implode(' / ', $saveAuthor); $currentUrl = api_request_uri(); $redirect = false; if ($removeExist) { Display::addFlash(Display::return_message(get_lang('DeletedAuthors'))); $redirect = true; } elseif ($price > 0) { Display::addFlash(Display::return_message(get_lang('PriceUpdated'))); $redirect = true; } elseif (!empty($messages)) { Display::addFlash(Display::return_message(get_lang('RegisteredAuthors').' '.$messages)); $redirect = true; } if ($redirect) { api_location($currentUrl); } } } } Display::display_header(null, 'Path'); echo $learnPath->build_action_menu( false, true, false, true, '', $extraField ); echo '
'; echo '
'; echo $learnPath->return_new_tree(null, false); // Show the template list. if (($type === 'document' || $type === 'step') && !isset($_GET['file'])) { // Show the template list. echo '
'; echo '
'; } echo '
'; $form->display(); echo '
'; echo ''; Display::display_footer();