userInfoLib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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; //taken from userInfo.php
  39. $title = Database::escape_string(trim($title));
  40. $comment = Database::escape_string(trim($comment));
  41. $nbline = strval(intval($nbline));
  42. if ( 0 == (int) $nbline || empty($title))
  43. {
  44. return false;
  45. }
  46. $sql = "SELECT MAX(rank) as maxRank FROM ".$TBL_USERINFO_DEF;
  47. $result = api_sql_query($sql,__FILE__,__LINE__);
  48. if ($result) $maxRank = Database::fetch_array($result);
  49. $maxRank = $maxRank['maxRank'];
  50. $thisRank = $maxRank + 1;
  51. $sql = "INSERT INTO $TBL_USERINFO_DEF SET
  52. title = '$title',
  53. comment = '$comment',
  54. line_count = '$nbline',
  55. rank = '$thisRank'";
  56. api_sql_query($sql,__FILE__,__LINE__);
  57. return true;
  58. }
  59. /**
  60. * modify the definition of a user information category
  61. *
  62. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  63. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  64. * @param - int $id - id of the category
  65. * @param - string $title - category title
  66. * @param - string $comment - title comment
  67. * @param - int$nbline - lines number for the field the user will fill.
  68. * @return - boolean true if succeed, else otherwise
  69. */
  70. function edit_cat_def($id, $title, $comment, $nbline)
  71. {
  72. global $TBL_USERINFO_DEF;
  73. if ( 0 == $nbline || 0 == $id )
  74. {
  75. return false;
  76. }
  77. $id = strval(intval($id)); //make sure id is integer
  78. $title = Database::escape_string(trim($title));
  79. $comment = Database::escape_string(trim($comment));
  80. $nbline = strval(intval($nbline));
  81. $sql = "UPDATE ".$TBL_USERINFO_DEF." SET
  82. title = '$title',
  83. comment = '$comment',
  84. line_count = '$nbline'
  85. WHERE id = '$id'";
  86. api_sql_query($sql,__FILE__,__LINE__);
  87. return true;
  88. }
  89. /**
  90. * remove a category from the category list
  91. *
  92. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  93. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  94. *
  95. * @param - int $id - id of the category
  96. * or "ALL" for all category
  97. * @param - boolean $force - FALSE (default) : prevents removal if users have
  98. * already fill this category
  99. * TRUE : bypass user content existence check
  100. * @param - int $nbline - lines number for the field the user will fill.
  101. * @return - bollean - TRUE if succeed, ELSE otherwise
  102. */
  103. function remove_cat_def($id, $force = false)
  104. {
  105. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  106. $id = strval(intval($id));
  107. if ( (0 == (int) $id || $id == "ALL") || ! is_bool($force))
  108. {
  109. return false;
  110. }
  111. $sqlCondition = " WHERE id = '$id'";
  112. if ($force == false)
  113. {
  114. $sql = "SELECT * FROM $TBL_USERINFO_CONTENT $sqlCondition";
  115. $result = api_sql_query($sql,__FILE__,__LINE__);
  116. if ( Database::num_rows($result) > 0)
  117. {
  118. return false;
  119. }
  120. }
  121. $sql = "DELETE FROM $TBL_USERINFO_DEF $sqlCondition";
  122. api_sql_query($sql,__FILE__,__LINE__);
  123. }
  124. /**
  125. * move a category in the category list
  126. *
  127. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  128. * @author - Christophe Gesch� <gesche@ipm.ucl.ac.be>
  129. *
  130. * @param - int $id - id of the category
  131. * @param - direction "up" or "down" :
  132. * "up" decrease the rank of gived $id by switching rank with the just lower
  133. * "down" increase the rank of gived $id by switching rank with the just upper
  134. *
  135. * @return - boolean true if succeed, else bolean false
  136. */
  137. function move_cat_rank($id, $direction) // up & down.
  138. {
  139. global $TBL_USERINFO_DEF;
  140. $id = strval(intval($id));
  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 (Database::num_rows($result) < 1)
  148. {
  149. return false;
  150. }
  151. $cat = Database::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 Gesche <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 (Database::num_rows($result) < 2)
  190. {
  191. return false;
  192. }
  193. $thisCat = Database::fetch_array($result);
  194. $nextCat = Database::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,$_user;
  213. $sqlChangeStatus = "";
  214. $user_id = strval(intval($user_id));//filter integer
  215. $course_code = Database::escape_string($course_code);
  216. if ($user_id != $_user['user_id'])
  217. {
  218. $sqlChangeStatus = "status = '".$properties['status']."',";
  219. }
  220. $result = api_sql_query("UPDATE $tbl_coursUser
  221. SET role = '".$properties['role']."',
  222. ".$sqlChangeStatus."
  223. tutor_id = '".$properties['tutor']."'
  224. WHERE user_id = '".$user_id."'
  225. AND course_code = '".$course_code."'",__FILE__,__LINE__);
  226. if (mysql_affected_rows() > 0)
  227. {
  228. return true;
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. /*----------------------------------------
  236. CATEGORIES CONTENT TREATMENT
  237. --------------------------------------*/
  238. /**
  239. * fill a bloc for information category
  240. *
  241. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  242. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  243. * @param - $definition_id,
  244. * @param - $user_id,
  245. * @param - $user_ip,
  246. * @param - $content
  247. * @return - boolean true if succeed, else bolean false
  248. */
  249. function fill_new_cat_content($definition_id, $user_id, $content="", $user_ip="")
  250. {
  251. global $TBL_USERINFO_CONTENT;
  252. if (empty($user_ip))
  253. {
  254. $user_ip = $_SERVER['REMOTE_ADDR'];
  255. }
  256. $definition_id = strval(intval($definition_id));
  257. $user_id = strval(intval($user_id));
  258. $content = Database::escape_string(trim($content));
  259. $user_ip = Database::escape_string(trim($user_ip));
  260. if ( 0 == $definition_id || 0 == $user_id || $content == "")
  261. {
  262. // Here we should introduce an error handling system...
  263. return false;
  264. }
  265. // Do not create if already exist
  266. $sql = "SELECT id FROM ".$TBL_USERINFO_CONTENT."
  267. WHERE definition_id = '$definition_id'
  268. AND user_id = '$user_id'";
  269. $result = api_sql_query($sql,__FILE__,__LINE__);
  270. if (Database::num_rows($result) > 0)
  271. {
  272. return false;
  273. }
  274. $sql = "INSERT INTO ".$TBL_USERINFO_CONTENT." SET
  275. content = '$content',
  276. definition_id = '$definition_id',
  277. user_id = '$user_id',
  278. editor_ip = '$user_ip',
  279. edition_time = now()";
  280. api_sql_query($sql,__FILE__,__LINE__);
  281. return true;
  282. }
  283. /**
  284. * Edit a bloc for information category
  285. *
  286. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  287. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  288. * @param - $definition_id,
  289. * @param - $user_id,
  290. * @param - $user_ip, DEFAULT $REMOTE_ADDR
  291. * @param - $content ; if empty call delete the bloc
  292. * @return - boolean true if succeed, else bolean false
  293. */
  294. function edit_cat_content($definition_id, $user_id, $content ="", $user_ip="")
  295. {
  296. global $TBL_USERINFO_CONTENT;
  297. $definition_id = strval(intval($definition_id));
  298. $user_id = strval(intval($user_id));
  299. $content = Database::escape_string(trim($content));
  300. if (empty($user_ip))
  301. {
  302. $user_ip = $_SERVER['REMOTE_ADDR'];
  303. }
  304. $user_ip = Database::escape_string($user_ip);
  305. if (0 == $user_id || 0 == $definition_id)
  306. {
  307. return false;
  308. }
  309. if ( $content == "")
  310. {
  311. return cleanout_cat_content($user_id, $definition_id);
  312. }
  313. $sql= "UPDATE ".$TBL_USERINFO_CONTENT." SET
  314. content = '$content',
  315. editor_ip = '$user_ip',
  316. edition_time = now()
  317. WHERE definition_id = '$definition_id' AND user_id = '$user_id'";
  318. api_sql_query($sql,__FILE__,__LINE__);
  319. return true;
  320. }
  321. /**
  322. * clean the content of a bloc for information category
  323. *
  324. * @author - Hugues peeters <peeters@ipm.ucl.ac.be>
  325. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  326. * @param - $definition_id,
  327. * @param - $user_id
  328. * @return - boolean true if succeed, else bolean false
  329. */
  330. function cleanout_cat_content($user_id, $definition_id)
  331. {
  332. global $TBL_USERINFO_CONTENT;
  333. $user_id = strval(intval($user_id));
  334. $definition_id = strval(intval($definition_id));
  335. if (0 == $user_id || 0 == $definition_id)
  336. {
  337. return false;
  338. }
  339. $sql = "DELETE FROM ".$TBL_USERINFO_CONTENT."
  340. WHERE user_id = '$user_id' AND definition_id = '$definition_id'";
  341. api_sql_query($sql,__FILE__,__LINE__);
  342. return true;
  343. }
  344. /*----------------------------------------
  345. SHOW USER INFORMATION TREATMENT
  346. --------------------------------------*/
  347. /**
  348. * get the user info from the user id
  349. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  350. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  351. * @param - int $user_id user id as stored in the Dokeos main db
  352. * @return - array containg user info sort by categories rank
  353. * each rank contains 'title', 'comment', 'content', 'cat_id'
  354. */
  355. function get_course_user_info($user_id)
  356. {
  357. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  358. $sql = "SELECT cat.id catId, cat.title,
  359. cat.comment , content.content
  360. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  361. ON cat.id = content.definition_id AND content.user_id = '$user_id'
  362. ORDER BY cat.rank, content.id";
  363. $result = api_sql_query($sql,__FILE__,__LINE__);
  364. if (Database::num_rows($result) > 0)
  365. {
  366. while ($userInfo = Database::fetch_array($result, 'ASSOC'))
  367. {
  368. $userInfos[]=$userInfo;
  369. }
  370. return $userInfos;
  371. }
  372. return false;
  373. }
  374. /**
  375. * get the main user information
  376. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  377. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  378. * @param - int $user_id user id as stored in the Dokeos main db
  379. * @return - array containing user info as 'lastName', 'firstName'
  380. * 'email', 'role'
  381. */
  382. function get_main_user_info($user_id, $courseCode)
  383. {
  384. $user_id = strval(intval($user_id));
  385. $courseCode = Database::escape_string($courseCode);
  386. if (0 == $user_id)
  387. {
  388. return false;
  389. }
  390. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  391. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  392. $sql = "SELECT u.*, u.lastname lastName, u.firstname firstName,
  393. u.email, u.picture_uri picture, cu.role,
  394. cu.status status, cu.tutor_id
  395. FROM $table_user u, $table_course_user cu
  396. WHERE u.user_id = cu.user_id
  397. AND u.user_id = '$user_id'
  398. AND cu.course_code = '$courseCode'";
  399. $result = api_sql_query($sql,__FILE__,__LINE__);
  400. if (Database::num_rows($result) > 0)
  401. {
  402. $userInfo = Database::fetch_array($result, 'ASSOC');
  403. $userInfo['password']='';
  404. return $userInfo;
  405. }
  406. return false;
  407. }
  408. /**
  409. * get the user content of a categories plus the categories definition
  410. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  411. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  412. * @param - int $userId - id of the user
  413. * @param - int $catId - id of the categories
  414. * @return - array containing 'catId', 'title', 'comment',
  415. * 'nbline', 'contentId' and 'content'
  416. */
  417. function get_cat_content($userId, $catId)
  418. {
  419. global $TBL_USERINFO_CONTENT, $TBL_USERINFO_DEF;
  420. $userId = strval(intval($userId));
  421. $catId = strval(intval($catId));
  422. $sql = "SELECT cat.id catId, cat.title,
  423. cat.comment , cat.line_count,
  424. content.id contentId, content.content
  425. FROM ".$TBL_USERINFO_DEF." cat LEFT JOIN ".$TBL_USERINFO_CONTENT." content
  426. ON cat.id = content.definition_id
  427. AND content.user_id = '$userId'
  428. WHERE cat.id = '$catId' ";
  429. $result = api_sql_query($sql,__FILE__,__LINE__);
  430. if (Database::num_rows($result) > 0)
  431. {
  432. $catContent = Database::fetch_array($result, 'ASSOC');
  433. $catContent['nbline'] = $catContent['line_count'];
  434. return $catContent;
  435. }
  436. return false;
  437. }
  438. /**
  439. * get the definition of a category
  440. *
  441. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  442. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  443. * @param - int $catId - id of the categories
  444. * @return - array containing 'id', 'title', 'comment', and 'nbline',
  445. */
  446. function get_cat_def($catId)
  447. {
  448. global $TBL_USERINFO_DEF;
  449. $catId = strval(intval($catId));
  450. $sql = "SELECT id, title, comment, line_count, rank FROM ".$TBL_USERINFO_DEF." WHERE id = '$catId'";
  451. $result = api_sql_query($sql,__FILE__,__LINE__);
  452. if (Database::num_rows($result) > 0)
  453. {
  454. $catDef = Database::fetch_array($result, 'ASSOC');
  455. $catDef['nbline'] = $catDef['line_count'];
  456. return $catDef;
  457. }
  458. return false;
  459. }
  460. /**
  461. * get list of all this course categories
  462. *
  463. * @author - Christophe Gesche <gesche@ipm.ucl.ac.be>
  464. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  465. * @return - array containing a list of arrays.
  466. * And each of these arrays contains
  467. * 'catId', 'title', 'comment', and 'nbline',
  468. */
  469. function get_cat_def_list()
  470. {
  471. global $TBL_USERINFO_DEF;
  472. $sql = "SELECT id catId, title, comment , line_count
  473. FROM ".$TBL_USERINFO_DEF."
  474. ORDER BY rank";
  475. $result = api_sql_query($sql,__FILE__,__LINE__);
  476. if (Database::num_rows($result) > 0)
  477. {
  478. while ($cat_def = Database::fetch_array($result, 'ASSOC'))
  479. {
  480. $cat_def_list[]=$cat_def;
  481. }
  482. return $cat_def_list;
  483. }
  484. return false;
  485. }
  486. /**
  487. * transform content in a html display
  488. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  489. * @param - string $string string to htmlize
  490. * @ return - string htmlized
  491. */
  492. function htmlize($phrase)
  493. {
  494. global $charset;
  495. return nl2br(htmlspecialchars($phrase,ENT_QUOTES,$charset));
  496. }
  497. /**
  498. * replaces some dangerous character in a string for HTML use
  499. *
  500. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  501. * @param - string (string) string
  502. * @return - the string cleaned of dangerous character
  503. */
  504. function replace_dangerous_char($string)
  505. {
  506. $search[]="/" ; $replace[]="-";
  507. $search[]="\|"; $replace[]="-";
  508. $search[]="\""; $replace[]=" ";
  509. foreach($search as $key=>$char )
  510. {
  511. $string = str_replace($char, $replace[$key], $string);
  512. }
  513. return $string;
  514. }