userInfoLib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 user content of a categories plus the categories definition.
  398. *
  399. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  400. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  401. *
  402. * @param int $userId id of the user
  403. * @param int $catId id of the categories
  404. *
  405. * @return array containing 'catId', 'title', 'comment', 'nbline', 'contentId' and 'content'
  406. */
  407. function get_cat_content($userId, $catId)
  408. {
  409. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  410. $TBL_USERINFO_CONTENT = Database:: get_course_table(TABLE_USER_INFO_CONTENT);
  411. $userId = (int) $userId;
  412. $catId = (int) $catId;
  413. $sql = "SELECT cat.id catId, cat.title,
  414. cat.comment , cat.line_count,
  415. content.id contentId, content.content
  416. FROM $TBL_USERINFO_DEF cat LEFT JOIN $TBL_USERINFO_CONTENT content
  417. ON cat.id = content.definition_id
  418. AND content.user_id = $userId
  419. WHERE cat.id = $catId ";
  420. $result = Database::query($sql);
  421. if (Database::num_rows($result) > 0) {
  422. $catContent = Database::fetch_array($result, 'ASSOC');
  423. $catContent['nbline'] = $catContent['line_count'];
  424. return $catContent;
  425. }
  426. return false;
  427. }
  428. /**
  429. * get the definition of a category.
  430. *
  431. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  432. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  433. *
  434. * @param int $catId - id of the categories
  435. *
  436. * @return array containing 'id', 'title', 'comment', and 'nbline',
  437. */
  438. function get_cat_def($catId)
  439. {
  440. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  441. $catId = (int) $catId;
  442. $sql = "SELECT id, title, comment, line_count, rank FROM $TBL_USERINFO_DEF WHERE id = $catId";
  443. $result = Database::query($sql);
  444. if (Database::num_rows($result) > 0) {
  445. $catDef = Database::fetch_array($result, 'ASSOC');
  446. $catDef['nbline'] = $catDef['line_count'];
  447. return $catDef;
  448. }
  449. return false;
  450. }
  451. /**
  452. * get list of all this course categories.
  453. *
  454. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  455. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  456. *
  457. * @return array containing a list of arrays.
  458. * And each of these arrays contains
  459. * 'catId', 'title', 'comment', and 'nbline',
  460. */
  461. function get_cat_def_list()
  462. {
  463. $TBL_USERINFO_DEF = Database:: get_course_table(TABLE_USER_INFO_DEF);
  464. $sql = "SELECT id catId, title, comment , line_count
  465. FROM $TBL_USERINFO_DEF
  466. ORDER BY rank";
  467. $result = Database::query($sql);
  468. if (Database::num_rows($result) > 0) {
  469. while ($cat_def = Database::fetch_array($result, 'ASSOC')) {
  470. $cat_def_list[] = $cat_def;
  471. }
  472. return $cat_def_list;
  473. }
  474. return false;
  475. }
  476. /**
  477. * transform content in a html display.
  478. *
  479. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  480. *
  481. * @param string $string string to htmlize
  482. *
  483. * @return string htmlized
  484. */
  485. function htmlize($string)
  486. {
  487. global $charset;
  488. return nl2br(htmlspecialchars($string, ENT_QUOTES, $charset));
  489. }