1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class UrlManager
- * This library provides functions for the access_url management.
- * Include/require it in your code to use its functionality.
- *
- * @package chamilo.library
- */
- class UrlManager
- {
- /**
- * Creates a new url access
- *
- * @author Julio Montoya <gugli100@gmail.com>,
- *
- * @param string $url The URL of the site
- * @param string $description The description of the site
- * @param int $active is active or not
- * @return boolean if success
- */
- public static function add($url, $description, $active)
- {
- $tms = time();
- $table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "INSERT INTO $table
- SET url = '".Database::escape_string($url)."',
- description = '".Database::escape_string($description)."',
- active = '".intval($active)."',
- created_by = '".api_get_user_id()."',
- tms = FROM_UNIXTIME(".$tms.")";
- $result = Database::query($sql);
- return $result;
- }
- /**
- * Updates an URL access
- * @author Julio Montoya <gugli100@gmail.com>,
- *
- * @param int $url_id The url id
- * @param string $url
- * @param string $description The description of the site
- * @param int $active is active or not
- * @return boolean if success
- */
- public static function update($url_id, $url, $description, $active)
- {
- $url_id = intval($url_id);
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $tms = time();
- $sql = "UPDATE $table
- SET url = '".Database::escape_string($url)."',
- description = '".Database::escape_string($description)."',
- active = '".intval($active)."',
- created_by = '".api_get_user_id()."',
- tms = FROM_UNIXTIME(".$tms.")
- WHERE id = '$url_id'";
- $result = Database::query($sql);
- return $result;
- }
- /**
- * Deletes an url
- * @author Julio Montoya
- * @param int $id url id
- *
- * @return boolean true if success
- * */
- public static function delete($id)
- {
- $id = intval($id);
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql= "DELETE FROM $table WHERE id = ".$id;
- $result = Database::query($sql);
- return $result;
- }
- /**
- * @param string $url
- *
- * @return int
- */
- public static function url_exist($url)
- {
- $table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT id FROM $table
- WHERE url = '".Database::escape_string($url)."' ";
- $res = Database::query($sql);
- $num = Database::num_rows($res);
- return $num;
- }
- /**
- * @param string $url
- *
- * @return int
- */
- public static function url_id_exist($url)
- {
- if (empty($url)) {
- return false;
- }
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT id FROM $table WHERE id = ".intval($url)."";
- $res = Database::query($sql);
- $num = Database::num_rows($res);
- return $num;
- }
- /**
- * This function get the quantity of URLs
- * @author Julio Montoya
- * @return int count of urls
- * */
- public static function url_count()
- {
- $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT count(id) as count_result FROM $table_access_url";
- $res = Database::query($sql);
- $url = Database::fetch_array($res,'ASSOC');
- $result = $url['count_result'];
- return $result;
- }
- /**
- * Gets the id, url, description, and active status of ALL URLs
- * @author Julio Montoya
- * @return array
- * */
- public static function get_url_data()
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT id, url, description, active
- FROM $table
- ORDER BY id";
- $res = Database::query($sql);
- $urls = array ();
- while ($url = Database::fetch_array($res)) {
- $urls[] = $url;
- }
- return $urls;
- }
- /**
- * Gets the id, url, description, and active status of ALL URLs
- * @author Julio Montoya
- * @param int $url_id
- * @return array
- * */
- public static function get_url_data_from_id($url_id)
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT id, url, description, active
- FROM $table
- WHERE id = ".intval($url_id);
- $res = Database::query($sql);
- $row = Database::fetch_array($res);
- return $row;
- }
- /**
- * Gets the inner join of users and urls table
- * @author Julio Montoya
- * @param int access url id
- * @param string $order_by
- * @return array Database::store_result of the result
- **/
- public static function get_url_rel_user_data($access_url_id = null, $order_by = null)
- {
- $where = '';
- $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
- if (!empty($access_url_id)) {
- $where = "WHERE $table_url_rel_user.access_url_id = ".intval($access_url_id);
- }
- if (empty($order_by)) {
- $order_clause = api_sort_by_first_name(
- ) ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
- } else {
- $order_clause = $order_by;
- }
- $sql = "SELECT u.user_id, lastname, firstname, username, official_code, access_url_id
- FROM $tbl_user u
- INNER JOIN $table_url_rel_user
- ON $table_url_rel_user.user_id = u.user_id
- $where $order_clause";
- $result = Database::query($sql);
- $users = Database::store_result($result);
- return $users;
- }
- /**
- * Gets the inner join of access_url and the course table
- *
- * @author Julio Montoya
- * @param int access url id
- * @return array Database::store_result of the result
- **/
- public static function get_url_rel_course_data($access_url_id = null)
- {
- $where = '';
- $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
- if (!empty($access_url_id)) {
- $where = " WHERE uc.access_url_id = ".intval($access_url_id);
- }
- $sql = "SELECT u.id, c_id, title, uc.access_url_id
- FROM $tbl_course u
- INNER JOIN $table_url_rel_course uc
- ON uc.c_id = u.id
- $where
- ORDER BY title, code";
- $result = Database::query($sql);
- $courses = Database::store_result($result);
- return $courses;
- }
- /**
- * Gets the number of rows with a specific course_code in access_url_rel_course table
- * @author Yoselyn Castillo
- * @param int $courseId
- *
- * @return int Database::num_rows($res);
- **/
- public static function getCountUrlRelCourse($courseId)
- {
- $courseId = intval($courseId);
- $tableUrlRelCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $sql = "SELECT *
- FROM $tableUrlRelCourse
- WHERE c_id = '$courseId'";
- $res = Database::query($sql);
- return Database::num_rows($res);
- }
- /**
- * Gets the inner join of access_url and the session table
- * @author Julio Montoya
- * @param int $access_url_id access url id
- *
- * @return array Database::store_result of the result
- *
- **/
- public static function get_url_rel_session_data($access_url_id = null)
- {
- $where ='';
- $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
- if (!empty($access_url_id))
- $where ="WHERE $table_url_rel_session.access_url_id = ".intval($access_url_id);
- $sql = "SELECT id, name, access_url_id
- FROM $tbl_session u
- INNER JOIN $table_url_rel_session
- ON $table_url_rel_session.session_id = id
- $where
- ORDER BY name, id";
- $result = Database::query($sql);
- $sessions = Database::store_result($result);
- return $sessions;
- }
- /**
- * 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 get_url_rel_usergroup_data($access_url_id = null)
- {
- $where = '';
- $table_url_rel_usergroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
- $table_user_group = Database::get_main_table(TABLE_USERGROUP);
- if (!empty($access_url_id)) {
- $where ="WHERE $table_url_rel_usergroup.access_url_id = ".intval($access_url_id);
- }
- $sql = "SELECT id, name, access_url_id
- FROM $table_user_group u
- INNER JOIN $table_url_rel_usergroup
- ON $table_url_rel_usergroup.usergroup_id = u.id
- $where
- ORDER BY name";
- $result = Database::query($sql);
- $courses = Database::store_result($result);
- 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
- * @param string lock || unlock
- * @param int url id
- * */
- public static function set_url_status($status, $url_id)
- {
- $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- if ($status == 'lock') {
- $status_db = '0';
- }
- if ($status == 'unlock') {
- $status_db = '1';
- }
- if (($status_db == '1' || $status_db == '0') && is_numeric($url_id)) {
- $sql = "UPDATE $url_table SET active='".intval($status_db)."'
- WHERE id='".intval($url_id)."'";
- Database::query($sql);
- }
- }
- /**
- * Checks the relationship between an URL and a User (return the num_rows)
- * @author Julio Montoya
- * @param int user id
- * @param int url id
- * @return boolean true if success
- * */
- public static function relation_url_user_exist($user_id, $url_id)
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $sql= "SELECT user_id FROM $table
- WHERE access_url_id = ".intval($url_id)." AND user_id = ".intval($user_id)." ";
- $result = Database::query($sql);
- $num = Database::num_rows($result);
- return $num;
- }
- /**
- * Checks the relationship between an URL and a Course (return the num_rows)
- * @author Julio Montoya
- * @param int $courseId
- * @param int $urlId
- * @return boolean true if success
- * */
- public static function relation_url_course_exist($courseId, $urlId)
- {
- $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $sql= "SELECT c_id FROM $table_url_rel_course
- WHERE
- access_url_id = ".intval($urlId)." AND
- c_id = '".intval($courseId)."'";
- $result = Database::query($sql);
- $num = Database::num_rows($result);
- return $num;
- }
- /**
- * Checks the relationship between an URL and a UserGr
- * oup (return the num_rows)
- * @author Julio Montoya
- * @param int $userGroupId
- * @param int $urlId
- * @return boolean true if success
- * */
- public static function relationUrlUsergroupExist($userGroupId, $urlId)
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
- $sql= "SELECT usergroup_id FROM $table
- WHERE access_url_id = ".intval($urlId)." AND
- usergroup_id = ".intval($userGroupId);
- $result = Database::query($sql);
- $num = Database::num_rows($result);
- return $num;
- }
- /**
- * Checks the relationship between an URL and a Session (return the num_rows)
- * @author Julio Montoya
- * @param int user id
- * @param int url id
- * @return boolean true if success
- * */
- public static function relation_url_session_exist($session_id, $url_id)
- {
- $table_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $session_id = intval($session_id);
- $url_id = intval($url_id);
- $sql = "SELECT session_id FROM $table_url_rel_session
- WHERE
- access_url_id = ".intval($url_id)." AND
- session_id = ".Database::escape_string($session_id);
- $result = Database::query($sql);
- $num = Database::num_rows($result);
- return $num;
- }
- /**
- * Add a group of users into a group of URLs
- * @author Julio Montoya
- * @param array of user_ids
- * @param array of url_ids
- * @return array
- * */
- public static function add_users_to_urls($user_list, $url_list)
- {
- $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $result_array = array();
- if (is_array($user_list) && is_array($url_list)){
- foreach ($url_list as $url_id) {
- foreach ($user_list as $user_id) {
- $count = UrlManager::relation_url_user_exist($user_id,$url_id);
- if ($count==0) {
- $sql = "INSERT INTO $table_url_rel_user
- SET user_id = ".intval($user_id).", access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- if ($result) {
- $result_array[$url_id][$user_id] = 1;
- } else {
- $result_array[$url_id][$user_id] = 0;
- }
- }
- }
- }
- }
- return $result_array;
- }
- /**
- * Add a group of courses into a group of URLs
- * @author Julio Montoya
- * @param array of course ids
- * @param array of url_ids
- * @return array
- **/
- public static function add_courses_to_urls($course_list,$url_list)
- {
- $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $result_array = array();
- if (is_array($course_list) && is_array($url_list)){
- foreach ($url_list as $url_id) {
- foreach ($course_list as $course_code) {
- $courseInfo = api_get_course_info($course_code);
- $courseId = $courseInfo['real_id'];
- $count = self::relation_url_course_exist($courseId, $url_id);
- if ($count==0) {
- $sql = "INSERT INTO $table_url_rel_course
- SET c_id = '".$courseId."', access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- if ($result) {
- $result_array[$url_id][$course_code] = 1;
- } else {
- $result_array[$url_id][$course_code] = 0;
- }
- }
- }
- }
- }
- return $result_array;
- }
- /**
- * Add a group of user group into a group of URLs
- * @author Julio Montoya
- * @param array $userGroupList of course ids
- * @param array $urlList of url_ids
- * @return array
- **/
- public static function addUserGroupListToUrl($userGroupList, $urlList)
- {
- $resultArray = array();
- if (is_array($userGroupList) && is_array($urlList)) {
- foreach ($urlList as $urlId) {
- foreach ($userGroupList as $userGroupId) {
- $count = self::relationUrlUsergroupExist($userGroupId, $urlId);
- if ($count == 0) {
- $result = self::addUserGroupToUrl($userGroupId, $urlId);
- if ($result) {
- $resultArray[$urlId][$userGroupId] = 1;
- } else {
- $resultArray[$urlId][$userGroupId] = 0;
- }
- }
- }
- }
- }
- return $resultArray;
- }
- /**
- * Add a group of user group into a group of URLs
- * @author Julio Montoya
- * @param array of course ids
- * @param array of url_ids
- * @return array
- **/
- public static function addCourseCategoryListToUrl($courseCategoryList, $urlList)
- {
- $resultArray = array();
- if (is_array($courseCategoryList) && is_array($urlList)) {
- foreach ($urlList as $urlId) {
- foreach ($courseCategoryList as $categoryCourseId) {
- $count = self::relationUrlCourseCategoryExist($categoryCourseId, $urlId);
- if ($count == 0) {
- $result = self::addCourseCategoryToUrl($categoryCourseId, $urlId);
- if ($result) {
- $resultArray[$urlId][$categoryCourseId] = 1;
- } else {
- $resultArray[$urlId][$categoryCourseId] = 0;
- }
- }
- }
- }
- }
- return $resultArray;
- }
- /**
- * Checks the relationship between an URL and a UserGr
- * oup (return the num_rows)
- * @author Julio Montoya
- * @param int $categoryCourseId
- * @param int $urlId
- * @return boolean true if success
- * */
- public static function relationUrlCourseCategoryExist($categoryCourseId, $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)." AND
- course_category_id = ".intval($categoryCourseId);
- $result = Database::query($sql);
- $num = Database::num_rows($result);
- return $num;
- }
- /**
- * @param int $userGroupId
- * @param int $urlId
- * @return int
- */
- public static function addUserGroupToUrl($userGroupId, $urlId)
- {
- $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 $categoryId
- * @param int $urlId
- * @return int
- */
- public static function addCourseCategoryToUrl($categoryId, $urlId)
- {
- $exists = self::relationUrlCourseCategoryExist($categoryId, $urlId);
- if (empty($exists)) {
- $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
- $sql = "INSERT INTO $table
- SET
- course_category_id = '".intval($categoryId)."',
- access_url_id = ".intval($urlId);
- Database::query($sql);
- return Database::insert_id();
- }
- return 0;
- }
- /**
- * Add a group of sessions into a group of URLs
- * @author Julio Montoya
- * @param array of session ids
- * @param array of url_ids
- * @return array
- * */
- public static function add_sessions_to_urls($session_list, $url_list)
- {
- $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $result_array = array();
- if (is_array($session_list) && is_array($url_list)) {
- foreach ($url_list as $url_id) {
- foreach ($session_list as $session_id) {
- $count = UrlManager::relation_url_session_exist($session_id, $url_id);
- if ($count == 0) {
- $sql = "INSERT INTO $table_url_rel_session
- SET session_id = ".intval(
- $session_id
- ).", access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- if ($result) {
- $result_array[$url_id][$session_id] = 1;
- } else {
- $result_array[$url_id][$session_id] = 0;
- }
- }
- }
- }
- }
- return $result_array;
- }
- /**
- * Add a user into a url
- * @author Julio Montoya
- * @param $user_id
- * @param $url_id
- *
- * @return boolean true if success
- * */
- public static function add_user_to_url($user_id, $url_id = 1)
- {
- $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- if (empty($url_id)) {
- $url_id = 1;
- }
- $count = UrlManager::relation_url_user_exist($user_id, $url_id);
- $result = true;
- if (empty($count)) {
- $sql = "INSERT INTO $table_url_rel_user (user_id, access_url_id)
- VALUES ('".intval($user_id)."', '".intval($url_id)."') ";
- $result = Database::query($sql);
- }
- return $result;
- }
- /**
- * @param int $courseId
- * @param int $url_id
- *
- * @return resource
- */
- public static function add_course_to_url($courseId, $url_id = 1)
- {
- $table_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- if (empty($url_id)) {
- $url_id = 1;
- }
- $count = UrlManager::relation_url_course_exist($courseId, $url_id);
- if (empty($count)) {
- $sql = "INSERT INTO $table_url_rel_course
- SET c_id = '".intval($courseId)."', access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- }
- return $result;
- }
- /**
- * Inserts a session to a URL (access_url_rel_session table)
- * @param int Session ID
- * @param int URL ID
- *
- * @return bool True on success, false session already exists or insert failed
- */
- public static function add_session_to_url($session_id, $url_id = 1)
- {
- $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- if (empty($url_id)) {
- $url_id = 1;
- }
- $result = false;
- $count = UrlManager::relation_url_session_exist($session_id, $url_id);
- $session_id = intval($session_id);
- if (empty($count) && !empty($session_id)) {
- $url_id = intval($url_id);
- $sql = "INSERT INTO $table_url_rel_session
- SET session_id = ".intval($session_id).", access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- }
- return $result;
- }
- /**
- * Deletes an url and user relationship
- * @author Julio Montoya
- * @param int user id
- * @param int url id
- *
- * @return boolean true if success
- * */
- public static function delete_url_rel_user($user_id, $url_id)
- {
- $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $result = true;
- if (!empty($user_id) && !empty($url_id)) {
- $sql= "DELETE FROM $table_url_rel_user
- WHERE user_id = ".intval($user_id)." AND access_url_id = ".intval($url_id);
- $result = Database::query($sql);
- }
- return $result;
- }
- /**
- * Deletes an url and course relationship
- * @author Julio Montoya
- * @param int $courseId
- * @param int $urlId
- *
- * @return boolean true if success
- * */
- public static function delete_url_rel_course($courseId, $urlId)
- {
- $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $sql= "DELETE FROM $table_url_rel_course
- WHERE c_id = '".intval($courseId)."' AND access_url_id=".intval($urlId)." ";
- $result = Database::query($sql);
- 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 delete_url_rel_usergroup($userGroupId, $urlId)
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
- $sql= "DELETE FROM $table
- WHERE usergroup_id = '".intval($userGroupId)."' AND
- access_url_id=".intval($urlId)." ";
- $result = Database::query($sql);
- 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
- * @author Julio Montoya
- * @param char course code
- * @param int url id
- *
- * @return boolean true if success
- * */
- public static function delete_url_rel_session($session_id, $url_id)
- {
- $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $sql= "DELETE FROM $table_url_rel_session
- WHERE session_id = ".intval($session_id)." AND access_url_id=".intval($url_id)." ";
- $result = Database::query($sql,'ASSOC');
- return $result;
- }
- /**
- * Updates the access_url_rel_user table with a given user list
- * @author Julio Montoya
- * @param array user list
- * @param int access_url_id
- * */
- public static function update_urls_rel_user($user_list, $access_url_id)
- {
- $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = ".intval($access_url_id);
- $result = Database::query($sql);
- $existing_users = array();
- //Getting all users
- while ($row = Database::fetch_array($result)) {
- $existing_users[] = $row['user_id'];
- }
- // Adding users
- $users_added = array();
- foreach ($user_list as $user_id_to_add) {
- if (!in_array($user_id_to_add, $existing_users)) {
- $result = UrlManager::add_user_to_url($user_id_to_add, $access_url_id);
- if ($result) {
- $users_added[] = $user_id_to_add;
- }
- }
- }
- $users_deleted = array();
- // Deleting old users
- foreach ($existing_users as $user_id_to_delete) {
- if (!in_array($user_id_to_delete, $user_list)) {
- $result = UrlManager::delete_url_rel_user($user_id_to_delete, $access_url_id);
- if ($result) {
- $users_deleted[] = $user_id_to_delete;
- }
- }
- }
- if (empty($users_added) && empty($users_deleted)) {
- return false;
- }
- return array('users_added' => $users_added, 'users_deleted' => $users_deleted);
- }
- /**
- * Updates the access_url_rel_course table with a given user list
- * @author Julio Montoya
- * @param array $course_list
- * @param int access_url_id
- * */
- public static function update_urls_rel_course($course_list, $access_url_id)
- {
- $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
- $sql = "SELECT c_id FROM $table_url_rel_course
- WHERE access_url_id = ".intval($access_url_id);
- $result = Database::query($sql);
- $existing_courses = array();
- while ($row = Database::fetch_array($result)){
- $existing_courses[] = $row['c_id'];
- }
- // Adding courses
- foreach ($course_list as $course_code) {
- if (!in_array($course_code, $courseId)) {
- UrlManager::add_course_to_url($courseId, $access_url_id);
- CourseManager::update_course_ranking($courseId, 0, $access_url_id);
- }
- }
- // Deleting old courses
- foreach ($existing_courses as $courseId) {
- if (!in_array($courseId, $course_list)) {
- UrlManager::delete_url_rel_course($courseId, $access_url_id);
- CourseManager::update_course_ranking($courseId, 0, $access_url_id);
- }
- }
- }
- /**
- * Updates the access_url_rel_course table with a given user list
- * @author Julio Montoya
- * @param array user list
- * @param int access_url_id
- * */
- public static function update_urls_rel_usergroup($userGroupList, $urlId)
- {
- $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
- $sql = "SELECT usergroup_id FROM $table WHERE access_url_id = ".intval($urlId);
- $result = Database::query($sql);
- $existingItems = array();
- while ($row = Database::fetch_array($result)){
- $existingItems[] = $row['usergroup_id'];
- }
- // Adding
- foreach ($userGroupList as $userGroupId) {
- if (!in_array($userGroupId, $existingItems)) {
- UrlManager::addUserGroupToUrl($userGroupId, $urlId);
- }
- }
- // Deleting old items
- foreach ($existingItems as $userGroupId) {
- if (!in_array($userGroupId, $userGroupList)) {
- UrlManager::delete_url_rel_usergroup($userGroupId, $urlId);
- }
- }
- }
- /**
- * 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) {
- 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
- * @param array user list
- * @param int access_url_id
- * */
- public static function update_urls_rel_session($session_list,$access_url_id)
- {
- $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id=".intval($access_url_id);
- $result = Database::query($sql);
- $existing_sessions = array();
- while ($row = Database::fetch_array($result)){
- $existing_sessions[] = $row['session_id'];
- }
- // Adding users
- foreach ($session_list as $session) {
- if (!in_array($session, $existing_sessions)) {
- if (!empty($session) && !empty($access_url_id)) {
- UrlManager::add_session_to_url($session, $access_url_id);
- }
- }
- }
- // Deleting old users
- foreach ($existing_sessions as $existing_session) {
- if (!in_array($existing_session, $session_list)) {
- if (!empty($existing_session) && !empty($access_url_id)) {
- UrlManager::delete_url_rel_session($existing_session, $access_url_id);
- }
- }
- }
- }
- /**
- * @param int $user_id
- * @return array
- */
- public static function get_access_url_from_user($user_id)
- {
- $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT url, access_url_id FROM $table_url_rel_user url_rel_user INNER JOIN $table_url u
- ON (url_rel_user.access_url_id = u.id)
- WHERE user_id = ".intval($user_id);
- $result = Database::query($sql);
- $url_list = Database::store_result($result,'ASSOC');
- return $url_list;
- }
- /**
- * @param $session_id
- * @return array
- */
- public static function get_access_url_from_session($session_id)
- {
- $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
- $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT url, access_url_id FROM $table_url_rel_session url_rel_session INNER JOIN $table_url u
- ON (url_rel_session.access_url_id = u.id)
- WHERE session_id = ".intval($session_id);
- $result = Database::query($sql);
- $url_list = Database::store_result($result);
- return $url_list;
- }
- /**
- * @param string $url
- * @return bool|mixed|null
- */
- public static function get_url_id($url)
- {
- $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
- $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."'";
- $result = Database::query($sql);
- $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 .= '<a
- href="javascript: void(0);"
- onclick="javascript: add_user_to_url(\''.addslashes($data['id']).'\',\''.addslashes($data['name']).' \')">'.$data['name'].' </a><br />';
- } else {
- $return .= '...<br />';
- }
- }
- }
- $response->addAssign('ajax_list_courses', 'innerHTML', api_utf8_encode($return));
- return $response;
- }
- }
|