diff --git a/main/admin/access_url_edit_course_category_to_url.php b/main/admin/access_url_edit_course_category_to_url.php
new file mode 100644
index 0000000000..ea57ce06e3
--- /dev/null
+++ b/main/admin/access_url_edit_course_category_to_url.php
@@ -0,0 +1,337 @@
+
+*/
+
+// name of the language file that needs to be included
+$language_file = 'admin';
+
+// resetting the course id
+$cidReset = true;
+
+require_once '../inc/global.inc.php';
+require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php';
+require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
+require_once api_get_path(LIBRARY_PATH).'xajax/xajax.inc.php';
+
+$xajax = new xajax();
+$xajax->registerFunction(array('searchCourseCategoryAjax', 'UrlManager', 'searchCourseCategoryAjax'));
+
+// Setting the section (for the tabs)
+$this_section = SECTION_PLATFORM_ADMIN;
+
+// Access restrictions
+api_protect_global_admin_script();
+if (!api_get_multiple_access_url()) {
+ header('Location: index.php');
+ exit;
+}
+
+// Setting breadcrumbs
+$tool_name = get_lang('EditUserGroupToURL');
+$interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
+$interbreadcrumb[] = array ('url' => 'access_urls.php', 'name' => get_lang('MultipleAccessURLs'));
+
+$add_type = 'multiple';
+if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
+ $add_type = Security::remove_XSS($_REQUEST['add_type']);
+}
+
+$access_url_id = 1;
+if (isset($_REQUEST['access_url_id']) && $_REQUEST['access_url_id'] != '') {
+ $access_url_id = Security::remove_XSS($_REQUEST['access_url_id']);
+}
+
+$xajax->processRequests();
+$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
+$htmlHeadXtra[] = '
+';
+
+$form_sent=0;
+$errorMsg='';
+$UserList=$SessionList = array();
+$users=$sessions = array();
+
+if (isset($_POST['form_sent']) && $_POST['form_sent']) {
+ $form_sent = $_POST['form_sent'];
+ $list = $_POST['course_list'];
+
+ if (!is_array($list)) {
+ $list = array();
+ }
+
+ if ($form_sent == 1) {
+ if ($access_url_id == 0) {
+ header('Location: access_url_edit_users_to_url.php?action=show_message&message='.get_lang('SelectURL'));
+ } elseif (is_array($list)) {
+ UrlManager::updateUrlRelCourseCategory($list, $access_url_id);
+ header('Location: access_urls.php?action=show_message&message='.get_lang('Updated'));
+ }
+ exit;
+ }
+}
+
+Display::display_header($tool_name);
+
+api_display_tool_title($tool_name);
+
+if ($_GET['action'] == 'show_message') {
+ Display :: display_normal_message(Security::remove_XSS(stripslashes($_GET['message'])));
+}
+
+$noUserGroupList = $userGroupList = array();
+$ajax_search = $add_type == 'unique' ? true : false;
+
+if ($ajax_search) {
+ $userGroups = UrlManager::getUrlRelCourseCategory($access_url_id);
+ foreach ($userGroups as $item) {
+ $userGroupList[$item['id']] = $item;
+ }
+} else {
+ $userGroups = UrlManager::getUrlRelCourseCategory();
+
+ foreach ($userGroups as $item) {
+ if ($item['access_url_id'] == $access_url_id) {
+ $userGroupList[$item['id']] = $item ;
+ }
+ }
+ $noUserGroupList = getCourseCategoryNotInList(array_keys($userGroupList));
+}
+
+if ($add_type == 'multiple') {
+ $link_add_type_unique = ''.get_lang('SessionAddTypeUnique').'';
+ $link_add_type_multiple = get_lang('SessionAddTypeMultiple');
+} else {
+ $link_add_type_unique = get_lang('SessionAddTypeUnique');
+ $link_add_type_multiple = ''.get_lang('SessionAddTypeMultiple').'';
+}
+
+$url_list = UrlManager::get_url_data();
+?>
+
+ |
+
+
+
+
+ 'index.php', 'name' => get_lang('PlatformAdmin'));
diff --git a/main/admin/access_urls.php b/main/admin/access_urls.php
index 91a42dff4f..b881bb69da 100644
--- a/main/admin/access_urls.php
+++ b/main/admin/access_urls.php
@@ -25,6 +25,7 @@ Display :: display_header($tool_name);
require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php';
require_once api_get_path(LIBRARY_PATH).'usergroup.lib.php';
+require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
$my_user_url_list = api_get_access_url_from_user(api_get_user_id());
$current_access_url_id = api_get_current_access_url_id();
@@ -108,10 +109,22 @@ echo Display::url(Display::return_icon('new_link.png', get_lang('AddUrl'), arra
echo Display::url(Display::return_icon('user.png', get_lang('ManageUsers'), array(), ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'admin/access_url_edit_users_to_url.php');
echo Display::url(Display::return_icon('course.png', get_lang('ManageCourses'), array(), ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'admin/access_url_edit_courses_to_url.php');
//echo Display::url(Display::return_icon('session.png', get_lang('ManageSessions'), array(), ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'admin/access_url_edit_sessions_to_url.php');
+
$userGroup = new Usergroup();
if ($userGroup->getUseMultipleUrl()) {
- echo Display::url(Display::return_icon('class.png', get_lang('ManageUserGroup'), array(), ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'admin/access_url_edit_usergroup_to_url.php');
+ echo Display::url(
+ Display::return_icon('class.png', get_lang('ManageUserGroup'), array(), ICON_SIZE_MEDIUM),
+ api_get_path(WEB_CODE_PATH).'admin/access_url_edit_usergroup_to_url.php'
+ );
+}
+
+if (isMultipleUrlSupport()) {
+ echo Display::url(
+ Display::return_icon('folder.png', get_lang('ManageCourseCategories'), array(), ICON_SIZE_MEDIUM),
+ api_get_path(WEB_CODE_PATH).'admin/access_url_edit_course_category_to_url.php'
+ );
}
+
echo '';
//$table = new SortableTable('urls', 'url_count_mask', 'get_url_data_mask',2);
diff --git a/main/inc/lib/course_category.lib.php b/main/inc/lib/course_category.lib.php
index 6fe7535d6b..a701bbfc98 100644
--- a/main/inc/lib/course_category.lib.php
+++ b/main/inc/lib/course_category.lib.php
@@ -10,6 +10,22 @@ function isMultipleUrlSupport()
return false;
}
+/**
+ * @param int $categoryId
+ * @return array
+ */
+function getCategoryById($categoryId)
+{
+ $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
+ $categoryId = Database::escape_string($categoryId);
+ $sql = "SELECT * FROM $tbl_category WHERE id = '$categoryId'";
+ $result = Database::query($sql);
+ if (Database::num_rows($result)) {
+ return Database::fetch_array($result, 'ASSOC');
+ }
+ return array();
+}
+
/**
* @param string $category
* @return array
@@ -238,6 +254,24 @@ function compterFils($pere, $cpt)
return ($cpt + 1);
}
+/**
+ * @param string $categoryCode
+ * @return array
+ */
+function getChildren($categoryCode)
+{
+ $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
+ $categoryCode = Database::escape_string($categoryCode);
+ $result = Database::query("SELECT code, id FROM $tbl_category WHERE parent_id = '$categoryCode'");
+ $children = array();
+ while ($row = Database::fetch_array($result, 'ASSOC')) {
+ $children[] = $row;
+ $subChildren = getChildren($row['code']);
+ $children = array_merge($children, $subChildren);
+ }
+ return $children;
+}
+
/**
* @param string $categorySource
* @return string
@@ -583,6 +617,27 @@ function setCategoriesInForm($form, $defaultCode = null, $parentCode = null , $p
}
}
+/**
+ * @param array $list
+ * @return array
+ */
+function getCourseCategoryNotInList($list)
+{
+ $table = Database::get_main_table(TABLE_MAIN_CATEGORY);
+ if (empty($list)) {
+ return array();
+ }
+
+ $list = array_map('intval', $list);
+ $listToString = implode("','", $list);
+
+ $sql = "SELECT * FROM $table WHERE id NOT IN ('$listToString') AND (parent_id IS NULL) ";
+ $result = Database::query($sql);
+ return Database::store_result($result, 'ASSOC');
+}
+
+
+
/**
CREATE TABLE IF NOT EXISTS access_url_rel_course_category (access_url_id int unsigned NOT NULL, course_category_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, course_category_id));
*/
\ No newline at end of file
diff --git a/main/inc/lib/urlmanager.lib.php b/main/inc/lib/urlmanager.lib.php
index 989c17d000..be21c2ad6f 100644
--- a/main/inc/lib/urlmanager.lib.php
+++ b/main/inc/lib/urlmanager.lib.php
@@ -264,6 +264,37 @@ class UrlManager
return $courses;
}
+ /**
+ * Gets the inner join of access_url and the usergroup table
+ *
+ * @author Julio Montoya
+ * @param int access url id
+ * @return array Database::store_result of the result
+ **/
+
+ public static function getUrlRelCourseCategory($access_url_id = null)
+ {
+
+ $table_url_rel = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
+ $table = Database::get_main_table(TABLE_MAIN_CATEGORY);
+ $where = " WHERE 1=1 ";
+ if (!empty($access_url_id)) {
+ $where .= " AND $table_url_rel.access_url_id = ".intval($access_url_id);
+ }
+ $where .= " AND (parent_id IS NULL) ";
+
+ $sql = "SELECT id, name, access_url_id
+ FROM $table u
+ INNER JOIN $table_url_rel
+ ON $table_url_rel.course_category_id = u.id
+ $where
+ ORDER BY name";
+
+ $result = Database::query($sql);
+ $courses = Database::store_result($result, 'ASSOC');
+ return $courses;
+ }
+
/**
* Sets the status of an URL 1 or 0
* @author Julio Montoya
@@ -501,34 +532,35 @@ class UrlManager
}
/**
- * @param int $categoryCourseId
+ * @param int $userGroupId
* @param int $urlId
* @return int
*/
- public static function addCourseCategoryToUrl($categoryCourseId, $urlId)
+ public static function addUserGroupToUrl($userGroupId, $urlId)
{
- $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
- $sql = "INSERT INTO $table SET
- course_category_id = '".intval($categoryCourseId)."',
+ $urlRelUserGroupTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
+ $sql = "INSERT INTO $urlRelUserGroupTable
+ SET
+ usergroup_id = '".intval($userGroupId)."',
access_url_id = ".intval($urlId);
Database::query($sql);
return Database::insert_id();
}
-
/**
- * @param int $userGroupId
+ * @param int $id
* @param int $urlId
* @return int
*/
- public static function addUserGroupToUrl($userGroupId, $urlId)
+ public static function addCourseCategoryToUrl($id, $urlId)
{
- $urlRelUserGroupTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
- $sql = "INSERT INTO $urlRelUserGroupTable
+ $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
+ $sql = "INSERT INTO $table
SET
- usergroup_id = '".intval($userGroupId)."',
+ course_category_id = '".intval($id)."',
access_url_id = ".intval($urlId);
Database::query($sql);
+
return Database::insert_id();
}
@@ -680,6 +712,24 @@ class UrlManager
return $result;
}
+ /**
+ * Deletes an url and $userGroup relationship
+ * @author Julio Montoya
+ * @param int $userGroupId
+ * @param int $urlId
+ * @return boolean true if success
+ * */
+ public static function deleteUrlRelCourseCategory($userGroupId, $urlId)
+ {
+ $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
+ $sql= "DELETE FROM $table
+ WHERE course_category_id = '".intval($userGroupId)."' AND
+ access_url_id=".intval($urlId)." ";
+ $result = Database::query($sql);
+ return $result;
+ }
+
+
/**
* Deletes an url and session relationship
@@ -813,6 +863,56 @@ class UrlManager
}
}
+ /**
+ * Updates the access_url_rel_course_category table with a given list
+ * @author Julio Montoya
+ * @param array course category list
+ * @param int access_url_id
+ **/
+ public static function updateUrlRelCourseCategory($list, $urlId)
+ {
+ $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
+
+ $sql = "SELECT course_category_id FROM $table WHERE access_url_id = ".intval($urlId);
+ $result = Database::query($sql);
+ $existingItems = array();
+
+ while ($row = Database::fetch_array($result)){
+ $existingItems[] = $row['course_category_id'];
+ }
+
+ // Adding
+ foreach ($list as $id) {
+ if (!in_array($id, $existingItems)) {
+ UrlManager::addCourseCategoryToUrl($id, $urlId);
+ $categoryInfo = getCategoryById($id);
+ $children = getChildren($categoryInfo['code']);
+ if (!empty($children)) {
+ foreach ($children as $category) {
+ UrlManager::addCourseCategoryToUrl($category['id'], $urlId);
+ }
+ }
+ }
+ }
+
+ // Deleting old items
+ foreach ($existingItems as $id) {
+ if (!in_array($id, $list)) {
+ UrlManager::deleteUrlRelCourseCategory($id, $urlId);
+ $categoryInfo = getCategoryById($id);
+
+ $children = getChildren($categoryInfo['code']);
+ if (!empty($children)) {
+ foreach ($children as $category) {
+ UrlManager::deleteUrlRelCourseCategory($category['id'], $urlId);
+ }
+ }
+ }
+ }
+ }
+
+
+
/**
* Updates the access_url_rel_session table with a given user list
* @author Julio Montoya
@@ -894,4 +994,42 @@ class UrlManager
$access_url_id = Database::result($result, 0, 0);
return $access_url_id;
}
+
+ /**
+ *
+ * @param string $needle
+ * @return XajaxResponse
+ */
+ public static function searchCourseCategoryAjax($needle)
+ {
+ $response = new XajaxResponse();
+ $return = '';
+
+ if (!empty($needle)) {
+ // xajax send utf8 datas... datas in db can be non-utf8 datas
+ $charset = api_get_system_encoding();
+ $needle = api_convert_encoding($needle, $charset, 'utf-8');
+ $needle = Database::escape_string($needle);
+ // search courses where username or firstname or lastname begins likes $needle
+ $sql = 'SELECT id, name FROM '.Database::get_main_table(TABLE_MAIN_CATEGORY).' u
+ WHERE name LIKE "'.$needle.'%" AND (parent_id IS NULL or parent_id = 0)
+ ORDER BY name
+ LIMIT 11';
+ $result = Database::query($sql);
+ $i = 0;
+ while ($data = Database::fetch_array($result)) {
+ $i++;
+ if ($i <= 10) {
+ $return .= ''.$data['name'].'
';
+ } else {
+ $return .= '...
';
+ }
+ }
+ }
+ $response->addAssign('ajax_list_courses', 'innerHTML', api_utf8_encode($return));
+ return $response;
+ }
+
}