specific_fields_manager.lib.php 8.4 KB

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