From 07e11a31b1cec43488fc539b10e5967549c16501 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Mon, 17 Sep 2012 16:29:59 -0500 Subject: [PATCH 1/3] Added optimization recommendation for lp_item table in optimization guide --- documentation/optimization.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/documentation/optimization.html b/documentation/optimization.html index e9a667ff7a..f66c91f02c 100644 --- a/documentation/optimization.html +++ b/documentation/optimization.html @@ -77,6 +77,14 @@ An optional additional caching mechanism you may use is the realpath_cache_size

2. Slow queries

Enable slow_queries in /etc/mysqld/my.cnf, restart MySQL then follow using sudo tail -f /var/log/mysql/mysql-slow.log +

+In Chamilo 1.9 in particular, due to the merge of all databases into one, you might experience performance issue if you have many learning paths with many items in them.
+To solve this performance issue, you can execute the following queries manually in your database:
+
+ALTER TABLE lp_item ADD INDEX idx_c_lp_item_cid_lp_id (c_id, lp_id);
+ALTER TABLE lp_item_view ADD INDEX  idx_c_lp_item_view_cid_lp_view_id_lp_item_id (c_id, lp_view_id, lp_item_id);
+
+These will be available in Chamilo 1.10 directly, but we cannot put them into Chamilo 1.9 from now on for organizational reasons.

3. Indexes caching

