skill.lib.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. define ('SKILL_TYPE_REQUIREMENT', 'required');
  4. define ('SKILL_TYPE_ACQUIRED', 'acquired');
  5. define ('SKILL_TYPE_BOTH', 'both');
  6. require_once api_get_path(LIBRARY_PATH).'model.lib.php';
  7. /**
  8. * Class SkillProfile
  9. * @package chamilo.library
  10. */
  11. class SkillProfile extends Model
  12. {
  13. public $columns = array('id', 'name', 'description');
  14. public function __construct()
  15. {
  16. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE);
  17. $this->table_rel_profile = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
  18. }
  19. public function get_profiles()
  20. {
  21. $sql = "SELECT * FROM $this->table p INNER JOIN $this->table_rel_profile sp ON(p.id = sp.profile_id) ";
  22. $result = Database::query($sql);
  23. $profiles = Database::store_result($result, 'ASSOC');
  24. return $profiles;
  25. }
  26. /**
  27. * This function is for editing profile info from profile_id.
  28. * @param int $profileId
  29. * @param string $name
  30. * @param string $description
  31. */
  32. public function updateProfileInfo($profileId, $name, $description)
  33. {
  34. $profileId = intval($profileId);
  35. $sql = "UPDATE $this->table SET name = '$name', description = '$description' WHERE id = $profileId ";
  36. $result = Database::query($sql);
  37. return $result;
  38. }
  39. /**
  40. * Call the save method of the parent class and the SkillRelProfile object
  41. * @param array Params
  42. * @param bool Whether to show the query in parent save() method
  43. * @return mixed Profile ID or false if incomplete params
  44. */
  45. public function save($params, $show_query = false)
  46. {
  47. if (!empty($params)) {
  48. $profile_id = parent::save($params, $show_query);
  49. if ($profile_id) {
  50. $skill_rel_profile = new SkillRelProfile();
  51. if (isset($params['skills'])) {
  52. foreach ($params['skills'] as $skill_id) {
  53. $attributes = array('skill_id' => $skill_id, 'profile_id' => $profile_id);
  54. $skill_rel_profile->save($attributes);
  55. }
  56. }
  57. return $profile_id;
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * Delete a skill profile
  64. * @param int $id The skill profile id
  65. * @return boolean Whether delete a skill profile
  66. */
  67. public function delete($id) {
  68. Database::delete(
  69. $this->table_rel_profile,
  70. array(
  71. 'profile_id' => $id
  72. )
  73. );
  74. return parent::delete($id);
  75. }
  76. }
  77. class SkillRelProfile extends Model
  78. {
  79. public $columns = array('id', 'skill_id', 'profile_id');
  80. public function __construct()
  81. {
  82. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
  83. $this->tableProfile = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE);
  84. }
  85. public function get_skills_by_profile($profileId)
  86. {
  87. $profileId = intval($profileId);
  88. $skills = $this->get_all(array('where' => array('profile_id = ? ' => $profileId)));
  89. $return_array = array();
  90. if (!empty($skills)) {
  91. foreach ($skills as $skill_data) {
  92. $return_array[] = $skill_data['skill_id'];
  93. }
  94. }
  95. return $return_array;
  96. }
  97. /**
  98. * This function is for getting profile info from profile_id.
  99. * @param int $profileId
  100. */
  101. public function getProfileInfo($profileId)
  102. {
  103. $sql = "SELECT * FROM $this->table p INNER JOIN $this->tableProfile pr ON(pr.id = p.profile_id) WHERE p.profile_id = ".intval($profileId);
  104. $result = Database::query($sql);
  105. $profileData = Database::fetch_array($result, 'ASSOC');
  106. return $profileData;
  107. }
  108. }
  109. class SkillRelSkill extends Model
  110. {
  111. public $columns = array('skill_id', 'parent_id', 'relation_type', 'level');
  112. public function __construct()
  113. {
  114. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL);
  115. }
  116. /**
  117. * Gets an element
  118. */
  119. public function get_skill_info($id)
  120. {
  121. if (empty($id)) {
  122. return array();
  123. }
  124. $result = Database::select('*', $this->table, array('where' => array('skill_id = ?' => intval($id))), 'first');
  125. return $result;
  126. }
  127. public function get_skill_parents($skill_id, $add_child_info = true)
  128. {
  129. $skill_id = intval($skill_id);
  130. $sql = 'SELECT child.* FROM '.$this->table.' child LEFT JOIN '.$this->table.' parent
  131. ON child.parent_id = parent.skill_id
  132. WHERE child.skill_id = '.$skill_id.' ';
  133. $result = Database::query($sql);
  134. $skill = Database::store_result($result, 'ASSOC');
  135. $skill = isset($skill[0]) ? $skill[0] : null;
  136. $parents = array();
  137. if (!empty($skill)) {
  138. if ($skill['parent_id'] != null) {
  139. $parents = self::get_skill_parents($skill['parent_id']);
  140. }
  141. if ($add_child_info) {
  142. $parents[] = $skill;
  143. }
  144. }
  145. return $parents;
  146. }
  147. public function get_direct_parents($skill_id)
  148. {
  149. $skill_id = intval($skill_id);
  150. $sql = 'SELECT parent_id as skill_id FROM '.$this->table.'
  151. WHERE skill_id = '.$skill_id.' ';
  152. $result = Database::query($sql);
  153. $skill = Database::store_result($result, 'ASSOC');
  154. $skill = isset($skill[0]) ? $skill[0] : null;
  155. $parents = array();
  156. if (!empty($skill)) {
  157. $parents[] = $skill;
  158. }
  159. return $parents;
  160. }
  161. public function get_children($skill_id, $load_user_data = false, $user_id = false)
  162. {
  163. $skills = $this->find('all', array('where' => array('parent_id = ? ' => $skill_id)));
  164. $skill_obj = new Skill();
  165. $skill_rel_user = new SkillRelUser();
  166. if ($load_user_data) {
  167. $passed_skills = $skill_rel_user->get_user_skills($user_id);
  168. $done_skills = array();
  169. foreach ($passed_skills as $done_skill) {
  170. $done_skills[] = $done_skill['skill_id'];
  171. }
  172. }
  173. if (!empty($skills)) {
  174. foreach ($skills as &$skill) {
  175. $skill['data'] = $skill_obj->get($skill['skill_id']);
  176. if (isset($skill['data']) && !empty($skill['data'])) {
  177. if (!empty($done_skills)) {
  178. $skill['data']['passed'] = 0;
  179. if (in_array($skill['skill_id'], $done_skills)) {
  180. $skill['data']['passed'] = 1;
  181. }
  182. }
  183. } else {
  184. $skill = null;
  185. }
  186. }
  187. }
  188. return $skills;
  189. }
  190. function update_by_skill($params)
  191. {
  192. $result = Database::update($this->table, $params, array('skill_id = ? ' => $params['skill_id']));
  193. if ($result) {
  194. return true;
  195. }
  196. return false;
  197. }
  198. function relation_exists($skill_id, $parent_id)
  199. {
  200. $result = $this->find(
  201. 'all',
  202. array('where' => array('skill_id = ? AND parent_id = ?' => array($skill_id, $parent_id)))
  203. );
  204. if (!empty($result)) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. }
  210. /**
  211. * @package chamilo.library
  212. */
  213. class SkillRelGradebook extends Model
  214. {
  215. public $columns = array('id', 'gradebook_id', 'skill_id');
  216. public function __construct()
  217. {
  218. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  219. }
  220. public function exists_gradebook_skill($gradebook_id, $skill_id)
  221. {
  222. $result = $this->find(
  223. 'all',
  224. array('where' => array('gradebook_id = ? AND skill_id = ?' => array($gradebook_id, $skill_id)))
  225. );
  226. if (!empty($result)) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * Gets an element
  233. */
  234. public function get_skill_info($skill_id, $gradebook_id)
  235. {
  236. if (empty($skill_id)) {
  237. return array();
  238. }
  239. $result = Database::select(
  240. '*',
  241. $this->table,
  242. array('where' => array('skill_id = ? AND gradebook_id = ? ' => array($skill_id, $gradebook_id))),
  243. 'first'
  244. );
  245. return $result;
  246. }
  247. public function update_gradebooks_by_skill($skill_id, $gradebook_list)
  248. {
  249. $original_gradebook_list = $this->find('all', array('where' => array('skill_id = ?' => array($skill_id))));
  250. $gradebooks_to_remove = array();
  251. $gradebooks_to_add = array();
  252. $original_gradebook_list_ids = array();
  253. if (!empty($original_gradebook_list)) {
  254. foreach ($original_gradebook_list as $gradebook) {
  255. if (!in_array($gradebook['gradebook_id'], $gradebook_list)) {
  256. $gradebooks_to_remove[] = $gradebook['id'];
  257. }
  258. }
  259. foreach ($original_gradebook_list as $gradebook_item) {
  260. $original_gradebook_list_ids[] = $gradebook_item['gradebook_id'];
  261. }
  262. }
  263. if (!empty($gradebook_list)) {
  264. foreach ($gradebook_list as $gradebook_id) {
  265. if (!in_array($gradebook_id, $original_gradebook_list_ids)) {
  266. $gradebooks_to_add[] = $gradebook_id;
  267. }
  268. }
  269. }
  270. if (!empty($gradebooks_to_remove)) {
  271. foreach ($gradebooks_to_remove as $id) {
  272. $this->delete($id);
  273. }
  274. }
  275. if (!empty($gradebooks_to_add)) {
  276. foreach ($gradebooks_to_add as $gradebook_id) {
  277. $attributes = array('skill_id' => $skill_id, 'gradebook_id' => $gradebook_id);
  278. $this->save($attributes);
  279. }
  280. }
  281. }
  282. function update_by_skill($params)
  283. {
  284. $skill_info = $this->exists_gradebook_skill($params['gradebook_id'], $params['skill_id']);
  285. if ($skill_info) {
  286. return;
  287. } else {
  288. $result = $this->save($params);
  289. }
  290. if ($result) {
  291. return true;
  292. }
  293. return false;
  294. }
  295. }
  296. /**
  297. * @package chamilo.library
  298. */
  299. class SkillRelUser extends Model
  300. {
  301. public $columns = array('id', 'user_id', 'skill_id', 'acquired_skill_at', 'assigned_by');
  302. public function __construct()
  303. {
  304. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
  305. //$this->table_user = Database::get_main_table(TABLE_MAIN_USER);
  306. }
  307. public function get_user_by_skills($skill_list)
  308. {
  309. $users = array();
  310. if (!empty($skill_list)) {
  311. $skill_list = array_map('intval', $skill_list);
  312. $skill_list = implode("', '", $skill_list);
  313. $sql = "SELECT user_id FROM {$this->table} WHERE skill_id IN ('$skill_list') ";
  314. $result = Database::query($sql);
  315. $users = Database::store_result($result, 'ASSOC');
  316. }
  317. return $users;
  318. }
  319. public function get_user_skills($user_id)
  320. {
  321. if (empty($user_id)) {
  322. return array();
  323. }
  324. $result = Database::select(
  325. 'skill_id',
  326. $this->table,
  327. array('where' => array('user_id = ?' => intval($user_id))),
  328. 'all'
  329. );
  330. return $result;
  331. }
  332. }
  333. class Skill extends Model
  334. {
  335. public $columns = array('id', 'name', 'description', 'access_url_id', 'short_code');
  336. public $required = array('name');
  337. /** Array of colours by depth, for the coffee wheel. Each depth has 4 col */
  338. /*var $colours = array(
  339. 0 => array('#f9f0ab', '#ecc099', '#e098b0', '#ebe378'),
  340. 1 => array('#d5dda1', '#4a5072', '#8dae43', '#72659d'),
  341. 2 => array('#b28647', '#2e6093', '#393e64', '#1e8323'),
  342. 3 => array('#9f6652', '#9f6652', '#9f6652', '#9f6652'),
  343. 4 => array('#af643c', '#af643c', '#af643c', '#af643c'),
  344. 5 => array('#72659d', '#72659d', '#72659d', '#72659d'),
  345. 6 => array('#8a6e9e', '#8a6e9e', '#8a6e9e', '#8a6e9e'),
  346. 7 => array('#92538c', '#92538c', '#92538c', '#92538c'),
  347. 8 => array('#2e6093', '#2e6093', '#2e6093', '#2e6093'),
  348. 9 => array('#3a5988', '#3a5988', '#3a5988', '#3a5988'),
  349. 10 => array('#393e64', '#393e64', '#393e64', '#393e64'),
  350. );*/
  351. public function __construct()
  352. {
  353. $this->table = Database::get_main_table(TABLE_MAIN_SKILL);
  354. $this->table_user = Database::get_main_table(TABLE_MAIN_USER);
  355. $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  356. $this->table_skill_rel_user = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
  357. $this->table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  358. $this->table_skill_rel_skill = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL);
  359. $this->table_gradebook = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  360. }
  361. function get_skill_info($id)
  362. {
  363. $skill_rel_skill = new SkillRelSkill();
  364. $skill_info = $this->get($id);
  365. if (!empty($skill_info)) {
  366. $skill_info['extra'] = $skill_rel_skill->get_skill_info($id);
  367. $skill_info['gradebooks'] = self::get_gradebooks_by_skill($id);
  368. }
  369. return $skill_info;
  370. }
  371. function get_skills_info($skill_list)
  372. {
  373. $skill_list = array_map('intval', $skill_list);
  374. $skill_list = implode("', '", $skill_list);
  375. $sql = "SELECT * FROM {$this->table} WHERE id IN ('$skill_list') ";
  376. $result = Database::query($sql);
  377. $users = Database::store_result($result, 'ASSOC');
  378. return $users;
  379. }
  380. function get_all($load_user_data = false, $user_id = false, $id = null, $parent_id = null)
  381. {
  382. $id_condition = '';
  383. if (isset($id) && !empty($id)) {
  384. $id = intval($id);
  385. $id_condition = " WHERE s.id = $id";
  386. }
  387. if (isset($parent_id) && !empty($parent_id)) {
  388. $parent_id = intval($parent_id);
  389. if (empty($id_condition)) {
  390. $id_condition = "WHERE ss.parent_id = $parent_id";
  391. } else {
  392. $id_condition = " AND ss.parent_id = $parent_id";
  393. }
  394. }
  395. $sql = "SELECT s.id, s.name, s.description, ss.parent_id, ss.relation_type
  396. FROM {$this->table} s INNER JOIN {$this->table_skill_rel_skill} ss ON (s.id = ss.skill_id) $id_condition
  397. ORDER BY ss.id, ss.parent_id";
  398. $result = Database::query($sql);
  399. $skills = array();
  400. if (Database::num_rows($result)) {
  401. while ($row = Database::fetch_array($result, 'ASSOC')) {
  402. $skill_rel_skill = new SkillRelSkill();
  403. $a = $skill_rel_skill->get_skill_parents($row['id']);
  404. $row['level'] = count($a) - 1;
  405. $row['gradebooks'] = self::get_gradebooks_by_skill($row['id']);
  406. $skills[$row['id']] = $row;
  407. }
  408. }
  409. /*
  410. if ($load_user_data && $user_id) {
  411. $passed_skills = $this->get_user_skills($user_id);
  412. foreach ($skills as &$skill) {
  413. $skill['done_by_user'] = 0;
  414. if (in_array($skill['id'], $passed_skills)) {
  415. $skill['done_by_user'] = 1;
  416. }
  417. }
  418. }*/
  419. //Load all children of the parent_id
  420. if (!empty($skills) && !empty($parent_id)) {
  421. foreach ($skills as $skill) {
  422. $children = self::get_all($load_user_data, $user_id, $id, $skill['id']);
  423. if (!empty($children)) {
  424. //$skills = array_merge($skills, $children);
  425. $skills = $skills + $children;
  426. }
  427. }
  428. }
  429. return $skills;
  430. }
  431. function get_gradebooks_by_skill($skill_id)
  432. {
  433. $skill_id = intval($skill_id);
  434. $sql = "SELECT g.* FROM {$this->table_gradebook} g INNER JOIN {$this->table_skill_rel_gradebook} sg
  435. ON g.id = sg.gradebook_id
  436. WHERE sg.skill_id = $skill_id";
  437. $result = Database::query($sql);
  438. $result = Database::store_result($result,'ASSOC');
  439. return $result;
  440. }
  441. /* Get one level childrens */
  442. function get_children($skill_id, $load_user_data = false)
  443. {
  444. $skill_rel_skill = new SkillRelSkill();
  445. if ($load_user_data) {
  446. $user_id = api_get_user_id();
  447. $skills = $skill_rel_skill->get_children($skill_id, true, $user_id);
  448. } else {
  449. $skills = $skill_rel_skill->get_children($skill_id);
  450. }
  451. return $skills;
  452. }
  453. /* Get all children of the current node (recursive)*/
  454. function get_all_children($skill_id)
  455. {
  456. $skill_rel_skill = new SkillRelSkill();
  457. $children = $skill_rel_skill->get_children($skill_id);
  458. foreach ($children as $child) {
  459. $sub_children = $this->get_all_children($child['skill_id']);
  460. }
  461. if (!empty($sub_children)) {
  462. $children = array_merge($children, $sub_children);
  463. }
  464. return $children;
  465. }
  466. /**
  467. * Gets all parents from from the wanted skill
  468. */
  469. function get_parents($skill_id)
  470. {
  471. $skill_rel_skill = new SkillRelSkill();
  472. $skills = $skill_rel_skill->get_skill_parents($skill_id, true);
  473. foreach ($skills as &$skill) {
  474. $skill['data'] = self::get($skill['skill_id']);
  475. }
  476. return $skills;
  477. }
  478. /**
  479. * All direct parents
  480. */
  481. function get_direct_parents($skill_id)
  482. {
  483. $skill_rel_skill = new SkillRelSkill();
  484. $skills = $skill_rel_skill->get_direct_parents($skill_id, true);
  485. foreach($skills as &$skill) {
  486. $skill['data'] = self::get($skill['skill_id']);
  487. $skill_info2 = $skill_rel_skill->get_skill_info($skill['skill_id']);
  488. $skill['data']['parent_id'] = $skill_info2['parent_id'];
  489. }
  490. return $skills;
  491. }
  492. /*
  493. * Adds a new skill
  494. */
  495. public function add($params)
  496. {
  497. if (!isset($params['parent_id'])) {
  498. $params['parent_id'] = 1;
  499. }
  500. if (!is_array($params['parent_id'])) {
  501. $params['parent_id'] = array($params['parent_id']);
  502. }
  503. $skill_rel_skill = new SkillRelSkill();
  504. $skill_rel_gradebook = new SkillRelGradebook();
  505. //Saving name, description
  506. $skill_id = $this->save($params);
  507. if ($skill_id) {
  508. //Saving skill_rel_skill (parent_id, relation_type)
  509. foreach ($params['parent_id'] as $parent_id) {
  510. $relation_exists = $skill_rel_skill->relation_exists($skill_id, $parent_id);
  511. if (!$relation_exists) {
  512. $attributes = array(
  513. 'skill_id' => $skill_id,
  514. 'parent_id' => $parent_id,
  515. 'relation_type' => $params['relation_type'],
  516. //'level' => $params['level'],
  517. );
  518. $skill_rel_skill->save($attributes);
  519. }
  520. }
  521. if (!empty($params['gradebook_id'])) {
  522. foreach ($params['gradebook_id'] as $gradebook_id) {
  523. $attributes = array();
  524. $attributes['gradebook_id'] = $gradebook_id;
  525. $attributes['skill_id'] = $skill_id;
  526. $skill_rel_gradebook->save($attributes);
  527. }
  528. }
  529. return $skill_id;
  530. }
  531. return null;
  532. }
  533. public function add_skill_to_user($user_id, $gradebook_id)
  534. {
  535. $skill_gradebook = new SkillRelGradebook();
  536. $skill_rel_user = new SkillRelUser();
  537. $skill_gradebooks = $skill_gradebook->get_all(array('where' => array('gradebook_id = ?' => $gradebook_id)));
  538. if (!empty($skill_gradebooks)) {
  539. foreach ($skill_gradebooks as $skill_gradebook) {
  540. $user_has_skill = $this->user_has_skill($user_id, $skill_gradebook['skill_id']);
  541. if (!$user_has_skill) {
  542. $params = array(
  543. 'user_id' => $user_id,
  544. 'skill_id' => $skill_gradebook['skill_id'],
  545. 'acquired_skill_at' => api_get_utc_datetime(),
  546. );
  547. $skill_rel_user->save($params);
  548. }
  549. }
  550. }
  551. }
  552. /* Deletes a skill */
  553. public function delete($skill_id)
  554. {
  555. /*$params = array('skill_id' => $skill_id);
  556. $skill_rel_skill = new SkillRelSkill();
  557. $skills = $skill_rel_skill->get_all(array('where'=>array('skill_id = ?' =>$skill_id)));
  558. $skill_rel_profile = new SkillRelProfile();
  559. $skill_rel_gradebook = new SkillRelGradebook();
  560. $skill_rel_user = new SkillRelUser();
  561. $this->delete($skill_id);
  562. $skill_rel_gradebook->delete($params);*/
  563. }
  564. public function edit($params)
  565. {
  566. if (!isset($params['parent_id'])) {
  567. $params['parent_id'] = 1;
  568. }
  569. $skill_rel_skill = new SkillRelSkill();
  570. $skill_rel_gradebook = new SkillRelGradebook();
  571. //Saving name, description
  572. $this->update($params);
  573. $skill_id = $params['id'];
  574. if ($skill_id) {
  575. //Saving skill_rel_skill (parent_id, relation_type)
  576. if (!is_array($params['parent_id'])) {
  577. $params['parent_id'] = array($params['parent_id']);
  578. }
  579. foreach ($params['parent_id'] as $parent_id) {
  580. $relation_exists = $skill_rel_skill->relation_exists($skill_id, $parent_id);
  581. if (!$relation_exists) {
  582. $attributes = array(
  583. 'skill_id' => $skill_id,
  584. 'parent_id' => $parent_id,
  585. 'relation_type' => $params['relation_type'],
  586. //'level' => $params['level'],
  587. );
  588. $skill_rel_skill->update_by_skill($attributes);
  589. }
  590. }
  591. $skill_rel_gradebook->update_gradebooks_by_skill($skill_id, $params['gradebook_id']);
  592. return $skill_id;
  593. }
  594. return null;
  595. }
  596. /**
  597. * Get user's skills
  598. *
  599. * @param int $userId User's id
  600. */
  601. public function get_user_skills($user_id, $get_skill_data = false)
  602. {
  603. $user_id = intval($user_id);
  604. $sql = 'SELECT DISTINCT s.id, s.name FROM '.$this->table_skill_rel_user.' u INNER JOIN '.$this->table.' s
  605. ON u.skill_id = s.id
  606. WHERE user_id = '.$user_id;
  607. $result = Database::query($sql);
  608. $skills = Database::store_result($result, 'ASSOC');
  609. $clean_skill = array();
  610. if (!empty($skills)) {
  611. foreach ($skills as $skill) {
  612. if ($get_skill_data) {
  613. $clean_skill[$skill['id']] = $skill;
  614. } else {
  615. $clean_skill[$skill['id']] = $skill['id'];
  616. }
  617. }
  618. }
  619. return $clean_skill;
  620. }
  621. public function get_skills_tree($user_id = null, $skill_id = null, $return_flat_array = false, $add_root = false)
  622. {
  623. if ($skill_id == 1) {
  624. $skill_id = 0;
  625. }
  626. if (isset($user_id) && !empty($user_id)) {
  627. $skills = $this->get_all(true, $user_id, null, $skill_id);
  628. } else {
  629. $skills = $this->get_all(false, false, null, $skill_id);
  630. }
  631. $original_skill = $skills;
  632. //Show 1 item
  633. if (!empty($skill_id)) {
  634. if ($add_root) {
  635. if (!empty($skill_id)) {
  636. //Default root node
  637. $skills[1] = array('id' => '1', 'name' => get_lang('Root'), 'parent_id' => '0');
  638. $skill_info = $this->get_skill_info($skill_id);
  639. //2nd node
  640. $skills[$skill_id] = $skill_info;
  641. //Uncomment code below to hide the searched skill
  642. $skills[$skill_id]['data']['parent_id'] = $skill_info['parent_id'];
  643. $skills[$skill_id]['parent_id'] = 1;
  644. }
  645. }
  646. }
  647. $refs = array();
  648. $skills_tree = null;
  649. // Create references for all nodes
  650. $flat_array = array();
  651. $family = array();
  652. if (!empty($skills)) {
  653. foreach ($skills as &$skill) {
  654. if ($skill['parent_id'] == 0) {
  655. $skill['parent_id'] = 'root';
  656. }
  657. // because except main keys (id, name, children) others keys are not saved while in the space tree
  658. $skill['data'] = array('parent_id' => $skill['parent_id']);
  659. //In order to paint all members of a family with the same color
  660. if (empty($skill_id)) {
  661. if ($skill['parent_id'] == 1) {
  662. $family[$skill['id']] = $this->get_all_children($skill['id']);
  663. }
  664. } else {
  665. if ($skill['parent_id'] == $skill_id) {
  666. $family[$skill['id']] = $this->get_all_children($skill['id']);
  667. }
  668. /*if ($skill_id == $skill['id']) {
  669. $skill['parent_id'] = 1;
  670. }*/
  671. }
  672. if (!isset($skill['data']['real_parent_id'])) {
  673. $skill['data']['real_parent_id'] = $skill['parent_id'];
  674. }
  675. //User achieved the skill (depends in the gradebook with certification)
  676. $skill['data']['achieved'] = false;
  677. if ($user_id) {
  678. $skill['data']['achieved'] = $this->user_has_skill($user_id, $skill['id']);
  679. }
  680. //Check if the skill has related gradebooks
  681. $skill['data']['skill_has_gradebook'] = false;
  682. if (isset($skill['gradebooks']) && !empty($skill['gradebooks'])) {
  683. $skill['data']['skill_has_gradebook'] = true;
  684. }
  685. $refs[$skill['id']] = &$skill;
  686. $flat_array[$skill['id']] = &$skill;
  687. }
  688. //Checking family value
  689. $family_id = 1;
  690. $new_family_array = array();
  691. foreach ($family as $main_family_id => $family_items) {
  692. if (!empty($family_items)) {
  693. foreach ($family_items as $item) {
  694. $new_family_array[$item['skill_id']] = $family_id;
  695. }
  696. }
  697. $new_family_array[$main_family_id] = $family_id;
  698. $family_id++;
  699. }
  700. if (empty($original_skill)) {
  701. $refs['root']['children'][0] = $skills[1];
  702. $skills[$skill_id]['data']['family_id'] = 1;
  703. $refs['root']['children'][0]['children'][0] = $skills[$skill_id];
  704. $flat_array[$skill_id] = $skills[$skill_id];
  705. } else {
  706. // Moving node to the children index of their parents
  707. foreach ($skills as $my_skill_id => &$skill) {
  708. $skill['data']['family_id'] = $new_family_array[$skill['id']];
  709. $refs[$skill['parent_id']]['children'][] = &$skill;
  710. $flat_array[$my_skill_id] = $skill;
  711. }
  712. }
  713. $skills_tree = array(
  714. 'name' => get_lang('SkillRootName'),
  715. 'id' => 'root',
  716. 'children' => $refs['root']['children'],
  717. 'data' => array()
  718. );
  719. }
  720. if ($return_flat_array) {
  721. return $flat_array;
  722. }
  723. unset($skills);
  724. return $skills_tree;
  725. }
  726. /**
  727. * Get skills tree as a simplified JSON structure
  728. * @param int user id
  729. * @param int skill id
  730. * @param bool return a flat array or not
  731. * @param int depth of the skills
  732. *
  733. */
  734. public function get_skills_tree_json($user_id = null, $skill_id = null, $return_flat_array = false, $main_depth = 2)
  735. {
  736. $tree = $this->get_skills_tree($user_id, $skill_id, $return_flat_array, true);
  737. $simple_tree = array();
  738. if (!empty($tree['children'])) {
  739. foreach ($tree['children'] as $element) {
  740. $simple_tree[] = array(
  741. 'name' => $element['name'],
  742. 'children' => $this->get_skill_json($element['children'], 1, $main_depth),
  743. );
  744. }
  745. }
  746. return json_encode($simple_tree[0]['children']);
  747. }
  748. /**
  749. * Get JSON element
  750. */
  751. public function get_skill_json($subtree, $depth = 1, $max_depth = 2)
  752. {
  753. $simple_sub_tree = array();
  754. if (is_array($subtree)) {
  755. $counter = 1;
  756. foreach ($subtree as $elem) {
  757. $tmp = array();
  758. $tmp['name'] = $elem['name'];
  759. $tmp['id'] = $elem['id'];
  760. $tmp['isSearched'] = self::isSearched($elem['id']);
  761. if (is_array($elem['children'])) {
  762. $tmp['children'] = $this->get_skill_json($elem['children'], $depth + 1, $max_depth);
  763. } else {
  764. //$tmp['colour'] = $this->colours[$depth][rand(0,3)];
  765. }
  766. if ($depth > $max_depth) {
  767. continue;
  768. }
  769. $tmp['depth'] = $depth;
  770. $tmp['counter'] = $counter;
  771. $counter++;
  772. if (isset($elem['data']) && is_array($elem['data'])) {
  773. foreach ($elem['data'] as $key => $item) {
  774. $tmp[$key] = $item;
  775. }
  776. }
  777. $simple_sub_tree[] = $tmp;
  778. }
  779. return $simple_sub_tree;
  780. }
  781. return null;
  782. }
  783. function get_user_skill_ranking($user_id)
  784. {
  785. $user_id = intval($user_id);
  786. $sql = "SELECT count(skill_id) count FROM {$this->table} s INNER JOIN {$this->table_skill_rel_user} su ON (s.id = su.skill_id)
  787. WHERE user_id = $user_id";
  788. $result = Database::query($sql);
  789. if (Database::num_rows($result)) {
  790. $result = Database::fetch_row($result);
  791. return $result[0];
  792. }
  793. return false;
  794. }
  795. function get_user_list_skill_ranking($start, $limit, $sidx, $sord, $where_condition)
  796. {
  797. $start = intval($start);
  798. $limit = intval($limit);
  799. /* ORDER BY $sidx $sord */
  800. $sql = "SELECT *, @rownum:=@rownum+1 rank FROM (
  801. SELECT u.user_id, firstname, lastname, count(username) skills_acquired
  802. FROM {$this->table} s INNER JOIN {$this->table_skill_rel_user} su ON (s.id = su.skill_id)
  803. INNER JOIN {$this->table_user} u ON u.user_id = su.user_id, (SELECT @rownum:=0) r
  804. WHERE 1=1 $where_condition
  805. GROUP BY username
  806. ORDER BY skills_acquired desc
  807. LIMIT $start , $limit) AS T1, (SELECT @rownum:=0) r";
  808. $result = Database::query($sql);
  809. if (Database::num_rows($result)) {
  810. return Database::store_result($result, 'ASSOC');
  811. }
  812. return array();
  813. }
  814. function get_user_list_skill_ranking_count()
  815. {
  816. $sql = "SELECT count(*) FROM (
  817. SELECT count(distinct 1)
  818. FROM {$this->table} s INNER JOIN {$this->table_skill_rel_user} su ON (s.id = su.skill_id)
  819. INNER JOIN {$this->table_user} u ON u.user_id = su.user_id
  820. GROUP BY username
  821. ) as T1";
  822. $result = Database::query($sql);
  823. if (Database::num_rows($result)) {
  824. $result = Database::fetch_row($result);
  825. return $result[0];
  826. }
  827. return 0;
  828. }
  829. function get_count_skills_by_course($course_code)
  830. {
  831. $sql = "SELECT count(skill_id) as count FROM {$this->table_gradebook} g INNER JOIN {$this->table_skill_rel_gradebook} sg ON g.id = sg.gradebook_id
  832. WHERE course_code = '$course_code'";
  833. $result = Database::query($sql);
  834. if (Database::num_rows($result)) {
  835. $result = Database::fetch_row($result);
  836. return $result[0];
  837. }
  838. return 0;
  839. }
  840. function get_courses_by_skill($skill_id)
  841. {
  842. $skill_id = intval($skill_id);
  843. $sql = "SELECT c.title, c.code FROM {$this->table_gradebook} g INNER JOIN {$this->table_skill_rel_gradebook} sg ON g.id = sg.gradebook_id
  844. INNER JOIN {$this->table_course} c ON c.code = g.course_code
  845. WHERE sg.skill_id = $skill_id";
  846. $result = Database::query($sql);
  847. return Database::store_result($result, 'ASSOC');
  848. }
  849. /**
  850. * Return true if the user has the skill
  851. *
  852. * @param int $userId User's id
  853. * @param int $skillId Skill's id
  854. * @param int $checkInParents if true, function will search also in parents of the given skill id
  855. *
  856. * @return bool
  857. */
  858. public function user_has_skill($user_id, $skill_id)
  859. {
  860. $skills = $this->get_user_skills($user_id);
  861. foreach ($skills as $my_skill_id) {
  862. if ($my_skill_id == $skill_id) {
  863. return true;
  864. }
  865. }
  866. return false;
  867. }
  868. /**
  869. * Check if a skill is searched
  870. * @param int $id The skill id
  871. * @return boolean Whether el skill is searched return true. Otherwise return false
  872. */
  873. public static function isSearched($id)
  874. {
  875. $id = intval($id);
  876. if (empty($id)) {
  877. return false;
  878. }
  879. $skillRelProfileTable = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
  880. $result = Database::select(
  881. 'COUNT( DISTINCT `skill_id`) AS qty',
  882. $skillRelProfileTable,
  883. array(
  884. 'where' => array(
  885. 'skill_id = ?' => $id
  886. )
  887. ),
  888. 'first'
  889. );
  890. if ($result === false) {
  891. return false;
  892. }
  893. if ($result['qty'] > 0) {
  894. return true;
  895. }
  896. return false;
  897. }
  898. }