specific_fields_manager.lib.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Manage specific tools.
  4. *
  5. * @todo convert into a class
  6. *
  7. * @package chamilo.library
  8. */
  9. // Database table definitions
  10. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  11. $table_sf_val = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  12. /**
  13. * Add a specific field.
  14. *
  15. * @param string $name specific field name
  16. */
  17. function add_specific_field($name)
  18. {
  19. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  20. $name = trim($name);
  21. if (empty($name)) {
  22. return false;
  23. }
  24. $sql = 'INSERT INTO %s(id, code, name) VALUES(NULL, \'%s\', \'%s\')';
  25. $_safe_name = Database::escape_string($name);
  26. $_safe_code = substr($_safe_name, 0, 1);
  27. $_safe_code = get_specific_field_code_from_name($_safe_code);
  28. if ($_safe_code === false) {
  29. return false;
  30. }
  31. $sql = sprintf($sql, $table_sf, $_safe_code, $_safe_name);
  32. $result = Database::query($sql);
  33. if ($result) {
  34. return Database::insert_id();
  35. } else {
  36. return false;
  37. }
  38. }
  39. /**
  40. * Delete a specific field.
  41. *
  42. * @param int $id specific field id
  43. */
  44. function delete_specific_field($id)
  45. {
  46. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  47. $id = (int) $id;
  48. if (!is_numeric($id)) {
  49. return false;
  50. }
  51. $sql = 'DELETE FROM %s WHERE id=%s LIMIT 1';
  52. $sql = sprintf($sql, $table_sf, $id);
  53. Database::query($sql);
  54. //TODO also delete the corresponding values
  55. }
  56. /**
  57. * Edit a specific field.
  58. *
  59. * @param int $id specific field id
  60. * @param string $name new field name
  61. */
  62. function edit_specific_field($id, $name)
  63. {
  64. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  65. $id = (int) $id;
  66. if (!is_numeric($id)) {
  67. return false;
  68. }
  69. $sql = 'UPDATE %s SET name = \'%s\' WHERE id = %s LIMIT 1';
  70. $sql = sprintf($sql, $table_sf, $name, $id);
  71. Database::query($sql);
  72. }
  73. /**
  74. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  75. * @param array $order_by a list of fields on which to sort
  76. *
  77. * @return array An array with all specific fields, at platform level
  78. */
  79. function get_specific_field_list($conditions = [], $order_by = [])
  80. {
  81. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  82. $return_array = [];
  83. $sql = "SELECT * FROM $table_sf";
  84. if (count($conditions) > 0) {
  85. $sql .= ' WHERE ';
  86. $conditions_string_array = [];
  87. foreach ($conditions as $field => $value) {
  88. $conditions_string_array[] = $field.' = '.$value;
  89. }
  90. $sql .= implode(' AND ', $conditions_string_array);
  91. }
  92. if (count($order_by) > 0) {
  93. $sql .= ' ORDER BY '.implode(',', $order_by);
  94. }
  95. $sql_result = Database::query($sql);
  96. while ($result = Database::fetch_array($sql_result)) {
  97. $return_array[] = $result;
  98. }
  99. return $return_array;
  100. }
  101. /**
  102. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  103. * @param array $order_by a list of fields on which sort
  104. *
  105. * @return array an array with all users of the platform
  106. */
  107. function get_specific_field_values_list(
  108. $conditions = [],
  109. $order_by = []
  110. ) {
  111. $table_sfv = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  112. $return_array = [];
  113. $sql = "SELECT * FROM $table_sfv";
  114. if (count($conditions) > 0) {
  115. $sql .= ' WHERE ';
  116. //Fixing course id
  117. if (isset($conditions['c_id'])) {
  118. $course_info = api_get_course_info_by_id(intval($conditions['c_id']));
  119. $conditions['course_code'] = " '".$course_info['code']."' ";
  120. unset($conditions['c_id']);
  121. }
  122. //If any course_code is provided try to insert the current course code
  123. if (!isset($conditions['course_code'])) {
  124. $conditions['course_code'] = " '".api_get_course_id()."' ";
  125. }
  126. $conditions_string_array = [];
  127. foreach ($conditions as $field => $value) {
  128. $conditions_string_array[] = $field.' = '.$value;
  129. }
  130. $sql .= implode(' AND ', $conditions_string_array);
  131. }
  132. if (count($order_by) > 0) {
  133. $sql .= ' ORDER BY '.implode(',', $order_by);
  134. }
  135. $sql_result = Database::query($sql);
  136. while ($result = Database::fetch_array($sql_result)) {
  137. $return_array[] = $result;
  138. }
  139. return $return_array;
  140. }
  141. /**
  142. * @param char $prefix xapian prefix
  143. * @param string $course_code
  144. * @param string $tool_id Constant from mainapi.lib.php
  145. * @param int $ref_id representative id inside one tool item
  146. *
  147. * @return array
  148. */
  149. function get_specific_field_values_list_by_prefix(
  150. $prefix,
  151. $course_code,
  152. $tool_id,
  153. $ref_id
  154. ) {
  155. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  156. $table_sfv = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  157. $sql = 'SELECT sfv.value FROM %s sf LEFT JOIN %s sfv ON sf.id = sfv.field_id'.
  158. ' WHERE sf.code = \'%s\' AND sfv.c_id = \'%s\' AND tool_id = \'%s\' AND sfv.ref_id = %s';
  159. $sql = sprintf(
  160. $sql,
  161. $table_sf,
  162. $table_sfv,
  163. $prefix,
  164. $course_code,
  165. $tool_id,
  166. $ref_id
  167. );
  168. $sql_result = Database::query($sql);
  169. while ($result = Database::fetch_array($sql_result)) {
  170. $return_array[] = $result;
  171. }
  172. return $return_array;
  173. }
  174. /**
  175. * Add a specific field value.
  176. *
  177. * @param int $id_specific_field specific field id
  178. * @param string $course_id course code
  179. * @param string $tool_id tool id, from main.api.lib
  180. * @param int $ref_id intern id inside specific tool table
  181. * @param string $value specific field value
  182. */
  183. function add_specific_field_value(
  184. $id_specific_field,
  185. $course_id,
  186. $tool_id,
  187. $ref_id,
  188. $value
  189. ) {
  190. $table_sf_values = Database::get_main_table(
  191. TABLE_MAIN_SPECIFIC_FIELD_VALUES
  192. );
  193. $value = trim($value);
  194. if (empty($value)) {
  195. return false;
  196. }
  197. $sql = 'INSERT INTO %s(id, course_code, tool_id, ref_id, field_id, value) VALUES(NULL, \'%s\', \'%s\', %s, %s, \'%s\')';
  198. $sql = sprintf(
  199. $sql,
  200. $table_sf_values,
  201. $course_id,
  202. $tool_id,
  203. $ref_id,
  204. $id_specific_field,
  205. Database::escape_string($value)
  206. );
  207. $result = Database::query($sql);
  208. if ($result) {
  209. return Database::insert_id();
  210. } else {
  211. return false;
  212. }
  213. }
  214. /**
  215. * Delete all values from a specific field id, course_id, ref_id and tool.
  216. *
  217. * @param string $course_id course code
  218. * @param int $id_specific_field specific field id
  219. * @param string $tool_id tool id, from main.api.lib
  220. * @param int $ref_id intern id inside specific tool table
  221. */
  222. function delete_all_specific_field_value($course_id, $id_specific_field, $tool_id, $ref_id)
  223. {
  224. $table_sf_values = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  225. $sql = 'DELETE FROM %s WHERE course_code = \'%s\' AND tool_id = \'%s\' AND ref_id = %s AND field_id = %s';
  226. $sql = sprintf($sql, $table_sf_values, $course_id, $tool_id, $ref_id, $id_specific_field);
  227. Database::query($sql);
  228. }
  229. /**
  230. * Delete all values from a specific item (course_id, tool_id and ref_id).
  231. * To be used when deleting such item from Dokeos.
  232. *
  233. * @param string Course code
  234. * @param string Tool ID
  235. * @param int Internal ID used in specific tool table
  236. */
  237. function delete_all_values_for_item($course_id, $tool_id, $ref_id)
  238. {
  239. $table_sf_values = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  240. $sql = 'DELETE FROM %s WHERE course_code = \'%s\' AND tool_id = \'%s\' AND ref_id = %s';
  241. $sql = sprintf($sql, $table_sf_values, $course_id, $tool_id, $ref_id);
  242. Database::query($sql);
  243. }
  244. /**
  245. * Generates a code (one-letter string) for a given field name
  246. * Defaults to the first letter of the name, otherwise iterate through available
  247. * letters.
  248. *
  249. * @param string Name
  250. *
  251. * @return string One-letter code, upper-case
  252. */
  253. function get_specific_field_code_from_name($name)
  254. {
  255. // Z is used internally by Xapian
  256. // O & C already used by tool_id and course_id
  257. $list = [
  258. 'A',
  259. 'B',
  260. 'D',
  261. 'E',
  262. 'F',
  263. 'G',
  264. 'H',
  265. 'I',
  266. 'J',
  267. 'K',
  268. 'L',
  269. 'M',
  270. 'N',
  271. 'P',
  272. 'Q',
  273. 'R',
  274. 'S',
  275. 'T',
  276. 'U',
  277. 'V',
  278. 'W',
  279. 'X',
  280. 'Y',
  281. ];
  282. $table_sf = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD);
  283. $sql = "SELECT code FROM $table_sf ORDER BY code";
  284. $res = Database::query($sql);
  285. $code = strtoupper(substr($name, 0, 1));
  286. //if no code exists in DB, return current one
  287. if (Database::num_rows($res) < 1) {
  288. return $code;
  289. }
  290. $existing_list = [];
  291. while ($row = Database::fetch_array($res)) {
  292. $existing_list[] = $row['code'];
  293. }
  294. //if the current code doesn't exist in DB, return current one
  295. if (!in_array($code, $existing_list)) {
  296. return $code;
  297. }
  298. $idx = array_search($code, $list);
  299. $c = count($list);
  300. for ($i = $idx + 1, $j = 0; $j < $c; $i++, $j++) {
  301. if (!in_array($list[$i], $existing_list)) {
  302. return $idx[$i];
  303. }
  304. }
  305. // all 26 codes are used
  306. return false;
  307. }