specific_fields_manager.lib.php 9.0 KB

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