userInfoLib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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($result) > 0) {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * fill a bloc for information category
  218. *
  219. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  220. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  221. * @param - $definition_id,
  222. * @param - $user_id,
  223. * @param - $user_ip,
  224. * @param - $content
  225. * @return - boolean true if succeed, else bolean false
  226. */
  227. function fill_new_cat_content($definition_id, $user_id, $content="", $user_ip="")
  228. {
  229. global $TBL_USERINFO_CONTENT;
  230. if (empty($user_ip))
  231. {
  232. $user_ip = $_SERVER['REMOTE_ADDR'];
  233. }
  234. $definition_id = strval(intval($definition_id));
  235. $user_id = strval(intval($user_id));
  236. $content = Database::escape_string(trim($content));
  237. $user_ip = Database::escape_string(trim($user_ip));
  238. if ( 0 == $definition_id || 0 == $user_id || $content == "")
  239. {
  240. // Here we should introduce an error handling system...
  241. return false;
  242. }
  243. // Do not create if already exist
  244. $sql = "SELECT id FROM ".$TBL_USERINFO_CONTENT."
  245. WHERE definition_id = '$definition_id'
  246. AND user_id = '$user_id'";
  247. $result = Database::query($sql);
  248. if (Database::num_rows($result) > 0)
  249. {
  250. return false;
  251. }
  252. $sql = "INSERT INTO ".$TBL_USERINFO_CONTENT." SET
  253. content = '$content',
  254. definition_id = '$definition_id',
  255. user_id = '$user_id',
  256. editor_ip = '$user_ip',
  257. edition_time = now()";
  258. Database::query($sql);
  259. return true;
  260. }
  261. /**
  262. * Edit 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. * @param - $definition_id,
  267. * @param - $user_id,
  268. * @param - $user_ip, DEFAULT $REMOTE_ADDR
  269. * @param - $content ; if empty call delete the bloc
  270. * @return - boolean true if succeed, else bolean false
  271. */
  272. function edit_cat_content($definition_id, $user_id, $content ="", $user_ip="")
  273. {
  274. global $TBL_USERINFO_CONTENT;
  275. $definition_id = strval(intval($definition_id));
  276. $user_id = strval(intval($user_id));
  277. $content = Database::escape_string(trim($content));
  278. if (empty($user_ip))
  279. {
  280. $user_ip = $_SERVER['REMOTE_ADDR'];
  281. }
  282. $user_ip = Database::escape_string($user_ip);
  283. if (0 == $user_id || 0 == $definition_id)
  284. {
  285. return false;
  286. }
  287. if ( $content == "")
  288. {
  289. return cleanout_cat_content($user_id, $definition_id);
  290. }
  291. $sql= "UPDATE ".$TBL_USERINFO_CONTENT." SET
  292. content = '$content',
  293. editor_ip = '$user_ip',
  294. edition_time = now()
  295. WHERE definition_id = '$definition_id' AND user_id = '$user_id'";
  296. Database::query($sql);
  297. return true;
  298. }
  299. /**
  300. * clean the content of a bloc for information category
  301. *
  302. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  303. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  304. * @param - $definition_id,
  305. * @param - $user_id
  306. * @return - boolean true if succeed, else bolean false
  307. */
  308. function cleanout_cat_content($user_id, $definition_id)
  309. {
  310. global $TBL_USERINFO_CONTENT;
  311. $user_id = strval(intval($user_id));
  312. $definition_id = strval(intval($definition_id));
  313. if (0 == $user_id || 0 == $definition_id)
  314. {
  315. return false;
  316. }
  317. $sql = "DELETE FROM ".$TBL_USERINFO_CONTENT."
  318. WHERE user_id = '$user_id' AND definition_id = '$definition_id'";
  319. Database::query($sql);
  320. return true;
  321. }
  322. /*----------------------------------------
  323. SHOW USER INFORMATION TREATMENT
  324. --------------------------------------*/
  325. /**
  326. * get the user info from the user id
  327. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  328. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  329. * @param - int $user_id user id as stored in the Dokeos main db
  330. * @return - array containg user info sort by categories rank
  331. * each rank contains 'title', 'comment', 'content', 'cat_id'
  332. */
  333. function get_course_user_info($user_id)
  334. {
  335. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  336. $TBL_USERINFO_CONTENT = Database :: get_course_table(TABLE_USER_INFO_CONTENT);
  337. $sql = "SELECT cat.id catId, cat.title,
  338. cat.comment , content.content
  339. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  340. ON cat.id = content.definition_id AND content.user_id = '$user_id'
  341. ORDER BY cat.rank, content.id";
  342. $result = Database::query($sql);
  343. if (Database::num_rows($result) > 0)
  344. {
  345. while ($userInfo = Database::fetch_array($result, 'ASSOC'))
  346. {
  347. $userInfos[]=$userInfo;
  348. }
  349. return $userInfos;
  350. }
  351. return false;
  352. }
  353. /**
  354. * get the main user information
  355. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  356. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  357. * @param - int $user_id user id as stored in the Dokeos main db
  358. * @return - array containing user info as 'lastName', 'firstName'
  359. * 'email', 'role'
  360. */
  361. function get_main_user_info($user_id, $courseCode)
  362. {
  363. $user_id = strval(intval($user_id));
  364. $courseCode = Database::escape_string($courseCode);
  365. if (0 == $user_id)
  366. {
  367. return false;
  368. }
  369. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  370. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  371. $sql = "SELECT u.*, u.lastname lastName, u.firstname firstName,
  372. u.email, u.picture_uri picture, cu.role,
  373. cu.status status, cu.tutor_id
  374. FROM $table_user u, $table_course_user cu
  375. WHERE u.user_id = cu.user_id AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  376. AND u.user_id = '$user_id'
  377. AND cu.course_code = '$courseCode'";
  378. $result = Database::query($sql);
  379. if (Database::num_rows($result) > 0)
  380. {
  381. $userInfo = Database::fetch_array($result, 'ASSOC');
  382. $userInfo['password']='';
  383. return $userInfo;
  384. }
  385. return false;
  386. }
  387. /**
  388. * get the user content of a categories plus the categories definition
  389. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  390. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  391. * @param - int $userId - id of the user
  392. * @param - int $catId - id of the categories
  393. * @return - array containing 'catId', 'title', 'comment',
  394. * 'nbline', 'contentId' and 'content'
  395. */
  396. function get_cat_content($userId, $catId)
  397. {
  398. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  399. $TBL_USERINFO_CONTENT = Database :: get_course_table(TABLE_USER_INFO_CONTENT);
  400. $userId = strval(intval($userId));
  401. $catId = strval(intval($catId));
  402. $sql = "SELECT cat.id catId, cat.title,
  403. cat.comment , cat.line_count,
  404. content.id contentId, content.content
  405. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  406. ON cat.id = content.definition_id
  407. AND content.user_id = '$userId'
  408. WHERE cat.id = '$catId' ";
  409. $result = Database::query($sql);
  410. if (Database::num_rows($result) > 0)
  411. {
  412. $catContent = Database::fetch_array($result, 'ASSOC');
  413. $catContent['nbline'] = $catContent['line_count'];
  414. return $catContent;
  415. }
  416. return false;
  417. }
  418. /**
  419. * get the definition of a category
  420. *
  421. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  422. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  423. * @param - int $catId - id of the categories
  424. * @return - array containing 'id', 'title', 'comment', and 'nbline',
  425. */
  426. function get_cat_def($catId)
  427. {
  428. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  429. $catId = strval(intval($catId));
  430. $sql = "SELECT id, title, comment, line_count, rank FROM ".$TBL_USERINFO_DEF." WHERE id = '$catId'";
  431. $result = Database::query($sql);
  432. if (Database::num_rows($result) > 0)
  433. {
  434. $catDef = Database::fetch_array($result, 'ASSOC');
  435. $catDef['nbline'] = $catDef['line_count'];
  436. return $catDef;
  437. }
  438. return false;
  439. }
  440. /**
  441. * get list of all this course categories
  442. *
  443. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  444. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  445. * @return - array containing a list of arrays.
  446. * And each of these arrays contains
  447. * 'catId', 'title', 'comment', and 'nbline',
  448. */
  449. function get_cat_def_list()
  450. {
  451. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  452. $sql = "SELECT id catId, title, comment , line_count
  453. FROM ".$TBL_USERINFO_DEF."
  454. ORDER BY rank";
  455. $result = Database::query($sql);
  456. if (Database::num_rows($result) > 0)
  457. {
  458. while ($cat_def = Database::fetch_array($result, 'ASSOC'))
  459. {
  460. $cat_def_list[]=$cat_def;
  461. }
  462. return $cat_def_list;
  463. }
  464. return false;
  465. }
  466. /**
  467. * transform content in a html display
  468. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  469. * @param - string $string string to htmlize
  470. * @ return - string htmlized
  471. */
  472. function htmlize($phrase)
  473. {
  474. global $charset;
  475. return nl2br(htmlspecialchars($phrase,ENT_QUOTES,$charset));
  476. }