Skip to content

Commit

Permalink
Fix session course coach selection, using FormValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Sep 15, 2017
1 parent 92aa87c commit c6b264f
Showing 1 changed file with 35 additions and 62 deletions.
97 changes: 35 additions & 62 deletions main/session/session_course_edit.php
Expand Up @@ -13,9 +13,11 @@
$id_session = intval($_GET['id_session']);
SessionManager::protectSession($id_session);
$course_code = $_GET['course_code'];
$course_info = api_get_course_info($_REQUEST['course_code']);

$formSent = 0;
$errorMsg = '';
if (empty($course_info)) {
api_not_allowed(true);
}

// Database Table Definitions
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
Expand All @@ -24,7 +26,6 @@
$tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);

$course_info = api_get_course_info($_REQUEST['course_code']);
$courseId = $course_info['real_id'];
$tool_name = $course_info['name'];
$sql = "SELECT s.name, c.title
Expand All @@ -41,15 +42,18 @@
exit();
}

//$interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
$interbreadcrumb[] = array('url' => "session_list.php", "name" => get_lang("SessionList"));
$interbreadcrumb[] = array('url' => "resume_session.php?id_session=".$id_session, "name" => get_lang('SessionOverview'));
$interbreadcrumb[] = array('url' => "session_course_list.php?id_session=$id_session", "name" =>api_htmlentities($session_name, ENT_QUOTES, $charset));
$interbreadcrumb[] = array(
'url' => "resume_session.php?id_session=".$id_session,
"name" => get_lang('SessionOverview'),
);
$interbreadcrumb[] = array(
'url' => "session_course_list.php?id_session=$id_session",
"name" => api_htmlentities($session_name, ENT_QUOTES, $charset),
);

$arr_infos = array();
if (isset($_POST['formSent']) && $_POST['formSent']) {
$formSent = 1;

// get all tutor by course_code in the session
$sql = "SELECT user_id
FROM $tbl_session_rel_course_rel_user
Expand All @@ -63,10 +67,8 @@
}
}

$id_coaches = $_POST['id_coach'];

$id_coaches = isset($_POST['id_coach']) ? $_POST['id_coach'] : [0];
if (is_array($id_coaches) && count($id_coaches) > 0) {

foreach ($id_coaches as $id_coach) {
$id_coach = intval($id_coach);
$rs1 = SessionManager::set_coach_to_course_session(
Expand All @@ -87,7 +89,7 @@
true
);
}

Display::addFlash(Display::return_message(get_lang('Updated')));
header('Location: '.Security::remove_XSS($_GET['page']).'?id_session='.$id_session);
exit();
}
Expand Down Expand Up @@ -142,54 +144,25 @@
$tool_name = get_lang('ModifySessionCourse');
api_display_tool_title($tool_name);

?>
<div class="session-course-edit">

<form method="post" action="<?php echo api_get_self(); ?>?id_session=<?php echo $id_session; ?>&course_code=<?php echo urlencode($course_code); ?>&page=<?php echo Security::remove_XSS($_GET['page']) ?>" style="margin:0px;">
<input type="hidden" name="formSent" value="1">

<div class="row">
<div class="col-md-12">
<div class="title"></div>
<?php
if (!empty($errorMsg)) {
echo Display::return_message($errorMsg);
}
?>
</div>
</div>
<div class="row">
<div class="col-md-2">
<?php echo get_lang("CoachName") ?>
</div>
<div class="col-md-8">

<select name="id_coach[]" class="form-control" multiple>
<option value="0">----- <?php echo get_lang("Choose") ?> -----</option>
<option value="0" <?php if (count($arr_infos) == 0) echo 'selected="selected"'; ?>>
<?php echo get_lang('None') ?>
</option>
<?php
foreach ($coaches as $enreg) {
?>
<option value="<?php echo $enreg['user_id']; ?>" <?php if (((is_array($arr_infos) && in_array($enreg['user_id'], $arr_infos)))) echo 'selected="selected"'; ?>>
<?php echo api_get_person_name($enreg['firstname'], $enreg['lastname']).' ('.$enreg['username'].')'; ?>
</option>
<?php
}
unset($coaches);
?>
</select>
<div class="control">
<button class="btn btn-success" type="submit" name="name" value="<?php echo get_lang('AssignCoach') ?>">
<em class="fa fa-plus"></em>
<?php echo get_lang('AssignCoach') ?>
</button>
</div>
</div>
<div class="col-md-2"></div>
</div>
</form>
</div>
<?php
$form = new FormValidator(
'form',
'post',
api_get_self().'?id_session='.$id_session.'&course_code='.$course_code.'&page='.Security::remove_XSS($_GET['page'])
);

$options = [];
$selected = [];
foreach ($coaches as $enreg) {
$options[$enreg['user_id']] = api_get_person_name($enreg['firstname'], $enreg['lastname']).' ('.$enreg['username'].')';
if (in_array($enreg['user_id'], $arr_infos)) {
$selected[] = $enreg['user_id'];
}
}

$form->addSelect('id_coach', get_lang('CoachName'), $options, ['multiple' => 'multiple']);
$form->addHidden('formSent', 1);
$form->addButtonSave(get_lang('AssignCoach'));
$form->setDefaults(['id_coach' => $selected]);
$form->display();

Display::display_footer();

0 comments on commit c6b264f

Please sign in to comment.