PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/functions.php

https://bitbucket.org/chux/csdad
PHP | 396 lines | 248 code | 83 blank | 65 comment | 30 complexity | dc6720ce73d1f91eaa3f822e250200ab MD5 | raw file
  1. <?php
  2. require("db_login.php");
  3. error_reporting(0);
  4. /**
  5. * takes an array with string values and makes them into one comma separated string
  6. * @param $array an array with strings
  7. * @return a comma separated string
  8. */
  9. function commaseparate($array) { // Why not use implode? -- Viktor
  10. $in_string = "";
  11. foreach ($array as $string) {
  12. $in_string .= "'" . $string . "',";
  13. }
  14. //the last comma should be removed, this feels wrong :) but works
  15. $in_string = substr_replace($in_string ,"",-1);
  16. return $in_string;
  17. }
  18. /**
  19. * Function returns false if one of the skills in $skills is not found (does not exist)
  20. *
  21. * @param $skills array with skillnames (strings)
  22. * @return array with corresponding skill ids
  23. */
  24. function get_skill_ids_by_name($skills) {
  25. $number_of_requested_skills = sizeof($skills);
  26. foreach ($skills as $skill) {
  27. $skills2[] = '"' . strtolower($skill) . '"'; // So that we can make a case-insensitive check against skills
  28. }
  29. $skills2 = implode(",",$skills2);
  30. $result = mysql_query("
  31. SELECT id, name
  32. FROM skills
  33. WHERE LOWER(name) IN (" . $skills2 . ")
  34. ");
  35. $skill_ids = Array();
  36. if(!$result) {
  37. return false;
  38. }
  39. while ($skill_id = mysql_fetch_assoc($result)) {
  40. $skill_ids[] = $skill_id['id'];
  41. }
  42. if (sizeof($skill_ids) != $number_of_requested_skills) { //We didn't find as many skills as we searched for
  43. return false;
  44. }
  45. return $skill_ids;
  46. }
  47. /**
  48. *
  49. * @param $skill_id
  50. * @return en skill som en assoc array
  51. */
  52. function get_skill_by_id($skill_id) {
  53. $result = mysql_query("
  54. SELECT *
  55. FROM skills
  56. WHERE id = {$skill_id}
  57. LIMIT 1
  58. ");
  59. return mysql_fetch_assoc($result);
  60. }
  61. /**
  62. * Will return all skills as an array.
  63. * @return array
  64. */
  65. function get_skills() {
  66. $skills = Array();
  67. $result = mysql_query("
  68. SELECT *
  69. FROM skills
  70. ");
  71. while($row = mysql_fetch_assoc($result)) {
  72. $skills[] = $row;
  73. }
  74. return $skills;
  75. }
  76. /**
  77. *
  78. * @param $skills array with skill names (strings)
  79. * @return array of people, they're experience and personal rating in a skill like this: $people['skillid']['score'] for example.
  80. */
  81. function get_people_by_known_skills($skills) {
  82. $number_of_skills = sizeof($skills);
  83. $skills = get_skill_ids_by_name($skills);
  84. $people = Array();
  85. foreach($skills as $skill) {
  86. $result = mysql_query("
  87. SELECT people.id AS 'person_id',
  88. skills.name as 'skill_name',
  89. skills.id as 'skill_id',
  90. person_has_skill.personal_rating,
  91. SUM(experiences.hours * skill_used_in_experience.amount) /40 AS 'weeks_of_experience',
  92. (SUM(experiences.hours * skill_used_in_experience.amount) /40) * person_has_skill.personal_rating AS 'score',
  93. CONCAT(people.first_name, ' ', people.surname) as name
  94. FROM experiences,
  95. people,
  96. skill_used_in_experience,
  97. person_has_skill,
  98. skills
  99. WHERE experiences.person_id=people.id
  100. AND skill_used_in_experience.experience_id=experiences.id
  101. AND person_has_skill.person_id = people.id
  102. AND person_has_skill.skill_id = {$skill}
  103. AND person_has_skill.skill_id = skills.id
  104. GROUP BY people.id, skill_used_in_experience.skill_id
  105. HAVING skill_used_in_experience.skill_id = {$skill}
  106. ORDER BY score DESC
  107. ");
  108. if ($result) {
  109. while ($person = mysql_fetch_assoc($result)) {
  110. $people[strval($skill)][] = $person; //strval s? att det inte blir ett index utan en key
  111. $people_score["{$person['person_id']}"] += $person['score'];
  112. }
  113. } else {return false;}
  114. }
  115. return array($people,$people_score);
  116. }
  117. /**
  118. *
  119. * @param $people_ids array of people ids
  120. * @param $people_names array of corresponding people names
  121. */
  122. function print_people($people_ids, $people_names) {
  123. if (!is_array($people_ids) || !is_array($people_names)) {
  124. echo '<li>Inga resultat</li>';
  125. } else {
  126. for ($i = 0; $i < sizeof($people_ids); $i++) {
  127. echo '<li><a href="?page=view&viewperson=' .
  128. $people_ids[$i] .
  129. '">' .
  130. $people_names[$i] .
  131. '</a></li>'
  132. ;
  133. }
  134. }
  135. }
  136. /**
  137. *
  138. * @param $people array of people ids
  139. * @return array of people names
  140. */
  141. function get_people_names_by_id($people) {
  142. $people = implode(",",$people);
  143. $result = mysql_query("
  144. SELECT first_name, surname
  145. FROM people
  146. WHERE id IN (" . $people . ")
  147. ");
  148. $people = Array();
  149. if ($result) {
  150. while ($person = mysql_fetch_assoc($result)) {
  151. $people[] = $person['first_name'] . " " . $person['surname'];
  152. }
  153. }
  154. return $people;
  155. }
  156. /**
  157. *
  158. * @param $person_id
  159. * @return associative_array with 'experience_name', 'skills_used', 'experience_description' , 'experience_type' sorted in order of type
  160. */
  161. function get_experiences_by_person_id($person_id) {
  162. $experiences = Array();
  163. $result = mysql_query("
  164. SELECT experiences.`name` AS 'experience_name',
  165. skills.`name` AS 'skills_used',
  166. experiences.description AS 'experience_description',
  167. experiences.experience_type AS 'experience_type'
  168. FROM experiences, skill_used_in_experience, skills
  169. WHERE skill_used_in_experience.experience_id = experiences.id
  170. AND skills.id = skill_used_in_experience.skill_id
  171. AND experiences.person_id = " . $person_id . "
  172. ORDER BY experiences.experience_type DESC
  173. ");
  174. if (!$result) {return false;}
  175. $count = 0;
  176. while($row = mysql_fetch_assoc($result)) {
  177. if ($count == 0) { // dirty cheap to solve the notice wrong offset blabla /antte & hasse
  178. $experiences[$count]['experience_name'] = $row['experience_name'];
  179. $experiences[$count]['skills_used'] = $row['skills_used'];
  180. $experiences[$count]['experience_description'] = $row['experience_description'];
  181. $experiences[$count]['experience_type'] = $row['experience_type'];
  182. $count++;
  183. } else {
  184. if ($experiences[$count-1]['experience_name'] == $row['experience_name']) {
  185. $experiences[$count-1]['skills_used'] .= ", " . $row['skills_used'];
  186. } else {
  187. $experiences[$count]['experience_name'] = $row['experience_name'];
  188. $experiences[$count]['skills_used'] = $row['skills_used'];
  189. $experiences[$count]['experience_description'] = $row['experience_description'];
  190. $experiences[$count]['experience_type'] = $row['experience_type'];
  191. $count++;
  192. }
  193. }
  194. }
  195. return $experiences;
  196. }
  197. /**
  198. *
  199. * @param $people array of people ids
  200. * @return array of people names
  201. */
  202. function get_people_names_by_ids($people) {
  203. if (!is_array($people)) {
  204. return false;
  205. }
  206. $people = implode(",", $people);
  207. $result = mysql_query("
  208. SELECT first_name, surname
  209. FROM people
  210. WHERE id IN (" . $people . ")
  211. ");
  212. $people = Array();
  213. if ($result) {
  214. while ($person = mysql_fetch_assoc($result)) {
  215. $people[] = $person['first_name'] . " " . $person['surname'];
  216. }
  217. }
  218. return $people;
  219. }
  220. /**
  221. *
  222. * @param $person_id
  223. * @return associative array with X
  224. */
  225. function get_person($person_id) {
  226. $person_id = mysql_real_escape_string($person_id);
  227. $person = Array();
  228. $result = mysql_query("
  229. SELECT *
  230. FROM people
  231. WHERE id = " . $person_id . "
  232. LIMIT 1
  233. ");
  234. return mysql_fetch_assoc($result);
  235. }
  236. /**
  237. *
  238. * @return array with all people
  239. */
  240. function get_people() {
  241. $people = Array();
  242. $result = mysql_query("
  243. SELECT *
  244. FROM people
  245. ");
  246. if (!$result) {return false;}
  247. while ($row = mysql_fetch_assoc($result)) {
  248. $people[] = $row;
  249. }
  250. return $people;
  251. }
  252. /**
  253. *
  254. * @param $skill_id1 The id of a skill that relate to another skill($skill_id2)
  255. * @param $skill_id2 The id of a skill that $skill_id1 is compared to
  256. * @param $ratio Float that in percentage represents how skill1 compares to skill2
  257. * @return Bool whether the insert succeeded(true) or not(false)
  258. */
  259. function insert_skill_relation($skill_id1,$skill_id2,$ratio) {
  260. if($ratio<0 || $ratio>1) {
  261. // Att n??nting ?Īr mindre ?Īn 0% likt eller mer ?Īn 100% likt g??r ju inte an
  262. return false;
  263. }
  264. else {
  265. // Replace: finns en rad med samma "unique keypair" sl?Īngs den. Om det inte finns s?? skapas en ny rad
  266. $sql = "REPLACE INTO skill_relates_to_skill
  267. (skill1_id, skill2_id, ratio)
  268. VALUES($skill_id1, $skill_id2, $ratio)";
  269. return mysql_query($sql);
  270. }
  271. }
  272. function add_skill($name,$description) {
  273. // Replace: finns en rad med samma "unique keypair" sl?Īngs den. Om det inte finns s?? skapas en ny rad
  274. $sql = "INSERT INTO skills
  275. (name, description)
  276. VALUES('{$name}', '{$description}')";
  277. return mysql_query($sql);
  278. }
  279. function update_skill($id, $description) {
  280. $sql = "UPDATE skills
  281. SET description = '{$description}'
  282. WHERE id = {$id}
  283. ";
  284. mysql_query($sql);
  285. }
  286. function get_skill_relation_by_skill_id($skill_id) {
  287. $result = mysql_query("
  288. SELECT *
  289. FROM skill_relates_to_skill
  290. WHERE skill1_id = {$skill_id}
  291. ");
  292. $relations = Array();
  293. while($row = mysql_fetch_assoc($result)) {
  294. $relations[] = $row;
  295. }
  296. return $relations;
  297. }
  298. function get_skills_by_person_id($person_id){
  299. $result = mysql_query ("
  300. SELECT skills.name
  301. FROM people, skills, person_has_skill
  302. WHERE people.id = $person_id
  303. AND person_has_skill.person_id = people.id
  304. AND skills.id = person_has_skill.skill_id
  305. ");
  306. $skills = Array();
  307. if ($result) {
  308. while ($skill = mysql_fetch_assoc($result)) {
  309. $skills[] = $skill['name'];
  310. }
  311. }
  312. return $skills;
  313. }