Use CourseManager::get_user_course_categories instead of custom code.

- Improve code foreach
- Fix PHP warning
- Format code
pull/2487/head
jmontoyaa 9 years ago
parent 1d19142825
commit a444e67791
  1. 1
      main/auth/courses_controller.php
  2. 256
      main/auth/courses_list.php
  3. 79
      main/inc/lib/auth.lib.php
  4. 23
      main/inc/lib/course.lib.php
  5. 6
      main/inc/lib/userportal.lib.php

@ -43,7 +43,6 @@ class CoursesController
$data['user_courses'] = $this->model->get_courses_of_user($user_id); $data['user_courses'] = $this->model->get_courses_of_user($user_id);
$data['user_course_categories'] = $this->model->get_user_course_categories(); $data['user_course_categories'] = $this->model->get_user_course_categories();
$data['courses_in_category'] = $this->model->get_courses_in_category(); $data['courses_in_category'] = $this->model->get_courses_in_category();
$data['all_user_categories'] = $this->model->get_user_course_categories();
$data['action'] = $action; $data['action'] = $action;
$data['message'] = $message; $data['message'] = $message;

@ -3,6 +3,7 @@
/** /**
* View (MVC patter) for courses * View (MVC patter) for courses
* @todo fix this. use twig templates
* @author Christian Fasanando <christian1827@gmail.com> - Beeznest * @author Christian Fasanando <christian1827@gmail.com> - Beeznest
* @package chamilo.auth * @package chamilo.auth
*/ */
@ -11,9 +12,7 @@
api_block_anonymous_users(); api_block_anonymous_users();
$stok = Security::get_token(); $stok = Security::get_token();
$courses_without_category = isset($courses_in_category[0]) ? $courses_in_category[0] : null; $courses_without_category = isset($courses_in_category[0]) ? $courses_in_category[0] : null;
?> ?>
<!-- Actions: The menu with the different options in cathe course management --> <!-- Actions: The menu with the different options in cathe course management -->
<div id="actions" class="actions"> <div id="actions" class="actions">
<?php if ($action != 'createcoursecategory') { ?> <?php if ($action != 'createcoursecategory') { ?>
@ -21,7 +20,6 @@ $courses_without_category = isset($courses_in_category[0]) ? $courses_in_categor
<?php echo Display::return_icon('new_folder.png', get_lang('CreateCourseCategory'),'','32'); ?></a> <?php echo Display::return_icon('new_folder.png', get_lang('CreateCourseCategory'),'','32'); ?></a>
<?php } ?> <?php } ?>
</div> </div>
<?php <?php
if (!empty($message)) { if (!empty($message)) {
Display::display_confirmation_message($message, false); Display::display_confirmation_message($message, false);
@ -29,151 +27,147 @@ if (!empty($message)) {
// COURSES WITH CATEGORIES // COURSES WITH CATEGORIES
if (!empty($user_course_categories)) { if (!empty($user_course_categories)) {
foreach ($user_course_categories as $row) { $counter = 0;
echo Display::page_subheader($row['title']); $last = end($user_course_categories);
echo '<a name="category'.$row['id'].'"></a>'; foreach ($user_course_categories as $row) {
echo Display::page_subheader($row['title']);
if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) { ?> echo '<a name="category'.$row['id'].'"></a>';
<!-- We display the edit form for the category --> if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) { ?>
<!-- We display the edit form for the category -->
<form name="edit_course_category" method="post" action="courses.php?action=<?php echo $action; ?>">
<input type="hidden" name="edit_course_category" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
<input type="text" name="title_course_category" value="<?php echo $row['title']; ?>" />
<button class="save" type="submit" name="submit_edit_course_category"><?php echo get_lang('Ok'); ?></button>
</form>
<?php
}
$max_category_key = count($user_course_categories);
if ($action != 'unsubscribe') { ?>
<a href="courses.php?action=sortmycourses&amp;categoryid=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>#category<?php echo $row['id']; ?>">
<?php echo Display::display_icon('edit.png', get_lang('Edit'),'',22); ?>
</a>
<?php if (0 != $counter) { ?>
<a href="courses.php?action=<?php echo $action ?>&amp;move=up&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::return_icon('up.png', get_lang('Up'),'',22); ?>
</a>
<?php } else { ?>
<?php echo Display::return_icon('up_na.png', get_lang('Up'),'',22); ?>
<?php } ?>
<form name="edit_course_category" method="post" action="courses.php?action=<?php echo $action; ?>"> <?php if ($row['id'] != $last['id']) { ?>
<input type="hidden" name="edit_course_category" value="<?php echo $row['id']; ?>" /> <a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
<input type="hidden" name="sec_token" value="<?php echo $stok; ?>"> <?php echo Display::return_icon('down.png', get_lang('Down'),'',22); ?>
<input type="text" name="title_course_category" value="<?php echo $row['title']; ?>" /> </a>
<button class="save" type="submit" name="submit_edit_course_category"><?php echo get_lang('Ok'); ?></button> <?php } else { ?>
</form> <?php echo Display::return_icon('down_na.png', get_lang('Down'),'',22); ?>
<?php } ?> <?php } ?>
<a href="courses.php?action=deletecoursecategory&amp;id=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::display_icon('delete.png', get_lang('Delete'), array('onclick' => "javascript: if (!confirm('".addslashes(api_htmlentities(get_lang("CourseCategoryAbout2bedeleted"), ENT_QUOTES, api_get_system_encoding()))."')) return false;"),22) ?>
</a>
<?php }
$counter++;
echo '<br /><br />';
// Show the courses inside this category
echo '<table class="data_table">';
$number_of_courses = isset($courses_in_category[$row['id']]) ? count($courses_in_category[$row['id']]) : 0;
$key = 0;
if (!empty($courses_in_category[$row['id']])) {
foreach ($courses_in_category[$row['id']] as $course) {
?>
<tr>
<td>
<a name="course<?php echo $course['code']; ?>"></a>
<strong><?php echo $course['title']; ?></strong><br />
<?php
if (api_get_setting('display_coursecode_in_courselist') === 'true') {
echo $course['visual_code'];
}
if (api_get_setting('display_coursecode_in_courselist') === 'true' &&
api_get_setting('display_teacher_in_courselist') === 'true'
) {
echo " - ";
}
if (api_get_setting('display_teacher_in_courselist') === 'true') {
echo $course['tutor'];
}
?>
</td>
<td valign="top">
<!-- edit -->
<?php
if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) {
$edit_course = Security::remove_XSS($_GET['edit']); ?>
<!-- display category icons --> <form name="edit_course_category" method="post" action="courses.php?action=<?php echo $action; ?>">
<?php <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
$max_category_key = count($user_course_categories); <input type="hidden" name="course_2_edit_category" value="<?php echo $edit_course; ?>" />
if ($action != 'unsubscribe') { ?> <select name="course_categories">
<a href="courses.php?action=sortmycourses&amp;categoryid=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>#category<?php echo $row['id']; ?>"> <option value="0"><?php echo get_lang("NoCourseCategory"); ?></option>
<?php echo Display::display_icon('edit.png', get_lang('Edit'),'',22); ?> <?php foreach ($user_course_categories as $row) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['title']; ?></option>
<?php } ?>
</select>
<button class="save" type="submit" name="submit_change_course_category"><?php echo get_lang('Ok'); ?></button>
</form>
<?php } ?>
<div style="float:left;width:110px;">
<?php
if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
$icon_title = get_lang('CourseDetails') . ' - ' . $course['title'];
?>
<a href="<?php echo api_get_path(WEB_CODE_PATH); ?>inc/ajax/course_home.ajax.php?a=show_course_information&code=<?php echo $course['code'] ?>" data-title="<?php echo $icon_title ?>" title="<?php echo $icon_title ?>" class="ajax">
<?php echo Display::return_icon('info.png', $icon_title,'','22') ?>
<?php } ?>
</a> </a>
<?php if ($row['id'] != $user_course_categories[0]['id']) { ?> <?php if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) { ?>
<a href="courses.php?action=<?php echo $action ?>&amp;move=up&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>"> <?php echo Display::display_icon('edit_na.png', get_lang('Edit'),'',22); ?>
<?php echo Display::return_icon('up.png', get_lang('Up'),'',22); ?>
</a>
<?php } else { ?> <?php } else { ?>
<?php echo Display::return_icon('up_na.png', get_lang('Up'),'',22); ?> <a href="courses.php?action=<?php echo $action; ?>&amp;edit=<?php echo $course['code']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php } ?> <?php echo Display::display_icon('edit.png', get_lang('Edit'),'',22); ?>
</a>
<?php } ?>
<?php if ($row['id'] != $user_course_categories[$max_category_key - 1]['id']) { ?> <?php if ($key > 0) { ?>
<a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>"> <a href="courses.php?action=<?php echo $action; ?>&amp;move=up&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::return_icon('down.png', get_lang('Down'),'',22); ?> <?php echo Display::display_icon('up.png', get_lang('Up'),'',22); ?>
</a> </a>
<?php } else { ?> <?php } else { ?>
<?php echo Display::return_icon('down_na.png', get_lang('Down'),'',22); ?> <?php echo Display::display_icon('up_na.png', get_lang('Up'),'',22); ?>
<?php } ?> <?php } ?>
<a href="courses.php?action=deletecoursecategory&amp;id=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::display_icon('delete.png', get_lang('Delete'), array('onclick' => "javascript: if (!confirm('".addslashes(api_htmlentities(get_lang("CourseCategoryAbout2bedeleted"), ENT_QUOTES, api_get_system_encoding()))."')) return false;"),22) ?>
</a>
<?php }
echo '<br /><br />';
// Show the courses inside this category
echo '<table class="data_table">';
$number_of_courses = isset($courses_in_category[$row['id']]) ? count($courses_in_category[$row['id']]) : 0;
$key = 0;
if (!empty($courses_in_category[$row['id']])) {
foreach ($courses_in_category[$row['id']] as $course) {
?>
<tr>
<td>
<a name="course<?php echo $course['code']; ?>"></a>
<strong><?php echo $course['title']; ?></strong><br />
<?php
if (api_get_setting('display_coursecode_in_courselist') === 'true') {
echo $course['visual_code'];
}
if (api_get_setting('display_coursecode_in_courselist') === 'true' &&
api_get_setting('display_teacher_in_courselist') === 'true'
) {
echo " - ";
}
if (api_get_setting('display_teacher_in_courselist') === 'true') {
echo $course['tutor'];
}
?>
</td>
<td valign="top">
<!-- edit -->
<?php
if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) {
$edit_course = Security::remove_XSS($_GET['edit']); ?>
<form name="edit_course_category" method="post" action="courses.php?action=<?php echo $action; ?>">
<input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
<input type="hidden" name="course_2_edit_category" value="<?php echo $edit_course; ?>" />
<select name="course_categories">
<option value="0"><?php echo get_lang("NoCourseCategory"); ?></option>
<?php foreach ($user_course_categories as $row) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['title']; ?></option>
<?php } ?>
</select>
<button class="save" type="submit" name="submit_change_course_category"><?php echo get_lang('Ok'); ?></button>
</form>
<?php } ?>
<div style="float:left;width:110px;"> <?php if ($key < $number_of_courses - 1) { ?>
<?php <a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') { <?php echo Display::display_icon('down.png', get_lang('Down'),'',22); ?>
$icon_title = get_lang('CourseDetails') . ' - ' . $course['title'];
?>
<a href="<?php echo api_get_path(WEB_CODE_PATH); ?>inc/ajax/course_home.ajax.php?a=show_course_information&code=<?php echo $course['code'] ?>" data-title="<?php echo $icon_title ?>" title="<?php echo $icon_title ?>" class="ajax">
<?php echo Display::return_icon('info.png', $icon_title,'','22') ?>
<?php } ?>
</a> </a>
<?php } else { ?>
<?php echo Display::display_icon('down_na.png', get_lang('Down'),'',22); ?>
<?php } ?>
<?php if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) { ?> </div>
<?php echo Display::display_icon('edit_na.png', get_lang('Edit'),'',22); ?> <div style="float:left; margin-right:10px;">
<?php } else { ?> <?php
<a href="courses.php?action=<?php echo $action; ?>&amp;edit=<?php echo $course['code']; ?>&amp;sec_token=<?php echo $stok; ?>"> if ($course['status'] != 1) {
<?php echo Display::display_icon('edit.png', get_lang('Edit'),'',22); ?> if ($course['unsubscr'] == 1) {
</a> ?>
<?php } ?>
<?php if ($key > 0) { ?>
<a href="courses.php?action=<?php echo $action; ?>&amp;move=up&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::display_icon('up.png', get_lang('Up'),'',22); ?>
</a>
<?php } else { ?>
<?php echo Display::display_icon('up_na.png', get_lang('Up'),'',22); ?>
<?php } ?>
<?php if ($key < $number_of_courses - 1) { ?>
<a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
<?php echo Display::display_icon('down.png', get_lang('Down'),'',22); ?>
</a>
<?php } else { ?>
<?php echo Display::display_icon('down_na.png', get_lang('Down'),'',22); ?>
<?php } ?>
</div>
<div style="float:left; margin-right:10px;">
<?php if ($course['status'] != 1) {
if ($course['unsubscr'] == 1) {
?>
<form action="<?php echo api_get_self(); ?>" method="post" onsubmit="javascript: if (!confirm('<?php echo addslashes(api_htmlentities(get_lang("ConfirmUnsubscribeFromCourse"), ENT_QUOTES, api_get_system_encoding()))?>')) return false"> <form action="<?php echo api_get_self(); ?>" method="post" onsubmit="javascript: if (!confirm('<?php echo addslashes(api_htmlentities(get_lang("ConfirmUnsubscribeFromCourse"), ENT_QUOTES, api_get_system_encoding()))?>')) return false">
<input type="hidden" name="sec_token" value="<?php echo $stok; ?>"> <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
<input type="hidden" name="unsubscribe" value="<?php echo $course['code']; ?>" /> <input type="hidden" name="unsubscribe" value="<?php echo $course['code']; ?>" />
<button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub"> <button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub">
<?php echo get_lang('Unsubscribe'); ?> <?php echo get_lang('Unsubscribe'); ?>
</button> </button>
</form> </form>
</div> </div>
<?php } <?php }
}
$key++;
} }
echo '</table>'; $key++;
} }
echo '</table>';
}
} }
} }

