Format code, avoid send request twice

pull/2487/head
jmontoyaa 8 years ago
parent ea9f74d34a
commit e41eabf25d
  1. 11
      main/auth/courses.php
  2. 14
      main/auth/courses_categories.php
  3. 113
      main/auth/courses_controller.php

@ -62,6 +62,7 @@ if (isset($_GET['action']) && in_array($_GET['action'], $actions)) {
} }
$categoryCode = isset($_GET['category_code']) && !empty($_GET['category_code']) ? $_GET['category_code'] : 'ALL'; $categoryCode = isset($_GET['category_code']) && !empty($_GET['category_code']) ? $_GET['category_code'] : 'ALL';
$searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
$nameTools = CourseCategory::getCourseCatalogNameTools($action); $nameTools = CourseCategory::getCourseCatalogNameTools($action);
if (empty($nameTools)) { if (empty($nameTools)) {
@ -145,13 +146,14 @@ if (isset($_POST['create_course_category']) &&
if (isset($_REQUEST['search_course'])) { if (isset($_REQUEST['search_course'])) {
if ($ctok == $_REQUEST['sec_token']) { if ($ctok == $_REQUEST['sec_token']) {
$courses_controller->search_courses( $courses_controller->search_courses(
$_REQUEST['search_term'], $searchTerm,
null, null,
null, null,
null, null,
$limit, $limit,
true true
); );
exit;
} }
} }
@ -160,7 +162,7 @@ if (isset($_REQUEST['subscribe_course'])) {
if ($ctok == $_GET['sec_token']) { if ($ctok == $_GET['sec_token']) {
$courses_controller->subscribe_user( $courses_controller->subscribe_user(
$_GET['subscribe_course'], $_GET['subscribe_course'],
$_GET['search_term'], $searchTerm,
$categoryCode $categoryCode
); );
} }
@ -168,11 +170,10 @@ if (isset($_REQUEST['subscribe_course'])) {
// We are unsubscribing from a course (=Unsubscribe from course). // We are unsubscribing from a course (=Unsubscribe from course).
if (isset($_GET['unsubscribe'])) { if (isset($_GET['unsubscribe'])) {
$search_term = isset($_GET['search_term']) ? $_GET['search_term'] : null;
if ($ctok == $_GET['sec_token']) { if ($ctok == $_GET['sec_token']) {
$courses_controller->unsubscribe_user_from_course( $courses_controller->unsubscribe_user_from_course(
$_GET['unsubscribe'], $_GET['unsubscribe'],
$search_term, $searchTerm,
$categoryCode $categoryCode
); );
} }
@ -188,7 +189,7 @@ switch ($action) {
case 'subscribe_user_with_password': case 'subscribe_user_with_password':
$courses_controller->subscribe_user( $courses_controller->subscribe_user(
isset($_POST['subscribe_user_with_password']) ? $_POST['subscribe_user_with_password'] : '', isset($_POST['subscribe_user_with_password']) ? $_POST['subscribe_user_with_password'] : '',
isset($_POST['search_term']) ? $_POST['search_term'] : '', $searchTerm,
isset($_POST['category_code']) ? $_POST['category_code'] : '' isset($_POST['category_code']) ? $_POST['category_code'] : ''
); );
break; break;

@ -19,7 +19,7 @@ $pageCurrent = isset($pageCurrent) ? $pageCurrent : isset($_GET['pageCurrent'])
$pageLength = isset($pageLength) ? $pageLength : isset($_GET['pageLength']) ? intval($_GET['pageLength']) : CoursesAndSessionsCatalog::PAGE_LENGTH; $pageLength = isset($pageLength) ? $pageLength : isset($_GET['pageLength']) ? intval($_GET['pageLength']) : CoursesAndSessionsCatalog::PAGE_LENGTH;
$pageTotal = intval(ceil(intval($countCoursesInCategory) / $pageLength)); $pageTotal = intval(ceil(intval($countCoursesInCategory) / $pageLength));
$cataloguePagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($pageCurrent, $pageLength, $pageTotal) : ''; $cataloguePagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($pageCurrent, $pageLength, $pageTotal) : '';
$search_term = isset($search_term) ? $search_term : null; $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
if ($showSessions && isset($_POST['date'])) { if ($showSessions && isset($_POST['date'])) {
$date = $_POST['date']; $date = $_POST['date'];
@ -78,7 +78,7 @@ $code = isset($code) ? $code : null;
<input class="form-control" type="text" name="search_term" <input class="form-control" type="text" name="search_term"
value="<?php echo(empty($_POST['search_term']) value="<?php echo(empty($_POST['search_term'])
? '' ? ''
: api_htmlentities(Security::remove_XSS($_POST['search_term']))); ?>"/> : api_htmlentities($searchTerm)); ?>"/>
<div class="input-group-btn"> <div class="input-group-btn">
<button class="btn btn-default" type="submit"> <button class="btn btn-default" type="submit">
<em class="fa fa-search"></em> <?php echo get_lang('Search'); ?> <em class="fa fa-search"></em> <?php echo get_lang('Search'); ?>
@ -158,8 +158,8 @@ if ($showCourses && $action != 'display_sessions') {
echo $content; echo $content;
} }
if (!empty($search_term)) { if (!empty($searchTerm)) {
echo "<p><strong>".get_lang('SearchResultsFor')." ".Security::remove_XSS($_POST['search_term'])."</strong><br />"; echo "<p><strong>".get_lang('SearchResultsFor')." ".$searchTerm."</strong><br />";
} }
$ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote'; $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
@ -202,7 +202,7 @@ if ($showCourses && $action != 'display_sessions') {
$html .= returnThumbnail($course, $userRegistered); $html .= returnThumbnail($course, $userRegistered);
$separator = null; $separator = null;
$subscribeButton = return_register_button($course, $stok, $code, $search_term); $subscribeButton = return_register_button($course, $stok, $code, $searchTerm);
// start buycourse validation // start buycourse validation
// display the course price and buy button if the buycourses plugin is enabled and this course is configured // display the course price and buy button if the buycourses plugin is enabled and this course is configured
@ -239,13 +239,13 @@ if ($showCourses && $action != 'display_sessions') {
$html .= return_already_registered_label('student'); $html .= return_already_registered_label('student');
if (!$course_closed) { if (!$course_closed) {
if ($course_unsubscribe_allowed) { if ($course_unsubscribe_allowed) {
$html .= return_unregister_button($course, $stok, $search_term, $code); $html .= return_unregister_button($course, $stok, $searchTerm, $code);
} }
} }
} elseif ($userRegisteredInCourseAsTeacher) { } elseif ($userRegisteredInCourseAsTeacher) {
// if user registered as teacher // if user registered as teacher
if ($course_unsubscribe_allowed) { if ($course_unsubscribe_allowed) {
$html .= return_unregister_button($course, $stok, $search_term, $code); $html .= return_unregister_button($course, $stok, $searchTerm, $code);
} }
} else { } else {
// if user not registered in the course // if user not registered in the course

@ -101,14 +101,21 @@ class CoursesController
$data['countCoursesInCategory'] = $this->model->count_courses_in_category($category_code); $data['countCoursesInCategory'] = $this->model->count_courses_in_category($category_code);
if ($action === 'display_random_courses') { if ($action === 'display_random_courses') {
// Random value is used instead limit filter // Random value is used instead limit filter
$data['browse_courses_in_category'] = $this->model->browse_courses_in_category(null, 12); $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(
null,
12
);
$data['countCoursesInCategory'] = count($data['browse_courses_in_category']); $data['countCoursesInCategory'] = count($data['browse_courses_in_category']);
} else { } else {
if (!isset($category_code)) { if (!isset($category_code)) {
$category_code = $browse_course_categories[0][1]['code']; // by default first category $category_code = $browse_course_categories[0][1]['code']; // by default first category
} }
$limit = isset($limit) ? $limit : CourseCategory::getLimitArray(); $limit = isset($limit) ? $limit : CourseCategory::getLimitArray();
$data['browse_courses_in_category'] = $this->model->browse_courses_in_category($category_code, null, $limit); $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(
$category_code,
null,
$limit
);
} }
$data['browse_course_categories'] = $browse_course_categories; $data['browse_course_categories'] = $browse_course_categories;
@ -170,10 +177,16 @@ class CoursesController
$data = array(); $data = array();
$limit = !empty($limit) ? $limit : CourseCategory::getLimitArray(); $limit = !empty($limit) ? $limit : CourseCategory::getLimitArray();
$browse_course_categories = $this->model->browse_course_categories(); $browse_course_categories = $this->model->browse_course_categories();
$data['countCoursesInCategory'] = $this->model->count_courses_in_category('ALL', $search_term); $data['countCoursesInCategory'] = $this->model->count_courses_in_category(
$data['browse_courses_in_category'] = $this->model->search_courses($search_term, $limit, $justVisible); 'ALL',
$search_term
);
$data['browse_courses_in_category'] = $this->model->search_courses(
$search_term,
$limit,
$justVisible
);
$data['browse_course_categories'] = $browse_course_categories; $data['browse_course_categories'] = $browse_course_categories;
$data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template $data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template
// getting all the courses to which the user is subscribed to // getting all the courses to which the user is subscribed to
@ -254,9 +267,16 @@ class CoursesController
{ {
$result = $this->model->store_course_category($category_title); $result = $this->model->store_course_category($category_title);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('CourseCategoryStored'))); Display::addFlash(
Display::return_message(get_lang('CourseCategoryStored'))
);
} else { } else {
Display::addFlash(Display::return_message(get_lang('ACourseCategoryWithThisNameAlreadyExists'), 'error')); Display::addFlash(
Display::return_message(
get_lang('ACourseCategoryWithThisNameAlreadyExists'),
'error'
)
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -275,7 +295,9 @@ class CoursesController
$result = $this->model->updateCourseCategory($courseId, $category_id); $result = $this->model->updateCourseCategory($courseId, $category_id);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('EditCourseCategorySucces'))); Display::addFlash(
Display::return_message(get_lang('EditCourseCategorySucces'))
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -292,7 +314,9 @@ class CoursesController
{ {
$result = $this->model->move_course($move, $course_code, $category_id); $result = $this->model->move_course($move, $course_code, $category_id);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('CourseSortingDone'))); Display::addFlash(
Display::return_message(get_lang('CourseSortingDone'))
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -308,7 +332,9 @@ class CoursesController
{ {
$result = $this->model->move_category($move, $category_id); $result = $this->model->move_category($move, $category_id);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('CategorySortingDone'))); Display::addFlash(
Display::return_message(get_lang('CategorySortingDone'))
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -324,7 +350,9 @@ class CoursesController
{ {
$result = $this->model->store_edit_course_category($title, $category); $result = $this->model->store_edit_course_category($title, $category);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('CourseCategoryEditStored'))); Display::addFlash(
Display::return_message(get_lang('CourseCategoryEditStored'))
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -339,7 +367,9 @@ class CoursesController
{ {
$result = $this->model->delete_course_category($category_id); $result = $this->model->delete_course_category($category_id);
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('CourseCategoryDeleted'))); Display::addFlash(
Display::return_message(get_lang('CourseCategoryDeleted'))
);
} }
$action = 'sortmycourses'; $action = 'sortmycourses';
$this->courses_list($action); $this->courses_list($action);
@ -352,21 +382,30 @@ class CoursesController
* @param string $search_term * @param string $search_term
* @param string $category_code * @param string $category_code
*/ */
public function unsubscribe_user_from_course($course_code, $search_term = null, $category_code = null) public function unsubscribe_user_from_course(
{ $course_code,
$search_term = null,
$category_code = null
) {
$result = $this->model->remove_user_from_course($course_code); $result = $this->model->remove_user_from_course($course_code);
$message = ''; $message = '';
$error = ''; $error = '';
if ($result) { if ($result) {
Display::addFlash(Display::return_message(get_lang('YouAreNowUnsubscribed'))); Display::addFlash(
Display::return_message(get_lang('YouAreNowUnsubscribed'))
);
} }
$action = 'sortmycourses';
if (!empty($search_term)) { if (!empty($search_term)) {
$this->search_courses($search_term, $message, $error); $this->search_courses($search_term, $message, $error);
} else { } else {
$this->courses_categories('subcribe', $category_code, $message, $error); $this->courses_categories(
'subcribe',
$category_code,
$message,
$error
);
} }
} }
@ -390,19 +429,21 @@ class CoursesController
$html .= '<li>'; $html .= '<li>';
$categoryLink = CourseCategory::getCourseCategoryUrl(
1,
$limit['length'],
$categoryCode,
$hiddenLinks,
$action
);
if ($code == $categoryCode) { if ($code == $categoryCode) {
$html .= '<strong>'; $html .= '<strong>';
$html .= "$categoryName ($categoryCourses)"; $html .= "$categoryName ($categoryCourses)";
$html .= '</strong>'; $html .= '</strong>';
} else { } else {
if (!empty($categoryCourses)) { if (!empty($categoryCourses)) {
$html .= '<a href="'.CourseCategory::getCourseCategoryUrl( $html .= '<a href="'.$categoryLink.'">';
1,
$limit['length'],
$categoryCode,
$hiddenLinks,
$action
).'">';
$html .= "$categoryName ($categoryCourses)"; $html .= "$categoryName ($categoryCourses)";
$html .= '</a>'; $html .= '</a>';
} else { } else {
@ -421,13 +462,7 @@ class CoursesController
if ($code == $subCategory1Code) { if ($code == $subCategory1Code) {
$html .= "<strong>$subCategory1Name ($subCategory1Courses)</strong>"; $html .= "<strong>$subCategory1Name ($subCategory1Courses)</strong>";
} else { } else {
$html .= '<a href="'.CourseCategory::getCourseCategoryUrl( $html .= '<a href="'.$categoryLink.'">';
1,
$limit['length'],
$categoryCode,
$hiddenLinks,
$action
).'">';
$html .= "$subCategory1Name ($subCategory1Courses)"; $html .= "$subCategory1Name ($subCategory1Courses)";
$html .= '</a>'; $html .= '</a>';
} }
@ -445,13 +480,7 @@ class CoursesController
if ($code == $subCategory2Code) { if ($code == $subCategory2Code) {
$html .= "<strong>$subCategory2Name ($subCategory2Courses)</strong>"; $html .= "<strong>$subCategory2Name ($subCategory2Courses)</strong>";
} else { } else {
$html .= '<a href="'.CourseCategory::getCourseCategoryUrl( $html .= '<a href="'.$categoryLink.'">';
1,
$limit['length'],
$categoryCode,
$hiddenLinks,
$action
).'">';
$html .= "$subCategory2Name ($subCategory2Courses)"; $html .= "$subCategory2Name ($subCategory2Courses)";
$html .= '</a>'; $html .= '</a>';
} }
@ -469,13 +498,7 @@ class CoursesController
if ($code == $subCategory3Code) { if ($code == $subCategory3Code) {
$html .= "<strong>$subCategory3Name ($subCategory3Courses)</strong>"; $html .= "<strong>$subCategory3Name ($subCategory3Courses)</strong>";
} else { } else {
$html .= '<a href="'.CourseCategory::getCourseCategoryUrl( $html .= '<a href="'.$categoryLink.'">';
1,
$limit['length'],
$categoryCode,
$hiddenLinks,
$action
).'">';
$html .= "$subCategory3Name ($subCategory3Courses)"; $html .= "$subCategory3Name ($subCategory3Courses)";
$html .= '</a>'; $html .= '</a>';
} }

Loading…
Cancel
Save