Skip to content

Commit 3322b97

Browse files
authored
Session: Replace session course ordering by arrows for drag and drop on resume_session and always available - refs BT#22724 (#6425)
Author: @yverhenne
1 parent cc625ed commit 3322b97

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

main/session/resume_session.php

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,35 @@
5656
$action = isset($_GET['action']) ? $_GET['action'] : null;
5757
$url_id = api_get_current_access_url_id();
5858

59+
// Course DRAG & DROP via AJAX
60+
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'reorder_courses') {
61+
api_protect_admin_script(true);
62+
$order = $_POST['order'] ?? [];
63+
if (!empty($order) && is_array($order)) {
64+
foreach ($order as $position => $courseId) {
65+
$qb = $em->createQueryBuilder();
66+
$qb->update('ChamiloCoreBundle:SessionRelCourse', 'src')
67+
->set('src.position', ':position')
68+
->where('src.session = :session')
69+
->andWhere('src.course = :course')
70+
->setParameter('position', $position + 1)
71+
->setParameter('session', $sessionId)
72+
->setParameter('course', $courseId)
73+
->getQuery()
74+
->execute();
75+
}
76+
echo json_encode(['success' => true]);
77+
exit;
78+
}
79+
}
80+
5981
switch ($action) {
6082
case 'export_certified_course_users':
6183
$courseCode = $_GET['course_code'] ?? null;
6284
if (!empty($courseCode)) {
6385
SessionManager::exportCourseSessionReport($sessionId, $courseCode);
6486
}
6587
break;
66-
case 'move_up':
67-
SessionManager::moveUp($sessionId, $_GET['course_id']);
68-
header('Location: resume_session.php?id_session='.$sessionId);
69-
exit;
70-
break;
71-
case 'move_down':
72-
SessionManager::moveDown($sessionId, $_GET['course_id']);
73-
header('Location: resume_session.php?id_session='.$sessionId);
74-
exit;
75-
break;
7688
case 'add_user_to_url':
7789
$user_id = $_REQUEST['user_id'];
7890
$result = UrlManager::add_user_to_url($user_id, $url_id);
@@ -167,12 +179,14 @@
167179
$courseListToShow = Display::page_subheader(get_lang('CourseList').$url);
168180

169181
$courseListToShow .= '<table id="session-list-course" class="table table-hover table-striped data_table">
182+
<thead>
170183
<tr>
184+
<th></th>
171185
<th width="35%">'.get_lang('CourseTitle').'</th>
172186
<th width="30%">'.get_lang('CourseCoach').'</th>
173187
<th width="10%">'.get_lang('UsersNumber').'</th>
174188
<th width="25%">'.get_lang('Actions').'</th>
175-
</tr>';
189+
</tr></thead><tbody id="sortable-course-list">';
176190

177191
if ($session->getNbrCourses() === 0) {
178192
$courseListToShow .= '<tr>
@@ -229,36 +243,14 @@
229243
}
230244
}
231245

232-
$orderButtons = '';
233-
if (SessionManager::orderCourseIsEnabled()) {
234-
$orderButtons = Display::url(
235-
Display::return_icon(
236-
!$count ? 'up_na.png' : 'up.png',
237-
get_lang('MoveUp')
238-
),
239-
!$count
240-
? '#'
241-
: api_get_self().'?id_session='.$sessionId.'&course_id='.$course->getId().'&action=move_up'
242-
);
243-
244-
$orderButtons .= Display::url(
245-
Display::return_icon(
246-
$count + 1 == count($courses) ? 'down_na.png' : 'down.png',
247-
get_lang('MoveDown')
248-
),
249-
$count + 1 == count($courses)
250-
? '#'
251-
: api_get_self().'?id_session='.$sessionId.'&course_id='.$course->getId().'&action=move_down'
252-
);
253-
}
254-
255246
$courseUrl = api_get_course_url($course->getCode(), $sessionId);
256247
$courseBaseUrl = api_get_course_url($course->getCode());
257248

258249
// hide_course_breadcrumb the parameter has been added to hide the name
259250
// of the course, that appeared in the default $interbreadcrumb
260-
$courseItem .= '<tr>
261-
<td class="title">'
251+
$courseItem .= '<tr data-course-id="'.$course->getId().'">';
252+
$courseItem .= '<td class="handle" style="cursor:move;text-align:center;width:30px;"><span style="font-size:1.4em;">&#9776;</span></td>';
253+
$courseItem .= '<td class="title">'
262254
.Display::url(
263255
$course->getTitle().' ('.$course->getVisualCode().')',
264256
$courseUrl
@@ -281,7 +273,6 @@
281273
$codePath.'admin/skill_rel_course.php?session_id='.$sessionId.'&course_id='.$course->getId()
282274
);
283275
}
284-
$courseItem .= $orderButtons;
285276

286277
$courseItem .= Display::url(
287278
Display::return_icon('new_user.png', get_lang('AddUsers')),
@@ -495,6 +486,37 @@
495486
$programmedAnnouncement = $programmedAnnouncement->allowed();
496487

497488
$htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
489+
$htmlHeadXtra[] = <<<EOD
490+
<script>
491+
$(function() {
492+
$("#sortable-course-list").sortable({
493+
handle: '.handle',
494+
placeholder: "ui-sortable-placeholder",
495+
update: function(event, ui) {
496+
var order = [];
497+
$("#sortable-course-list tr").each(function() {
498+
order.push($(this).data('course-id'));
499+
});
500+
$.ajax({
501+
url: window.location.pathname + window.location.search,
502+
method: "POST",
503+
data: {
504+
action: "reorder_courses",
505+
order: order
506+
},
507+
success: function(response) {
508+
// Optionnel : affiche un message
509+
}
510+
});
511+
}
512+
}).disableSelection();
513+
});
514+
</script>
515+
<style>
516+
.ui-sortable-placeholder { background: #fffae6; height:40px; }
517+
.handle { cursor:move; }
518+
</style>
519+
EOD;
498520

499521
$tpl = new Template($tool_name);
500522
$tpl->assign('session_header', $sessionHeader);

0 commit comments

Comments
 (0)