From 7a4bdcf3867cc853e00146f2d93fdfa2ac4d96c7 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Tue, 25 Aug 2015 16:48:36 -0500 Subject: [PATCH] Fix Configuration page with new DB structure - refs #7768 --- plugin/buycourses/js/buycourses.js | 44 +++--- plugin/buycourses/src/BuyCoursesUtils.php | 146 ------------------ plugin/buycourses/src/buy_course.lib.php | 25 --- .../src/buy_course_plugin.class.php | 138 ++++++++++++++--- plugin/buycourses/src/configuration.php | 19 --- plugin/buycourses/src/function.php | 86 ++++++++--- plugin/buycourses/view/configuration.tpl | 90 ++++++----- 7 files changed, 250 insertions(+), 298 deletions(-) diff --git a/plugin/buycourses/js/buycourses.js b/plugin/buycourses/js/buycourses.js index 2c0bf8fbd5..7aefbd4cf4 100644 --- a/plugin/buycourses/js/buycourses.js +++ b/plugin/buycourses/js/buycourses.js @@ -22,42 +22,34 @@ $(document).ready(function () { }); }); - $("input[name='visible']").change(function () { - $(this).parent().next().next().children().attr("style", "display:none"); - $(this).parent().next().next().children().next().attr("style", "display:''"); - $(this).parent().parent().addClass("fmod"); - $(this).parent().parent().children().each(function () { - $(this).addClass("btop"); - }); - }); - - $(".save").click(function () { + $(".bc-button-save").click(function () { var currentRow = $(this).closest("tr"); - var courseOrSessionObject ={ + var courseOrSessionObject = { tab: "save_mod", visible: currentRow.find("[name='visible']").is(':checked') ? 1 : 0, price: currentRow.find("[name='price']").val() }; - var course_id = $(this).attr('id'); - var courseOrSession = ($(this).closest("td").attr('id')).indexOf("session") > -1 ? "session_id" : "course_id"; + var itemField = currentRow.data('type') + '_id'; - courseOrSessionObject[courseOrSession] = course_id; + courseOrSessionObject[itemField] = currentRow.data('item') || 0; - $.post("function.php", courseOrSessionObject, + $.post( + "function.php", + courseOrSessionObject, function (data) { - if (data.status == "false") { - alert("Database Error"); - } else { - courseOrSession = courseOrSession.replace("_id", ""); - $("#" + courseOrSession + data.course_id).children().attr("style", "display:''"); - $("#" + courseOrSession + data.course_id).children().next().attr("style", "display:none"); - $("#" + courseOrSession + data.course_id).parent().removeClass("fmod") - $("#" + courseOrSession + data.course_id).parent().children().each(function () { - $(this).removeClass("btop"); - }); + if (!data.status) { + return; } - }, "json"); + + currentRow.addClass('success'); + + window.setTimeout(function () { + currentRow.removeClass('success'); + }, 3000); + }, + "json" + ); }); $('#sync').click(function (e) { diff --git a/plugin/buycourses/src/BuyCoursesUtils.php b/plugin/buycourses/src/BuyCoursesUtils.php index 52bf1b4aee..b77f61d98b 100644 --- a/plugin/buycourses/src/BuyCoursesUtils.php +++ b/plugin/buycourses/src/BuyCoursesUtils.php @@ -186,150 +186,4 @@ class BuyCoursesUtils return $form; } - /** - * Sync the courses and sessions - */ - public static function sync() - { - $buySessionCourseTable = Database::get_main_table(TABLE_BUY_SESSION_COURSE); - $sessionCourseTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); - $tableBuyCourse = Database::get_main_table(TABLE_BUY_COURSE); - $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE); - $tableBuySession = Database::get_main_table(TABLE_BUY_SESSION); - $tableSession = Database::get_main_table(TABLE_MAIN_SESSION); - - Database::update( - $buySessionCourseTable, - ['sync' => 0] - ); - - $sql = " - SELECT session_id, c_id, nbr_users - FROM $sessionCourseTable"; - $res = Database::query($sql); - - while ($row = Database::fetch_assoc($res)) { - $sql = " - SELECT 1 FROM $buySessionCourseTable - WHERE session_id=" . $row['session_id']; - $result = Database::query($sql); - - if (Database::affected_rows($result) > 0) { - Database::update( - $buySessionCourseTable, - ['sync' => 1], - ['session_id = ?' => $row['session_id']] - ); - } else { - $courseCode = api_get_course_info_by_id($row['c_id'])['code']; - Database::insert( - $buySessionCourseTable, - [ - 'session_id' => $row['session_id'], - 'course_code' => $courseCode, - 'nbr_users' => $row['nbr_users'], - 'sync' => 1 - ] - ); - } - } - - Database::delete( - $buySessionCourseTable, - ['sync = ?' => 0] - ); - - Database::update( - $tableBuyCourse, - ['sync' => 0] - ); - - $sql = "SELECT id, code, title FROM $tableCourse"; - $res = Database::query($sql); - - while ($row = Database::fetch_assoc($res)) { - $sql = " - SELECT session_id FROM $buySessionCourseTable - WHERE course_code = '" . $row['code'] . "' LIMIT 1"; - $courseIdSession = Database::fetch_assoc(Database::query($sql))['session_id']; - - if (!is_numeric($courseIdSession)) { - $courseIdSession = 0; - } - - $sql = " - SELECT 1 FROM $tableBuyCourse - WHERE course_id='" . $row['id'] . "'"; - $result = Database::query($sql); - - if (Database::affected_rows($result) > 0) { - Database::update( - $tableBuyCourse, - [ - 'sync' => 1, - 'session_id' => $courseIdSession - ], - ['course_id = ?' => $row['id']] - ); - } else { - Database::insert( - $tableBuyCourse, - [ - 'session_id' => $courseIdSession, - 'course_id' => $row['id'], - 'code' => $row['code'], - 'title' => $row['title'], - 'visible' => 0, - 'sync' => 1 - ] - ); - } - } - - Database::delete( - $tableBuyCourse, - ['sync = ?' => 0] - ); - - Database::update( - $tableBuySession, - ['sync' => 0] - ); - - $sql = " - SELECT id, name, access_start_date, access_end_date - FROM $tableSession"; - $res = Database::query($sql); - - while ($row = Database::fetch_assoc($res)) { - $sql = "SELECT 1 FROM $tableBuySession WHERE session_id='" . $row['id'] . "';"; - $result = Database::query($sql); - - if (Database::affected_rows($result) > 0) { - Database::update( - $tableBuySession, - ['sync' => 1], - ['session_id = ?' => $row['id']] - ); - } else { - Database::insert( - $tableBuySession, - [ - 'session_id' => $row['id'], - 'name' => $row['name'], - 'date_start' => $row['access_start_date'], - 'date_end' => $row['access_end_date'], - 'visible' => 0, - 'sync' => 1 - ] - ); - } - } - - Database::delete( - $tableBuySession, - ['sync = ?' => 0] - ); - } - } diff --git a/plugin/buycourses/src/buy_course.lib.php b/plugin/buycourses/src/buy_course.lib.php index d6712fdfce..ddab69d2b3 100644 --- a/plugin/buycourses/src/buy_course.lib.php +++ b/plugin/buycourses/src/buy_course.lib.php @@ -71,31 +71,6 @@ function buyCourseListCategories() return $aux; } -/** - * Return an icon representing the visibility of the course - * @param int $option The course visibility - * @return string HTML string of the visibility icon - */ -function getCourseVisibilityIcon($option) -{ - $style = 'margin-bottom:-5px;margin-right:5px;'; - switch ($option) { - case 0: - return Display::return_icon('bullet_red.gif', get_plugin_lang('CourseVisibilityClosed', 'BuyCoursesPlugin'), array('style' => $style)); - break; - case 1: - return Display::return_icon('bullet_orange.gif', get_plugin_lang('Private', 'BuyCoursesPlugin'), array('style' => $style)); - break; - case 2: - return Display::return_icon('bullet_green.gif', get_plugin_lang('OpenToThePlatform', 'BuyCoursesPlugin'), array('style' => $style)); - break; - case 3: - return Display::return_icon('bullet_blue.gif', get_plugin_lang('OpenToTheWorld', 'BuyCoursesPlugin'), array('style' => $style)); - break; - default: - return Display::return_icon('bullet_grey.gif', get_plugin_lang('CourseVisibilityHidden', 'BuyCoursesPlugin'), array('style' => $style)); - } -} /** * Gets the list of accounts from the buy_course_transfer table * @return array The list of accounts diff --git a/plugin/buycourses/src/buy_course_plugin.class.php b/plugin/buycourses/src/buy_course_plugin.class.php index 7a87e0ee31..c4d584daef 100644 --- a/plugin/buycourses/src/buy_course_plugin.class.php +++ b/plugin/buycourses/src/buy_course_plugin.class.php @@ -207,20 +207,64 @@ class BuyCoursesPlugin extends Plugin */ public function getCourses() { - $buyCourseTable = Database::get_main_table(TABLE_BUY_COURSE); $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); + $sessionCourseTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); + $buyItemTable = Database::get_main_table(BuyCoursesUtils::TABLE_ITEM); + $buyCurrencyTable = Database::get_main_table(BuyCoursesUtils::TABLE_CURRENCY); + + $currency = $this->getSelectedCurrency(); + + $items = []; + + $fakeCourseFrom = " + $courseTable c + LEFT JOIN $sessionCourseTable sc + ON c.id = sc.c_id + WHERE sc.c_id IS NULL + "; + + $courses = Database::select('c.*', $fakeCourseFrom); + + $fakeItemFrom = " + $buyItemTable i + INNER JOIN $buyCurrencyTable c + ON i.currency_id = c.id + "; + + foreach ($courses as $course) { + $courseItem = [ + 'course_id' => $course['id'], + 'course_visual_code' => $course['visual_code'], + 'course_code' => $course['code'], + 'course_title' => $course['title'], + 'course_visibility' => $course['visibility'], + 'visible' => false, + 'currency' => empty($currency) ? null : $currency['iso_code'], + 'price' => 0.00 + ]; - $courses = Database::select( - ['a.course_id', 'a.visible', 'a.price', 'b.*'], - "$buyCourseTable a, $courseTable b", - [ - 'where' => [ - 'a.course_id = b.id AND a.session_id = ?' => [0] - ] - ] - ); + $item = Database::select( + ['i.*', 'c.iso_code'], + $fakeItemFrom, + [ + 'where' => [ + 'i.product_id = ? AND ' => $course['id'], + 'i.product_type = ?' => self::PRODUCT_TYPE_COURSE + ] + ], + 'first' + ); - return $courses; + if ($item !== false) { + $courseItem['visible'] = true; + $courseItem['currency'] = $item['iso_code']; + $courseItem['price'] = $item['price']; + } + + $items[] = $courseItem; + } + + return $items; } /** @@ -229,20 +273,68 @@ class BuyCoursesPlugin extends Plugin */ public function getSessions() { - $buySessionTable = Database::get_main_table(TABLE_BUY_SESSION); - $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION); + $buyItemTable = Database::get_main_table(BuyCoursesUtils::TABLE_ITEM); + $buyCurrencyTable = Database::get_main_table(BuyCoursesUtils::TABLE_CURRENCY); + + $auth = new Auth(); + $sessions = $auth->browseSessions(); + + $currency = $this->getSelectedCurrency(); + + $items = []; + + $fakeItemFrom = " + $buyItemTable i + INNER JOIN $buyCurrencyTable c + ON i.currency_id = c.id + "; + + foreach ($sessions as $session) { + $sessionItem = [ + 'session_id' => $session->getId(), + 'session_name' => $session->getName(), + 'session_visibility' => $session->getVisibility(), + 'session_display_start_date' => null, + 'session_display_end_date' => null, + 'visible' => false, + 'currency' => empty($currency) ? null : $currency['iso_code'], + 'price' => 0.00 + ]; - $sessions = Database::select( - ['a.session_id', 'a.visible', 'a.price', 'b.*'], - "$buySessionTable a, $sessionTable b", - [ - 'where' => [ - 'a.session_id = b.id' - ] - ] - ); + if (!empty($session->getDisplayStartDate())) { + $sessionItem['session_display_start_date'] = api_format_date( + $session->getDisplayStartDate()->format('Y-m-d h:i:s') + ); + } - return $sessions; + if (!empty($session->getDisplayEndDate())) { + $sessionItem['session_display_end_date'] = api_format_date( + $session->getDisplayEndDate()->format('Y-m-d h:i:s') + ); + } + + $item = Database::select( + ['i.*', 'c.iso_code'], + $fakeItemFrom, + [ + 'where' => [ + 'i.product_id = ? AND ' => $session->getId(), + 'i.product_type = ?' => self::PRODUCT_TYPE_SESSION + ] + ], + 'first' + ); + + if ($item !== false) { + $sessionItem['visible'] = true; + $sessionItem['currency'] = $item['iso_code']; + $sessionItem['price'] = $item['price']; + } + + $items[] = $sessionItem; + } + + return $items; } /** diff --git a/plugin/buycourses/src/configuration.php b/plugin/buycourses/src/configuration.php index b9d9751ebc..ad57e21119 100644 --- a/plugin/buycourses/src/configuration.php +++ b/plugin/buycourses/src/configuration.php @@ -9,30 +9,13 @@ */ require_once dirname(__FILE__) . '/buy_course.lib.php'; require_once '../../../main/inc/global.inc.php'; -require_once 'buy_course_plugin.class.php'; $plugin = BuyCoursesPlugin::create(); $includeSession = $plugin->get('include_sessions') === 'true'; api_protect_admin_script(true); -// sync course table with the plugin -BuyCoursesUtils::sync(); - -$visibility = array(); -$visibility[] = getCourseVisibilityIcon('0'); -$visibility[] = getCourseVisibilityIcon('1'); -$visibility[] = getCourseVisibilityIcon('2'); -$visibility[] = getCourseVisibilityIcon('3'); -$visibility[] = getCourseVisibilityIcon('4'); - $courses = $plugin->getCourses(); -$currency = $plugin->getSelectedCurrency(); -$currencyCode = null; - -if (isset($currency['currency_code'])) { - $currencyCode = $currency['currency_code']; -} //view $interbreadcrumb[] = [ @@ -47,8 +30,6 @@ $interbreadcrumb[] = [ $templateName = $plugin->get_lang('AvailableCourses'); $tpl = new Template($templateName); $tpl->assign('courses', $courses); -$tpl->assign('visibility', $visibility); -$tpl->assign('currency', $currencyCode); $tpl->assign('sessions_are_included', $includeSession); if ($includeSession) { diff --git a/plugin/buycourses/src/function.php b/plugin/buycourses/src/function.php index cf4dd74c60..e020981e07 100644 --- a/plugin/buycourses/src/function.php +++ b/plugin/buycourses/src/function.php @@ -25,8 +25,11 @@ $tableSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); $tableCourseRelUser = Database::get_main_table(TABLE_MAIN_COURSE_USER); $tableUser = Database::get_main_table(TABLE_MAIN_USER); +$itemTable = Database::get_main_table(BuyCoursesUtils::TABLE_ITEM); + $plugin = BuyCoursesPlugin::create(); $buy_name = $plugin->get_lang('Buy'); +$currency = $plugin->getSelectedCurrency(); if ($_REQUEST['tab'] == 'sync') { $sql = "SELECT code, title FROM $tableCourse;"; @@ -358,36 +361,73 @@ if ($_REQUEST['tab'] == 'courses_filter') { } if ($_REQUEST['tab'] == 'save_mod') { - - $id; - $tableBuy; - $tableField; - if (isset($_REQUEST['course_id'])) { - $id = intval($_REQUEST['course_id']); - $tableBuy = $tableBuyCourse; - $tableField = 'course_id'; + $productId = $_REQUEST['course_id']; + $productType = BuyCoursesPlugin::PRODUCT_TYPE_COURSE; } else { - $id = intval($_REQUEST['session_id']); - $tableBuy = $tableBuySession; - $tableField = 'session_id'; + $productId = $_REQUEST['session_id']; + $productType = BuyCoursesPlugin::PRODUCT_TYPE_SESSION; } - $visible = intval($_REQUEST['visible']); - $price = Database::escape_string($_REQUEST['price']); - - $sql = "UPDATE $tableBuy - SET visible = " . $visible . ", - price = '" . $price . "' - WHERE " . $tableField . " = '" . $id . "';"; + $affectedRows = false; + + if ($_POST['visible'] == 1) { + $item = Database::select( + 'COUNT(1) AS qty', + $itemTable, + [ + 'where' => [ + 'product_id = ? AND ' => intval($productId), + 'product_type = ?' => $productType + ] + ], + 'first' + ); + + if ($item['qty'] > 0) { + $affectedRows = Database::update( + $itemTable, + ['price' => floatval($_POST['price'])], + [ + 'product_id = ? AND ' => intval($productId), + 'product_type' => $productType + ] + ); + } else { + $affectedRows = Database::insert( + $itemTable, + [ + 'currency_id' => $currency['id'], + 'product_type' => $productType, + 'product_id' => intval($productId), + 'price' => floatval($_POST['price']) + ] + ); + } + } else { + $affectedRows = Database::delete( + $itemTable, + [ + 'product_id = ? AND ' => intval($productId), + 'product_type = ?' => $productType + ] + ); + } - $res = Database::query($sql); - if (!$res) { - $content = $plugin->get_lang('ProblemToSaveTheMessage'); - echo json_encode(array("status" => "false", "content" => $content)); + if ($affectedRows > 0) { + $jsonResult = [ + "status" => true, + "itemId" => $productId + ]; } else { - echo json_encode(array("status" => "true", "course_id" => $id)); + $jsonResult = [ + "status" => false, + "content" => $plugin->get_lang('ProblemToSaveTheMessage') + ]; } + + echo json_encode($jsonResult); + exit; } if ($_REQUEST['tab'] == 'unset_variables') { diff --git a/plugin/buycourses/view/configuration.tpl b/plugin/buycourses/view/configuration.tpl index b61ef06b7f..9609b62b1c 100644 --- a/plugin/buycourses/view/configuration.tpl +++ b/plugin/buycourses/view/configuration.tpl @@ -23,43 +23,53 @@ {{ 'OfficialCode'|get_lang }} {{ 'Visible'|get_lang }} {{ 'Price'|get_plugin_lang('BuyCoursesPlugin') }} - {{ 'Option'|get_lang }} + {{ 'Option'|get_lang }} - {% for course in courses %} - + {% for item in courses %} + - {{ visibility[course.visibility] }} - {{course.title}} - {{ course.visual_code }} + {% if item.course_visibility == 0 %} + {{ 'CourseVisibilityClosed'|get_lang }} + {% elseif item.course_visibility == 1 %} + {{ 'Private'|get_lang }} + {% elseif item.course_visibility == 2 %} + {{ 'OpenToThePlatform'|get_lang }} + {% elseif item.course_visibility == 3 %} + {{ 'OpenToTheWorld'|get_lang }} + {% elseif item.course_visibility == 4 %} + {{ 'CourseVisibilityHidden'|get_lang }} + {% endif %} + + {{ item.course_title }} + {{ item.course_visual_code }} - {{course.code}} + {{ item.course_code }} - {% if course.visible == 1 %} + {% if item.visible == 1 %} {% else %} {% endif %} - {% if currency %} + {% if item.currency %}
- {{ currency }} - + {{ item.currency }} +
{% else %} - + {% endif %} - -
ok
- + + {% endfor %} @@ -79,46 +89,54 @@ {{ 'EndDate'|get_lang }} {{ 'Visible'|get_lang }} {{ 'Price'|get_plugin_lang('BuyCoursesPlugin') }} - {{ 'Option'|get_lang }} + {{ 'Option'|get_lang }} - {% for session in sessions %} - + {% for item in sessions %} + - {{ visibility[session.visibility] }} - {{session.name}} + {% if item.session_visibility == 0 %} + {{ 'CourseVisibilityClosed'|get_lang }} + {% elseif item.session_visibility == 1 %} + {{ 'Private'|get_lang }} + {% elseif item.session_visibility == 2 %} + {{ 'OpenToThePlatform'|get_lang }} + {% elseif item.session_visibility == 3 %} + {{ 'OpenToTheWorld'|get_lang }} + {% elseif item.session_visibility == 4 %} + {{ 'CourseVisibilityHidden'|get_lang }} + {% endif %} + + {{ item.session_name }} - {{ session.access_start_date }} + {{ item.session_display_start_date }} - {{ session.access_end_date }} + {{ item.session_display_end_date }} - {% if session.visible == 1 %} + {% if item.visible %} {% else %} {% endif %} - {% if currency %} + {% if item.currency %}
- {{ currency }} - + {{ item.currency }} +
{% else %} - + {% endif %} - -
- ok -
- + + {% endfor %}