Fix Configuration page with new DB structure - refs #7768

1.10.x
Angel Fernando Quiroz Campos 11 years ago
parent d4c1b86acb
commit 7a4bdcf386
  1. 44
      plugin/buycourses/js/buycourses.js
  2. 146
      plugin/buycourses/src/BuyCoursesUtils.php
  3. 25
      plugin/buycourses/src/buy_course.lib.php
  4. 138
      plugin/buycourses/src/buy_course_plugin.class.php
  5. 19
      plugin/buycourses/src/configuration.php
  6. 86
      plugin/buycourses/src/function.php
  7. 90
      plugin/buycourses/view/configuration.tpl

@ -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) {

@ -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]
);
}
}

@ -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

@ -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;
}
/**

@ -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) {

@ -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') {

@ -23,43 +23,53 @@
<th class="text-center">{{ 'OfficialCode'|get_lang }}</th>
<th class="text-center">{{ 'Visible'|get_lang }}</th>
<th class="text-right" width="200">{{ 'Price'|get_plugin_lang('BuyCoursesPlugin') }}</th>
<th class="text-center">{{ 'Option'|get_lang }}</th>
<th class="text-right">{{ 'Option'|get_lang }}</th>
</tr>
</thead>
<tbody>
{% for course in courses %}
<tr>
{% for item in courses %}
<tr data-item="{{ item.course_id }}" data-type="course">
<td>
{{ visibility[course.visibility] }}
<a href="{{ _p.web_course ~ course.code ~ '/index.php' }}">{{course.title}}</a>
<span class="label label-info">{{ course.visual_code }}</span>
{% if item.course_visibility == 0 %}
<img src="{{ 'bullet_red.gif'|icon() }}" alt="{{ 'CourseVisibilityClosed'|get_lang }}" title="{{ 'CourseVisibilityClosed'|get_lang }}">
{% elseif item.course_visibility == 1 %}
<img src="{{ 'bullet_orange.gif'|icon() }}" alt="{{ 'Private'|get_lang }}" title="{{ 'Private'|get_lang }}">
{% elseif item.course_visibility == 2 %}
<img src="{{ 'bullet_green.gif'|icon() }}" alt="{{ 'OpenToThePlatform'|get_lang }}" title="{{ 'OpenToThePlatform'|get_lang }}">
{% elseif item.course_visibility == 3 %}
<img src="{{ 'bullet_blue.gif'|icon() }}" alt="{{ 'OpenToTheWorld'|get_lang }}" title="{{ 'OpenToTheWorld'|get_lang }}">
{% elseif item.course_visibility == 4 %}
<img src="{{ 'bullet_gray.gif'|icon() }}" alt="{{ 'CourseVisibilityHidden'|get_lang }}" title="{{ 'CourseVisibilityHidden'|get_lang }}">
{% endif %}
<a href="{{ _p.web_course ~ item.course_code ~ '/index.php' }}">{{ item.course_title }}</a>
<span class="label label-info">{{ item.course_visual_code }}</span>
</td>
<td class="text-center">
{{course.code}}
{{ item.course_code }}
</td>
<td class="text-center">
{% if course.visible == 1 %}
{% if item.visible == 1 %}
<input type="checkbox" name="visible" value="1" checked="checked" size="6">
{% else %}
<input type="checkbox" name="visible" value="1" size="6">
{% endif %}
</td>
<td width="200">
{% if currency %}
{% if item.currency %}
<div class="input-group">
<span class="input-group-addon" id="price-{{ course.course_id }}">{{ currency }}</span>
<input type="number" name="price" value="{{course.price}}" step="0.01" class="text-right form-control" aria-describedby="price-{{ course.course_id }}">
<span class="input-group-addon" id="price-{{ item.course_id }}">{{ item.currency }}</span>
<input type="number" name="price" value="{{ item.price }}" step="0.01" min="0" class="text-right form-control" aria-describedby="price-{{ item.course_id }}">
</div>
{% else %}
<input type="number" name="price" value="{{course.price}}" step="0.01" class="text-right form-control">
<input type="number" name="price" value="{{ item.price }}" step="0.01" min="0" class="text-right form-control">
{% endif %}
</td>
<td class=" text-center" id="course{{ course.id }}">
<div class="confirmed"><img src="{{ _p.web_plugin ~ 'buycourses/resources/img/32/accept.png' }}" alt="ok"/></div>
<div class="modified" style="display:none">
<img id="{{course.course_id}}" src="{{ _p.web_plugin ~ 'buycourses/resources/img/32/save.png' }}" alt="save" class="cursor save"/>
</div>
<td class="text-right">
<button class="btn btn-success btn-sm bc-button-save" type="button">
<i class="fa fa-save"></i> {{ 'Save'|get_lang }}
</button>
</td>
</tr>
{% endfor %}
@ -79,46 +89,54 @@
<th class="text-center">{{ 'EndDate'|get_lang }}</th>
<th class="text-center">{{ 'Visible'|get_lang }}</th>
<th class="text-right">{{ 'Price'|get_plugin_lang('BuyCoursesPlugin') }}</th>
<th class="text-center">{{ 'Option'|get_lang }}</th>
<th class="text-right">{{ 'Option'|get_lang }}</th>
</tr>
</thead>
<tbody>
{% for session in sessions %}
<tr>
{% for item in sessions %}
<tr data-item="{{ item.session_id }}" data-type="session">
<td>
{{ visibility[session.visibility] }}
<a href="{{ _p.web_main ~ 'session/index.php?' ~ {'session_id': session.id}|url_encode() }}">{{session.name}}</a>
{% if item.session_visibility == 0 %}
<img src="{{ 'bullet_red.gif'|icon() }}" alt="{{ 'CourseVisibilityClosed'|get_lang }}" title="{{ 'CourseVisibilityClosed'|get_lang }}">
{% elseif item.session_visibility == 1 %}
<img src="{{ 'bullet_orange.gif'|icon() }}" alt="{{ 'Private'|get_lang }}" title="{{ 'Private'|get_lang }}">
{% elseif item.session_visibility == 2 %}
<img src="{{ 'bullet_green.gif'|icon() }}" alt="{{ 'OpenToThePlatform'|get_lang }}" title="{{ 'OpenToThePlatform'|get_lang }}">
{% elseif item.session_visibility == 3 %}
<img src="{{ 'bullet_blue.gif'|icon() }}" alt="{{ 'OpenToTheWorld'|get_lang }}" title="{{ 'OpenToTheWorld'|get_lang }}">
{% elseif item.session_visibility == 4 %}
<img src="{{ 'bullet_gray.gif'|icon() }}" alt="{{ 'CourseVisibilityHidden'|get_lang }}" title="{{ 'CourseVisibilityHidden'|get_lang }}">
{% endif %}
<a href="{{ _p.web_main ~ 'session/index.php?' ~ {'session_id': item.session_id}|url_encode() }}">{{ item.session_name }}</a>
</td>
<td class="text-center">
{{ session.access_start_date }}
{{ item.session_display_start_date }}
</td>
<td class="text-center">
{{ session.access_end_date }}
{{ item.session_display_end_date }}
</td>
<td class="text-center">
{% if session.visible == 1 %}
{% if item.visible %}
<input type="checkbox" name="visible" value="1" checked="checked" size="6" />
{% else %}
<input type="checkbox" name="visible" value="1" size="6" />
{% endif %}
</td>
<td class="text-right" width="200">
{% if currency %}
{% if item.currency %}
<div class="input-group">
<span class="input-group-addon" id="price-{{ session.id }}">{{ currency }}</span>
<input type="number" name="price" value="{{session.price}}" step="0.01" class="text-right form-control" aria-describedby="price-{{ session.id }}">
<span class="input-group-addon" id="price-{{ item.session_id }}">{{ item.currency }}</span>
<input type="number" name="price" value="{{ item.price }}" step="0.01" min="0" class="text-right form-control" aria-describedby="price-{{ item.session_id }}">
</div>
{% else %}
<input type="number" name="price" value="{{session.price}}" step="0.01" class="text-right form-control">
<input type="number" name="price" value="{{ item.price }}" step="0.01" min="0" class="text-right form-control">
{% endif %}
</td>
<td class=" text-center" id="session{{ session.id }}">
<div class="confirmed">
<img src="{{ _p.web_plugin ~ 'buycourses/resources/img/32/accept.png' }}" alt="ok"/>
</div>
<div class="modified" style="display:none">
<img id="{{session.id}}" src="{{ _p.web_plugin ~ 'buycourses/resources/img/32/save.png' }}" alt="save" class="cursor save"/>
</div>
<td class=" text-center" id="session{{ item.session_id }}">
<button class="btn btn-success btn-sm bc-button-save" type="button">
<i class="fa fa-save"></i> {{ 'Save'|get_lang }}
</button>
</td>
</tr>
{% endfor %}

Loading…
Cancel
Save