userInfoLib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * @package dokeos.user
  21. ==============================================================================
  22. */
  23. /*----------------------------------------
  24. CATEGORIES DEFINITION TREATMENT
  25. --------------------------------------*/
  26. /**
  27. * create a new category definition for the user information
  28. *
  29. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  30. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  31. * @param - string $title - category title
  32. * @param - string $comment - title comment
  33. * @param - int$nbline - lines number for the field the user will fill.
  34. * @return - bollean true if succeed, else bolean false
  35. */
  36. function create_cat_def($title="", $comment="", $nbline="5")
  37. {
  38. global $TBL_USERINFO_DEF;
  39. if ( 0 == (int) $nbline || empty($title))
  40. {
  41. return false;
  42. }
  43. $sql = "SELECT MAX(`rank`) maxRank FROM ".$TBL_USERINFO_DEF."";
  44. $result = api_sql_query($sql,__FILE__,__LINE__);
  45. if ($result) $maxRank = mysql_fetch_array($result);
  46. $maxRank = $maxRank['maxRank'];
  47. $thisRank = $maxRank + 1;
  48. $title = trim($title);
  49. $comment = trim($comment);
  50. $sql = "INSERT INTO ".$TBL_USERINFO_DEF." SET
  51. `title` = '$title',
  52. `comment` = '$comment',
  53. `line_count` = '$nbline',
  54. `rank` = '$thisRank'";
  55. api_sql_query($sql,__FILE__,__LINE__);
  56. return true;
  57. }
  58. /**
  59. * modify the definition of a user information category
  60. *
  61. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  62. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  63. * @param - int $id - id of the category
  64. * @param - string $title - category title
  65. * @param - string $comment - title comment
  66. * @param - int$nbline - lines number for the field the user will fill.
  67. * @return - boolean true if succeed, else otherwise
  68. */
  69. function edit_cat_def($id, $title, $comment, $nbline)
  70. {
  71. global $TBL_USERINFO_DEF;
  72. if ( 0 == (int) $nbline || 0 == (int) $id )
  73. {
  74. return false;
  75. }
  76. $title = trim($title);
  77. $comment = trim($comment);
  78. $sql = "UPDATE ".$TBL_USERINFO_DEF." SET
  79. `title` = '$title',
  80. `comment` = '$comment',
  81. `line_count` = '$nbline'
  82. WHERE id = '$id'";
  83. api_sql_query($sql,__FILE__,__LINE__);
  84. return true;
  85. }
  86. /**
  87. * remove a category from the category list
  88. *
  89. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  90. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  91. *
  92. * @param - int $id - id of the category
  93. * or "ALL" for all category
  94. * @param - boolean $force - FALSE (default) : prevents removal if users have
  95. * already fill this category
  96. * TRUE : bypass user content existence check
  97. * @param - int $nbline - lines number for the field the user will fill.
  98. * @return - bollean - TRUE if succeed, ELSE otherwise
  99. */
  100. function remove_cat_def($id, $force = false)
  101. {
  102. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  103. if ( (0 == (int) $id || $id == "ALL") || ! is_bool($force))
  104. {
  105. return false;
  106. }
  107. if ( $id != "ALL")
  108. {
  109. $sqlCondition = " WHERE id = '$id'";
  110. } else {
  111. $sqlCondition = "";
  112. }
  113. if ($force == false)
  114. {
  115. $sql = "SELECT * FROM ".$TBL_USERINFO_CONTENT." ".$sqlCondition;
  116. $result = api_sql_query($sql,__FILE__,__LINE__);
  117. if ( mysql_num_rows($result) > 0)
  118. {
  119. return false;
  120. }
  121. }
  122. $sql = "DELETE FROM ".$TBL_USERINFO_DEF." ".$sqlCondition;
  123. api_sql_query($sql,__FILE__,__LINE__);
  124. }
  125. /**
  126. * move a category in the category list
  127. *
  128. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  129. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  130. *
  131. * @param - int $id - id of the category
  132. * @param - direction "up" or "down" :
  133. * "up" decrease the rank of gived $id by switching rank with the just lower
  134. * "down" increase the rank of gived $id by switching rank with the just upper
  135. *
  136. * @return - boolean true if succeed, else bolean false
  137. */
  138. function move_cat_rank($id, $direction) // up & down.
  139. {
  140. global $TBL_USERINFO_DEF;
  141. if ( 0 == (int) $id || ! ($direction == "up" || $direction == "down") )
  142. {
  143. return false;
  144. }
  145. $sql = "SELECT rank FROM ".$TBL_USERINFO_DEF." WHERE id = '$id'";
  146. $result = api_sql_query($sql,__FILE__,__LINE__);
  147. if (mysql_num_rows($result) < 1)
  148. {
  149. return false;
  150. }
  151. $cat = mysql_fetch_array($result);
  152. $rank = (int) $cat["rank"];
  153. return move_cat_rank_by_rank($rank, $direction);
  154. }
  155. /**
  156. * move a category in the category list
  157. *
  158. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  159. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  160. *
  161. * @param - int $rank - actual rank of the category
  162. * @param - direction "up" or "down" :
  163. * "up" decrease the rank of gived $rank by switching rank with the just lower
  164. * "down" increase the rank of gived $rank by switching rank with the just upper
  165. *
  166. * @return - boolean true if succeed, else bolean false
  167. */
  168. function move_cat_rank_by_rank($rank, $direction) // up & down.
  169. {
  170. global $TBL_USERINFO_DEF;
  171. if ( 0 == (int) $rank || ! ($direction == "up" || $direction == "down") )
  172. {
  173. return false;
  174. }
  175. if ($direction == "down") // thus increase rank ...
  176. {
  177. $sort = "ASC";
  178. $compOp = ">=";
  179. }
  180. else // thus decrease rank ...
  181. {
  182. $sort = "DESC";
  183. $compOp = "<=";
  184. }
  185. // this request find the 2 line to be switched (on rank value)
  186. $sql = "SELECT id, rank FROM ".$TBL_USERINFO_DEF." WHERE rank $compOp $rank
  187. ORDER BY rank $sort LIMIT 2";
  188. $result = api_sql_query($sql,__FILE__,__LINE__);
  189. if (mysql_num_rows($result) < 2)
  190. {
  191. return false;
  192. }
  193. $thisCat = mysql_fetch_array($result);
  194. $nextCat = mysql_fetch_array($result);
  195. $sql1 = "UPDATE ".$TBL_USERINFO_DEF." SET rank ='".$nextCat['rank'].
  196. "' WHERE id = '".$thisCat['id']."'";
  197. $sql2 = "UPDATE ".$TBL_USERINFO_DEF." SET rank ='".$thisCat['rank'].
  198. "' WHERE id = '".$nextCat['id']."'";
  199. api_sql_query($sql1,__FILE__,__LINE__);
  200. api_sql_query($sql2,__FILE__,__LINE__);
  201. return true;
  202. }
  203. /**
  204. * @author Hugues Peeters - peeters@ipm.ucl.ac.be
  205. * @param int $user_id
  206. * @param string $course_code
  207. * @param array $properties - should contain 'role', 'status', 'tutor_id'
  208. * @return boolean true if succeed false otherwise
  209. */
  210. function update_user_course_properties($user_id, $course_code, $properties)
  211. {
  212. global $tbl_coursUser,$_uid;
  213. $sqlChangeStatus = "";
  214. if ($user_id != $_uid)
  215. $sqlChangeStatus = "`status` = '".$properties['status']."',";
  216. $result = api_sql_query("UPDATE $tbl_coursUser
  217. SET `role` = '".$properties['role']."',
  218. ".$sqlChangeStatus."
  219. `tutor_id` = '".$properties['tutor']."'
  220. WHERE `user_id` = '".$user_id."'
  221. AND `course_code` = '".$course_code."'",__FILE__,__LINE__);
  222. if (mysql_affected_rows() > 0)
  223. {
  224. return true;
  225. }
  226. else
  227. {
  228. return false;
  229. }
  230. }
  231. /*----------------------------------------
  232. CATEGORIES CONTENT TREATMENT
  233. --------------------------------------*/
  234. /**
  235. * fill a bloc for information category
  236. *
  237. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  238. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  239. * @param - $definition_id,
  240. * @param - $user_id,
  241. * @param - $user_ip,
  242. * @param - $content
  243. * @return - boolean true if succeed, else bolean false
  244. */
  245. function fill_new_cat_content($definition_id, $user_id, $content="", $user_ip="")
  246. {
  247. global $TBL_USERINFO_CONTENT;
  248. if (empty($user_ip))
  249. {
  250. global $REMOTE_ADDR;
  251. $user_ip = $REMOTE_ADDR;
  252. }
  253. $content = trim($content);
  254. if ( 0 == (int) $definition_id || 0 == (int) $user_id || $content == "")
  255. {
  256. // Here we should introduce an error handling system...
  257. return false;
  258. }
  259. // Do not create if already exist
  260. $sql = "SELECT id FROM ".$TBL_USERINFO_CONTENT."
  261. WHERE `definition_id` = '$definition_id'
  262. AND `user_id` = '$user_id'";
  263. $result = api_sql_query($sql,__FILE__,__LINE__);
  264. if (mysql_num_rows($result) > 0)
  265. {
  266. return false;
  267. }
  268. $sql = "INSERT INTO ".$TBL_USERINFO_CONTENT." SET
  269. `content` = '$content',
  270. `definition_id` = '$definition_id',
  271. `user_id` = '$user_id',
  272. `editor_ip` = '$user_ip',
  273. `edition_time` = now()";
  274. api_sql_query($sql,__FILE__,__LINE__);
  275. return true;
  276. }
  277. /**
  278. * edit a bloc for information category
  279. *
  280. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  281. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  282. * @param - $definition_id,
  283. * @param - $user_id,
  284. * @param - $user_ip, DEFAULT $REMOTE_ADDR
  285. * @param - $content ; if empty call delete the bloc
  286. * @return - boolean true if succeed, else bolean false
  287. */
  288. function edit_cat_content($definition_id, $user_id, $content ="", $user_ip="")
  289. {
  290. global $TBL_USERINFO_CONTENT;
  291. if (empty($user_ip))
  292. {
  293. global $REMOTE_ADDR;
  294. $user_ip = $REMOTE_ADDR;
  295. }
  296. if (0 == (int) $user_id || 0 == (int) $definition_id)
  297. {
  298. return false;
  299. }
  300. $content = trim($content);
  301. if ( trim($content) == "")
  302. {
  303. return cleanout_cat_content($user_id, $definition_id);
  304. }
  305. $sql= "UPDATE ".$TBL_USERINFO_CONTENT." SET
  306. `content` = '$content',
  307. `editor_ip` = '$user_ip',
  308. `edition_time` = now()
  309. WHERE definition_id = '$definition_id' AND user_id = '$user_id'";
  310. api_sql_query($sql,__FILE__,__LINE__);
  311. return true;
  312. }
  313. /**
  314. * clean the content of a bloc for information category
  315. *
  316. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  317. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  318. * @param - $definition_id,
  319. * @param - $user_id
  320. * @return - boolean true if succeed, else bolean false
  321. */
  322. function cleanout_cat_content($user_id, $definition_id)
  323. {
  324. global $TBL_USERINFO_CONTENT;
  325. if (0 == (int) $user_id || 0 == (int) $definition_id)
  326. {
  327. return false;
  328. }
  329. $sql = "DELETE FROM ".$TBL_USERINFO_CONTENT."
  330. WHERE user_id = '$user_id' AND definition_id = '$definition_id'";
  331. api_sql_query($sql,__FILE__,__LINE__);
  332. return true;
  333. }
  334. /*----------------------------------------
  335. SHOW USER INFORMATION TREATMENT
  336. --------------------------------------*/
  337. /**
  338. * get the user info from the user id
  339. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  340. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  341. * @param - int $user_id user id as stored in the Dokeos main db
  342. * @return - array containg user info sort by categories rank
  343. * each rank contains 'title', 'comment', 'content', 'cat_id'
  344. */
  345. function get_course_user_info($user_id)
  346. {
  347. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  348. $sql = "SELECT cat.id catId, cat.title,
  349. cat.comment , content.content
  350. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  351. ON cat.id = content.definition_id AND content.user_id = '$user_id'
  352. ORDER BY cat.rank, content.id";
  353. $result = api_sql_query($sql,__FILE__,__LINE__);
  354. if (mysql_num_rows($result) > 0)
  355. {
  356. while ($userInfo = mysql_fetch_array($result, MYSQL_ASSOC))
  357. {
  358. $userInfos[]=$userInfo;
  359. }
  360. return $userInfos;
  361. }
  362. return false;
  363. }
  364. /**
  365. * get the main user information
  366. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  367. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  368. * @param - int $user_id user id as stored in the Dokeos main db
  369. * @return - array containing user info as 'lastName', 'firstName'
  370. * 'email', 'role'
  371. */
  372. function get_main_user_info($user_id, $courseCode)
  373. {
  374. if (0 == (int) $user_id)
  375. {
  376. return false;
  377. }
  378. $table_course_user = Database::get_main_table(MAIN_COURSE_USER_TABLE);
  379. $table_user = Database::get_main_table(MAIN_USER_TABLE);
  380. $sql = "SELECT u.lastname lastName, u.firstname firstName,
  381. u.email, u.picture_uri picture, cu.role,
  382. cu.`status` `status`, cu.tutor_id
  383. FROM $table_user u, $table_course_user cu
  384. WHERE u.user_id = cu.user_id
  385. AND u.user_id = '$user_id'
  386. AND cu.course_code = '$courseCode'";
  387. $result = api_sql_query($sql,__FILE__,__LINE__);
  388. if (mysql_num_rows($result) > 0)
  389. {
  390. $userInfo = mysql_fetch_array($result, MYSQL_ASSOC);
  391. return $userInfo;
  392. }
  393. return false;
  394. }
  395. /**
  396. * get the user content of a categories plus the categories definition
  397. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  398. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  399. * @param - int $userId - id of the user
  400. * @param - int $catId - id of the categories
  401. * @return - array containing 'catId', 'title', 'comment',
  402. * 'nbline', 'contentId' and 'content'
  403. */
  404. function get_cat_content($userId, $catId)
  405. {
  406. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  407. $sql = "SELECT cat.id catId, cat.title,
  408. cat.comment , cat.line_count,
  409. content.id contentId, content.content
  410. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  411. ON cat.id = content.definition_id
  412. AND content.user_id = '$userId'
  413. WHERE cat.id = '$catId' ";
  414. $result = api_sql_query($sql,__FILE__,__LINE__);
  415. if (mysql_num_rows($result) > 0)
  416. {
  417. $catContent = mysql_fetch_array($result, MYSQL_ASSOC);
  418. $catContent['nbline'] = $catContent['line_count'];
  419. return $catContent;
  420. }
  421. return false;
  422. }
  423. /**
  424. * get the definition of a category
  425. *
  426. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  427. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  428. * @param - int $catId - id of the categories
  429. * @return - array containing 'id', 'title', 'comment', and 'nbline',
  430. */
  431. function get_cat_def($catId)
  432. {
  433. global $TBL_USERINFO_DEF;
  434. $sql = "SELECT id, title, comment, line_count, rank FROM ".$TBL_USERINFO_DEF." WHERE id = '$catId'";
  435. $result = api_sql_query($sql,__FILE__,__LINE__);
  436. if (mysql_num_rows($result) > 0)
  437. {
  438. $catDef = mysql_fetch_array($result, MYSQL_ASSOC);
  439. $catDef['nbline'] = $catDef['line_count'];
  440. return $catDef;
  441. }
  442. return false;
  443. }
  444. /**
  445. * get list of all this course categories
  446. *
  447. * @author - Christophe Gesché <gesche@ipm.ucl.ac.be>
  448. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  449. * @return - array containing a list of arrays.
  450. * And each of these arrays contains
  451. * 'catId', 'title', 'comment', and 'nbline',
  452. */
  453. function get_cat_def_list()
  454. {
  455. global $TBL_USERINFO_DEF;
  456. $sql = "SELECT id catId, title, comment , line_count
  457. FROM ".$TBL_USERINFO_DEF."
  458. ORDER BY rank";
  459. $result = api_sql_query($sql,__FILE__,__LINE__);
  460. if (mysql_num_rows($result) > 0)
  461. {
  462. while ($cat_def = mysql_fetch_array($result, MYSQL_ASSOC))
  463. {
  464. $cat_def_list[]=$cat_def;
  465. }
  466. return $cat_def_list;
  467. }
  468. return false;
  469. }
  470. /**
  471. * transform content in a html display
  472. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  473. * @param - string $string string to htmlize
  474. * @ return - string htmlized
  475. */
  476. function htmlize($phrase)
  477. {
  478. return nl2br(htmlspecialchars($phrase));
  479. }
  480. /**
  481. * replaces some dangerous character in a string for HTML use
  482. *
  483. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  484. * @param - string (string) string
  485. * @return - the string cleaned of dangerous character
  486. */
  487. function replace_dangerous_char($string)
  488. {
  489. $search[]="/" ; $replace[]="-";
  490. $search[]="\|"; $replace[]="-";
  491. $search[]="\""; $replace[]=" ";
  492. foreach($search as $key=>$char )
  493. {
  494. $string = str_replace($char, $replace[$key], $string);
  495. }
  496. return $string;
  497. }