Browse Source

Use CourseManager::get_user_course_categories instead of custom code.

- Improve code foreach
- Fix PHP warning
- Format code
jmontoyaa 8 years ago
parent
commit
a444e67791

+ 0 - 1
main/auth/courses_controller.php

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

+ 125 - 131
main/auth/courses_list.php

@@ -3,6 +3,7 @@
 
 /**
 * View (MVC patter) for courses
+ * @todo fix this. use twig templates
 * @author Christian Fasanando <christian1827@gmail.com> - Beeznest
 * @package chamilo.auth
 */
@@ -11,9 +12,7 @@
 api_block_anonymous_users();
 $stok = Security::get_token();
 $courses_without_category = isset($courses_in_category[0]) ? $courses_in_category[0] : null;
-
 ?>
-
 <!-- Actions: The menu with the different options in cathe course management -->
 <div id="actions" class="actions">
     <?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 } ?>
 </div>
-
 <?php
 if (!empty($message)) {
     Display::display_confirmation_message($message, false);
@@ -29,151 +27,147 @@ if (!empty($message)) {
 
 // COURSES WITH CATEGORIES
 if (!empty($user_course_categories)) {
-        foreach ($user_course_categories as $row) {
-            echo Display::page_subheader($row['title']);
-            echo '<a name="category'.$row['id'].'"></a>';
-
-            if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) { ?>
-            <!-- We display the edit form for the category -->
+    $counter = 0;
+    $last = end($user_course_categories);
+    foreach ($user_course_categories as $row) {
+        echo Display::page_subheader($row['title']);
+        echo '<a name="category'.$row['id'].'"></a>';
+        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; ?>">
-                <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 if ($row['id'] != $last['id']) { ?>
+                <a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
+                <?php echo Display::return_icon('down.png', get_lang('Down'),'',22); ?>
+                </a>
+            <?php } else { ?>
+                <?php echo Display::return_icon('down_na.png', get_lang('Down'),'',22); ?>
             <?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 -->
-            <?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); ?>
+                    <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 (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>
 
-                <?php if ($row['id'] != $user_course_categories[0]['id']) { ?>
-                        <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 if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) { ?>
+                      <?php echo Display::display_icon('edit_na.png', get_lang('Edit'),'',22); ?>
                 <?php } else { ?>
-                    <?php echo Display::return_icon('up_na.png', get_lang('Up'),'',22); ?>
-               <?php } ?>
+                    <a href="courses.php?action=<?php echo $action; ?>&amp;edit=<?php echo $course['code']; ?>&amp;sec_token=<?php echo $stok; ?>">
+                        <?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']) { ?>
-                    <a href="courses.php?action=<?php echo $action; ?>&amp;move=down&amp;category=<?php echo $row['id']; ?>&amp;sec_token=<?php echo $stok; ?>">
-                    <?php echo Display::return_icon('down.png', get_lang('Down'),'',22); ?>
+                <?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::return_icon('down_na.png', get_lang('Down'),'',22); ?>
+                    <?php echo Display::display_icon('up_na.png', get_lang('Up'),'',22); ?>
                 <?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 (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 } ?>
+                <?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 } ?>
 
-                    <?php if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) { ?>
-                          <?php echo Display::display_icon('edit_na.png', get_lang('Edit'),'',22); ?>
-                    <?php } else { ?>
-                        <a href="courses.php?action=<?php echo $action; ?>&amp;edit=<?php echo $course['code']; ?>&amp;sec_token=<?php echo $stok; ?>">
-                            <?php echo Display::display_icon('edit.png', get_lang('Edit'),'',22); ?>
-                            </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) {
-                    ?>
+              </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">
-                        <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
-                        <input type="hidden" name="unsubscribe" value="<?php echo $course['code']; ?>" />
-                         <button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub">
-                        <?php echo get_lang('Unsubscribe'); ?>
-                        </button>
-                    </form>
-                  </div>
-                      <?php }
-                    }
-                    $key++;
+                <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="unsubscribe" value="<?php echo $course['code']; ?>" />
+                     <button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub">
+                    <?php echo get_lang('Unsubscribe'); ?>
+                    </button>
+                </form>
+              </div>
+                  <?php }
                 }
-                echo '</table>';
+                $key++;
             }
+            echo '</table>';
+        }
     }
 }
 

+ 28 - 51
main/inc/lib/auth.lib.php

@@ -104,18 +104,7 @@ class Auth
      */
     public function get_user_course_categories()
     {
-        $user_id = 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;
+        return CourseManager::get_user_course_categories(api_get_user_id());
     }
 
     /**
@@ -283,65 +272,53 @@ class Auth
 
     /**
      * Moves the course one place up or down
-     * @param string    Direction up/down
-     * @param string    Category id
+     * @param string    $direction Direction up/down
+     * @param string    $category2move Category id
      * @return bool     True If it success
      */
     public function move_category($direction, $category2move)
     {
-        // the database definition of the table that stores the user defined course categories
-        $table_user_defined_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
+        $userId = api_get_user_id();
+        $userCategories = $this->get_user_course_categories();
+        $categories = array_values($userCategories);
 
-        $current_user_id = api_get_user_id();
-        $user_coursecategories = $this->get_user_course_categories();
-        $user_course_categories_info = $this->get_user_course_categories_info();
-        $result = false;
-
-        foreach ($user_coursecategories as $key => $category) {
+        $previous = null;
+        $target_category = [];
+        foreach ($categories as $key => $category) {
             $category_id = $category['id'];
             if ($category2move == $category_id) {
                 // 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)
                 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 {
-                    $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) {
-            $sql_update1 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($target_category['sort']) . "'
-                            WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $current_user_id . "'";
-            $sql_update2 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($source_category['sort']) . "'
-                            WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $current_user_id . "'";
-
-            $result1 = Database::query($sql_update2);
-            $result2 = Database::query($sql_update1);
-            if (Database::affected_rows($result1) && Database::affected_rows($result2)) {
+            $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
+            $sql = "UPDATE $table SET 
+                    sort = '" . Database::escape_string($target_category['sort']) . "'
+                    WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $userId . "'";
+            $resultFirst = Database::query($sql);
+            $sql = "UPDATE $table SET 
+                    sort = '" . Database::escape_string($source_category['sort']) . "'
+                    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;
             }
         }
-        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;
+        return $result;
     }
 
     /**

+ 10 - 13
main/inc/lib/course.lib.php

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

+ 2 - 4
main/inc/lib/userportal.lib.php

@@ -1853,15 +1853,13 @@ class IndexManager
         $listUserCategories[0] = '';
 
         $html .= '<div class="session-view-block">';
-
-        foreach ($listUserCategories as $userCategoryId => $userCatTitle) {
+        foreach ($listUserCategories as $userCategoryId => $userCat) {
             // add user category
             $userCategoryHtml = '';
             if ($userCategoryId != 0) {
                 $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
             $htmlCategory = '';
             if (isset($listCoursesInSession[$userCategoryId])) {