userInfoLib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /* For licensing terms, see /license.txt*/
  3. /**
  4. * @package chamilo.user
  5. */
  6. /* CATEGORIES DEFINITION TREATMENT */
  7. /**
  8. * create a new category definition for the user information
  9. *
  10. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  11. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  12. * @param - string $title - category title
  13. * @param - string $comment - title comment
  14. * @param - int$nbline - lines number for the field the user will fill.
  15. * @return - bollean true if succeed, else bolean false
  16. */
  17. function create_cat_def($title="", $comment="", $nbline="5")
  18. {
  19. global $TBL_USERINFO_DEF; //taken from userInfo.php
  20. $title = Database::escape_string(trim($title));
  21. $comment = Database::escape_string(trim($comment));
  22. $nbline = strval(intval($nbline));
  23. if ( 0 == (int) $nbline || empty($title))
  24. {
  25. return false;
  26. }
  27. $sql = "SELECT MAX(rank) as maxRank FROM ".$TBL_USERINFO_DEF;
  28. $result = Database::query($sql);
  29. if ($result) $maxRank = Database::fetch_array($result);
  30. $maxRank = $maxRank['maxRank'];
  31. $thisRank = $maxRank + 1;
  32. $sql = "INSERT INTO $TBL_USERINFO_DEF SET
  33. title = '$title',
  34. comment = '$comment',
  35. line_count = '$nbline',
  36. rank = '$thisRank'";
  37. Database::query($sql);
  38. return true;
  39. }
  40. /**
  41. * modify the definition of a user information category
  42. *
  43. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  44. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  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. * @return - boolean true if succeed, else otherwise
  50. */
  51. function edit_cat_def($id, $title, $comment, $nbline)
  52. {
  53. global $TBL_USERINFO_DEF;
  54. if ( 0 == $nbline || 0 == $id )
  55. {
  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 - boolean $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. * @return - bollean - TRUE if succeed, ELSE otherwise
  83. */
  84. function remove_cat_def($id, $force = false)
  85. {
  86. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  87. $TBL_USERINFO_CONTENT = Database :: get_course_table(TABLE_USER_INFO_CONTENT);
  88. $id = strval(intval($id));
  89. if ( (0 == (int) $id || $id == "ALL") || ! is_bool($force))
  90. {
  91. return false;
  92. }
  93. $sqlCondition = " WHERE id = '$id'";
  94. if (!$force)
  95. {
  96. $sql = "SELECT * FROM $TBL_USERINFO_CONTENT $sqlCondition";
  97. $result = Database::query($sql);
  98. if ( Database::num_rows($result) > 0)
  99. {
  100. return false;
  101. }
  102. }
  103. $sql = "DELETE FROM $TBL_USERINFO_DEF $sqlCondition";
  104. Database::query($sql);
  105. }
  106. /**
  107. * move a category in the category list
  108. *
  109. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  110. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  111. *
  112. * @param - int $id - id of the category
  113. * @param - direction "up" or "down" :
  114. * "up" decrease the rank of gived $id by switching rank with the just lower
  115. * "down" increase the rank of gived $id by switching rank with the just upper
  116. *
  117. * @return - boolean true if succeed, else bolean false
  118. */
  119. function move_cat_rank($id, $direction) // up & down.
  120. {
  121. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  122. $id = strval(intval($id));
  123. if ( 0 == (int) $id || ! ($direction == "up" || $direction == "down") )
  124. {
  125. return false;
  126. }
  127. $sql = "SELECT rank FROM $TBL_USERINFO_DEF WHERE id = '$id'";
  128. $result = Database::query($sql);
  129. if (Database::num_rows($result) < 1)
  130. {
  131. return false;
  132. }
  133. $cat = Database::fetch_array($result);
  134. $rank = (int) $cat['rank'];
  135. return move_cat_rank_by_rank($rank, $direction);
  136. }
  137. /**
  138. * move a category in the category list
  139. *
  140. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  141. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  142. *
  143. * @param - int $rank - actual rank of the category
  144. * @param - direction "up" or "down" :
  145. * "up" decrease the rank of gived $rank by switching rank with the just lower
  146. * "down" increase the rank of gived $rank by switching rank with the just upper
  147. *
  148. * @return - boolean true if succeed, else bolean false
  149. */
  150. function move_cat_rank_by_rank($rank, $direction) // up & down.
  151. {
  152. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  153. if ( 0 == (int) $rank || ! ($direction == "up" || $direction == "down") )
  154. {
  155. return false;
  156. }
  157. if ($direction == "down") // thus increase rank ...
  158. {
  159. $sort = "ASC";
  160. $compOp = ">=";
  161. }
  162. else // thus decrease rank ...
  163. {
  164. $sort = "DESC";
  165. $compOp = "<=";
  166. }
  167. // this request find the 2 line to be switched (on rank value)
  168. $sql = "SELECT id, rank FROM ".$TBL_USERINFO_DEF." WHERE rank $compOp $rank
  169. ORDER BY rank $sort LIMIT 2";
  170. $result = Database::query($sql);
  171. if (Database::num_rows($result) < 2)
  172. {
  173. return false;
  174. }
  175. $thisCat = Database::fetch_array($result);
  176. $nextCat = Database::fetch_array($result);
  177. $sql1 = "UPDATE ".$TBL_USERINFO_DEF." SET rank ='".$nextCat['rank'].
  178. "' WHERE id = '".$thisCat['id']."'";
  179. $sql2 = "UPDATE ".$TBL_USERINFO_DEF." SET rank ='".$thisCat['rank'].
  180. "' WHERE id = '".$nextCat['id']."'";
  181. Database::query($sql1);
  182. Database::query($sql2);
  183. return true;
  184. }
  185. /**
  186. * @author Hugues Peeters - peeters@ipm.ucl.ac.be
  187. * @param int $user_id
  188. * @param string $course_code
  189. * @param array $properties - should contain 'role', 'status', 'tutor_id'
  190. * @return boolean true if succeed false otherwise
  191. */
  192. function update_user_course_properties($user_id, $course_code, $properties)
  193. {
  194. global $tbl_coursUser,$_user;
  195. $sqlChangeStatus = "";
  196. $user_id = strval(intval($user_id));//filter integer
  197. $course_code = Database::escape_string($course_code);
  198. if ($user_id != $_user['user_id'])
  199. {
  200. $sqlChangeStatus = "status = '".Database::escape_string($properties['status'])."',";
  201. }
  202. //feature deprecated tutor_id = '".Database::escape_string($properties['tutor'])."'
  203. $sql = "UPDATE $tbl_coursUser
  204. SET ".$sqlChangeStatus."
  205. role = '".Database::escape_string($properties['role'])."',
  206. tutor_id = '".Database::escape_string($properties['tutor'])."'
  207. WHERE user_id = '".$user_id."'
  208. AND course_code = '".$course_code."'";
  209. $result = Database::query($sql);
  210. if (Database::affected_rows() > 0)
  211. {
  212. return true;
  213. }
  214. else
  215. {
  216. return false;
  217. }
  218. }
  219. /*----------------------------------------
  220. CATEGORIES CONTENT TREATMENT
  221. --------------------------------------*/
  222. /**
  223. * fill a bloc for information category
  224. *
  225. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  226. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  227. * @param - $definition_id,
  228. * @param - $user_id,
  229. * @param - $user_ip,
  230. * @param - $content
  231. * @return - boolean true if succeed, else bolean false
  232. */
  233. function fill_new_cat_content($definition_id, $user_id, $content="", $user_ip="")
  234. {
  235. global $TBL_USERINFO_CONTENT;
  236. if (empty($user_ip))
  237. {
  238. $user_ip = $_SERVER['REMOTE_ADDR'];
  239. }
  240. $definition_id = strval(intval($definition_id));
  241. $user_id = strval(intval($user_id));
  242. $content = Database::escape_string(trim($content));
  243. $user_ip = Database::escape_string(trim($user_ip));
  244. if ( 0 == $definition_id || 0 == $user_id || $content == "")
  245. {
  246. // Here we should introduce an error handling system...
  247. return false;
  248. }
  249. // Do not create if already exist
  250. $sql = "SELECT id FROM ".$TBL_USERINFO_CONTENT."
  251. WHERE definition_id = '$definition_id'
  252. AND user_id = '$user_id'";
  253. $result = Database::query($sql);
  254. if (Database::num_rows($result) > 0)
  255. {
  256. return false;
  257. }
  258. $sql = "INSERT INTO ".$TBL_USERINFO_CONTENT." SET
  259. content = '$content',
  260. definition_id = '$definition_id',
  261. user_id = '$user_id',
  262. editor_ip = '$user_ip',
  263. edition_time = now()";
  264. Database::query($sql);
  265. return true;
  266. }
  267. /**
  268. * Edit a bloc for information category
  269. *
  270. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  271. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  272. * @param - $definition_id,
  273. * @param - $user_id,
  274. * @param - $user_ip, DEFAULT $REMOTE_ADDR
  275. * @param - $content ; if empty call delete the bloc
  276. * @return - boolean true if succeed, else bolean false
  277. */
  278. function edit_cat_content($definition_id, $user_id, $content ="", $user_ip="")
  279. {
  280. global $TBL_USERINFO_CONTENT;
  281. $definition_id = strval(intval($definition_id));
  282. $user_id = strval(intval($user_id));
  283. $content = Database::escape_string(trim($content));
  284. if (empty($user_ip))
  285. {
  286. $user_ip = $_SERVER['REMOTE_ADDR'];
  287. }
  288. $user_ip = Database::escape_string($user_ip);
  289. if (0 == $user_id || 0 == $definition_id)
  290. {
  291. return false;
  292. }
  293. if ( $content == "")
  294. {
  295. return cleanout_cat_content($user_id, $definition_id);
  296. }
  297. $sql= "UPDATE ".$TBL_USERINFO_CONTENT." SET
  298. content = '$content',
  299. editor_ip = '$user_ip',
  300. edition_time = now()
  301. WHERE definition_id = '$definition_id' AND user_id = '$user_id'";
  302. Database::query($sql);
  303. return true;
  304. }
  305. /**
  306. * clean the content of 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. * @param - $definition_id,
  311. * @param - $user_id
  312. * @return - boolean true if succeed, else bolean false
  313. */
  314. function cleanout_cat_content($user_id, $definition_id)
  315. {
  316. global $TBL_USERINFO_CONTENT;
  317. $user_id = strval(intval($user_id));
  318. $definition_id = strval(intval($definition_id));
  319. if (0 == $user_id || 0 == $definition_id)
  320. {
  321. return false;
  322. }
  323. $sql = "DELETE FROM ".$TBL_USERINFO_CONTENT."
  324. WHERE user_id = '$user_id' AND definition_id = '$definition_id'";
  325. Database::query($sql);
  326. return true;
  327. }
  328. /*----------------------------------------
  329. SHOW USER INFORMATION TREATMENT
  330. --------------------------------------*/
  331. /**
  332. * get the user info from the user id
  333. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  334. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  335. * @param - int $user_id user id as stored in the Dokeos main db
  336. * @return - array containg user info sort by categories rank
  337. * each rank contains 'title', 'comment', 'content', 'cat_id'
  338. */
  339. function get_course_user_info($user_id)
  340. {
  341. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  342. $TBL_USERINFO_CONTENT = Database :: get_course_table(TABLE_USER_INFO_CONTENT);
  343. $sql = "SELECT cat.id catId, cat.title,
  344. cat.comment , content.content
  345. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  346. ON cat.id = content.definition_id AND content.user_id = '$user_id'
  347. ORDER BY cat.rank, content.id";
  348. $result = Database::query($sql);
  349. if (Database::num_rows($result) > 0)
  350. {
  351. while ($userInfo = Database::fetch_array($result, 'ASSOC'))
  352. {
  353. $userInfos[]=$userInfo;
  354. }
  355. return $userInfos;
  356. }
  357. return false;
  358. }
  359. /**
  360. * get the main user information
  361. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  362. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  363. * @param - int $user_id user id as stored in the Dokeos main db
  364. * @return - array containing user info as 'lastName', 'firstName'
  365. * 'email', 'role'
  366. */
  367. function get_main_user_info($user_id, $courseCode)
  368. {
  369. $user_id = strval(intval($user_id));
  370. $courseCode = Database::escape_string($courseCode);
  371. if (0 == $user_id)
  372. {
  373. return false;
  374. }
  375. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  376. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  377. $sql = "SELECT u.*, u.lastname lastName, u.firstname firstName,
  378. u.email, u.picture_uri picture, cu.role,
  379. cu.status status, cu.tutor_id
  380. FROM $table_user u, $table_course_user cu
  381. WHERE u.user_id = cu.user_id AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  382. AND u.user_id = '$user_id'
  383. AND cu.course_code = '$courseCode'";
  384. $result = Database::query($sql);
  385. if (Database::num_rows($result) > 0)
  386. {
  387. $userInfo = Database::fetch_array($result, 'ASSOC');
  388. $userInfo['password']='';
  389. return $userInfo;
  390. }
  391. return false;
  392. }
  393. /**
  394. * get the user content of a categories plus the categories definition
  395. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  396. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  397. * @param - int $userId - id of the user
  398. * @param - int $catId - id of the categories
  399. * @return - array containing 'catId', 'title', 'comment',
  400. * 'nbline', 'contentId' and 'content'
  401. */
  402. function get_cat_content($userId, $catId)
  403. {
  404. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  405. $TBL_USERINFO_CONTENT = Database :: get_course_table(TABLE_USER_INFO_CONTENT);
  406. $userId = strval(intval($userId));
  407. $catId = strval(intval($catId));
  408. $sql = "SELECT cat.id catId, cat.title,
  409. cat.comment , cat.line_count,
  410. content.id contentId, content.content
  411. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  412. ON cat.id = content.definition_id
  413. AND content.user_id = '$userId'
  414. WHERE cat.id = '$catId' ";
  415. $result = Database::query($sql);
  416. if (Database::num_rows($result) > 0)
  417. {
  418. $catContent = Database::fetch_array($result, 'ASSOC');
  419. $catContent['nbline'] = $catContent['line_count'];
  420. return $catContent;
  421. }
  422. return false;
  423. }
  424. /**
  425. * get the definition of a category
  426. *
  427. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  428. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  429. * @param - int $catId - id of the categories
  430. * @return - array containing 'id', 'title', 'comment', and 'nbline',
  431. */
  432. function get_cat_def($catId)
  433. {
  434. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  435. $catId = strval(intval($catId));
  436. $sql = "SELECT id, title, comment, line_count, rank FROM ".$TBL_USERINFO_DEF." WHERE id = '$catId'";
  437. $result = Database::query($sql);
  438. if (Database::num_rows($result) > 0)
  439. {
  440. $catDef = Database::fetch_array($result, 'ASSOC');
  441. $catDef['nbline'] = $catDef['line_count'];
  442. return $catDef;
  443. }
  444. return false;
  445. }
  446. /**
  447. * get list of all this course categories
  448. *
  449. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  450. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  451. * @return - array containing a list of arrays.
  452. * And each of these arrays contains
  453. * 'catId', 'title', 'comment', and 'nbline',
  454. */
  455. function get_cat_def_list()
  456. {
  457. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  458. $sql = "SELECT id catId, title, comment , line_count
  459. FROM ".$TBL_USERINFO_DEF."
  460. ORDER BY rank";
  461. $result = Database::query($sql);
  462. if (Database::num_rows($result) > 0)
  463. {
  464. while ($cat_def = Database::fetch_array($result, 'ASSOC'))
  465. {
  466. $cat_def_list[]=$cat_def;
  467. }
  468. return $cat_def_list;
  469. }
  470. return false;
  471. }
  472. /**
  473. * transform content in a html display
  474. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  475. * @param - string $string string to htmlize
  476. * @ return - string htmlized
  477. */
  478. function htmlize($phrase)
  479. {
  480. global $charset;
  481. return nl2br(htmlspecialchars($phrase,ENT_QUOTES,$charset));
  482. }