skill.lib.php 31 KB

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