One good reference: MySQL documentation on multiple key caches
From d63a9ff1a8a9cd24869d3b63d3d5425a9bf72d7e Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 18 Sep 2012 10:58:45 +0200 Subject: [PATCH 2/3] Minor - cosmetic changes --- main/inc/lib/auth.lib.php | 464 +++++++++++++++++++------------------- 1 file changed, 236 insertions(+), 228 deletions(-) diff --git a/main/inc/lib/auth.lib.php b/main/inc/lib/auth.lib.php index 54dc6d58e6..2fc74ee475 100644 --- a/main/inc/lib/auth.lib.php +++ b/main/inc/lib/auth.lib.php @@ -1,4 +1,5 @@ 0) { + if (Database::num_rows($special_course_result) > 0) { $special_course_list = array(); while ($result_row = Database::fetch_array($special_course_result)) { - $special_course_list[] = '"'.$result_row['course_code'].'"'; + $special_course_list[] = '"' . $result_row['course_code'] . '"'; } } $without_special_courses = ''; if (!empty($special_course_list)) { - $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')'; + $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')'; } // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category @@ -56,8 +58,8 @@ class Auth course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat FROM $TABLECOURS course, $TABLECOURSUSER course_rel_user WHERE course.code = course_rel_user.course_code - AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH." - AND course_rel_user.user_id = '".$user_id."' $without_special_courses + AND course_rel_user.relation_type<>" . COURSE_RELATION_TYPE_RRHH . " + AND course_rel_user.user_id = '" . $user_id . "' $without_special_courses ORDER BY course_rel_user.sort ASC"; $result = Database::query($sql_select_courses); while ($row = Database::fetch_array($result)) { @@ -70,49 +72,49 @@ class Auth /** * retrieves the user defined course categories * @return array containing all the IDs of the user defined courses categories, sorted by the "sort" field - */ + */ public function get_user_course_categories() { $user_id = api_get_user_id(); $table_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); - $sql = "SELECT * FROM ".$table_category." WHERE user_id=$user_id ORDER BY sort ASC"; + $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; + $output[] = $row; } return $output; } /** - * This function get all the courses in the particular user category; - * @param int User category id - * @return string: the name of the user defined course category - */ + * This function get all the courses in the particular user category; + * @param int User category id + * @return string: the name of the user defined course category + */ public function get_courses_in_category() { $user_id = api_get_user_id(); // table definitions - $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE); + $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $TABLE_USER_COURSE_CATEGORY = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); - $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); - $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); + $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); + $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); // get course list auto-register $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " . - " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; + " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; $special_course_result = Database::query($sql); - if(Database::num_rows($special_course_result)>0) { - $special_course_list = array(); - while ($result_row = Database::fetch_array($special_course_result)) { - $special_course_list[] = '"'.$result_row['course_code'].'"'; - } + if (Database::num_rows($special_course_result) > 0) { + $special_course_list = array(); + while ($result_row = Database::fetch_array($special_course_result)) { + $special_course_list[] = '"' . $result_row['course_code'] . '"'; + } } $without_special_courses = ''; if (!empty($special_course_list)) { - $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')'; + $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')'; } $sql_select_courses = "SELECT course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr, @@ -121,8 +123,8 @@ class Auth FROM $TABLECOURS course, $TABLECOURSUSER course_rel_user WHERE course.code = course_rel_user.course_code - AND course_rel_user.user_id = '".$user_id."' - AND course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH." + AND course_rel_user.user_id = '" . $user_id . "' + AND course_rel_user.relation_type <> " . COURSE_RELATION_TYPE_RRHH . " $without_special_courses ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC"; $result = Database::query($sql_select_courses); @@ -132,7 +134,6 @@ class Auth $data[$course['user_course_cat']][] = $course; } return $data; - } /** @@ -140,7 +141,7 @@ class Auth * @param string Course code * @param int Category id * @return bool True if it success - */ + */ public function store_changecoursecategory($course_code, $newcategory) { $course_code = Database::escape_string($course_code); $newcategory = intval($newcategory); @@ -150,9 +151,11 @@ class Auth $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $max_sort_value = api_max_sort_value($newcategory, $current_user); // max_sort_value($newcategory); - Database::query("UPDATE $TABLECOURSUSER SET user_course_cat='".$newcategory."', sort='".($max_sort_value + 1)."' WHERE course_code='".$course_code."' AND user_id='".$current_user."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." "); + Database::query("UPDATE $TABLECOURSUSER SET user_course_cat='" . $newcategory . "', sort='" . ($max_sort_value + 1) . "' WHERE course_code='" . $course_code . "' AND user_id='" . $current_user . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " "); - if (Database::affected_rows()) { $result = true; } + if (Database::affected_rows()) { + $result = true; + } return $result; } @@ -162,7 +165,7 @@ class Auth * @param string Course code * @param int Category id * @return bool True if it success - */ + */ public function move_course($direction, $course2move, $category) { // definition of tables @@ -175,30 +178,32 @@ class Auth // we need only the courses of the category we are moving in $user_courses = array(); foreach ($all_user_courses as $key => $course) { - if ($course['user_course_category'] == $category) { - $user_courses[] = $course; - } + if ($course['user_course_category'] == $category) { + $user_courses[] = $course; + } } foreach ($user_courses as $key => $course) { - if ($course2move == $course['code']) { - // source_course is the course where we clicked the up or down icon - $source_course = $course; - // target_course is the course before/after the source_course (depending on the up/down icon) - if ($direction == 'up') { - $target_course = $user_courses[$key - 1]; - } else { - $target_course = $user_courses[$key + 1]; - } - } + if ($course2move == $course['code']) { + // source_course is the course where we clicked the up or down icon + $source_course = $course; + // target_course is the course before/after the source_course (depending on the up/down icon) + if ($direction == 'up') { + $target_course = $user_courses[$key - 1]; + } else { + $target_course = $user_courses[$key + 1]; + } + } } if (count($target_course) > 0 && count($source_course) > 0) { - $sql_update1 = "UPDATE $TABLECOURSUSER SET sort='".$target_course['sort']."' WHERE course_code='".$source_course['code']."' AND user_id='".$current_user_id."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." "; - $sql_update2 = "UPDATE $TABLECOURSUSER SET sort='".$source_course['sort']."' WHERE course_code='".$target_course['code']."' AND user_id='".$current_user_id."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." "; - Database::query($sql_update2); - Database::query($sql_update1); - if (Database::affected_rows()) { $result = true; } + $sql_update1 = "UPDATE $TABLECOURSUSER SET sort='" . $target_course['sort'] . "' WHERE course_code='" . $source_course['code'] . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " "; + $sql_update2 = "UPDATE $TABLECOURSUSER SET sort='" . $source_course['sort'] . "' WHERE course_code='" . $target_course['code'] . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " "; + Database::query($sql_update2); + Database::query($sql_update1); + if (Database::affected_rows()) { + $result = true; + } } return $result; } @@ -220,27 +225,27 @@ class Auth $result = false; foreach ($user_coursecategories 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]; - // 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']]; - } else { - $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']]; - } + $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]; + // 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']]; + } else { + $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']]; } + } } 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."'"; - Database::query($sql_update2); - Database::query($sql_update1); - if (Database::affected_rows()) { - $result = true; - } + $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 . "'"; + Database::query($sql_update2); + Database::query($sql_update1); + if (Database::affected_rows()) { + $result = true; + } } return $result; } @@ -248,14 +253,14 @@ class Auth /** * 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_user_personal_table(TABLE_USER_COURSE_CATEGORY); - $sql = "SELECT * FROM ".$table_category." WHERE user_id='".$current_user_id."' ORDER BY sort ASC"; + $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; + $output[$row['id']] = $row; } return $output; } @@ -265,16 +270,18 @@ class Auth * @param string Category title * @param int Category id * @return bool True if it success - */ + */ public function store_edit_course_category($title, $category_id) { // protect data $title = Database::escape_string($title); $category_id = intval($category_id); $result = false; $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); - $sql_update = "UPDATE $tucc SET title='".api_htmlentities($title, ENT_QUOTES, api_get_system_encoding())."' WHERE id='".$category_id."'"; + $sql_update = "UPDATE $tucc SET title='" . api_htmlentities($title, ENT_QUOTES, api_get_system_encoding()) . "' WHERE id='" . $category_id . "'"; Database::query($sql_update); - if (Database::affected_rows()) { $result = true; } + if (Database::affected_rows()) { + $result = true; + } return $result; } @@ -282,18 +289,20 @@ class Auth * deletes a course category and moves all the courses that were in this category to main category * @param int Category id * @return bool True if it success - */ + */ public function delete_course_category($category_id) { $current_user_id = api_get_user_id(); $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $category_id = intval($category_id); $result = false; - $sql_delete = "DELETE FROM $tucc WHERE id='".$category_id."' and user_id='".$current_user_id."'"; + $sql_delete = "DELETE FROM $tucc WHERE id='" . $category_id . "' and user_id='" . $current_user_id . "'"; Database::query($sql_delete); - if (Database::affected_rows()) { $result = true; } - $sql_update = "UPDATE $TABLECOURSUSER SET user_course_cat='0' WHERE user_course_cat='".$category_id."' AND user_id='".$current_user_id."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." "; - Database::query($sql_update); + if (Database::affected_rows()) { + $result = true; + } + $sql_update = "UPDATE $TABLECOURSUSER SET user_course_cat='0' WHERE user_course_cat='" . $category_id . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " "; + Database::query($sql_update); return $result; } @@ -301,7 +310,7 @@ class Auth * unsubscribe the user from a given course * @param string Course code * @return bool True if it success - */ + */ public function remove_user_from_course($course_code) { $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); @@ -314,22 +323,22 @@ class Auth // we check (once again) if the user is not course administrator // because the course administrator cannot unsubscribe himself // (s)he can only delete the course - $sql_check = "SELECT * FROM $tbl_course_user WHERE user_id='".$current_user_id."' AND course_code='".$course_code."' AND status='1' "; + $sql_check = "SELECT * FROM $tbl_course_user WHERE user_id='" . $current_user_id . "' AND course_code='" . $course_code . "' AND status='1' "; $result_check = Database::query($sql_check); $number_of_rows = Database::num_rows($result_check); if ($number_of_rows > 0) { - $result = false; + $result = false; } CourseManager::unsubscribe_user($current_user_id, $course_code); - return $result; + return $result; } /** * stores the user course category in the chamilo_user database * @param string Category title * @return bool True if it success - */ + */ public function store_course_category($category_title) { $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); @@ -340,20 +349,22 @@ class Auth $result = false; // step 1: we determine the max value of the user defined course categories - $sql = "SELECT sort FROM $tucc WHERE user_id='".$current_user_id."' ORDER BY sort DESC"; + $sql = "SELECT sort FROM $tucc WHERE user_id='" . $current_user_id . "' ORDER BY sort DESC"; $rs_sort = Database::query($sql); $maxsort = Database::fetch_array($rs_sort); $nextsort = $maxsort['sort'] + 1; // step 2: we check if there is already a category with this name, if not we store it, else we give an error. - $sql = "SELECT * FROM $tucc WHERE user_id='".$current_user_id."' AND title='".$category_title."'ORDER BY sort DESC"; + $sql = "SELECT * FROM $tucc WHERE user_id='" . $current_user_id . "' AND title='" . $category_title . "'ORDER BY sort DESC"; $rs = Database::query($sql); if (Database::num_rows($rs) == 0) { - $sql_insert = "INSERT INTO $tucc (user_id, title,sort) VALUES ('".$current_user_id."', '".api_htmlentities($category_title, ENT_QUOTES, api_get_system_encoding())."', '".$nextsort."')"; - Database::query($sql_insert); - if (Database::affected_rows()) { $result = true; } + $sql_insert = "INSERT INTO $tucc (user_id, title,sort) VALUES ('" . $current_user_id . "', '" . api_htmlentities($category_title, ENT_QUOTES, api_get_system_encoding()) . "', '" . $nextsort . "')"; + Database::query($sql_insert); + if (Database::affected_rows()) { + $result = true; + } } else { - $result = false; + $result = false; } return $result; } @@ -362,39 +373,39 @@ class Auth * Counts the number of courses in a given course category * @param string Category code * @return int Count of courses - */ + */ public function count_courses_in_category($category_code) { $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); - $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); + $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); + $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); // get course list auto-register $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " . - " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; + " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; $special_course_result = Database::query($sql); - if(Database::num_rows($special_course_result)>0) { - $special_course_list = array(); - while ($result_row = Database::fetch_array($special_course_result)) { - $special_course_list[] = '"'.$result_row['course_code'].'"'; - } + if (Database::num_rows($special_course_result) > 0) { + $special_course_list = array(); + while ($result_row = Database::fetch_array($special_course_result)) { + $special_course_list[] = '"' . $result_row['course_code'] . '"'; + } } $without_special_courses = ''; if (!empty($special_course_list)) { - $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')'; + $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')'; } - $sql = "SELECT * FROM $tbl_course WHERE category_code".(empty($category_code) ? " IS NULL" : "='".$category_code."'").$without_special_courses; + $sql = "SELECT * FROM $tbl_course WHERE category_code" . (empty($category_code) ? " IS NULL" : "='" . $category_code . "'") . $without_special_courses; // Showing only the courses of the current Dokeos access_url_id. global $_configuration; if ($_configuration['multiple_access_urls']) { - $url_access_id = api_get_current_access_url_id(); - if ($url_access_id != -1) { - $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); - $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course + $url_access_id = api_get_current_access_url_id(); + if ($url_access_id != -1) { + $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) - WHERE access_url_id = $url_access_id AND category_code".(empty($category_code) ? " IS NULL" : "='".$category_code."'").$without_special_courses; - } + WHERE access_url_id = $url_access_id AND category_code" . (empty($category_code) ? " IS NULL" : "='" . $category_code . "'") . $without_special_courses; + } } return Database::num_rows(Database::query($sql)); } @@ -418,116 +429,115 @@ class Auth } } return $categories; - } + } /** * Display all the courses in the given course category. I could have used a parameter here * @param string Category code * @return array Courses data - */ + */ public function browse_courses_in_category($category_code, $random_value = null) { - global $_configuration; - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); + global $_configuration; + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); - + // Get course list auto-register $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; $special_course_result = Database::query($sql); - if (Database::num_rows($special_course_result)>0) { + if (Database::num_rows($special_course_result) > 0) { $special_course_list = array(); while ($result_row = Database::fetch_array($special_course_result)) { - $special_course_list[] = '"'.$result_row['course_code'].'"'; + $special_course_list[] = '"' . $result_row['course_code'] . '"'; } } - + $without_special_courses = ''; if (!empty($special_course_list)) { - $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')'; - } - + $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')'; + } + if (!empty($random_value)) { $random_value = intval($random_value); - + $sql = "SELECT COUNT(*) FROM $tbl_course"; $result = Database::query($sql); list($num_records) = Database::fetch_row($result); - + if ($_configuration['multiple_access_urls']) { - - $url_access_id = api_get_current_access_url_id(); + + $url_access_id = api_get_current_access_url_id(); $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); - + $sql = "SELECT COUNT(*) FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) WHERE access_url_id = $url_access_id "; $result = Database::query($sql); list($num_records) = Database::fetch_row($result); - + $sql = "SELECT course.id FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) WHERE access_url_id = $url_access_id AND RAND()*$num_records< $random_value - $without_special_courses ORDER BY RAND() LIMIT 0, $random_value"; + $without_special_courses ORDER BY RAND() LIMIT 0, $random_value"; } else { $sql = "SELECT id FROM $tbl_course course WHERE RAND()*$num_records< $random_value $without_special_courses ORDER BY RAND() LIMIT 0, $random_value"; } - + $result = Database::query($sql); $id_in = null; - while (list($id) = Database::fetch_row($result)){ + while (list($id) = Database::fetch_row($result)) { if ($id_in) { - $id_in.=",$id"; - } else { - $id_in="$id"; + $id_in.=",$id"; + } else { + $id_in = "$id"; } - } + } $sql = "SELECT * FROM $tbl_course WHERE id IN($id_in)"; - } else { $category_code = Database::escape_string($category_code); $sql = "SELECT * FROM $tbl_course WHERE category_code='$category_code' $without_special_courses ORDER BY title "; - + //showing only the courses of the current Chamilo access_url_id if ($_configuration['multiple_access_urls']) { - $url_access_id = api_get_current_access_url_id(); + $url_access_id = api_get_current_access_url_id(); $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) - WHERE access_url_id = $url_access_id AND category_code='$category_code' $without_special_courses ORDER BY title"; - } + WHERE access_url_id = $url_access_id AND category_code='$category_code' $without_special_courses ORDER BY title"; + } } $result = Database::query($sql); $courses = array(); - while ($row = Database::fetch_array($result)) { + while ($row = Database::fetch_array($result)) { $row['registration_code'] = !empty($row['registration_code']); - $count_users = CourseManager::get_users_count_in_course($row['code']); - $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time()-(30*86400))); + $count_users = CourseManager::get_users_count_in_course($row['code']); + $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400))); if ($row['tutor_name'] == '0') { $row['tutor_name'] = get_lang('NoManager'); - } + } $point_info = CourseManager::get_course_ranking($row['id'], 0); $courses[] = array( - 'real_id' => $row['id'], - 'point_info' => $point_info, - 'code' => $row['code'], - 'directory' => $row['directory'], - 'db' => $row['db_name'], - 'visual_code' => $row['visual_code'], - 'title' => $row['title'], - 'tutor' => $row['tutor_name'], - 'subscribe' => $row['subscribe'], - 'unsubscribe' => $row['unsubscribe'], - 'registration_code' => $row['registration_code'], - 'creation_date' => $row['creation_date'], - 'visibility' => $row['visibility'], - 'count_users' => $count_users, - 'count_connections' => $count_connections_last_month - ); + 'real_id' => $row['id'], + 'point_info' => $point_info, + 'code' => $row['code'], + 'directory' => $row['directory'], + 'db' => $row['db_name'], + 'visual_code' => $row['visual_code'], + 'title' => $row['title'], + 'tutor' => $row['tutor_name'], + 'subscribe' => $row['subscribe'], + 'unsubscribe' => $row['unsubscribe'], + 'registration_code' => $row['registration_code'], + 'creation_date' => $row['creation_date'], + 'visibility' => $row['visibility'], + 'count_users' => $count_users, + 'count_connections' => $count_connections_last_month + ); } return $courses; @@ -538,108 +548,106 @@ class Auth * The search is done on the code, title and tutor field of the course table. * @param string $search_term: the string that the user submitted, what we are looking for * @return array an array containing a list of all the courses (the code, directory, dabase, visual_code, title, ... ) matching the the search term. - */ + */ public function search_courses($search_term) { - $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE); - $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); - $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); + $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE); + $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD); + $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); // get course list auto-register $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; $special_course_result = Database::query($sql); - if (Database::num_rows($special_course_result)>0) { + if (Database::num_rows($special_course_result) > 0) { $special_course_list = array(); while ($result_row = Database::fetch_array($special_course_result)) { - $special_course_list[] = '"'.$result_row['course_code'].'"'; + $special_course_list[] = '"' . $result_row['course_code'] . '"'; } } $without_special_courses = ''; if (!empty($special_course_list)) { - $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')'; + $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')'; } $search_term_safe = Database::escape_string($search_term); - $sql_find = "SELECT * FROM $TABLECOURS WHERE (code LIKE '%".$search_term_safe."%' OR title LIKE '%".$search_term_safe."%' OR tutor_name LIKE '%".$search_term_safe."%') $without_special_courses ORDER BY title, visual_code ASC"; + $sql_find = "SELECT * FROM $TABLECOURS WHERE (code LIKE '%" . $search_term_safe . "%' OR title LIKE '%" . $search_term_safe . "%' OR tutor_name LIKE '%" . $search_term_safe . "%') $without_special_courses ORDER BY title, visual_code ASC"; global $_configuration; if ($_configuration['multiple_access_urls']) { - $url_access_id = api_get_current_access_url_id(); - if ($url_access_id != -1) { - $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); - $sql_find = "SELECT * FROM $TABLECOURS as course INNER JOIN $tbl_url_rel_course as url_rel_course + $url_access_id = api_get_current_access_url_id(); + if ($url_access_id != -1) { + $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + $sql_find = "SELECT * FROM $TABLECOURS as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) - WHERE access_url_id = $url_access_id AND (code LIKE '%".$search_term_safe."%' OR title LIKE '%".$search_term_safe."%' OR tutor_name LIKE '%".$search_term_safe."%' ) $without_special_courses ORDER BY title, visual_code ASC "; - } + WHERE access_url_id = $url_access_id AND (code LIKE '%" . $search_term_safe . "%' OR title LIKE '%" . $search_term_safe . "%' OR tutor_name LIKE '%" . $search_term_safe . "%' ) $without_special_courses ORDER BY title, visual_code ASC "; + } } $result_find = Database::query($sql_find); $courses = array(); while ($row = Database::fetch_array($result_find)) { $row['registration_code'] = !empty($row['registration_code']); $count_users = count(CourseManager::get_user_list_from_course_code($row['code'])); - $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time()-(30*86400))); + $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400))); $courses[] = array( - 'code' => $row['code'], - 'directory' => $row['directory'], - 'db' => $row['db_name'], - 'visual_code' => $row['visual_code'], - 'title' => $row['title'], - 'tutor' => $row['tutor_name'], - 'subscribe' => $row['subscribe'], - 'unsubscribe' => $row['unsubscribe'], - 'registration_code' => $row['registration_code'], - 'creation_date' => $row['creation_date'], - 'visibility' => $row['visibility'], - 'count_users' => $count_users, - 'count_connections' => $count_connections_last_month - ); + 'code' => $row['code'], + 'directory' => $row['directory'], + 'db' => $row['db_name'], + 'visual_code' => $row['visual_code'], + 'title' => $row['title'], + 'tutor' => $row['tutor_name'], + 'subscribe' => $row['subscribe'], + 'unsubscribe' => $row['unsubscribe'], + 'registration_code' => $row['registration_code'], + 'creation_date' => $row['creation_date'], + 'visibility' => $row['visibility'], + 'count_users' => $count_users, + 'count_connections' => $count_connections_last_month + ); } return $courses; } - /** - * Subscribe the user to a given course - * @param string Course code - * @return string Message about results + /** + * Subscribe the user to a given course + * @param string Course code + * @return string Message about results */ public function subscribe_user($course_code) { global $_user; - $all_course_information = CourseManager::get_course_information($course_code); + $all_course_information = CourseManager::get_course_information($course_code); if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) { - if (api_is_platform_admin()) { - $status_user_in_new_course = COURSEMANAGER; - } else { - $status_user_in_new_course=null; - } - if (CourseManager::add_user_to_course($_user['user_id'], $course_code, $status_user_in_new_course)) { - $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code); - if ($send == 1) { - CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = false); - } else if ($send == 2){ - CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = true); - } - return get_lang('EnrollToCourseSuccessful'); - } else { - return get_lang('ErrorContactPlatformAdmin'); + if (api_is_platform_admin()) { + $status_user_in_new_course = COURSEMANAGER; + } else { + $status_user_in_new_course = null; + } + if (CourseManager::add_user_to_course($_user['user_id'], $course_code, $status_user_in_new_course)) { + $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code); + if ($send == 1) { + CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = false); + } else if ($send == 2) { + CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = true); } + return get_lang('EnrollToCourseSuccessful'); + } else { + return get_lang('ErrorContactPlatformAdmin'); + } } else { - $return = ''; - if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) { - return false; - } - $return .= get_lang('CourseRequiresPassword').'
'; - $return .= $all_course_information['visual_code'].' - '.$all_course_information['title']; - - $return .= "
"; - $return .= ''; - //$return .= ""; - //$return .= ""; - $return .= ""; - $return .= "
"; - return $return; + $return = ''; + if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) { + return false; + } + $return .= get_lang('CourseRequiresPassword') . '
'; + $return .= $all_course_information['visual_code'] . ' - ' . $all_course_information['title']; + + $return .= "
"; + $return .= ''; + $return .= ""; + $return .= "
"; + return $return; } } -} +} \ No newline at end of file From e9a9a1a17ed9991485583dd159d50d44d93856c2 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 18 Sep 2012 11:31:07 +0200 Subject: [PATCH 3/3] Fixing course protected by password see #5470 --- main/auth/courses.php | 16 ++++++-- main/auth/courses_controller.php | 17 ++++----- main/inc/lib/auth.lib.php | 38 ++++++++++--------- .../default/auth/courses_categories.php | 11 +++--- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/main/auth/courses.php b/main/auth/courses.php index 319851b356..b55e468a85 100644 --- a/main/auth/courses.php +++ b/main/auth/courses.php @@ -73,7 +73,7 @@ if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create } // filter actions -$actions = array('sortmycourses', 'createcoursecategory', 'subscribe', 'deletecoursecategory', 'display_courses','display_random_courses'); +$actions = array('sortmycourses', 'createcoursecategory', 'subscribe', 'deletecoursecategory', 'display_courses', 'display_random_courses' , 'subscribe_user_with_password'); $action = 'display_random_courses'; $nameTools = get_lang('SortMyCourses'); @@ -88,6 +88,10 @@ if ($action == 'subscribe') { $nameTools = get_lang('CourseManagement'); } +if ($action == 'subscribe_user_with_password') { + $nameTools = get_lang('CourseManagement'); +} + if ($action == 'display_random_courses' || $action == 'display_courses' ) { $nameTools = get_lang('CourseManagement'); } @@ -164,9 +168,9 @@ if (isset($_REQUEST['search_course'])) { } } -// subscribe user to course -if (isset($_GET['subscribe_course'])) { - if ($ctok == $_GET['sec_token']) { +// Subscribe user to course +if (isset($_REQUEST['subscribe_course'])) { + if ($ctok == $_GET['sec_token']) { $courses_controller->subscribe_user($_GET['subscribe_course'], $_GET['search_term'], $_GET['category_code']); } } @@ -185,6 +189,10 @@ if (isset($_POST['unsubscribe'])) { } } switch ($action) { + case 'subscribe_user_with_password': + $courses_controller->subscribe_user($_POST['subscribe_user_with_password'], $_POST['search_term'], $_POST['category_code']); + exit; + break; case 'createcoursecategory': $courses_controller->categories_list($action); break; diff --git a/main/auth/courses_controller.php b/main/auth/courses_controller.php index 947c33d655..747e13b237 100644 --- a/main/auth/courses_controller.php +++ b/main/auth/courses_controller.php @@ -79,7 +79,7 @@ class CoursesController { // extends Controller { * @param string action * @param string Category code (optional) */ - public function courses_categories($action, $category_code = null, $message = '', $error = '') { + public function courses_categories($action, $category_code = null, $message = '', $error = '', $content = null) { $data = array(); $browse_course_categories = $this->model->browse_course_categories(); @@ -117,6 +117,7 @@ class CoursesController { // extends Controller { $data['user_coursecodes'] = $user_coursecodes; $data['action'] = $action; $data['message'] = $message; + $data['content'] = $content; $data['error'] = $error; // render to the view @@ -170,23 +171,19 @@ class CoursesController { // extends Controller { */ public function subscribe_user($course_code, $search_term, $category_code) { $data = array(); - $result = $this->model->subscribe_user($course_code); + $result = $this->model->subscribe_user($course_code); if (!$result) { $error = get_lang('CourseRegistrationCodeIncorrect'); } else { //Redirect directly to the course after subscription - /* - if ($result) { - $url = api_get_course_url($course_code); - header('Location: '.$url); - exit; - }*/ - $message = $result; + $message = $result['message']; + $content = $result['content']; } + if (!empty($search_term)) { $this->search_courses($search_term, $message, $error); } else { - $this->courses_categories('subcribe', $category_code, $message, $error); + $this->courses_categories('subcribe', $category_code, $message, $error, $content); } return $result; } diff --git a/main/inc/lib/auth.lib.php b/main/inc/lib/auth.lib.php index 2fc74ee475..6325cc7a88 100644 --- a/main/inc/lib/auth.lib.php +++ b/main/inc/lib/auth.lib.php @@ -614,40 +614,42 @@ class Auth { * @return string Message about results */ public function subscribe_user($course_code) { - global $_user; - + $user_id = api_get_user_id(); $all_course_information = CourseManager::get_course_information($course_code); + if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) { if (api_is_platform_admin()) { $status_user_in_new_course = COURSEMANAGER; } else { $status_user_in_new_course = null; } - if (CourseManager::add_user_to_course($_user['user_id'], $course_code, $status_user_in_new_course)) { + if (CourseManager::add_user_to_course($user_id, $course_code, $status_user_in_new_course)) { $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code); if ($send == 1) { - CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = false); + CourseManager::email_to_tutor($user_id, $course_code, $send_to_tutor_also = false); } else if ($send == 2) { - CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = true); + CourseManager::email_to_tutor($user_id, $course_code, $send_to_tutor_also = true); } - return get_lang('EnrollToCourseSuccessful'); + $message = get_lang('EnrollToCourseSuccessful'); } else { - return get_lang('ErrorContactPlatformAdmin'); - } + $message = get_lang('ErrorContactPlatformAdmin'); + } + return array('message' => $message); } else { - - $return = ''; if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) { return false; } - $return .= get_lang('CourseRequiresPassword') . '
'; - $return .= $all_course_information['visual_code'] . ' - ' . $all_course_information['title']; - - $return .= "
"; - $return .= ''; - $return .= ""; - $return .= "
"; - return $return; + $message = get_lang('CourseRequiresPassword') . '
'; + $message .= $all_course_information['title'].' ('.$all_course_information['visual_code'].') '; + + $action = api_get_path(WEB_CODE_PATH) . "auth/courses.php?action=subscribe_user_with_password&sec_token=" . $_SESSION['sec_token']; + $form = new FormValidator('subscribe_user_with_password', 'post', $action); + $form->addElement('hidden', 'sec_token', $_SESSION['sec_token']); + $form->addElement('hidden', 'subscribe_user_with_password', $all_course_information['code']); + $form->addElement('text', 'course_registration_code'); + $form->addElement('button', 'submit', get_lang('SubmitRegistrationCode')); + $content = $form->return_form(); + return array('message' => $message, 'content' => $content); } } } \ No newline at end of file diff --git a/main/template/default/auth/courses_categories.php b/main/template/default/auth/courses_categories.php index d94114f7c5..86942317a6 100644 --- a/main/template/default/auth/courses_categories.php +++ b/main/template/default/auth/courses_categories.php @@ -8,7 +8,7 @@ */ $stok = Security::get_token(); ?> -