@ -104,18 +104,7 @@ class Auth
*/ */
public function get_user_course_categories() public function get_user_course_categories()
{ {
$user_id = api_get_user_id(); return CourseManager::get_user_course_categories(api_get_user_id());
$table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
$sql = "SELECT * FROM " . $table_category . "
WHERE user_id=$user_id
ORDER BY sort ASC";
$result = Database::query($sql);
$output = array();
while ($row = Database::fetch_array($result)) {
$output[] = $row;
}
return $output;
} }
/** /**
@ -283,65 +272,53 @@ class Auth
/** /**
* Moves the course one place up or down * Moves the course one place up or down
* @param string Direction up/down * @param string $direction Direction up/down
* @param string Category id * @param string $category2move Category id
* @return bool True If it success * @return bool True If it success
*/ */
public function move_category($direction, $category2move) public function move_category($direction, $category2move)
{ {
// the database definition of the table that stores the user defined course categories $userId = api_get_user_id();
$table_user_defined_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); $userCategories = $this->get_user_course_categories();
$categories = array_values($userCategories);
$current_user_id = api_get_user_id(); $previous = null;
$user_coursecategories = $this->get_user_course_categories(); $target_category = [];
$user_course_categories_info = $this->get_user_course_categories_info(); foreach ($categories as $key => $category) {
$result = false;
foreach ($user_coursecategories as $key => $category) {
$category_id = $category['id']; $category_id = $category['id'];
if ($category2move == $category_id) { if ($category2move == $category_id) {
// source_course is the course where we clicked the up or down icon // source_course is the course where we clicked the up or down icon
$source_category = $user_course_categories_info[$category2move]; $source_category = $userCategories[$category2move];
// target_course is the course before/after the source_course (depending on the up/down icon) // target_course is the course before/after the source_course (depending on the up/down icon)
if ($direction == 'up') { if ($direction == 'up') {
$target_category = $user_course_categories_info[$user_coursecategories[$key - 1]['id']]; if (isset($categories[$key - 1])) {
$target_category = $userCategories[$categories[$key - 1]['id']];
}
} else { } else {
$target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']]; if (isset($categories[$key + 1])) {
$target_category = $userCategories[$categories[$key + 1]['id']];
}
} }
} }
} }
$result = false;
if (count($target_category) > 0 && count($source_category) > 0) { if (count($target_category) > 0 && count($source_category) > 0) {
$sql_update1 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($target_category['sort']) . "' $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $current_user_id . "'"; $sql = "UPDATE $table SET
$sql_update2 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($source_category['sort']) . "' sort = '" . Database::escape_string($target_category['sort']) . "'
WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $current_user_id . "'"; WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $userId . "'";
$resultFirst = Database::query($sql);
$result1 = Database::query($sql_update2); $sql = "UPDATE $table SET
$result2 = Database::query($sql_update1); sort = '" . Database::escape_string($source_category['sort']) . "'
if (Database::affected_rows($result1) && Database::affected_rows($result2)) { WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $userId . "'";
$resultSecond = Database::query($sql);
if (Database::affected_rows($resultFirst) && Database::affected_rows($resultSecond)) {
$result = true; $result = true;
} }
} }
return $result;
}
/** return $result;
* Retrieves the user defined course categories and all the info that goes with it
* @return array containing all the info of the user defined courses categories with the id as key of the array
*/
public function get_user_course_categories_info()
{
$current_user_id = api_get_user_id();
$table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
$sql = "SELECT * FROM " . $table_category . "
WHERE user_id='" . $current_user_id . "'
ORDER BY sort ASC";
$result = Database::query($sql);
while ($row = Database::fetch_array($result)) {
$output[$row['id']] = $row;
}
return $output;
} }
/** /**

@ -3965,24 +3965,21 @@ class CourseManager
/** /**
* Retrieves the user defined course categories * Retrieves the user defined course categories
* @param string $userId * @param int $userId
* @return array containing all the titles of the user defined courses with the id as key of the array * @return array
*/ */
public static function get_user_course_categories($userId = '') public static function get_user_course_categories($userId = 0)
{ {
if ($userId == '') { $userId = empty($userId) ? api_get_user_id() : (int) $userId;
$realUserId = api_get_user_id();
} else {
$realUserId = $userId;
}
$output = array();
$table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
$sql = "SELECT * FROM $table_category $sql = "SELECT * FROM $table_category
WHERE user_id = '".intval($realUserId)."'"; WHERE user_id = $userId
ORDER BY sort ASC
";
$result = Database::query($sql); $result = Database::query($sql);
while ($row = Database::fetch_array($result)) { $output = array();
$output[$row['id']] = $row['title']; while ($row = Database::fetch_array($result, 'ASSOC')) {
$output[$row['id']] = $row;
} }
return $output; return $output;
} }

@ -1853,15 +1853,13 @@ class IndexManager
$listUserCategories[0] = ''; $listUserCategories[0] = '';
$html .= '<div class="session-view-block">'; $html .= '<div class="session-view-block">';
foreach ($listUserCategories as $userCategoryId => $userCat) {
foreach ($listUserCategories as $userCategoryId => $userCatTitle) {
// add user category // add user category
$userCategoryHtml = ''; $userCategoryHtml = '';
if ($userCategoryId != 0) { if ($userCategoryId != 0) {
$userCategoryHtml = '<div class="session-view-well ">'; $userCategoryHtml = '<div class="session-view-well ">';
$userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCat['title']);
} }
$userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCatTitle);
// look for course in this userCat in session courses : $listCoursesInSession // look for course in this userCat in session courses : $listCoursesInSession
$htmlCategory = ''; $htmlCategory = '';
if (isset($listCoursesInSession[$userCategoryId])) { if (isset($listCoursesInSession[$userCategoryId])) {

Loading…
Cancel
Save