userInfoLib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. /* For licensing terms, see /license.txt*/
  3. /**
  4. * create a new category definition for the user information.
  5. *
  6. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  7. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  8. *
  9. * @param string $title - category title
  10. * @param string $comment - title comment
  11. * @param int $nbline - lines number for the field the user will fill
  12. *
  13. * @return bool true if succeed, else bolean false
  14. */
  15. function create_cat_def($title = "", $comment = "", $nbline = "5")
  16. {
  17. global $TBL_USERINFO_DEF; //taken from userInfo.php
  18. $title = Database::escape_string(trim($title));
  19. $comment = Database::escape_string(trim($comment));
  20. $nbline = strval(intval($nbline));
  21. if (0 == (int) $nbline || empty($title)) {
  22. return false;
  23. }
  24. $sql = "SELECT MAX(rank) as maxRank FROM ".$TBL_USERINFO_DEF;
  25. $result = Database::query($sql);
  26. if ($result) {
  27. $maxRank = Database::fetch_array($result);
  28. }
  29. $maxRank = $maxRank['maxRank'];
  30. $thisRank = $maxRank + 1;
  31. $sql = "INSERT INTO $TBL_USERINFO_DEF SET
  32. title = '$title',
  33. comment = '$comment',
  34. line_count = '$nbline',
  35. rank = '$thisRank'";
  36. Database::query($sql);
  37. return true;
  38. }
  39. /**
  40. * modify the definition of a user information category.
  41. *
  42. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  43. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  44. *
  45. * @param int $id - id of the category
  46. * @param string $title - category title
  47. * @param string $comment - title comment
  48. * @param int $nbline - lines number for the field the user will fill
  49. *
  50. * @return - boolean true if succeed, else otherwise
  51. */
  52. function edit_cat_def($id, $title, $comment, $nbline)
  53. {
  54. global $TBL_USERINFO_DEF;
  55. if (0 == $nbline || 0 == $id) {
  56. return false;
  57. }
  58. $id = strval(intval($id)); //make sure id is integer
  59. $title = Database::escape_string(trim($title));
  60. $comment = Database::escape_string(trim($comment));
  61. $nbline = strval(intval($nbline));
  62. $sql = "UPDATE $TBL_USERINFO_DEF SET
  63. title = '$title',
  64. comment = '$comment',
  65. line_count = '$nbline'
  66. WHERE id = '$id'";
  67. Database::query($sql);
  68. return true;
  69. }
  70. /**
  71. * remove a category from the category list.
  72. *
  73. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  74. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  75. *
  76. * @param int $id - id of the category
  77. * or "ALL" for all category
  78. * @param bool $force - FALSE (default) : prevents removal if users have
  79. * already fill this category
  80. * TRUE : bypass user content existence check
  81. * @param int $nbline - lines number for the field the user will fill
  82. *
  83. * @return bool - TRUE if succeed, ELSE otherwise
  84. */
  85. function remove_cat_def($id, $force = false)
  86. {
  87. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  88. $TBL_USERINFO_CONTENT = Database:: get_course_table(TABLE_USER_INFO_CONTENT);
  89. $id = strval(intval($id));
  90. if ((0 == (int) $id || $id == "ALL") || !is_bool($force)) {
  91. return false;
  92. }
  93. $sqlCondition = " WHERE id = $id";
  94. if (!$force) {
  95. $sql = "SELECT * FROM $TBL_USERINFO_CONTENT $sqlCondition";
  96. $result = Database::query($sql);
  97. if (Database::num_rows($result) > 0) {
  98. return false;
  99. }
  100. }
  101. $sql = "DELETE FROM $TBL_USERINFO_DEF $sqlCondition";
  102. Database::query($sql);
  103. }
  104. /**
  105. * move a category in the category list.
  106. *
  107. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  108. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  109. *
  110. * @param int $id - id of the category
  111. * @param string $direction "up" or "down" :
  112. * "up" decrease the rank of gived $id by switching rank with the just lower
  113. * "down" increase the rank of gived $id by switching rank with the just upper
  114. *
  115. * @return bool true if succeed, else boolean false
  116. */
  117. function move_cat_rank($id, $direction) // up & down.
  118. {
  119. $TBL_USERINFO_DEF = Database:: get_course_table(userinfo_def);
  120. $id = strval(intval($id));
  121. if (0 == (int) $id || !($direction == "up" || $direction == "down")) {
  122. return false;
  123. }
  124. $sql = "SELECT rank FROM $TBL_USERINFO_DEF WHERE id = $id";
  125. $result = Database::query($sql);
  126. if (Database::num_rows($result) < 1) {
  127. return false;
  128. }
  129. $cat = Database::fetch_array($result);
  130. $rank = (int) $cat['rank'];
  131. return move_cat_rank_by_rank($rank, $direction);
  132. }
  133. /**
  134. * move a category in the category list.
  135. *
  136. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  137. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  138. *
  139. * @param int $rank - actual rank of the category
  140. * @param string $direction "up" or "down" :
  141. * "up" decrease the rank of gived $rank by switching rank with the just lower
  142. * "down" increase the rank of gived $rank by switching rank with the just upper
  143. *
  144. * @return bool true if succeed, else boolean false
  145. */
  146. function move_cat_rank_by_rank($rank, $direction) // up & down.
  147. {
  148. $TBL_USERINFO_DEF = Database:: get_course_table(userinfo_def);
  149. if (0 == (int) $rank || !($direction == "up" || $direction == "down")) {
  150. return false;
  151. }
  152. if ($direction == "down") {
  153. // thus increase rank ...
  154. $sort = "ASC";
  155. $compOp = ">=";
  156. } else {
  157. // thus decrease rank ...
  158. $sort = "DESC";
  159. $compOp = "<=";
  160. }
  161. // this request find the 2 line to be switched (on rank value)
  162. $sql = "SELECT id, rank FROM $TBL_USERINFO_DEF
  163. WHERE rank $compOp $rank
  164. ORDER BY rank $sort LIMIT 2";
  165. $result = Database::query($sql);
  166. if (Database::num_rows($result) < 2) {
  167. return false;
  168. }
  169. $thisCat = Database::fetch_array($result);
  170. $nextCat = Database::fetch_array($result);
  171. $sql1 = "UPDATE $TBL_USERINFO_DEF SET rank ='".$nextCat['rank'].
  172. "' WHERE id = '".$thisCat['id']."'";
  173. $sql2 = "UPDATE $TBL_USERINFO_DEF SET rank ='".$thisCat['rank'].
  174. "' WHERE id = '".$nextCat['id']."'";
  175. Database::query($sql1);
  176. Database::query($sql2);
  177. return true;
  178. }
  179. /**
  180. * @author Hugues Peeters - peeters@ipm.ucl.ac.be
  181. *
  182. * @param int $user_id
  183. * @param string $course_code
  184. * @param array $properties - should contain 'role', 'status', 'tutor_id'
  185. *
  186. * @return bool true if succeed false otherwise
  187. */
  188. function update_user_course_properties($user_id, $course_code, $properties, $horaire_name, $course_id)
  189. {
  190. global $tbl_coursUser, $_user;
  191. $sqlChangeStatus = "";
  192. $user_id = (int) $user_id; //filter integer
  193. $course_code = Database::escape_string($course_code);
  194. $course_id = (int) $course_id;
  195. $horaire_name = Database::escape_string($horaire_name);
  196. $status = Database::escape_string($properties['status']);
  197. $tutor = Database::escape_string($properties['tutor']);
  198. if ($user_id != $_user['user_id']) {
  199. $sqlChangeStatus = "status = '$status',";
  200. }
  201. $sql = "UPDATE $tbl_coursUser
  202. SET $sqlChangeStatus
  203. is_tutor = '$tutor'
  204. WHERE user_id = $user_id AND c_id = $course_id";
  205. Database::query($sql);
  206. //update official-code: Horaire
  207. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  208. $sql2 = "UPDATE $table_user
  209. SET official_code = '$horaire_name'
  210. WHERE user_id = $user_id";
  211. Database::query($sql2);
  212. //on récupère l'horaire
  213. $tbl_personal_agenda = Database:: get_main_table(TABLE_PERSONAL_AGENDA);
  214. $TABLECALDATES = Database:: get_course_table(cal_dates);
  215. $jour = 0;
  216. $sql3 = "SELECT date FROM $TABLECALDATES
  217. WHERE
  218. horaire_name = '$horaire_name' AND
  219. status = 'C' AND
  220. c_id = $course_id
  221. ORDER BY date ";
  222. $result3 = Database::query($sql3);
  223. if (Database::num_rows($result3) == '0') {
  224. return false;
  225. }
  226. //on efface ce qui est déjà inscrit
  227. $sql4 = "DELETE FROM $tbl_personal_agenda
  228. WHERE user = $user_id
  229. AND text = 'Pour le calendrier, ne pas effacer'";
  230. Database::query($sql4);
  231. $sql = "DELETE FROM $tbl_personal_agenda
  232. WHERE user = $user_id AND title = 'Examen*'";
  233. Database::query($sql);
  234. //à chaque date dans l'horaire
  235. while ($res3 = Database::fetch_array($result3)) {
  236. $date = $res3['date'];
  237. //on incrémente les jours de cours
  238. $date = api_get_utc_datetime($date);
  239. $jour = $jour + 1;
  240. //on réinsère le nouvel horaire
  241. $sql = "INSERT ".$tbl_personal_agenda." (user,title,text,date)
  242. VALUES ($user_id, $jour, 'Pour le calendrier, ne pas effacer', '$date')";
  243. Database::query($sql);
  244. // pour les inscrire examens dans agenda
  245. $sql5 = "SELECT date FROM $TABLECALDATES
  246. WHERE horaire_name = '$horaire_name' AND status = 'E'
  247. AND c_id = '$course_id'
  248. ORDER BY date
  249. ";
  250. $result5 = Database::query($sql5);
  251. }
  252. //à chaque date dans l'horaire
  253. while ($res5 = Database::fetch_array($result5)) {
  254. $date = $res5['date'];
  255. $date = api_get_utc_datetime($date);
  256. //on réinsère le nouvel horaire
  257. $sql7 = "INSERT $tbl_personal_agenda (user, title, date) VALUES ($user_id, 'Examen*', '$date')";
  258. Database::query($sql7);
  259. }
  260. }
  261. /**
  262. * fill a bloc for information category.
  263. *
  264. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  265. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  266. *
  267. * @param $definition_id
  268. * @param $user_id
  269. * @param $user_ip
  270. * @param $content
  271. *
  272. * @return bool true if succeed, else boolean false
  273. */
  274. function fill_new_cat_content($definition_id, $user_id, $content = "", $user_ip = "")
  275. {
  276. global $TBL_USERINFO_CONTENT;
  277. if (empty($user_ip)) {
  278. $user_ip = $_SERVER['REMOTE_ADDR'];
  279. }
  280. $definition_id = (int) $definition_id;
  281. $user_id = (int) $user_id;
  282. $content = Database::escape_string(trim($content));
  283. $user_ip = Database::escape_string(trim($user_ip));
  284. if (0 == $definition_id || 0 == $user_id || $content == "") {
  285. // Here we should introduce an error handling system...
  286. return false;
  287. }
  288. // Do not create if already exist
  289. $sql = "SELECT id FROM $TBL_USERINFO_CONTENT
  290. WHERE definition_id = '$definition_id'
  291. AND user_id = $user_id";
  292. $result = Database::query($sql);
  293. if (Database::num_rows($result) > 0) {
  294. return false;
  295. }
  296. $sql = "INSERT INTO $TBL_USERINFO_CONTENT SET
  297. content = '$content',
  298. definition_id = $definition_id,
  299. user_id = $user_id,
  300. editor_ip = '$user_ip',
  301. edition_time = now()";
  302. Database::query($sql);
  303. return true;
  304. }
  305. /**
  306. * Edit a bloc for information category.
  307. *
  308. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  309. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  310. *
  311. * @param $definition_id
  312. * @param $user_id
  313. * @param $user_ip DEFAULT $REMOTE_ADDR
  314. * @param $content if empty call delete the bloc
  315. *
  316. * @return bool true if succeed, else boolean false
  317. */
  318. function edit_cat_content($definition_id, $user_id, $content = "", $user_ip = "")
  319. {
  320. global $TBL_USERINFO_CONTENT;
  321. $definition_id = (int) $definition_id;
  322. $user_id = (int) $user_id;
  323. $content = Database::escape_string(trim($content));
  324. if (empty($user_ip)) {
  325. $user_ip = $_SERVER['REMOTE_ADDR'];
  326. }
  327. $user_ip = Database::escape_string($user_ip);
  328. if (0 == $user_id || 0 == $definition_id) {
  329. return false;
  330. }
  331. if ($content == "") {
  332. return cleanout_cat_content($user_id, $definition_id);
  333. }
  334. $sql = "UPDATE $TBL_USERINFO_CONTENT SET
  335. content = '$content',
  336. editor_ip = '$user_ip',
  337. edition_time = now()
  338. WHERE definition_id = $definition_id AND user_id = $user_id";
  339. Database::query($sql);
  340. return true;
  341. }
  342. /**
  343. * clean the content of a bloc for information category.
  344. *
  345. * @author Hugues peeters <peeters@ipm.ucl.ac.be>
  346. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  347. *
  348. * @param $definition_id
  349. * @param $user_id
  350. *
  351. * @return bool true if succeed, else boolean false
  352. */
  353. function cleanout_cat_content($user_id, $definition_id)
  354. {
  355. global $TBL_USERINFO_CONTENT;
  356. $user_id = (int) $user_id;
  357. $definition_id = (int) $definition_id;
  358. if (0 == $user_id || 0 == $definition_id) {
  359. return false;
  360. }
  361. $sql = "DELETE FROM $TBL_USERINFO_CONTENT
  362. WHERE user_id = $user_id AND definition_id = $definition_id";
  363. Database::query($sql);
  364. return true;
  365. }
  366. /**
  367. * get the user info from the user id.
  368. *
  369. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  370. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  371. *
  372. * @param int $user_id user id as stored in the Dokeos main db
  373. *
  374. * @return array containg user info sort by categories rank
  375. * each rank contains 'title', 'comment', 'content', 'cat_id'
  376. */
  377. function get_course_user_info($user_id)
  378. {
  379. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  380. $TBL_USERINFO_CONTENT = Database:: get_course_table(TABLE_USER_INFO_CONTENT);
  381. $user_id = (int) $user_id;
  382. $sql = "SELECT cat.id catId, cat.title,
  383. cat.comment , content.content
  384. FROM $TBL_USERINFO_DEF cat LEFT JOIN $TBL_USERINFO_CONTENT content
  385. ON cat.id = content.definition_id AND content.user_id = $user_id
  386. ORDER BY cat.rank, content.id";
  387. $result = Database::query($sql);
  388. if (Database::num_rows($result) > 0) {
  389. while ($userInfo = Database::fetch_array($result, 'ASSOC')) {
  390. $userInfos[] = $userInfo;
  391. }
  392. return $userInfos;
  393. }
  394. return false;
  395. }
  396. /**
  397. * get the main user information.
  398. *
  399. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  400. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  401. *
  402. * @param int $user_id user id as stored in the Dokeos main db
  403. *
  404. * @return array containing user info as 'lastName', 'firstName', 'email', 'role'
  405. */
  406. function get_main_user_info($user_id, $courseCode)
  407. {
  408. $user_id = (int) $user_id;
  409. $courseCode = Database::escape_string($courseCode);
  410. $courseId = api_get_course_int_id($courseCode);
  411. if (0 == $user_id) {
  412. return false;
  413. }
  414. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  415. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  416. $sql = "SELECT u.*, u.lastname lastName, u.firstname firstName,
  417. u.email, u.picture_uri picture,
  418. cu.status status, cu.is_tutor as tutor_id
  419. FROM $table_user u, $table_course_user cu
  420. WHERE u.user_id = cu.user_id AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  421. AND u.user_id = $user_id
  422. AND cu.c_id = $courseId";
  423. $result = Database::query($sql);
  424. if (Database::num_rows($result) > 0) {
  425. $userInfo = Database::fetch_array($result, 'ASSOC');
  426. $userInfo['password'] = '';
  427. return $userInfo;
  428. }
  429. return false;
  430. }
  431. /**
  432. * get the user content of a categories plus the categories definition.
  433. *
  434. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  435. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  436. *
  437. * @param int $userId id of the user
  438. * @param int $catId id of the categories
  439. *
  440. * @return array containing 'catId', 'title', 'comment', 'nbline', 'contentId' and 'content'
  441. */
  442. function get_cat_content($userId, $catId)
  443. {
  444. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  445. $TBL_USERINFO_CONTENT = Database:: get_course_table(TABLE_USER_INFO_CONTENT);
  446. $userId = (int) $userId;
  447. $catId = (int) $catId;
  448. $sql = "SELECT cat.id catId, cat.title,
  449. cat.comment , cat.line_count,
  450. content.id contentId, content.content
  451. FROM $TBL_USERINFO_DEF cat LEFT JOIN $TBL_USERINFO_CONTENT content
  452. ON cat.id = content.definition_id
  453. AND content.user_id = $userId
  454. WHERE cat.id = $catId ";
  455. $result = Database::query($sql);
  456. if (Database::num_rows($result) > 0) {
  457. $catContent = Database::fetch_array($result, 'ASSOC');
  458. $catContent['nbline'] = $catContent['line_count'];
  459. return $catContent;
  460. }
  461. return false;
  462. }
  463. /**
  464. * get the definition of a category.
  465. *
  466. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  467. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  468. *
  469. * @param int $catId - id of the categories
  470. *
  471. * @return array containing 'id', 'title', 'comment', and 'nbline',
  472. */
  473. function get_cat_def($catId)
  474. {
  475. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  476. $catId = (int) $catId;
  477. $sql = "SELECT id, title, comment, line_count, rank FROM $TBL_USERINFO_DEF WHERE id = $catId";
  478. $result = Database::query($sql);
  479. if (Database::num_rows($result) > 0) {
  480. $catDef = Database::fetch_array($result, 'ASSOC');
  481. $catDef['nbline'] = $catDef['line_count'];
  482. return $catDef;
  483. }
  484. return false;
  485. }
  486. /**
  487. * get list of all this course categories.
  488. *
  489. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  490. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  491. *
  492. * @return array containing a list of arrays.
  493. * And each of these arrays contains
  494. * 'catId', 'title', 'comment', and 'nbline',
  495. */
  496. function get_cat_def_list()
  497. {
  498. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  499. $sql = "SELECT id catId, title, comment , line_count
  500. FROM $TBL_USERINFO_DEF
  501. ORDER BY rank";
  502. $result = Database::query($sql);
  503. if (Database::num_rows($result) > 0) {
  504. while ($cat_def = Database::fetch_array($result, 'ASSOC')) {
  505. $cat_def_list[] = $cat_def;
  506. }
  507. return $cat_def_list;
  508. }
  509. return false;
  510. }
  511. /**
  512. * transform content in a html display.
  513. *
  514. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  515. *
  516. * @param string $string string to htmlize
  517. *
  518. * @return string htmlized
  519. */
  520. function htmlize($string)
  521. {
  522. global $charset;
  523. return nl2br(htmlspecialchars($string, ENT_QUOTES, $charset));
  524. }