sessionmanager.lib.php 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the session library for Chamilo.
  5. * All main sessions functions should be placed here.
  6. * This class provides methods for sessions management.
  7. * Include/require it in your code to use its features.
  8. * @package chamilo.library
  9. */
  10. /**
  11. * The SessionManager class manages all the Chamilo sessions (as in course
  12. * groups).
  13. * @package chamilo.library.session
  14. */
  15. class SessionManager
  16. {
  17. //See BT#4871
  18. CONST SESSION_CHANGE_USER_REASON_SCHEDULE = 1;
  19. CONST SESSION_CHANGE_USER_REASON_CLASSROOM = 2;
  20. CONST SESSION_CHANGE_USER_REASON_LOCATION = 3;
  21. CONST SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION = 4;
  22. CONST DEFAULT_VISIBILITY = 4; //SESSION_AVAILABLE
  23. /**
  24. * Fetches a session from the database
  25. * @param int Session ID
  26. * @return array Session details
  27. * @assert (-1) === array()
  28. */
  29. public static function fetch($id) {
  30. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  31. if ($id != strval(intval($id))) { return array(); }
  32. $s = "SELECT * FROM $t WHERE id = $id";
  33. $r = Database::query($s);
  34. if (Database::num_rows($r) != 1) { return array(); }
  35. return Database::fetch_array($r,'ASSOC');
  36. }
  37. /**
  38. * Creates a session
  39. * @param array Fields to use in the creation of the session
  40. * @param boolean Whether to allow for same-name sessions or not.
  41. * @assert (array()) === false
  42. */
  43. public static function add($params, $allow_homonyms=false) {
  44. global $_configuration;
  45. //just in case
  46. if (isset($params['id'])) {
  47. unset($params['id']);
  48. }
  49. //Check portal limits
  50. $access_url_id = 1;
  51. if (api_is_multiple_url_enabled()) {
  52. $access_url_id = api_get_current_access_url_id();
  53. }
  54. if (is_array($_configuration[$access_url_id]) && isset($_configuration[$access_url_id]['hosting_limit_sessions']) && $_configuration[$access_url_id]['hosting_limit_sessions'] > 0) {
  55. $num = self::count_sessions();
  56. if ($num >= $_configuration[$access_url_id]['hosting_limit_sessions']) {
  57. return get_lang('PortalSessionsLimitReached');
  58. }
  59. }
  60. if (!$allow_homonyms) {
  61. $my_session_result = SessionManager::get_session_by_name($params['name']);
  62. $session_id = null;
  63. if ($my_session_result == false) {
  64. $session_model = new SessionModel();
  65. $session_id = $session_model->save($params);
  66. } else {
  67. error_log('Session already exists with name: '.$params['name']." session_id: ".$my_session_result['id']);
  68. }
  69. } else {
  70. //with the allow_homonyms option, two sessions can share names
  71. $session_model = new SessionModel();
  72. $session_id = $session_model->save($params);
  73. }
  74. if (!empty($session_id)) {
  75. /*
  76. Sends a message to the user_id = 1
  77. $user_info = api_get_user_info(1);
  78. $complete_name = $user_info['firstname'].' '.$user_info['lastname'];
  79. $subject = api_get_setting('siteName').' - '.get_lang('ANewSessionWasCreated');
  80. $message = get_lang('ANewSessionWasCreated')." <br /> ".get_lang('NameOfTheSession').' : '.$name;
  81. api_mail_html($complete_name, $user_info['email'], $subject, $message);
  82. *
  83. */
  84. //Saving extra fields
  85. $session_field_value = new SessionFieldValue();
  86. $params['session_id'] = $session_id;
  87. $session_field_value->save_field_values($params);
  88. //Adding to the correct URL
  89. $access_url_id = api_get_current_access_url_id();
  90. UrlManager::add_session_to_url($session_id, $access_url_id);
  91. // Add event to system log
  92. event_system(LOG_SESSION_CREATE, LOG_SESSION_ID, $session_id, api_get_utc_datetime(), api_get_user_id());
  93. if (isset($params['course_code'])) {
  94. $courseInfo = api_get_course_info($params['course_code']);
  95. self::add_courses_to_session($session_id, array($courseInfo['real_id']));
  96. //Update default course gradebook to the session if exists
  97. $create_gradebook_evaluation = isset($params['create_gradebook_evaluation']) ? $params['create_gradebook_evaluation'] : false;
  98. if ($create_gradebook_evaluation) {
  99. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  100. $category_id = create_default_course_gradebook($params['course_code'], false, $session_id);
  101. /*
  102. if ($category_id && isset($params['gradebook_params'])) {
  103. $eval = new Evaluation();
  104. $eval->set_name($params['gradebook_params']['name']);
  105. $eval->set_user_id($params['gradebook_params']['user_id']);
  106. $eval->set_course_code($params['course_code']);
  107. $eval->set_category_id($category_id);
  108. $eval->set_weight($params['gradebook_params']['weight']);
  109. $eval->set_max($params['gradebook_params']['max']);
  110. $eval->set_visible(0);
  111. $eval->add();
  112. }*/
  113. /*$tbl_gradebook_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  114. $sql = "SELECT id FROM $tbl_gradebook_category WHERE course_code = {$params['course_code']} AND session_id = 0 LIMIT 1";
  115. $result = Database::query($sql);
  116. if (Database::num_rows($result)) {
  117. $gradebook_data = Database::fetch_array($result, 'ASSOC');
  118. if (!empty($gradebook_data)) {
  119. $sql = "UPDATE SET session_id = $session_id WHERE id = {$gradebook_data['id']}";
  120. Database::query($sql);
  121. }
  122. }*/
  123. }
  124. }
  125. } else {
  126. if (isset($params['return_item_if_already_exists']) && $params['return_item_if_already_exists']) {
  127. $my_session_result = SessionManager::get_session_by_name($params['name']);
  128. $session_id = $my_session_result['id'];
  129. }
  130. }
  131. return $session_id;
  132. }
  133. /**
  134. * Updates a session with the given array of field values
  135. * @param array An array of fields values
  136. * @return void
  137. * @assert (null) === false
  138. */
  139. public static function update($params) {
  140. if (empty($params) || count($params)<1) {
  141. return false;
  142. }
  143. $session_model = new SessionModel();
  144. $session_model->update($params);
  145. if (!empty($params['id'])) {
  146. $session_field_value = new SessionFieldValue();
  147. $params['session_id'] = $params['id'];
  148. unset($params['id']);
  149. $session_field_value->save_field_values($params);
  150. }
  151. }
  152. /**
  153. * Checks whether a session already exists with the given name (used in
  154. * add() to avoid homonym sessions)
  155. * @param string A session name
  156. * @assert ('') === false
  157. */
  158. function session_name_exists($session_name) {
  159. $session_name = Database::escape_string($session_name);
  160. $result = Database::fetch_array(Database::query("SELECT COUNT(*) as count FROM ".Database::get_main_table(TABLE_MAIN_SESSION)." WHERE name = '$session_name' "));
  161. return $result['count'] > 0;
  162. }
  163. /**
  164. * Gets the admin session list callback of the session/session_list.php page
  165. * @param array order and limit keys
  166. * @param boolean Whether to get all the results or only the count
  167. * @return mixed Integer for number of rows, or array of results
  168. * @assert (array(),true) !== false
  169. */
  170. public static function get_sessions_admin($options = array(), $get_count = false)
  171. {
  172. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  173. $where = 'WHERE 1 = 1 ';
  174. $user_id = api_get_user_id();
  175. if (api_is_session_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false') {
  176. $where .=" AND s.session_admin_id = $user_id ";
  177. }
  178. if (!api_is_platform_admin() && api_is_teacher() && api_get_setting('allow_teachers_to_create_sessions') == 'true') {
  179. $where .=" AND s.id_coach = $user_id ";
  180. }
  181. $extra_field = new ExtraField('session');
  182. $conditions = $extra_field->parseConditions($options);
  183. $inject_joins = $conditions['inject_joins'];
  184. $where .= $conditions['where'];
  185. $inject_where = $conditions['inject_where'];
  186. $inject_extra_fields = $conditions['inject_extra_fields'];
  187. $order = $conditions['order'];
  188. $limit = $conditions['limit'];
  189. if ($get_count == true) {
  190. $select = " SELECT count(*) as total_rows";
  191. } else {
  192. $select =
  193. "SELECT DISTINCT ".
  194. " s.name, ".
  195. " s.display_start_date, ".
  196. " s.display_end_date, ".
  197. " access_start_date, ".
  198. " access_end_date, ".
  199. " s.visibility, ".
  200. " $inject_extra_fields ".
  201. " s.id ";
  202. }
  203. $query = "$select FROM $tbl_session s $inject_joins $where $inject_where";
  204. if (api_is_multiple_url_enabled()) {
  205. $table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  206. $access_url_id = api_get_current_access_url_id();
  207. if ($access_url_id != -1) {
  208. $where.= " AND ar.access_url_id = $access_url_id ";
  209. $query = "$select FROM $tbl_session s $inject_joins INNER JOIN $table_access_url_rel_session ar ON (ar.session_id = s.id) $where";
  210. }
  211. }
  212. $query .= $order;
  213. $query .= $limit;
  214. $result = Database::query($query);
  215. $formatted_sessions = array();
  216. if (Database::num_rows($result)) {
  217. $sessions = Database::store_result($result, 'ASSOC');
  218. if ($get_count) {
  219. return $sessions[0]['total_rows'];
  220. }
  221. foreach ($sessions as $session) {
  222. $session_id = $session['id'];
  223. $session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
  224. if (isset($session['session_active']) && $session['session_active'] == 1) {
  225. $session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL);
  226. } else {
  227. $session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL);
  228. }
  229. $session = self::convert_dates_to_local($session);
  230. switch ($session['visibility']) {
  231. case SESSION_VISIBLE_READ_ONLY: //1
  232. $session['visibility'] = get_lang('ReadOnly');
  233. break;
  234. case SESSION_VISIBLE: //2
  235. case SESSION_AVAILABLE: //4
  236. $session['visibility'] = get_lang('Visible');
  237. break;
  238. case SESSION_INVISIBLE: //3
  239. $session['visibility'] = api_ucfirst(get_lang('Invisible'));
  240. break;
  241. }
  242. // Cleaning double selects.
  243. foreach ($session as $key => &$value) {
  244. if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
  245. $options = explode('::', $value);
  246. }
  247. $original_key = $key;
  248. if (strpos($key, '_second') === false) {
  249. } else {
  250. $key = str_replace('_second', '', $key);
  251. }
  252. if (isset($options_by_double[$key])) {
  253. if (isset($options[0])) {
  254. if (isset($options_by_double[$key][$options[0]])) {
  255. if (strpos($original_key, '_second') === false) {
  256. $value = $options_by_double[$key][$options[0]]['option_display_text'];
  257. } else {
  258. $value = $options_by_double[$key][$options[1]]['option_display_text'];
  259. }
  260. }
  261. }
  262. }
  263. }
  264. $formatted_sessions[$session_id] = $session;
  265. }
  266. }
  267. return $formatted_sessions;
  268. }
  269. /**
  270. * Gets the number of rows in the session table filtered through the given
  271. * array of parameters
  272. * @param array Array of options/filters/keys
  273. * @return integer The number of rows, or false on wrong param
  274. * @assert ('a') === false
  275. */
  276. static function get_count_admin_complete($options = array()) {
  277. if (!is_array($options)) {
  278. return false;
  279. }
  280. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  281. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  282. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  283. $where = 'WHERE 1 = 1 ';
  284. $user_id = api_get_user_id();
  285. if (api_is_session_admin() && api_get_setting('allow_session_admins_to_see_all_sessions') == 'false') {
  286. $where.=" WHERE s.session_admin_id = $user_id ";
  287. }
  288. if (!empty($options['where'])) {
  289. if (!empty($options['extra'])) {
  290. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  291. $options['where'] = str_replace('AND', 'OR', $options['where']);
  292. foreach ($options['extra'] as $extra) {
  293. $options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
  294. }
  295. }
  296. $where .= ' AND '.$options['where'];
  297. }
  298. $query_rows = "SELECT count(*) as total_rows
  299. FROM $tbl_session s
  300. LEFT JOIN $tbl_session_category sc ON s.session_category_id = sc.id
  301. INNER JOIN $tbl_user u ON s.id_coach = u.user_id $where ";
  302. if (api_is_multiple_url_enabled()) {
  303. $table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  304. $access_url_id = api_get_current_access_url_id();
  305. if ($access_url_id != -1) {
  306. $where.= " AND ar.access_url_id = $access_url_id ";
  307. $query_rows = "SELECT count(*) as total_rows
  308. FROM $tbl_session s
  309. LEFT JOIN $tbl_session_category sc ON s.session_category_id = sc.id
  310. INNER JOIN $tbl_user u ON s.id_coach = u.user_id
  311. INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id $where ";
  312. }
  313. }
  314. $result_rows = Database::query($query_rows);
  315. $recorset = Database::fetch_array($result_rows);
  316. $num = $recorset['total_rows'];
  317. return $num;
  318. }
  319. /**
  320. * Gets the admin session list callback of the session/session_list.php
  321. * page with all user/details in the right fomat
  322. * @param array order and limit keys
  323. * @result array Array of rows results
  324. * @asset ('a') === false
  325. */
  326. public static function get_sessions_admin_complete($options = array()) {
  327. if (!is_array($options)) {
  328. return false;
  329. }
  330. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  331. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  332. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  333. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  334. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  335. $tbl_session_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  336. $tbl_session_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
  337. $where = 'WHERE 1 = 1 ';
  338. $user_id = api_get_user_id();
  339. if (api_is_session_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false') {
  340. $where.=" AND s.session_admin_id = $user_id ";
  341. }
  342. $coach_name = " CONCAT(u.lastname , ' ', u.firstname) as coach_name ";
  343. if (api_is_western_name_order()) {
  344. $coach_name = " CONCAT(u.firstname, ' ', u.lastname) as coach_name ";
  345. }
  346. $today = api_get_utc_datetime();
  347. $inject_extra_fields = null;
  348. $extra_fields = array();
  349. $extra_fields_info = array();
  350. //for now only sessions
  351. $extra_field = new ExtraField('session');
  352. $double_fields = array();
  353. $extra_field_option = new ExtraFieldOption('session');
  354. if (isset($options['extra'])) {
  355. $extra_fields = $options['extra'];
  356. if (!empty($extra_fields)) {
  357. foreach ($extra_fields as $extra) {
  358. $inject_extra_fields .= " IF (fv.field_id = {$extra['id']}, fvo.option_display_text, NULL ) as {$extra['field']} , ";
  359. if (isset($extra_fields_info[$extra['id']])) {
  360. $info = $extra_fields_info[$extra['id']];
  361. } else {
  362. $info = $extra_field->get($extra['id']);
  363. $extra_fields_info[$extra['id']] = $info;
  364. }
  365. if ($info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  366. $double_fields[$info['id']] = $info;
  367. }
  368. }
  369. }
  370. }
  371. $options_by_double = array();
  372. foreach ($double_fields as $double) {
  373. $my_options = $extra_field_option->get_field_options_by_field($double['id'], true);
  374. $options_by_double['extra_'.$double['field_variable']] = $my_options;
  375. }
  376. //sc.name as category_name,
  377. $select = "
  378. SELECT * FROM (
  379. SELECT DISTINCT
  380. IF (
  381. (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
  382. (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
  383. (s.access_start_date <= '$today' AND '0000-00-00 00:00:00' = s.access_end_date) OR
  384. ('$today' < s.access_end_date AND '0000-00-00 00:00:00' = s.access_start_date)
  385. , 1, 0) as session_active,
  386. s.name,
  387. s.nbr_courses,
  388. s.nbr_users,
  389. s.display_start_date,
  390. s.display_end_date,
  391. $coach_name,
  392. access_start_date,
  393. access_end_date,
  394. s.visibility,
  395. u.user_id,
  396. $inject_extra_fields
  397. c.title as course_title,
  398. s.id ";
  399. if (!empty($options['where'])) {
  400. if (!empty($options['extra'])) {
  401. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  402. $options['where'] = str_replace('AND', 'OR', $options['where']);
  403. foreach ($options['extra'] as $extra) {
  404. $options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
  405. }
  406. }
  407. $options['where'] = str_replace('course_title', 'c.title', $options['where']);
  408. $where .= ' AND '.$options['where'];
  409. }
  410. if (!empty($options['limit'])) {
  411. $where .= " LIMIT ".$options['limit'];
  412. }
  413. $query = "$select FROM $tbl_session s
  414. LEFT JOIN $tbl_session_field_values fv ON (fv.session_id = s.id)
  415. INNER JOIN $tbl_session_field_options fvo ON (fv.field_id = fvo.field_id)
  416. LEFT JOIN $tbl_session_rel_course src ON (src.id_session = s.id)
  417. LEFT JOIN $tbl_course c ON (src.c_id = c.id)
  418. LEFT JOIN $tbl_session_category sc ON (s.session_category_id = sc.id)
  419. INNER JOIN $tbl_user u ON (s.id_coach = u.user_id) ".
  420. $where;
  421. if (api_is_multiple_url_enabled()) {
  422. $table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  423. $access_url_id = api_get_current_access_url_id();
  424. if ($access_url_id != -1) {
  425. $where.= " AND ar.access_url_id = $access_url_id ";
  426. $query = "$select
  427. FROM $tbl_session s
  428. LEFT JOIN $tbl_session_field_values fv ON (fv.session_id = s.id)
  429. LEFT JOIN $tbl_session_rel_course src ON (src.id_session = s.id)
  430. LEFT JOIN $tbl_course c ON (src.c_id = c.id)
  431. LEFT JOIN $tbl_session_category sc ON (s.session_category_id = sc.id)
  432. INNER JOIN $tbl_user u ON (s.id_coach = u.user_id)
  433. INNER JOIN $table_access_url_rel_session ar ON (ar.session_id = s.id)
  434. $where";
  435. }
  436. }
  437. $query .= ") AS session_table";
  438. if (!empty($options['order'])) {
  439. $query .= " ORDER BY ".$options['order'];
  440. }
  441. //error_log($query);
  442. // echo $query;
  443. $result = Database::query($query);
  444. $formatted_sessions = array();
  445. if (Database::num_rows($result)) {
  446. $sessions = Database::store_result($result, 'ASSOC');
  447. foreach ($sessions as $session) {
  448. $session_id = $session['id'];
  449. $session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
  450. $session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=".$session['user_id']);
  451. if ($session['session_active'] == 1) {
  452. $session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL);
  453. } else {
  454. $session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL);
  455. }
  456. $session = self::convert_dates_to_local($session);
  457. switch ($session['visibility']) {
  458. case SESSION_VISIBLE_READ_ONLY: //1
  459. $session['visibility'] = get_lang('ReadOnly');
  460. break;
  461. case SESSION_VISIBLE: //2
  462. case SESSION_AVAILABLE: //4
  463. $session['visibility'] = get_lang('Visible');
  464. break;
  465. case SESSION_INVISIBLE: //3
  466. $session['visibility'] = api_ucfirst(get_lang('Invisible'));
  467. break;
  468. }
  469. //Cleaning double selects
  470. foreach ($session as $key => &$value) {
  471. if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
  472. $options = explode('::', $value);
  473. }
  474. $original_key = $key;
  475. if (strpos($key, '_second') === false) {
  476. } else {
  477. $key = str_replace('_second', '', $key);
  478. }
  479. if (isset($options_by_double[$key])) {
  480. if (isset($options[0])) {
  481. if (isset($options_by_double[$key][$options[0]])) {
  482. if (strpos($original_key, '_second') === false) {
  483. $value = $options_by_double[$key][$options[0]]['option_display_text'];
  484. } else {
  485. $value = $options_by_double[$key][$options[1]]['option_display_text'];
  486. }
  487. }
  488. }
  489. }
  490. }
  491. //Magic filter
  492. if (isset($formatted_sessions[$session_id])) {
  493. $formatted_sessions[$session_id] = self::compare_arrays_to_merge($formatted_sessions[$session_id], $session);
  494. } else {
  495. $formatted_sessions[$session_id] = $session;
  496. }
  497. }
  498. }
  499. return $formatted_sessions;
  500. }
  501. /**
  502. *
  503. */
  504. static function compare_arrays_to_merge($array1, $array2)
  505. {
  506. if (empty($array2)) {
  507. return $array1;
  508. }
  509. foreach ($array1 as $key => $item) {
  510. if (!isset($array1[$key])) {
  511. //My string is empty try the other one
  512. if (isset($array2[$key]) && !empty($array2[$key])) {
  513. $array1[$key] = $array2[$key];
  514. }
  515. }
  516. }
  517. return $array1;
  518. }
  519. /**
  520. * Converts all dates sent through the param array (given form) to correct
  521. * dates with timezones
  522. * @param array The dates
  523. * @return array The same array, with times converted
  524. * @assert ('a') === false
  525. */
  526. static function convert_dates_to_local($params) {
  527. if (!is_array($params)) {
  528. return false;
  529. }
  530. $params['display_start_date'] = api_get_local_time($params['display_start_date'], null, null, true);
  531. $params['display_end_date'] = api_get_local_time($params['display_end_date'], null, null, true);
  532. $params['access_start_date'] = api_get_local_time($params['access_start_date'], null, null, true);
  533. $params['access_end_date'] = api_get_local_time($params['access_end_date'], null, null, true);
  534. $params['coach_access_start_date'] = isset($params['coach_access_start_date']) ? api_get_local_time($params['coach_access_start_date'], null, null, true) : null;
  535. $params['coach_access_end_date'] = isset($params['coach_access_end_date']) ? api_get_local_time($params['coach_access_end_date'], null, null, true) : null;
  536. return $params;
  537. }
  538. /**
  539. * Creates a new course code based in given code
  540. *
  541. * @param string wanted code
  542. * <code>
  543. * $wanted_code = 'sess' if there are names like sess, sess1, sess2 in the
  544. * database already, the function will return: sess3
  545. * if the session name doesn't exist in the DB the same name is returned
  546. * </code>
  547. * @return string wanted unused code
  548. */
  549. function generate_nice_next_session_name($session_name) {
  550. $session_name_ok = !self::session_name_exists($session_name);
  551. if (!$session_name_ok) {
  552. $table = Database::get_main_table(TABLE_MAIN_SESSION);
  553. $session_name = Database::escape_string($session_name);
  554. $sql = "SELECT count(*) as count FROM $table WHERE name LIKE '$session_name%'";
  555. $result = Database::query($sql);
  556. if (Database::num_rows($result) > 0 ) {
  557. $row = Database::fetch_array($result);
  558. $count = $row['count'] + 1;
  559. $session_name = $session_name.'_'.$count;
  560. $result = self::session_name_exists($session_name);
  561. if (!$result) {
  562. return $session_name;
  563. }
  564. }
  565. return false;
  566. }
  567. return $session_name;
  568. }
  569. /**
  570. * Delete session
  571. * @author Carlos Vargas from existing code
  572. * @param array id_checked
  573. * @param boolean optional, true if the function is called by a webservice, false otherwise.
  574. * @return void Nothing, or false on error
  575. * The parameters is a array to delete sessions
  576. **/
  577. public static function delete_session ($id_checked, $from_ws = false) {
  578. $tbl_session= Database::get_main_table(TABLE_MAIN_SESSION);
  579. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  580. $tbl_session_rel_course_rel_user= Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  581. $tbl_session_rel_user= Database::get_main_table(TABLE_MAIN_SESSION_USER);
  582. $tbl_url_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  583. global $_user;
  584. if(is_array($id_checked)) {
  585. $id_checked = Database::escape_string(implode(',',$id_checked));
  586. } else {
  587. $id_checked = intval($id_checked);
  588. }
  589. if (!api_is_platform_admin() && !$from_ws) {
  590. $sql = 'SELECT session_admin_id FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_checked;
  591. $rs = Database::query($sql);
  592. if (Database::result($rs,0,0)!=$_user['user_id']) {
  593. api_not_allowed(true);
  594. }
  595. }
  596. Database::query("DELETE FROM $tbl_session WHERE id IN($id_checked)");
  597. Database::query("DELETE FROM $tbl_session_rel_course WHERE id_session IN($id_checked)");
  598. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session IN($id_checked)");
  599. Database::query("DELETE FROM $tbl_session_rel_user WHERE id_session IN($id_checked)");
  600. Database::query("DELETE FROM $tbl_url_session WHERE session_id IN($id_checked)");
  601. // delete extra session fields
  602. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  603. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  604. // Delete extra fields from session where field variable is "SECCION"
  605. $sql = "SELECT t_sfv.field_id FROM $t_sfv t_sfv, $t_sf t_sf WHERE t_sfv.session_id = '$id_checked' AND t_sf.field_variable = 'SECCION' ";
  606. $rs_field = Database::query($sql);
  607. $field_id = 0;
  608. if (Database::num_rows($rs_field) == 1) {
  609. $row_field = Database::fetch_row($rs_field);
  610. $field_id = $row_field[0];
  611. $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
  612. $rs_delete_sfv = Database::query($sql_delete_sfv);
  613. }
  614. $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
  615. $rs_field_id = Database::query($sql);
  616. if (Database::num_rows($rs_field_id) == 0) {
  617. $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
  618. $rs_delete_sf = Database::query($sql_delete_sf);
  619. }
  620. /*
  621. $sql = "SELECT distinct field_id FROM $t_sfv WHERE session_id = '$id_checked'";
  622. $res_field_ids = @Database::query($sql);
  623. if (Database::num_rows($res_field_ids) > 0) {
  624. while($row_field_id = Database::fetch_row($res_field_ids)){
  625. $field_ids[] = $row_field_id[0];
  626. }
  627. }
  628. //delete from table_session_field_value from a given session id
  629. $sql_session_field_value = "DELETE FROM $t_sfv WHERE session_id = '$id_checked'";
  630. @Database::query($sql_session_field_value);
  631. $sql = "SELECT distinct field_id FROM $t_sfv";
  632. $res_field_all_ids = @Database::query($sql);
  633. if (Database::num_rows($res_field_all_ids) > 0) {
  634. while($row_field_all_id = Database::fetch_row($res_field_all_ids)){
  635. $field_all_ids[] = $row_field_all_id[0];
  636. }
  637. }
  638. if (count($field_ids) > 0 && count($field_all_ids) > 0) {
  639. foreach($field_ids as $field_id) {
  640. // check if field id is used into table field value
  641. if (in_array($field_id,$field_all_ids)) {
  642. continue;
  643. } else {
  644. $sql_session_field = "DELETE FROM $t_sf WHERE id = '$field_id'";
  645. Database::query($sql_session_field);
  646. }
  647. }
  648. }
  649. */
  650. // Add event to system log
  651. $user_id = api_get_user_id();
  652. event_system(LOG_SESSION_DELETE, LOG_SESSION_ID, $id_checked, api_get_utc_datetime(), $user_id);
  653. }
  654. /**
  655. * Subscribes users (students) to the given session and optionally (default) unsubscribes previous users
  656. * @author Carlos Vargas from existing code
  657. * @param integer Session ID
  658. * @param array List of user IDs
  659. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  660. * @return void Nothing, or false on error
  661. **/
  662. public static function suscribe_users_to_session($id_session, $user_list, $session_visibility = SESSION_VISIBLE_READ_ONLY, $empty_users = true, $send_email = false) {
  663. if ($id_session!= strval(intval($id_session))) return false;
  664. foreach ($user_list as $intUser){
  665. if ($intUser!= strval(intval($intUser))) return false;
  666. }
  667. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  668. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  669. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  670. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  671. $session_info = api_get_session_info($id_session);
  672. if ($session_info) {
  673. $session_name = $session_info['name'];
  674. } else {
  675. return false;
  676. }
  677. //from function parameter
  678. if (empty($session_visibility)) {
  679. $session_visibility = $session_info['visibility']; //loaded from DB
  680. //default status loaded if empty
  681. if (empty($session_visibility))
  682. $session_visibility = SESSION_VISIBLE_READ_ONLY; // by default readonly 1
  683. } else {
  684. if (!in_array($session_visibility, array(SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_INVISIBLE))) {
  685. $session_visibility = SESSION_VISIBLE_READ_ONLY;
  686. }
  687. }
  688. $sql = "SELECT id_user FROM $tbl_session_rel_course_rel_user WHERE id_session = '$id_session' AND status = 0";
  689. $result = Database::query($sql);
  690. $existingUsers = array();
  691. while ($row = Database::fetch_array($result)) {
  692. $existingUsers[] = $row['id_user'];
  693. }
  694. $sql = "SELECT c_id FROM $tbl_session_rel_course WHERE id_session = '$id_session'";
  695. $result = Database::query($sql);
  696. $course_list = array();
  697. while ($row = Database::fetch_array($result)) {
  698. $course_list[] = $row['c_id'];
  699. }
  700. if ($send_email) {
  701. //sending emails only
  702. if (is_array($user_list) && count($user_list)>0) {
  703. foreach ($user_list as $user_id) {
  704. if (!in_array($user_id, $existingUsers)) {
  705. $subject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  706. $user_info = api_get_user_info($user_id);
  707. $content = get_lang('Dear')." ".stripslashes($user_info['complete_name']).",\n\n".sprintf(get_lang('YouAreRegisterToSessionX'), $session_name) ." \n\n" .get_lang('Address') ." ". api_get_setting('siteName') ." ". get_lang('Is') ." : ". api_get_path(WEB_PATH) ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  708. MessageManager::send_message($user_id, $subject, $content, array(), array(), null, null, null, null, null);
  709. }
  710. }
  711. }
  712. }
  713. foreach ($course_list as $courseId) {
  714. // for each course in the session
  715. $nbr_users = 0;
  716. $courseId = Database::escape_string($courseId);
  717. // delete existing users
  718. if ($empty_users) {
  719. foreach ($existingUsers as $existing_user) {
  720. if (!in_array($existing_user, $user_list)) {
  721. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND c_id ='$courseId' AND id_user='$existing_user' AND status = 0";
  722. $result = Database::query($sql);
  723. if (Database::affected_rows($result)) {
  724. $nbr_users--;
  725. }
  726. }
  727. }
  728. }
  729. //Replace with this new function
  730. //
  731. // insert new users into session_rel_course_rel_user and ignore if they already exist
  732. foreach ($user_list as $enreg_user) {
  733. if(!in_array($enreg_user, $existingUsers)) {
  734. $enreg_user = Database::escape_string($enreg_user);
  735. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session, c_id, id_user, visibility, status) VALUES('$id_session','$courseId','$enreg_user','$session_visibility', '0')";
  736. $result = Database::query($insert_sql);
  737. if (Database::affected_rows($result)) {
  738. $nbr_users++;
  739. }
  740. }
  741. }
  742. // count users in this session-course relation
  743. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND c_id ='$courseId' AND status<>2";
  744. $rs = Database::query($sql);
  745. list($nbr_users) = Database::fetch_array($rs);
  746. // update the session-course relation to add the users total
  747. $update_sql = "UPDATE $tbl_session_rel_course SET nbr_users = $nbr_users WHERE id_session='$id_session' AND c_id ='$courseId'";
  748. Database::query($update_sql);
  749. }
  750. // Delete users from the session
  751. if ($empty_users === true) {
  752. Database::query("DELETE FROM $tbl_session_rel_user WHERE id_session = $id_session AND relation_type<>".SESSION_RELATION_TYPE_RRHH."");
  753. }
  754. // Insert missing users into session
  755. $nbr_users = 0;
  756. foreach ($user_list as $enreg_user) {
  757. $enreg_user = Database::escape_string($enreg_user);
  758. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user (id_session, id_user) VALUES ('$id_session', '$enreg_user')";
  759. Database::query($insert_sql);
  760. //Reset moved_to just in case
  761. $update_sql = "UPDATE $tbl_session_rel_user SET moved_to = 0, moved_status = 0, moved_at ='0000-00-00 00:00:00'
  762. WHERE id_session = $id_session AND id_user = $enreg_user";
  763. Database::query($update_sql);
  764. $nbr_users++;
  765. }
  766. // update number of users in the session
  767. $nbr_users = count($user_list);
  768. if ($empty_users) {
  769. // update number of users in the session
  770. $update_sql = "UPDATE $tbl_session SET nbr_users = $nbr_users WHERE id='$id_session' ";
  771. Database::query($update_sql);
  772. } else {
  773. $update_sql = "UPDATE $tbl_session SET nbr_users = nbr_users + $nbr_users WHERE id='$id_session' ";
  774. Database::query($update_sql);
  775. }
  776. }
  777. /**
  778. * @param $user_list
  779. * @param $session_id
  780. * @param $courseId
  781. * @param int $session_visibility
  782. * @return bool
  783. */
  784. static function subscribe_users_to_session_course($user_list, $session_id, $courseId, $session_visibility = SESSION_VISIBLE_READ_ONLY ) {
  785. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  786. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  787. if (empty($user_list) || empty($session_id) || empty($course_code)) {
  788. return false;
  789. }
  790. $session_id = intval($session_id);
  791. $courseId = Database::escape_string($courseId);
  792. $session_visibility = intval($session_visibility);
  793. $nbr_users = 0;
  794. foreach ($user_list as $enreg_user) {
  795. //if (!in_array($enreg_user, $existingUsers)) {
  796. $enreg_user = intval($enreg_user);
  797. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session, c_id, id_user, visibility)
  798. VALUES ('$session_id','$courseId','$enreg_user','$session_visibility')";
  799. $result = Database::query($insert_sql);
  800. if (Database::affected_rows($result)) {
  801. $nbr_users++;
  802. }
  803. //}
  804. }
  805. // count users in this session-course relation
  806. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$session_id' AND c_id = '$courseId' AND status<>2";
  807. $rs = Database::query($sql);
  808. list($nbr_users) = Database::fetch_array($rs);
  809. // update the session-course relation to add the users total
  810. $update_sql = "UPDATE $tbl_session_rel_course SET nbr_users = $nbr_users WHERE id_session='$session_id' AND c_id = '$courseId'";
  811. Database::query($update_sql);
  812. }
  813. /**
  814. * Unsubscribe user from session
  815. *
  816. * @param int session id
  817. * @param int user id
  818. * @param int reason of unsubscription see function get_session_change_user_reasons()
  819. * @return bool true in case of success, false in case of error
  820. */
  821. public static function unsubscribe_user_from_session($session_id, $user_id) {
  822. $session_id = intval($session_id);
  823. $user_id = intval($user_id);
  824. $reason_id = intval($reason_id);
  825. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  826. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  827. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  828. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  829. $user_status = SessionManager::get_user_status_in_session($session_id, $user_id);
  830. if ($user_status['moved_to'] != 0) {
  831. //You can't subscribe a user that was moved
  832. return false;
  833. }
  834. $delete_sql = "DELETE FROM $tbl_session_rel_user WHERE id_session = '$session_id' AND id_user ='$user_id' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  835. $result = Database::query($delete_sql);
  836. $return = Database::affected_rows($result);
  837. // Update number of users
  838. $update_sql = "UPDATE $tbl_session SET nbr_users = nbr_users - $return WHERE id='$session_id' ";
  839. Database::query($update_sql);
  840. // Get the list of courses related to this session
  841. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  842. if (!empty($course_list)) {
  843. foreach($course_list as $course) {
  844. $courseId = $course['id'];
  845. // Delete user from course
  846. $result = Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$session_id' AND c_id='$courseId' AND id_user='$user_id'");
  847. if (Database::affected_rows($result)) {
  848. // Update number of users in this relation
  849. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=nbr_users - 1 WHERE id_session='$session_id' AND c_id ='$courseId'");
  850. }
  851. }
  852. }
  853. return true;
  854. }
  855. /**
  856. *
  857. * @param int $session_id
  858. * @param array user id list
  859. * @param int $courseId
  860. */
  861. static function unsubscribe_user_from_course_session($session_id, $user_list, $courseId) {
  862. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  863. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  864. if (is_array($user_list) && count($user_list)>0 ) {
  865. array_map('intval', $user_list);
  866. $user_list = implode(',', $user_list);
  867. }
  868. $session_id = intval($session_id);
  869. $courseId = Database::escape_string($courseId);
  870. if (!empty($user_list)) {
  871. $result = Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$session_id' AND c_id='".$courseId."' AND id_user IN($user_list)");
  872. $nbr_affected_rows = Database::affected_rows($result);
  873. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=nbr_users-$nbr_affected_rows WHERE id_session='$session_id' AND c_id='".$courseId."'");
  874. }
  875. }
  876. /** Subscribes courses to the given session and optionally (default) unsubscribes previous users
  877. * @author Carlos Vargas from existing code
  878. * @param int Session ID
  879. * @param array List of courses IDs
  880. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  881. * @return void Nothing, or false on error
  882. **/
  883. public static function add_courses_to_session($id_session, $course_list, $empty_courses = true) {
  884. // security checks
  885. if ($id_session!= strval(intval($id_session))) { return false; }
  886. // initialisation
  887. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  888. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  889. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  890. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  891. // get general coach ID
  892. $id_coach = Database::query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  893. $id_coach = Database::fetch_array($id_coach);
  894. $id_coach = $id_coach[0];
  895. // get list of courses subscribed to this session
  896. $rs = Database::query("SELECT c_id FROM $tbl_session_rel_course WHERE id_session=$id_session");
  897. $existingCourses = Database::store_result($rs, 'ASSOC');
  898. $nbr_courses=count($existingCourses);
  899. // get list of users subscribed to this session
  900. $sql="SELECT id_user
  901. FROM $tbl_session_rel_user
  902. WHERE id_session = $id_session AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  903. $result=Database::query($sql);
  904. $user_list=Database::store_result($result, 'ASSOC');
  905. // Remove existing courses from the session
  906. if ($empty_courses === true) {
  907. foreach ($existingCourses as $existingCourse) {
  908. if (!in_array($existingCourse['c_id'], $course_list)){
  909. Database::query("DELETE FROM $tbl_session_rel_course WHERE c_id='".$existingCourse['c_id']."' AND id_session=$id_session");
  910. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE c_id='".$existingCourse['c_id']."' AND id_session=$id_session");
  911. }
  912. }
  913. $nbr_courses=0;
  914. }
  915. // Pass through the courses list we want to add to the session
  916. foreach ($course_list as $courseId) {
  917. $courseId = Database::escape_string($courseId);
  918. $exists = false;
  919. // check if the course we want to add is already subscribed
  920. foreach ($existingCourses as $existingCourse) {
  921. if ($courseId == $existingCourse['c_id']) {
  922. $exists=true;
  923. }
  924. }
  925. if ($exists == false) {
  926. CourseManager::update_course_ranking($courseId, $id_session);
  927. //if the course isn't subscribed yet
  928. $sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course (id_session, c_id) VALUES ('$id_session','$courseId')";
  929. Database::query($sql_insert_rel_course);
  930. //We add the current course in the existing courses array, to avoid adding another time the current course
  931. $existingCourses[]=array('c_id' => $courseId);
  932. $nbr_courses++;
  933. // subscribe all the users from the session to this course inside the session
  934. $nbr_users=0;
  935. foreach ($user_list as $enreg_user) {
  936. $enreg_user_id = Database::escape_string($enreg_user['id_user']);
  937. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (id_session, c_id, id_user) VALUES ('$id_session','$courseId','$enreg_user_id')";
  938. $result = Database::query($sql_insert);
  939. if (Database::affected_rows($result)) {
  940. $nbr_users++;
  941. }
  942. }
  943. SessionManager::subscribe_users_to_session_course($user_list, $id_session, $courseId);
  944. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND c_id = '$courseId'");
  945. }
  946. }
  947. Database::query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'");
  948. }
  949. /**
  950. * Unsubscribe course from a session
  951. *
  952. * @param int Session id
  953. * @param int Course id
  954. * @return bool True in case of success, false otherwise
  955. */
  956. public static function unsubscribe_course_from_session($session_id, $course_id) {
  957. $session_id = (int)$session_id;
  958. $course_id = (int)$course_id;
  959. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  960. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  961. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  962. // Unsubscribe course
  963. $result = Database::query("DELETE FROM $tbl_session_rel_course WHERE c_id ='$course_id' AND id_session='$session_id'");
  964. $nb_affected = Database::affected_rows($result);
  965. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE c_id ='$course_id' AND id_session='$session_id'");
  966. if ($nb_affected > 0) {
  967. // Update number of courses in the session
  968. Database::query("UPDATE $tbl_session SET nbr_courses= nbr_courses + $nb_affected WHERE id='$session_id' ");
  969. return true;
  970. } else {
  971. return false;
  972. }
  973. }
  974. /**
  975. * Creates a new extra field for a given session
  976. * @param string Field's internal variable name
  977. * @param int Field's type
  978. * @param string Field's language var name
  979. * @return int new extra field id
  980. */
  981. public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
  982. // database table definition
  983. $params = array('field_variable' => $fieldvarname,
  984. 'field_type' => $fieldtype,
  985. 'field_display_text' => $fieldtitle
  986. );
  987. $session_field = new SessionField();
  988. $field_id = $session_field->save($params);
  989. return $field_id;
  990. }
  991. /**
  992. * Update an extra field value for a given session
  993. * @param integer Course ID
  994. * @param string Field variable name
  995. * @param string Field value
  996. * @return boolean true if field updated, false otherwise
  997. */
  998. public static function update_session_extra_field_value ($session_id, $fname, $fvalue = null) {
  999. $session_field_value = new SessionFieldValue();
  1000. $session_field_value->update($params);
  1001. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  1002. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  1003. $fname = Database::escape_string($fname);
  1004. $session_id = (int)$session_id;
  1005. $tms = api_get_utc_datetime();
  1006. $fvalues = '';
  1007. if (is_array($fvalue)) {
  1008. foreach($fvalue as $val) {
  1009. $fvalues .= Database::escape_string($val).';';
  1010. }
  1011. if(!empty($fvalues)) {
  1012. $fvalues = substr($fvalues,0,-1);
  1013. }
  1014. } else {
  1015. $fvalues = Database::escape_string($fvalue);
  1016. }
  1017. $sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'";
  1018. $ressf = Database::query($sqlsf);
  1019. if (Database::num_rows($ressf)==1) {
  1020. // ok, the field exists
  1021. // Check if enumerated field, if the option is available
  1022. $rowsf = Database::fetch_array($ressf);
  1023. $sqlsfv = "SELECT * FROM $t_sfv WHERE session_id = '$session_id' AND field_id = '".$rowsf['id']."' ORDER BY id";
  1024. $ressfv = Database::query($sqlsfv);
  1025. $n = Database::num_rows($ressfv);
  1026. if ($n>1) {
  1027. //problem, we already have to values for this field and user combination - keep last one
  1028. while($rowsfv = Database::fetch_array($ressfv)) {
  1029. if($n > 1) {
  1030. $sqld = "DELETE FROM $t_sfv WHERE id = ".$rowsfv['id'];
  1031. $resd = Database::query($sqld);
  1032. $n--;
  1033. }
  1034. $rowsfv = Database::fetch_array($ressfv);
  1035. if ($rowsfv['field_value'] != $fvalues) {
  1036. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = '$tms' WHERE id = ".$rowsfv['id'];
  1037. $resu = Database::query($sqlu);
  1038. return($resu?true:false);
  1039. }
  1040. return true;
  1041. }
  1042. } else if ($n==1) {
  1043. //we need to update the current record
  1044. $rowsfv = Database::fetch_array($ressfv);
  1045. if($rowsfv['field_value'] != $fvalues) {
  1046. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = '$tms' WHERE id = ".$rowsfv['id'];
  1047. //error_log('UM::update_extra_field_value: '.$sqlu);
  1048. $resu = Database::query($sqlu);
  1049. return($resu?true:false);
  1050. }
  1051. return true;
  1052. } else {
  1053. $sqli = "INSERT INTO $t_sfv (session_id,field_id,field_value,tms) " .
  1054. "VALUES ('$session_id',".$rowsf['id'].",'$fvalues', '$tms')";
  1055. //error_log('UM::update_extra_field_value: '.$sqli);
  1056. $resi = Database::query($sqli);
  1057. return($resi?true:false);
  1058. }
  1059. } else {
  1060. return false; //field not found
  1061. }
  1062. }
  1063. /**
  1064. * Checks the relationship between a session and a course.
  1065. * @param int $session_id
  1066. * @param int $course_id
  1067. * @return bool Returns TRUE if the session and the course are related, FALSE otherwise.
  1068. * */
  1069. public static function relation_session_course_exist ($session_id, $courseId) {
  1070. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1071. $return_value = false;
  1072. $sql= "SELECT c_id FROM $tbl_session_course
  1073. WHERE id_session = ".Database::escape_string($session_id)." AND c_id = '".Database::escape_string($courseId)."'";
  1074. $result = Database::query($sql);
  1075. $num = Database::num_rows($result);
  1076. if ($num>0) {
  1077. $return_value = true;
  1078. }
  1079. return $return_value;
  1080. }
  1081. /**
  1082. * Get the session information by name
  1083. * @param string session name
  1084. * @return mixed false if the session does not exist, array if the session exist
  1085. * */
  1086. public static function get_session_by_name ($session_name) {
  1087. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1088. $sql = 'SELECT * FROM '.$tbl_session.' WHERE name="'.Database::escape_string($session_name).'"';
  1089. $result = Database::query($sql);
  1090. $num = Database::num_rows($result);
  1091. if ($num) {
  1092. return Database::fetch_array($result, 'ASSOC');
  1093. } else {
  1094. return false;
  1095. }
  1096. }
  1097. /**
  1098. * Create a session category
  1099. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  1100. * @param string name
  1101. * @param integer year_start
  1102. * @param integer month_start
  1103. * @param integer day_start
  1104. * @param integer year_end
  1105. * @param integer month_end
  1106. * @param integer day_end
  1107. * @return $id_session;
  1108. **/
  1109. public static function create_category_session($sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  1110. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1111. $name= trim($sname);
  1112. $year_start= intval($syear_start);
  1113. $month_start=intval($smonth_start);
  1114. $day_start=intval($sday_start);
  1115. $year_end=intval($syear_end);
  1116. $month_end=intval($smonth_end);
  1117. $day_end=intval($sday_end);
  1118. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  1119. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  1120. if (empty($name)) {
  1121. $msg=get_lang('SessionCategoryNameIsRequired');
  1122. return $msg;
  1123. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  1124. $msg=get_lang('InvalidStartDate');
  1125. return $msg;
  1126. } elseif (!$month_end && !$day_end && !$year_end) {
  1127. $date_end = "null";
  1128. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  1129. $msg=get_lang('InvalidEndDate');
  1130. return $msg;
  1131. } elseif($date_start >= $date_end) {
  1132. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  1133. return $msg;
  1134. }
  1135. $access_url_id = api_get_current_access_url_id();
  1136. $sql = "INSERT INTO $tbl_session_category (name, date_start, date_end, access_url_id)
  1137. VALUES('".Database::escape_string($name)."','$date_start','$date_end', '$access_url_id')";
  1138. Database::query($sql);
  1139. $id_session = Database::insert_id();
  1140. // Add event to system log
  1141. $user_id = api_get_user_id();
  1142. event_system(LOG_SESSION_CATEGORY_CREATE, LOG_SESSION_CATEGORY_ID, $id_session, api_get_utc_datetime(), $user_id);
  1143. return $id_session;
  1144. }
  1145. /**
  1146. * Edit a sessions categories
  1147. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,from existing code
  1148. * @param integer id
  1149. * @param string name
  1150. * @param integer year_start
  1151. * @param integer month_start
  1152. * @param integer day_start
  1153. * @param integer year_end
  1154. * @param integer month_end
  1155. * @param integer day_end
  1156. * @return $id;
  1157. * The parameter id is a primary key
  1158. **/
  1159. public static function edit_category_session($id, $sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  1160. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1161. $name= trim($sname);
  1162. $year_start= intval($syear_start);
  1163. $month_start=intval($smonth_start);
  1164. $day_start=intval($sday_start);
  1165. $year_end=intval($syear_end);
  1166. $month_end=intval($smonth_end);
  1167. $day_end=intval($sday_end);
  1168. $id=intval($id);
  1169. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  1170. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  1171. if (empty($name)) {
  1172. $msg=get_lang('SessionCategoryNameIsRequired');
  1173. return $msg;
  1174. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  1175. $msg=get_lang('InvalidStartDate');
  1176. return $msg;
  1177. } elseif (!$month_end && !$day_end && !$year_end) {
  1178. $date_end = null;
  1179. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  1180. $msg=get_lang('InvalidEndDate');
  1181. return $msg;
  1182. } elseif($date_start >= $date_end) {
  1183. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  1184. return $msg;
  1185. }
  1186. if ( $date_end <> null ) {
  1187. $sql = "UPDATE $tbl_session_category SET name = '".Database::escape_string($name)."', date_start = '$date_start' ".
  1188. ", date_end = '$date_end' WHERE id= '".$id."' ";
  1189. } else {
  1190. $sql = "UPDATE $tbl_session_category SET name = '".Database::escape_string($name)."', date_start = '$date_start' ".
  1191. ", date_end = NULL WHERE id= '".$id."' ";
  1192. }
  1193. $result = Database::query($sql);
  1194. return ($result? true:false);
  1195. }
  1196. /**
  1197. * Delete sessions categories
  1198. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  1199. * @param array id_checked
  1200. * @param bool include delete session
  1201. * @param bool optional, true if the function is called by a webservice, false otherwise.
  1202. * @return void Nothing, or false on error
  1203. * The parameters is a array to delete sessions
  1204. **/
  1205. public static function delete_session_category($id_checked, $delete_session = false,$from_ws = false){
  1206. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1207. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1208. if (is_array($id_checked)) {
  1209. $id_checked = Database::escape_string(implode(',',$id_checked));
  1210. } else {
  1211. $id_checked = intval($id_checked);
  1212. }
  1213. //Setting session_category_id to 0
  1214. $sql = "UPDATE $tbl_session SET session_category_id = 0 WHERE session_category_id IN (".$id_checked.")";
  1215. Database::query($sql);
  1216. $sql = "SELECT id FROM $tbl_session WHERE session_category_id IN (".$id_checked.")";
  1217. $result = @Database::query($sql);
  1218. while ($rows = Database::fetch_array($result)) {
  1219. $session_id = $rows['id'];
  1220. if ($delete_session) {
  1221. if ($from_ws) {
  1222. SessionManager::delete_session($session_id,true);
  1223. } else {
  1224. SessionManager::delete_session($session_id);
  1225. }
  1226. }
  1227. }
  1228. $sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")";
  1229. $rs = Database::query($sql);
  1230. $result = Database::affected_rows($rs);
  1231. // Add event to system log
  1232. $user_id = api_get_user_id();
  1233. event_system(LOG_SESSION_CATEGORY_DELETE, LOG_SESSION_CATEGORY_ID, $id_checked, api_get_utc_datetime(), $user_id);
  1234. // delete extra session fields where field variable is "PERIODO"
  1235. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  1236. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  1237. $sql = "SELECT t_sfv.field_id FROM $t_sfv t_sfv, $t_sf t_sf WHERE t_sfv.session_id = '$id_checked' AND t_sf.field_variable = 'PERIODO' ";
  1238. $rs_field = Database::query($sql);
  1239. $field_id = 0;
  1240. if (Database::num_rows($rs_field) > 0) {
  1241. $row_field = Database::fetch_row($rs_field);
  1242. $field_id = $row_field[0];
  1243. $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
  1244. $rs_delete_sfv = Database::query($sql_delete_sfv);
  1245. }
  1246. $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
  1247. $rs_field_id = Database::query($sql);
  1248. if (Database::num_rows($rs_field_id) == 0) {
  1249. $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
  1250. $rs_delete_sf = Database::query($sql_delete_sf);
  1251. }
  1252. return true;
  1253. }
  1254. /**
  1255. * Get a list of sessions of which the given conditions match with an = 'cond'
  1256. * @param array $conditions a list of condition (exemple : array('status =' =>STUDENT) or array('s.name LIKE' => "%$needle%")
  1257. * @param array $order_by a list of fields on which sort
  1258. * @return array An array with all sessions of the platform.
  1259. * @todo optional course code parameter, optional sorting parameters...
  1260. */
  1261. public static function get_sessions_list($conditions = array(), $order_by = array()) {
  1262. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  1263. $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1264. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1265. $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1266. $access_url_id = api_get_current_access_url_id();
  1267. $return_array = array();
  1268. $sql_query = " SELECT s.id, s.name, s.nbr_courses, s.access_start_date, s.access_end_date, u.firstname, u.lastname, sc.name as category_name, s.promotion_id
  1269. FROM $session_table s
  1270. INNER JOIN $user_table u ON s.id_coach = u.user_id
  1271. INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id
  1272. LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id
  1273. WHERE ar.access_url_id = $access_url_id ";
  1274. if (count($conditions) > 0) {
  1275. $sql_query .= ' AND ';
  1276. foreach ($conditions as $field=>$value) {
  1277. $field = Database::escape_string($field);
  1278. $value = Database::escape_string($value);
  1279. $sql_query .= $field." '".$value."'";
  1280. }
  1281. }
  1282. if (count($order_by)>0) {
  1283. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by));
  1284. }
  1285. //echo $sql_query;
  1286. $sql_result = Database::query($sql_query);
  1287. if (Database::num_rows($sql_result)>0) {
  1288. while ($result = Database::fetch_array($sql_result)) {
  1289. $return_array[$result['id']] = $result;
  1290. }
  1291. }
  1292. return $return_array;
  1293. }
  1294. /**
  1295. * Get the session category information by id
  1296. * @param string session category ID
  1297. * @return mixed false if the session category does not exist, array if the session category exists
  1298. */
  1299. public static function get_session_category ($id) {
  1300. $id = intval($id);
  1301. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1302. $sql = "SELECT id, name, date_start, date_end FROM $tbl_session_category WHERE id= $id";
  1303. $result = Database::query($sql);
  1304. $num = Database::num_rows($result);
  1305. if ($num>0){
  1306. return Database::fetch_array($result);
  1307. } else {
  1308. return false;
  1309. }
  1310. }
  1311. /**
  1312. * Get all session categories (filter by access_url_id)
  1313. * @return mixed false if the session category does not exist, array if the session category exists
  1314. */
  1315. public static function get_all_session_category() {
  1316. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1317. $id = api_get_current_access_url_id();
  1318. $sql = 'SELECT * FROM '.$tbl_session_category.' WHERE access_url_id ="'.$id.'" ORDER BY name ASC';
  1319. $result = Database::query($sql);
  1320. if (Database::num_rows($result) > 0 ){
  1321. $data = Database::store_result($result,'ASSOC');
  1322. return $data;
  1323. } else {
  1324. return false;
  1325. }
  1326. }
  1327. /**
  1328. * Assign a coach to course in session with status = 2
  1329. * @param int - user id
  1330. * @param int - session id
  1331. * @param int - course id
  1332. * @param bool - optional, if is true the user don't be a coach now, otherwise it'll assign a coach
  1333. * @return bool true if there are affected rows, otherwise false
  1334. */
  1335. public static function set_coach_to_course_session($user_id, $session_id = 0, $courseId = '',$nocoach = false) {
  1336. // Definition of variables
  1337. $user_id = intval($user_id);
  1338. if (!empty($session_id)) {
  1339. $session_id = intval($session_id);
  1340. } else {
  1341. $session_id = api_get_session_id();
  1342. }
  1343. if (!empty($courseId)) {
  1344. $courseId = Database::escape_string($courseId);
  1345. } else {
  1346. $courseId = api_get_course_int_id();
  1347. }
  1348. // definitios of tables
  1349. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1350. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1351. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1352. // check if user is a teacher
  1353. $sql= "SELECT * FROM $tbl_user WHERE status='1' AND user_id = '$user_id'";
  1354. $rs_check_user = Database::query($sql);
  1355. if (Database::num_rows($rs_check_user) > 0) {
  1356. if ($nocoach) {
  1357. // check if user_id exits int session_rel_user
  1358. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session = '$session_id' AND id_user = '$user_id'";
  1359. $res = Database::query($sql);
  1360. if (Database::num_rows($res) > 0) {
  1361. // The user don't be a coach now
  1362. $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 0 WHERE id_session = '$session_id' AND c_id = '$courseId' AND id_user = '$user_id' ";
  1363. $rs_update = Database::query($sql);
  1364. if (Database::affected_rows($rs_update) > 0) return true;
  1365. else return false;
  1366. } else {
  1367. // The user don't be a coach now
  1368. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session = '$session_id' AND c_id = '$courseId' AND id_user = '$user_id' ";
  1369. $rs_delete = Database::query($sql);
  1370. if (Database::affected_rows($rs_delete) > 0) return true;
  1371. else return false;
  1372. }
  1373. } else {
  1374. // Assign user like a coach to course
  1375. // First check if the user is registered in the course
  1376. $sql = "SELECT id_user FROM $tbl_session_rel_course_rel_user WHERE id_session = '$session_id' AND c_id = '$courseId' AND id_user = '$user_id'";
  1377. $rs_check = Database::query($sql);
  1378. //Then update or insert
  1379. if (Database::num_rows($rs_check) > 0) {
  1380. $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 2 WHERE id_session = '$session_id' AND c_id = '$courseId' AND id_user = '$user_id' ";
  1381. $rs_update = Database::query($sql);
  1382. if (Database::affected_rows($rs_update) > 0) return true;
  1383. else return false;
  1384. } else {
  1385. $sql = " INSERT INTO $tbl_session_rel_course_rel_user(id_session, c_id, id_user, status) VALUES('$session_id', '$courseId', '$user_id', 2)";
  1386. $rs_insert = Database::query($sql);
  1387. if (Database::affected_rows($rs_insert) > 0) return true;
  1388. else return false;
  1389. }
  1390. }
  1391. } else {
  1392. return false;
  1393. }
  1394. }
  1395. /**
  1396. * Subscribes sessions to human resource manager (Dashboard feature)
  1397. * @param int Human Resource Manager id
  1398. * @param array Sessions id
  1399. * @param int Relation type
  1400. **/
  1401. public static function suscribe_sessions_to_hr_manager($hr_manager_id,$sessions_list) {
  1402. global $_configuration;
  1403. // Database Table Definitions
  1404. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1405. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1406. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1407. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1408. $hr_manager_id = intval($hr_manager_id);
  1409. $affected_rows = 0;
  1410. //Deleting assigned sessions to hrm_id
  1411. if (api_is_multiple_url_enabled()) {
  1412. $sql = "SELECT id_session FROM $tbl_session_rel_user s INNER JOIN $tbl_session_rel_access_url a ON (a.session_id = s.id_session)
  1413. WHERE id_user = $hr_manager_id AND relation_type=".SESSION_RELATION_TYPE_RRHH." AND access_url_id = ".api_get_current_access_url_id()."";
  1414. } else {
  1415. $sql = "SELECT id_session FROM $tbl_session_rel_user s WHERE id_user = $hr_manager_id AND relation_type=".SESSION_RELATION_TYPE_RRHH."";
  1416. }
  1417. $result = Database::query($sql);
  1418. if (Database::num_rows($result) > 0) {
  1419. while ($row = Database::fetch_array($result)) {
  1420. $sql = "DELETE FROM $tbl_session_rel_user
  1421. WHERE id_session = {$row['id_session']} AND id_user = $hr_manager_id AND relation_type=".SESSION_RELATION_TYPE_RRHH." ";
  1422. Database::query($sql);
  1423. }
  1424. }
  1425. // inserting new sessions list
  1426. if (is_array($sessions_list)) {
  1427. foreach ($sessions_list as $session_id) {
  1428. $session_id = intval($session_id);
  1429. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user, relation_type) VALUES($session_id, $hr_manager_id, '".SESSION_RELATION_TYPE_RRHH."')";
  1430. $result = Database::query($insert_sql);
  1431. $affected_rows = Database::affected_rows($result);
  1432. }
  1433. }
  1434. return $affected_rows;
  1435. }
  1436. /**
  1437. * Get sessions followed by human resources manager
  1438. * @param int Human resources manager or Session admin id
  1439. * @return array sessions
  1440. */
  1441. public static function get_sessions_followed_by_drh($hr_manager_id) {
  1442. global $_configuration;
  1443. // Database Table Definitions
  1444. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1445. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1446. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1447. $hr_manager_id = intval($hr_manager_id);
  1448. $assigned_sessions_to_hrm = array();
  1449. if ($_configuration['multiple_access_urls']) {
  1450. $sql = "SELECT * FROM $tbl_session s INNER JOIN $tbl_session_rel_user sru ON (sru.id_session = s.id) LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
  1451. WHERE sru.id_user = '$hr_manager_id' AND sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND access_url_id = ".api_get_current_access_url_id()."";
  1452. } else {
  1453. $sql = "SELECT * FROM $tbl_session s
  1454. INNER JOIN $tbl_session_rel_user sru ON sru.id_session = s.id AND sru.id_user = '$hr_manager_id' AND sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' ";
  1455. }
  1456. $rs_assigned_sessions = Database::query($sql);
  1457. if (Database::num_rows($rs_assigned_sessions) > 0) {
  1458. while ($row_assigned_sessions = Database::fetch_array($rs_assigned_sessions)) {
  1459. $assigned_sessions_to_hrm[$row_assigned_sessions['id']] = $row_assigned_sessions;
  1460. }
  1461. }
  1462. return $assigned_sessions_to_hrm;
  1463. }
  1464. /**
  1465. * Gets the list of courses by session filtered by access_url
  1466. * @param int session id
  1467. * @return array list of courses
  1468. */
  1469. public static function get_course_list_by_session_id ($session_id) {
  1470. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1471. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1472. // select the courses
  1473. $sql = "SELECT c.*, c.id as real_id FROM $tbl_course c INNER JOIN $tbl_session_rel_course src ON c.id = src.c_id
  1474. WHERE src.id_session = '$session_id'
  1475. ORDER BY title;";
  1476. $result = Database::query($sql);
  1477. $num_rows = Database::num_rows($result);
  1478. $courses = array();
  1479. if ($num_rows > 0) {
  1480. while ($row = Database::fetch_array($result,'ASSOC')) {
  1481. $row['real_id'] = $row['id'];
  1482. $courses[$row['id']] = $row;
  1483. }
  1484. }
  1485. return $courses;
  1486. }
  1487. /**
  1488. * Get the session id based on the original id and field name in the extra fields. Returns 0 if session was not found
  1489. *
  1490. * @param string Original session id
  1491. * @param string Original field name
  1492. * @return int Session id
  1493. */
  1494. public static function get_session_id_from_original_id($original_session_id_value, $original_session_id_name) {
  1495. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  1496. $table_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  1497. $sql_session = "SELECT session_id FROM $table_field sf INNER JOIN $t_sfv sfv ON sfv.field_id=sf.id WHERE field_variable='$original_session_id_name' AND field_value='$original_session_id_value'";
  1498. $res_session = Database::query($sql_session);
  1499. $row = Database::fetch_object($res_session);
  1500. if ($row) {
  1501. return $row->session_id;
  1502. } else {
  1503. return 0;
  1504. }
  1505. }
  1506. /**
  1507. * Get users by session
  1508. * @param int session id
  1509. * @param int filter by status
  1510. * @return array a list with an user list
  1511. */
  1512. public static function get_users_by_session($id, $with_status = null) {
  1513. if (empty($id)) {
  1514. return array();
  1515. }
  1516. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1517. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1518. $id = intval($id);
  1519. /*
  1520. * sessions are not shared between urls
  1521. if (api_is_multiple_url_enabled()) {
  1522. $tbl_session_rel_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1523. $url_id = api_get_current_access_url_id();
  1524. $sql = "SELECT u.user_id, lastname, firstname, username, access_url_id, moved_to, moved_status, moved_at
  1525. FROM $tbl_user u
  1526. INNER JOIN $tbl_session_rel_user su
  1527. ON u.user_id = su.id_user AND su.relation_type<>".SESSION_RELATION_TYPE_RRHH."
  1528. INNER JOIN $tbl_session_rel_url s ON (s.session_id = su.id_session)
  1529. LEFT OUTER JOIN $tbl_session_rel_user uu ON (uu.user_id = u.user_id AND s.session_id = uu.id_session)
  1530. WHERE su.id_session = $id AND (access_url_id = $url_id OR access_url_id is null )";
  1531. } else {
  1532. $sql = "SELECT u.user_id, lastname, firstname, username, moved_to, moved_status, moved_at
  1533. FROM $tbl_user u INNER JOIN $tbl_session_rel_user su
  1534. ON u.user_id = su.id_user AND
  1535. su.id_session = $id
  1536. WHERE 1 = 1
  1537. ";
  1538. }*/
  1539. $sql = "SELECT u.user_id,
  1540. lastname,
  1541. firstname,
  1542. username,
  1543. moved_to,
  1544. moved_status,
  1545. moved_at
  1546. FROM $tbl_user u INNER JOIN $tbl_session_rel_user su
  1547. ON u.user_id = su.id_user AND
  1548. su.id_session = $id
  1549. WHERE 1 = 1
  1550. ";
  1551. if (isset($with_status) && $with_status != '') {
  1552. $with_status = intval($with_status);
  1553. $sql .= " AND relation_type = $with_status ";
  1554. }
  1555. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  1556. $sql .= $order_clause;
  1557. $return_array = array();
  1558. $result = Database::query($sql);
  1559. while ($row = Database::fetch_array($result,'ASSOC')) {
  1560. $return_array[] = $row;
  1561. }
  1562. return $return_array;
  1563. }
  1564. /**
  1565. * The general coach (field: session.id_coach)
  1566. * @param int user id
  1567. */
  1568. public static function get_sessions_by_general_coach($user_id) {
  1569. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  1570. $user_id = intval($user_id);
  1571. // session where we are general coach
  1572. $sql = "SELECT DISTINCT *
  1573. FROM $session_table
  1574. WHERE id_coach = $user_id";
  1575. if (api_is_multiple_url_enabled()) {
  1576. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1577. $access_url_id = api_get_current_access_url_id();
  1578. if ($access_url_id != -1) {
  1579. $sql = 'SELECT DISTINCT session.*
  1580. FROM '.$session_table.' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1581. ON (session.id = session_rel_url.session_id)
  1582. WHERE id_coach = '.$user_id.' AND access_url_id = '.$access_url_id;
  1583. }
  1584. }
  1585. $sql .= ' ORDER by name';
  1586. $result = Database::query($sql);
  1587. return Database::store_result($result, 'ASSOC');
  1588. }
  1589. /**
  1590. * Get all sessions if the user is a sesion course coach of any session
  1591. * @param int user id
  1592. */
  1593. static function get_sessions_by_coach($user_id) {
  1594. // table definition
  1595. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1596. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1597. $sql = 'SELECT DISTINCT session.*
  1598. FROM ' . $tbl_session . ' as session
  1599. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1600. ON session.id = session_course_user.id_session
  1601. AND session_course_user.id_user=' . $user_id.' AND session_course_user.status=2';
  1602. if (api_is_multiple_url_enabled()) {
  1603. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1604. $access_url_id = api_get_current_access_url_id();
  1605. if ($access_url_id != -1){
  1606. $sql = 'SELECT DISTINCT session.*
  1607. FROM '.$tbl_session.' as session
  1608. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1609. ON session.id = session_course_user.id_session AND session_course_user.id_user = '.$user_id.' AND session_course_user.status=2
  1610. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1611. ON (session.id = session_rel_url.session_id)
  1612. WHERE access_url_id = '.$access_url_id;
  1613. }
  1614. }
  1615. //$sql .= " ORDER BY session.name";
  1616. $result = Database::query($sql);
  1617. return Database::store_result($result, 'ASSOC');
  1618. }
  1619. public static function get_sessions_coached_by_user($coach_id, $check_session_rel_user_visibility = false) {
  1620. $sessions = self::get_sessions_by_general_coach($coach_id);
  1621. $sessions_by_coach = self::get_sessions_by_coach($coach_id);
  1622. if (!empty($sessions_by_coach)) {
  1623. $sessions = array_merge($sessions, $sessions_by_coach);
  1624. }
  1625. //Remove repeated sessions
  1626. if (!empty($sessions)) {
  1627. $clean_sessions = array();
  1628. foreach ($sessions as $session) {
  1629. $clean_sessions[$session['id']] = $session;
  1630. }
  1631. $sessions = $clean_sessions;
  1632. }
  1633. if ($check_session_rel_user_visibility) {
  1634. if (!empty($sessions)) {
  1635. $new_session = array();
  1636. foreach ($sessions as $session) {
  1637. $visibility = api_get_session_visibility($session['id']);
  1638. if ($visibility == SESSION_INVISIBLE) {
  1639. continue;
  1640. }
  1641. $new_session[] = $session;
  1642. }
  1643. $sessions = $new_session;
  1644. }
  1645. }
  1646. return $sessions;
  1647. }
  1648. /**
  1649. * Gets user status within a session
  1650. * @param $user_id
  1651. * @param $course_code
  1652. * @param $session_id
  1653. * @return unknown_type
  1654. */
  1655. public static function get_user_status_in_course_session($user_id, $courseId, $session_id) {
  1656. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1657. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1658. $sql = "SELECT session_rcru.status
  1659. FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user
  1660. WHERE session_rcru.id_user = user.user_id AND
  1661. session_rcru.id_session = '".intval($session_id)."' AND
  1662. session_rcru.c_id ='".Database::escape_string($courseId)."' AND
  1663. user.user_id = ".intval($user_id);
  1664. $result = Database::query($sql);
  1665. $status = false;
  1666. if (Database::num_rows($result)) {
  1667. $status = Database::fetch_row($result);
  1668. $status = $status['0'];
  1669. }
  1670. return $status;
  1671. }
  1672. static function get_user_status_in_session($session_id, $user_id) {
  1673. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1674. $session_id = intval($session_id);
  1675. $user_id = intval($user_id);
  1676. $sql = "SELECT * FROM $tbl_session_rel_user WHERE id_user = $user_id AND id_session = $session_id";
  1677. $result = Database::query($sql);
  1678. if (Database::num_rows($result)) {
  1679. $result = Database::store_result($result, 'ASSOC');
  1680. return $result[0];
  1681. }
  1682. return array();
  1683. }
  1684. function get_all_sessions_by_promotion($id) {
  1685. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  1686. return Database::select('*', $t, array('where'=>array('promotion_id = ?'=>$id)));
  1687. }
  1688. function suscribe_sessions_to_promotion($promotion_id, $list) {
  1689. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  1690. $params = array();
  1691. $params['promotion_id'] = 0;
  1692. Database::update($t, $params, array('promotion_id = ?'=>$promotion_id));
  1693. $params['promotion_id'] = $promotion_id;
  1694. if (!empty($list)) {
  1695. foreach ($list as $session_id) {
  1696. $session_id= intval($session_id);
  1697. Database::update($t, $params, array('id = ?'=>$session_id));
  1698. }
  1699. }
  1700. }
  1701. /**
  1702. * Updates a session status
  1703. * @param int session id
  1704. * @param int status
  1705. */
  1706. public function set_session_status($session_id, $status) {
  1707. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  1708. $params['visibility'] = $status;
  1709. Database::update($t, $params, array('id = ?'=>$session_id));
  1710. }
  1711. /**
  1712. * Copies a session with the same data to a new session.
  1713. * The new copy is not assigned to the same promotion. @see suscribe_sessions_to_promotions() for that
  1714. * @param int Session ID
  1715. * @param bool Whether to copy the relationship with courses
  1716. * @param bool Whether to copy the relationship with users
  1717. * @param bool New courses will be created
  1718. * @return int The new session ID on success, 0 otherwise
  1719. * @todo make sure the extra session fields are copied too
  1720. */
  1721. /**
  1722. * @param $id
  1723. * @param $copy_courses
  1724. * @param $copy_users
  1725. * @param $create_new_courses
  1726. * @param $set_exercises_lp_invisible
  1727. * @return int
  1728. */
  1729. public function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false) {
  1730. $id = intval($id);
  1731. $params = self::fetch($id);
  1732. $params['name'] = $params['name'].' '.get_lang('CopyLabelSuffix');
  1733. $sid = self::add($params);
  1734. if (!is_numeric($sid) || empty($sid)) {
  1735. return false;
  1736. }
  1737. if ($copy_courses) {
  1738. // Register courses from the original session to the new session
  1739. $courses = self::get_course_list_by_session_id($id);
  1740. $short_courses = $new_short_courses = array();
  1741. if (is_array($courses) && count($courses)>0) {
  1742. foreach ($courses as $course) {
  1743. $short_courses[] = $course;
  1744. }
  1745. }
  1746. $courses = null;
  1747. //We will copy the current courses of the session to new courses
  1748. if (!empty($short_courses)) {
  1749. if ($create_new_courses) {
  1750. //Just in case
  1751. if (function_exists('ini_set')) {
  1752. api_set_memory_limit('256M');
  1753. ini_set('max_execution_time',0);
  1754. }
  1755. $params = array();
  1756. $params['skip_lp_dates'] = true;
  1757. foreach ($short_courses as $course_data) {
  1758. $course_info = CourseManager::copy_course_simple($course_data['title'].' '.get_lang('CopyLabelSuffix'), $course_data['course_code'], $id, $sid, $params);
  1759. if ($course_info) {
  1760. //By default new elements are invisible
  1761. if ($set_exercises_lp_invisible) {
  1762. $list = new LearnpathList('', $course_info['code'], $sid);
  1763. $flat_list = $list->get_flat_list();
  1764. if (!empty($flat_list)) {
  1765. foreach ($flat_list as $lp_id => $data) {
  1766. api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0, 0, 0, $sid);
  1767. api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0, 0, 0);
  1768. }
  1769. }
  1770. $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
  1771. $course_id = $course_info['real_id'];
  1772. //@todo check this query
  1773. //Disabling quiz items
  1774. $sql = "UPDATE $quiz_table SET active = 0 WHERE c_id = $course_id ";
  1775. Database::query($sql);
  1776. }
  1777. $new_short_courses[] = $course_info['code'];
  1778. }
  1779. }
  1780. } else {
  1781. foreach($short_courses as $course_data) {
  1782. $new_short_courses[] = $course_data['id'];
  1783. }
  1784. }
  1785. $short_courses = $new_short_courses;
  1786. $res = self::add_courses_to_session($sid, $short_courses, true);
  1787. $short_courses = null;
  1788. }
  1789. }
  1790. if ($copy_users) {
  1791. // Register users from the original session to the new session
  1792. $users = self::get_users_by_session($id);
  1793. $short_users = array();
  1794. if (is_array($users) && count($users)>0) {
  1795. foreach ($users as $user) {
  1796. $short_users[] = $user['user_id'];
  1797. }
  1798. }
  1799. $users = null;
  1800. //Subscribing in read only mode
  1801. $res = self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true, false);
  1802. $short_users = null;
  1803. }
  1804. return $sid;
  1805. }
  1806. static function user_is_general_coach($user_id, $session_id) {
  1807. $session_id = intval($session_id);
  1808. $user_id = intval($user_id);
  1809. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  1810. $sql = "SELECT DISTINCT id
  1811. FROM $session_table
  1812. WHERE session.id_coach = '".$user_id."' AND id = '$session_id'";
  1813. $result = Database::query($sql);
  1814. if ($result && Database::num_rows($result)) {
  1815. return true;
  1816. }
  1817. return false;
  1818. }
  1819. /**
  1820. * Get the number of sessions
  1821. * @param int ID of the URL we want to filter on (optional)
  1822. * @return int Number of sessions
  1823. */
  1824. public function count_sessions($access_url_id=null) {
  1825. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  1826. $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1827. $sql = "SELECT count(id) FROM $session_table s";
  1828. if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
  1829. $sql .= ", $access_url_rel_session_table u ".
  1830. " WHERE s.id = u.session_id AND u.access_url_id = $access_url_id";
  1831. }
  1832. $res = Database::query($sql);
  1833. $row = Database::fetch_row($res);
  1834. return $row[0];
  1835. }
  1836. static function protect_session_edit($id = null) {
  1837. $blockTeachers = !api_is_platform_admin() && (!api_is_teacher() || (api_is_teacher() && api_get_setting('allow_teachers_to_create_sessions') == 'false'));
  1838. if ($blockTeachers) {
  1839. api_not_allowed(true);
  1840. }
  1841. if (!empty($id)) {
  1842. $session_info = self::fetch($id);
  1843. if (empty($session_info)) {
  1844. api_not_allowed(true);
  1845. }
  1846. if ($blockTeachers && !api_is_platform_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true') {
  1847. if ($session_info['session_admin_id'] != api_get_user_id()) {
  1848. api_not_allowed(true);
  1849. }
  1850. }
  1851. //Blocking teachers that want to edit another session
  1852. if ($blockTeachers == false && $session_info['id_coach'] != api_get_user_id()) {
  1853. api_not_allowed(true);
  1854. }
  1855. }
  1856. }
  1857. /**
  1858. * @param $courseId
  1859. * @return array
  1860. */
  1861. static function get_session_by_course($courseId) {
  1862. $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1863. $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1864. $courseId = Database::escape_string($courseId);
  1865. $sql = "SELECT name, s.id FROM $table_session_course sc INNER JOIN $table_session s ON (sc.id_session = s.id)
  1866. WHERE sc.c_id = '$courseId' ";
  1867. $result = Database::query($sql);
  1868. return Database::store_result($result);
  1869. }
  1870. /**
  1871. * @todo Add constatns in a DB table
  1872. */
  1873. static function get_session_change_user_reasons() {
  1874. return array (
  1875. self::SESSION_CHANGE_USER_REASON_SCHEDULE => get_lang('ScheduleChanged'),
  1876. self::SESSION_CHANGE_USER_REASON_CLASSROOM => get_lang('ClassRoomChanged'),
  1877. self::SESSION_CHANGE_USER_REASON_LOCATION => get_lang('LocationChanged'),
  1878. //self::SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION => get_lang('EnrollmentAnnulation'),
  1879. );
  1880. }
  1881. static function get_session_change_user_reasons_variations() {
  1882. return array (
  1883. self::SESSION_CHANGE_USER_REASON_SCHEDULE => array(
  1884. 'default' => get_lang('ScheduleChanged'),
  1885. 'from' => get_lang('ScheduleChangedFrom'),
  1886. 'to' => get_lang('ScheduleChangedTo'),
  1887. ),
  1888. self::SESSION_CHANGE_USER_REASON_CLASSROOM => array(
  1889. 'default' => get_lang('ClassRoomChanged'),
  1890. 'from' => get_lang('ClassRoomChangedFrom'),
  1891. 'to' => get_lang('ClassRoomChangedTo'),
  1892. ),
  1893. self::SESSION_CHANGE_USER_REASON_LOCATION => array(
  1894. 'default' => get_lang('LocationChanged'),
  1895. 'from' => get_lang('LocationChangedFrom'),
  1896. 'to' => get_lang('LocationChangedTo'),
  1897. )
  1898. );
  1899. }
  1900. static function get_session_change_user_reasons_variations_by_id($id, $type) {
  1901. $reasons = self::get_session_change_user_reasons_variations();
  1902. $my_reason = isset($reasons[$id]) ? $reasons[$id] : null;
  1903. return isset($my_reason[$type]) ? $my_reason[$type] : null;
  1904. }
  1905. static function get_session_changed_reason_label($id, $type) {
  1906. switch ($type) {
  1907. case 'origin':
  1908. break;
  1909. case 'destination':
  1910. break;
  1911. }
  1912. }
  1913. /**
  1914. * Gets the reason name
  1915. * @param int reason id
  1916. */
  1917. static function get_session_change_user_reason($id) {
  1918. $reasons = self::get_session_change_user_reasons();
  1919. return isset($reasons[$id]) ? $reasons[$id] : null;
  1920. }
  1921. /**
  1922. * Changes the user from one session to another due a reason
  1923. * @params int user_id
  1924. * @params int old session id
  1925. * @params int new session id
  1926. * @params int see SessionManager::get_session_change_user_reasons()
  1927. */
  1928. static function change_user_session($user_id, $old_session_id, $new_session_id, $reason_id) {
  1929. if (!empty($user_id) && !empty($old_session_id) && !empty($new_session_id)) {
  1930. $user_id = intval($user_id);
  1931. $old_session_id = intval($old_session_id);
  1932. $new_session_id = intval($new_session_id);
  1933. $reason_id = intval($reason_id);
  1934. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1935. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1936. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1937. // Update number of users
  1938. $update_sql = "UPDATE $tbl_session SET nbr_users = nbr_users - 1 WHERE id = '$old_session_id' ";
  1939. Database::query($update_sql);
  1940. // Get the list of courses related to this session
  1941. $course_list = SessionManager::get_course_list_by_session_id($old_session_id);
  1942. if (!empty($course_list)) {
  1943. foreach ($course_list as $course) {
  1944. $courseId = $course['id'];
  1945. // Delete user from course
  1946. //Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$session_id' AND course_code='$course_code' AND id_user='$user_id'");
  1947. //if (Database::affected_rows()) {
  1948. // Update number of users in this relation
  1949. $sql = "UPDATE $tbl_session_rel_course SET nbr_users = nbr_users - 1 WHERE id_session = '$old_session_id' AND c_id='$courseId'";
  1950. Database::query($sql);
  1951. //}
  1952. }
  1953. }
  1954. //Deal with reasons
  1955. switch ($reason_id) {
  1956. case self::SESSION_CHANGE_USER_REASON_SCHEDULE:
  1957. case self::SESSION_CHANGE_USER_REASON_CLASSROOM:
  1958. case self::SESSION_CHANGE_USER_REASON_LOCATION:
  1959. //Adding to the new session
  1960. self::suscribe_users_to_session($new_session_id, array($user_id), null, false);
  1961. //Setting move_to if session was provided
  1962. $sql = "UPDATE $tbl_session_rel_user SET moved_to = '$new_session_id'
  1963. WHERE id_session = '$old_session_id' AND id_user ='$user_id'";
  1964. Database::query($sql);
  1965. break;
  1966. case self::SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION:
  1967. UserManager::deactivate_users(array($user_id));
  1968. break;
  1969. }
  1970. $now = api_get_utc_datetime();
  1971. //Setting the moved_status
  1972. $sql = "UPDATE $tbl_session_rel_user SET moved_status = $reason_id, moved_at = '$now'
  1973. WHERE id_session = '$old_session_id' AND id_user ='$user_id'";
  1974. Database::query($sql);
  1975. return true;
  1976. }
  1977. return;
  1978. }
  1979. /**
  1980. * Get users inside a course session
  1981. */
  1982. static function get_users_in_course_session($courseId, $id_session, $sort, $direction, $from = null, $limit = null) {
  1983. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1984. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1985. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1986. $courseId = Database::escape_string($courseId);
  1987. $id_session = Database::escape_string($id_session);
  1988. $from = intval($from);
  1989. $limit = intval($limit);
  1990. $is_western_name_order = api_is_western_name_order();
  1991. //Select the number of users
  1992. $sql = " SELECT DISTINCT u.user_id,".($is_western_name_order ? 'u.firstname, u.lastname' : 'u.lastname, u.firstname').", u.username, scru.id_user as is_subscribed
  1993. FROM $tbl_session_rel_user sru INNER JOIN $tbl_user u ON (u.user_id=sru.id_user)
  1994. LEFT JOIN $tbl_session_rel_course_rel_user scru ON (u.user_id = scru.id_user AND scru.c_id = '".$courseId."' )
  1995. WHERE sru.id_session = '$id_session' AND
  1996. sru.moved_to = 0 AND sru.moved_status <> ".SessionManager::SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION." AND
  1997. sru.relation_type<>".SESSION_RELATION_TYPE_RRHH;
  1998. $sql .= " ORDER BY $sort $direction ";
  1999. if (!empty($from) && !empty($limit)) {
  2000. $sql .= " LIMIT $from, $limit";
  2001. }
  2002. $result = Database::query($sql);
  2003. if (Database::num_rows($result)) {
  2004. return Database::store_result($result);
  2005. }
  2006. return false;
  2007. }
  2008. /**
  2009. * @param $courseId
  2010. * @param $id_session
  2011. * @return int|mixed
  2012. */
  2013. static function get_count_users_in_course_session($courseId, $id_session) {
  2014. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  2015. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2016. //Select the number of users
  2017. $sql = " SELECT count(*) FROM $tbl_session_rel_user sru, $tbl_session_rel_course_rel_user srcru
  2018. WHERE srcru.id_user = sru.id_user AND
  2019. srcru.id_session = sru.id_session AND
  2020. srcru.c_id = '".Database::escape_string($courseId)."' AND
  2021. srcru.id_session = '".intval($id_session)."' AND
  2022. (sru.moved_to = 0 AND sru.moved_status <> ".SessionManager::SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION.") AND
  2023. sru.relation_type<>".SESSION_RELATION_TYPE_RRHH;
  2024. $result = Database::query($sql);
  2025. if (Database::num_rows($result)) {
  2026. return Database::result($result,0,0);
  2027. }
  2028. return 0;
  2029. }
  2030. /**
  2031. * Get the list of coaches (only user ids)
  2032. * @param string course_code
  2033. * @param in session_id
  2034. * @return array
  2035. */
  2036. static function get_session_course_coaches($courseId, $session_id)
  2037. {
  2038. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2039. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2040. // Get coachs of the courses in session
  2041. $sql = "SELECT user.user_id FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user
  2042. WHERE session_rcru.id_user = user.user_id AND
  2043. session_rcru.id_session = '".intval($session_id)."' AND
  2044. session_rcru.c_id ='".Database::escape_string($courseId)."' AND
  2045. session_rcru.status=2";
  2046. $result = Database::query($sql);
  2047. return Database::store_result($result);
  2048. }
  2049. static function get_session_course_coaches_by_user($courseId, $session_id, $user_id)
  2050. {
  2051. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2052. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2053. // Get coachs of the courses in session
  2054. $sql = "SELECT user.user_id FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user
  2055. WHERE session_rcru.id_user = user.user_id AND
  2056. session_rcru.id_session = '".intval($session_id)."' AND
  2057. session_rcru.c_id ='".Database::escape_string($courseId)."' AND
  2058. session_rcru.status=2";
  2059. $result = Database::query($sql);
  2060. return Database::store_result($result);
  2061. }
  2062. static function get_session_course_coaches_to_string($courseId, $session_id)
  2063. {
  2064. $coaches = self::get_session_course_coaches($courseId, $session_id);
  2065. if (!empty($coaches)) {
  2066. $coach_list = array();
  2067. foreach ($coaches as $coach_info) {
  2068. $user_info = api_get_user_info($coach_info['user_id']);
  2069. $coach_list[] = $user_info['complete_name'];
  2070. }
  2071. if (!empty($coach_list)) {
  2072. return implode(', ', $coach_list);
  2073. }
  2074. }
  2075. return get_lang('None');
  2076. }
  2077. /**
  2078. * @param $id_session
  2079. * @param $courseId
  2080. */
  2081. static function delete_course_in_session($id_session, $courseId)
  2082. {
  2083. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2084. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2085. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2086. $id_session = intval($id_session);
  2087. $courseId = Database::escape_string($courseId);
  2088. if (!empty($id_session) && !empty($courseId)) {
  2089. $result = Database::query("DELETE FROM $tbl_session_rel_course WHERE id_session='$id_session' AND c_id = '$courseId'");
  2090. $nbr_affected_rows = Database::affected_rows($result);
  2091. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND c_id = '$courseId'");
  2092. Database::query("UPDATE $tbl_session SET nbr_courses=nbr_courses-$nbr_affected_rows WHERE id='$id_session'");
  2093. }
  2094. }
  2095. static function get_sessions_by_user($user_id)
  2096. {
  2097. $session_categories = UserManager::get_sessions_by_category($user_id);
  2098. $session_array = array();
  2099. if (!empty($session_categories)) {
  2100. foreach ($session_categories as $category) {
  2101. if (isset($category['sessions'])) {
  2102. foreach ($category['sessions'] as $session) {
  2103. $session_array[] = $session;
  2104. }
  2105. }
  2106. }
  2107. }
  2108. return $session_array;
  2109. }
  2110. static function get_session_rel_user_by_moved_to($session_id, $user_id) {
  2111. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  2112. $sql = "SELECT id_session, moved_status, moved_at FROM $tbl_session_rel_user WHERE id_user = $user_id AND moved_to = $session_id LIMIT 1";
  2113. $result = Database::query($sql);
  2114. if (Database::num_rows($result)) {
  2115. $result = Database::store_result($result,'ASSOC');
  2116. return $result[0];
  2117. }
  2118. return false;
  2119. }
  2120. static function get_coaches_by_keyword($tag) {
  2121. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2122. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  2123. $select ="SELECT user.user_id, lastname, firstname, username ";
  2124. $sql = " $select FROM $tbl_user user WHERE status='1'";
  2125. $tag = Database::escape_string($tag);
  2126. $where_condition = array();
  2127. if (!empty($tag)) {
  2128. $condition = ' LIKE "%'.$tag.'%"';
  2129. $where_condition = array( "firstname $condition",
  2130. "lastname $condition",
  2131. "username $condition"
  2132. );
  2133. $where_condition = ' AND ('.implode(' OR ', $where_condition).') ';
  2134. }
  2135. if (api_is_multiple_url_enabled()) {
  2136. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2137. $access_url_id = api_get_current_access_url_id();
  2138. if ($access_url_id != -1){
  2139. $sql = $select.' FROM '.$tbl_user.' user
  2140. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  2141. WHERE access_url_id = '.$access_url_id.' AND status = 1';
  2142. }
  2143. }
  2144. $sql .= $where_condition.$order_clause;
  2145. $result = Database::query($sql);
  2146. return Database::store_result($result, 'ASSOC');
  2147. }
  2148. /**
  2149. * Returns a human readable string
  2150. * @params array An array with all the session dates
  2151. * @return string
  2152. */
  2153. static function parse_session_dates($session_info) {
  2154. //This will clean the variables if 0000-00-00 00:00:00 the variable will be empty
  2155. $start_date = api_get_local_time($session_info['display_start_date'], null, null, true);
  2156. $end_date = api_get_local_time($session_info['display_end_date'], null, null, true);
  2157. $msg_date = null;
  2158. if (!empty($start_date) && !empty($end_date)) {
  2159. //$msg_date = get_lang('From').' '.$start_date.' '.get_lang('To').' '.$end_date;
  2160. $msg_date = sprintf(get_lang('FromDateXToDateY'), $start_date, $end_date);
  2161. } else {
  2162. if (!empty($start_date)) {
  2163. $msg_date = get_lang('From').' '.$start_date;
  2164. }
  2165. if (!empty($end_date)) {
  2166. $msg_date = get_lang('Until').' '.$end_date;
  2167. }
  2168. }
  2169. return $msg_date;
  2170. }
  2171. public static function get_session_columns($list_type = 'simple') {
  2172. //Column config
  2173. $operators = array('cn', 'nc');
  2174. $date_operators = array('gt', 'ge', 'lt', 'le');
  2175. switch ($list_type) {
  2176. case 'simple':
  2177. $columns = array(
  2178. get_lang('Name'),
  2179. get_lang('SessionDisplayStartDate'),
  2180. get_lang('SessionDisplayEndDate'),
  2181. //get_lang('SessionCategoryName'),
  2182. //get_lang('Coach'),
  2183. //get_lang('Status'),
  2184. get_lang('Visibility'),
  2185. //get_lang('CourseTitle'),
  2186. );
  2187. $column_model = array (
  2188. array('name'=>'name', 'index'=>'name', 'width'=>'200', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('sopt' => $operators)),
  2189. array('name'=>'display_start_date', 'index'=>'display_start_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_today', 'sopt' => $date_operators)),
  2190. array('name'=>'display_end_date', 'index'=>'display_end_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_one_month', 'sopt' => $date_operators)),
  2191. // array('name'=>'category_name', 'index'=>'category_name', 'hidden' => 'true', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true', 'sopt' => $operators)),
  2192. //array('name'=>'access_start_date', 'index'=>'access_start_date', 'width'=>'60', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true')),
  2193. //array('name'=>'access_end_date', 'index'=>'access_end_date', 'width'=>'60', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true')),
  2194. //array('name'=>'coach_name', 'index'=>'coach_name', 'width'=>'70', 'align'=>'left', 'search' => 'false', 'searchoptions' => array('sopt' => $operators)),
  2195. //array('name'=>'session_active', 'index'=>'session_active', 'width'=>'25', 'align'=>'left', 'search' => 'true', 'stype'=>'select',
  2196. //for the bottom bar
  2197. // 'searchoptions' => array(
  2198. // 'defaultValue' => '1',
  2199. // 'value' => '1:'.get_lang('Active').';0:'.get_lang('Inactive')),
  2200. //for the top bar
  2201. //'editoptions' => array('value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive'))
  2202. //),
  2203. //array('name'=>'course_code', 'index'=>'course_code', 'width'=>'40', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
  2204. array('name'=>'visibility', 'index'=>'visibility', 'width'=>'40', 'align'=>'left', 'search' => 'false'),
  2205. //array('name'=>'course_title', 'index'=>'course_title', 'width'=>'50', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
  2206. );
  2207. break;
  2208. case 'complete':
  2209. $columns = array(
  2210. get_lang('Name'),
  2211. get_lang('SessionDisplayStartDate'),
  2212. get_lang('SessionDisplayEndDate'),
  2213. //get_lang('SessionCategoryName'),
  2214. get_lang('Coach'),
  2215. get_lang('Status'),
  2216. get_lang('Visibility'),
  2217. get_lang('CourseTitle'),
  2218. );
  2219. $column_model = array (
  2220. array('name'=>'name', 'index'=>'name', 'width'=>'200', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('sopt' => $operators)),
  2221. array('name'=>'display_start_date', 'index'=>'display_start_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_today', 'sopt' => $date_operators)),
  2222. array('name'=>'display_end_date', 'index'=>'display_end_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_one_month', 'sopt' => $date_operators)),
  2223. // array('name'=>'category_name', 'index'=>'category_name', 'hidden' => 'true', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true', 'sopt' => $operators)),
  2224. //array('name'=>'access_start_date', 'index'=>'access_start_date', 'width'=>'60', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true')),
  2225. //array('name'=>'access_end_date', 'index'=>'access_end_date', 'width'=>'60', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true')),
  2226. array('name'=>'coach_name', 'index'=>'coach_name', 'width'=>'70', 'align'=>'left', 'search' => 'false', 'searchoptions' => array('sopt' => $operators)),
  2227. array('name'=>'session_active', 'index'=>'session_active', 'width'=>'25', 'align'=>'left', 'search' => 'true', 'stype'=>'select',
  2228. //for the bottom bar
  2229. 'searchoptions' => array(
  2230. 'defaultValue' => '1',
  2231. 'value' => '1:'.get_lang('Active').';0:'.get_lang('Inactive')),
  2232. //for the top bar
  2233. 'editoptions' => array('value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive'))
  2234. ),
  2235. //array('name'=>'course_code', 'index'=>'course_code', 'width'=>'40', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
  2236. array('name'=>'visibility', 'index'=>'visibility', 'width'=>'40', 'align'=>'left', 'search' => 'false'),
  2237. array('name'=>'course_title', 'index'=>'course_title', 'width'=>'50', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
  2238. );
  2239. break;
  2240. }
  2241. // Inject extra session fields
  2242. $session_field = new SessionField();
  2243. $rules = $session_field->getRules($columns, $column_model);
  2244. $column_model[] = array('name'=>'actions', 'index'=>'actions', 'width'=>'80', 'align'=>'left','formatter'=>'action_formatter','sortable'=>'false', 'search' => 'false');
  2245. $columns[] = get_lang('Actions');
  2246. foreach ($column_model as $col_model) {
  2247. $simple_column_name[] = $col_model['name'];
  2248. }
  2249. $return_array = array(
  2250. 'columns' => $columns,
  2251. 'column_model' => $column_model,
  2252. 'rules' => $rules,
  2253. 'simple_column_name' => $simple_column_name
  2254. );
  2255. return $return_array;
  2256. }
  2257. static function getSessionsByCategory($categoryId)
  2258. {
  2259. $categoryId = intval($categoryId);
  2260. $tableSession = Database::get_main_table(TABLE_MAIN_SESSION);
  2261. $sql = "select * FROM $tableSession WHERE session_category_id = $categoryId";
  2262. $result = Database::query($sql);
  2263. return Database::store_result($result);
  2264. }
  2265. }