skill.lib.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class SkillProfile
  5. * @package chamilo.library
  6. */
  7. class SkillProfile extends Model
  8. {
  9. public $columns = array('id', 'name', 'description');
  10. /**
  11. * Constructor
  12. */
  13. public function __construct()
  14. {
  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. /**
  19. * @return array
  20. */
  21. public function get_profiles()
  22. {
  23. $sql = "SELECT * FROM $this->table p
  24. INNER JOIN $this->table_rel_profile sp
  25. ON(p.id = sp.profile_id) ";
  26. $result = Database::query($sql);
  27. $profiles = Database::store_result($result, 'ASSOC');
  28. return $profiles;
  29. }
  30. /**
  31. * This function is for editing profile info from profile_id.
  32. * @param int $profileId
  33. * @param string $name
  34. * @param string $description
  35. */
  36. public function updateProfileInfo($profileId, $name, $description)
  37. {
  38. $profileId = intval($profileId);
  39. $sql = "UPDATE $this->table SET
  40. name = '$name',
  41. description = '$description'
  42. WHERE id = $profileId ";
  43. $result = Database::query($sql);
  44. return $result;
  45. }
  46. /**
  47. * Call the save method of the parent class and the SkillRelProfile object
  48. * @param array Params
  49. * @param bool Whether to show the query in parent save() method
  50. * @return mixed Profile ID or false if incomplete params
  51. */
  52. public function save($params, $show_query = false)
  53. {
  54. if (!empty($params)) {
  55. $profile_id = parent::save($params, $show_query);
  56. if ($profile_id) {
  57. $skill_rel_profile = new SkillRelProfile();
  58. if (isset($params['skills'])) {
  59. foreach ($params['skills'] as $skill_id) {
  60. $attributes = array('skill_id' => $skill_id, 'profile_id' => $profile_id);
  61. $skill_rel_profile->save($attributes);
  62. }
  63. }
  64. return $profile_id;
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * Delete a skill profile
  71. * @param int $id The skill profile id
  72. * @return boolean Whether delete a skill profile
  73. */
  74. public function delete($id)
  75. {
  76. Database::delete(
  77. $this->table_rel_profile,
  78. array(
  79. 'profile_id' => $id
  80. )
  81. );
  82. return parent::delete($id);
  83. }
  84. }
  85. /**
  86. * Class SkillRelProfile
  87. */
  88. class SkillRelProfile extends Model
  89. {
  90. public $columns = array('id', 'skill_id', 'profile_id');
  91. /**
  92. * Constructor
  93. */
  94. public function __construct()
  95. {
  96. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
  97. $this->tableProfile = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE);
  98. }
  99. /**
  100. * @param int $profileId
  101. * @return array
  102. */
  103. public function get_skills_by_profile($profileId)
  104. {
  105. $profileId = intval($profileId);
  106. $skills = $this->get_all(array('where' => array('profile_id = ? ' => $profileId)));
  107. $return = array();
  108. if (!empty($skills)) {
  109. foreach ($skills as $skill_data) {
  110. $return[] = $skill_data['skill_id'];
  111. }
  112. }
  113. return $return;
  114. }
  115. /**
  116. * This function is for getting profile info from profile_id.
  117. * @param int $profileId
  118. */
  119. public function getProfileInfo($profileId)
  120. {
  121. $sql = "SELECT * FROM $this->table p
  122. INNER JOIN $this->tableProfile pr
  123. ON (pr.id = p.profile_id)
  124. WHERE p.profile_id = ".intval($profileId);
  125. $result = Database::query($sql);
  126. $profileData = Database::fetch_array($result, 'ASSOC');
  127. return $profileData;
  128. }
  129. }
  130. /**
  131. * Class SkillRelSkill
  132. */
  133. class SkillRelSkill extends Model
  134. {
  135. public $columns = array('skill_id', 'parent_id', 'relation_type', 'level');
  136. /**
  137. * Constructor
  138. */
  139. public function __construct()
  140. {
  141. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL);
  142. }
  143. /**
  144. * Gets an element
  145. * @param int $id
  146. * @return array
  147. */
  148. public function get_skill_info($id)
  149. {
  150. if (empty($id)) {
  151. return array();
  152. }
  153. $result = Database::select(
  154. '*',
  155. $this->table,
  156. array('where' => array('skill_id = ?' => intval($id))),
  157. 'first'
  158. );
  159. return $result;
  160. }
  161. public function get_skill_parents($skill_id, $add_child_info = true)
  162. {
  163. $skill_id = intval($skill_id);
  164. $sql = 'SELECT child.* FROM '.$this->table.' child
  165. LEFT JOIN '.$this->table.' parent
  166. ON child.parent_id = parent.skill_id
  167. WHERE child.skill_id = '.$skill_id.' ';
  168. $result = Database::query($sql);
  169. $skill = Database::store_result($result, 'ASSOC');
  170. $skill = isset($skill[0]) ? $skill[0] : null;
  171. $parents = array();
  172. if (!empty($skill)) {
  173. if ($skill['parent_id'] != null) {
  174. $parents = self::get_skill_parents($skill['parent_id']);
  175. }
  176. if ($add_child_info) {
  177. $parents[] = $skill;
  178. }
  179. }
  180. return $parents;
  181. }
  182. /**
  183. * @param int $skill_id
  184. * @return array
  185. */
  186. public function get_direct_parents($skill_id)
  187. {
  188. $skill_id = intval($skill_id);
  189. $sql = 'SELECT parent_id as skill_id FROM '.$this->table.'
  190. WHERE skill_id = '.$skill_id.' ';
  191. $result = Database::query($sql);
  192. $skill = Database::store_result($result, 'ASSOC');
  193. $skill = isset($skill[0]) ? $skill[0] : null;
  194. $parents = array();
  195. if (!empty($skill)) {
  196. $parents[] = $skill;
  197. }
  198. return $parents;
  199. }
  200. /**
  201. * @param int $skill_id
  202. * @param bool $load_user_data
  203. * @param bool $user_id
  204. * @return array
  205. */
  206. public function get_children($skill_id, $load_user_data = false, $user_id = false)
  207. {
  208. $skills = $this->find('all', array('where' => array('parent_id = ? ' => $skill_id)));
  209. $skill_obj = new Skill();
  210. $skill_rel_user = new SkillRelUser();
  211. if ($load_user_data) {
  212. $passed_skills = $skill_rel_user->get_user_skills($user_id);
  213. $done_skills = array();
  214. foreach ($passed_skills as $done_skill) {
  215. $done_skills[] = $done_skill['skill_id'];
  216. }
  217. }
  218. if (!empty($skills)) {
  219. foreach ($skills as &$skill) {
  220. $skill['data'] = $skill_obj->get($skill['skill_id']);
  221. if (isset($skill['data']) && !empty($skill['data'])) {
  222. if (!empty($done_skills)) {
  223. $skill['data']['passed'] = 0;
  224. if (in_array($skill['skill_id'], $done_skills)) {
  225. $skill['data']['passed'] = 1;
  226. }
  227. }
  228. } else {
  229. $skill = null;
  230. }
  231. }
  232. }
  233. return $skills;
  234. }
  235. /**
  236. * @param array $params
  237. * @return bool
  238. */
  239. public function update_by_skill($params)
  240. {
  241. $result = Database::update($this->table, $params, array('skill_id = ? ' => $params['skill_id']));
  242. if ($result) {
  243. return true;
  244. }
  245. return false;
  246. }
  247. /**
  248. * @param int $skill_id
  249. * @param int $parent_id
  250. * @return bool
  251. */
  252. public function relation_exists($skill_id, $parent_id)
  253. {
  254. $result = $this->find(
  255. 'all',
  256. array('where' => array('skill_id = ? AND parent_id = ?' => array($skill_id, $parent_id)))
  257. );
  258. if (!empty($result)) {
  259. return true;
  260. }
  261. return false;
  262. }
  263. }
  264. /**
  265. * Class SkillRelGradebook
  266. */
  267. class SkillRelGradebook extends Model
  268. {
  269. public $columns = array('id', 'gradebook_id', 'skill_id');
  270. public function __construct()
  271. {
  272. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  273. }
  274. /**
  275. * @param int $gradebook_id
  276. * @param int $skill_id
  277. * @return bool
  278. */
  279. public function exists_gradebook_skill($gradebook_id, $skill_id)
  280. {
  281. $result = $this->find(
  282. 'all',
  283. array('where' => array('gradebook_id = ? AND skill_id = ?' => array($gradebook_id, $skill_id)))
  284. );
  285. if (!empty($result)) {
  286. return true;
  287. }
  288. return false;
  289. }
  290. /**
  291. * Gets an element
  292. */
  293. public function get_skill_info($skill_id, $gradebook_id)
  294. {
  295. if (empty($skill_id)) {
  296. return array();
  297. }
  298. $result = Database::select(
  299. '*',
  300. $this->table,
  301. array('where' => array('skill_id = ? AND gradebook_id = ? ' => array($skill_id, $gradebook_id))),
  302. 'first'
  303. );
  304. return $result;
  305. }
  306. /**
  307. * @param int $skill_id
  308. * @param array $gradebook_list
  309. */
  310. public function update_gradebooks_by_skill($skill_id, $gradebook_list)
  311. {
  312. $original_gradebook_list = $this->find(
  313. 'all',
  314. array('where' => array('skill_id = ?' => array($skill_id)))
  315. );
  316. $gradebooks_to_remove = array();
  317. $gradebooks_to_add = array();
  318. $original_gradebook_list_ids = array();
  319. if (!empty($original_gradebook_list)) {
  320. foreach ($original_gradebook_list as $gradebook) {
  321. if (!in_array($gradebook['gradebook_id'], $gradebook_list)) {
  322. $gradebooks_to_remove[] = $gradebook['id'];
  323. }
  324. }
  325. foreach ($original_gradebook_list as $gradebook_item) {
  326. $original_gradebook_list_ids[] = $gradebook_item['gradebook_id'];
  327. }
  328. }
  329. if (!empty($gradebook_list)) {
  330. foreach ($gradebook_list as $gradebook_id) {
  331. if (!in_array($gradebook_id, $original_gradebook_list_ids)) {
  332. $gradebooks_to_add[] = $gradebook_id;
  333. }
  334. }
  335. }
  336. if (!empty($gradebooks_to_remove)) {
  337. foreach ($gradebooks_to_remove as $id) {
  338. $this->delete($id);
  339. }
  340. }
  341. if (!empty($gradebooks_to_add)) {
  342. foreach ($gradebooks_to_add as $gradebook_id) {
  343. $attributes = array('skill_id' => $skill_id, 'gradebook_id' => $gradebook_id);
  344. $this->save($attributes);
  345. }
  346. }
  347. }
  348. /**
  349. * @param array $params
  350. * @return bool|void
  351. */
  352. public function update_by_skill($params)
  353. {
  354. $skill_info = $this->exists_gradebook_skill($params['gradebook_id'], $params['skill_id']);
  355. if ($skill_info) {
  356. return;
  357. } else {
  358. $result = $this->save($params);
  359. }
  360. if ($result) {
  361. return true;
  362. }
  363. return false;
  364. }
  365. }
  366. /**
  367. * Class SkillRelUser
  368. */
  369. class SkillRelUser extends Model
  370. {
  371. public $columns = array('id', 'user_id', 'skill_id', 'acquired_skill_at', 'assigned_by', 'course_id', 'session_id');
  372. /**
  373. * Constructor
  374. */
  375. public function __construct()
  376. {
  377. $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
  378. }
  379. /**
  380. * @param array $skill_list
  381. * @return array
  382. */
  383. public function get_user_by_skills($skill_list)
  384. {
  385. $users = array();
  386. if (!empty($skill_list)) {
  387. $skill_list = array_map('intval', $skill_list);
  388. $skill_list = implode("', '", $skill_list);
  389. $sql = "SELECT user_id FROM {$this->table}
  390. WHERE skill_id IN ('$skill_list') ";
  391. $result = Database::query($sql);
  392. $users = Database::store_result($result, 'ASSOC');
  393. }
  394. return $users;
  395. }
  396. /**
  397. * Get the achieved skills for the user
  398. * @param int $userId
  399. * @param int $courseId Optional. The course id
  400. * @param int $sessionId Optional. The session id
  401. * @return array The skill list. Otherwise return false
  402. */
  403. public function get_user_skills($userId, $courseId = 0, $sessionId = 0)
  404. {
  405. if (empty($userId)) {
  406. return array();
  407. }
  408. $courseId = intval($courseId);
  409. $sessionId = intval($sessionId);
  410. $whereConditions = array(
  411. 'user_id = ? ' => intval($userId)
  412. );
  413. if ($courseId > 0) {
  414. $whereConditions['AND course_id = ? '] = $courseId;
  415. $whereConditions['AND session_id = ?'] = $sessionId;
  416. }
  417. $result = Database::select(
  418. 'skill_id',
  419. $this->table,
  420. array(
  421. 'where' => $whereConditions
  422. ),
  423. 'all'
  424. );
  425. return $result;
  426. }
  427. /**
  428. * Get the relation data between user and skill
  429. * @param int $userId The user id
  430. * @param int $skillId The skill id
  431. * @param int $courseId The course id
  432. * @param int $sessionId Optional. The session id
  433. * @return array The relation data. Otherwise return false
  434. */
  435. public function getByUserAndSkill($userId, $skillId, $courseId, $sessionId = 0)
  436. {
  437. $where = array(
  438. 'user_id = ? AND skill_id = ? AND course_id = ? AND session_id = ?' => array(
  439. intval($userId),
  440. intval($skillId),
  441. intval($courseId),
  442. intval($sessionId)
  443. )
  444. );
  445. return Database::select('*', $this->table, array(
  446. 'where' => $where
  447. ), 'first');
  448. }
  449. }
  450. /**
  451. * Class Skill
  452. */
  453. class Skill extends Model
  454. {
  455. public $columns = array('id', 'name', 'description', 'access_url_id', 'short_code', 'icon', 'criteria');
  456. public $required = array('name');
  457. /** Array of colours by depth, for the coffee wheel. Each depth has 4 col */
  458. /*var $colours = array(
  459. 0 => array('#f9f0ab', '#ecc099', '#e098b0', '#ebe378'),
  460. 1 => array('#d5dda1', '#4a5072', '#8dae43', '#72659d'),
  461. 2 => array('#b28647', '#2e6093', '#393e64', '#1e8323'),
  462. 3 => array('#9f6652', '#9f6652', '#9f6652', '#9f6652'),
  463. 4 => array('#af643c', '#af643c', '#af643c', '#af643c'),
  464. 5 => array('#72659d', '#72659d', '#72659d', '#72659d'),
  465. 6 => array('#8a6e9e', '#8a6e9e', '#8a6e9e', '#8a6e9e'),
  466. 7 => array('#92538c', '#92538c', '#92538c', '#92538c'),
  467. 8 => array('#2e6093', '#2e6093', '#2e6093', '#2e6093'),
  468. 9 => array('#3a5988', '#3a5988', '#3a5988', '#3a5988'),
  469. 10 => array('#393e64', '#393e64', '#393e64', '#393e64'),
  470. );*/
  471. public function __construct()
  472. {
  473. $this->table = Database::get_main_table(TABLE_MAIN_SKILL);
  474. $this->table_user = Database::get_main_table(TABLE_MAIN_USER);
  475. $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  476. $this->table_skill_rel_user = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
  477. $this->table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  478. $this->table_skill_rel_skill = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL);
  479. $this->table_gradebook = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  480. $this->sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
  481. }
  482. /**
  483. * Gets an element
  484. * @param int $id
  485. *
  486. * @return array|mixed
  487. */
  488. public function get($id)
  489. {
  490. $result = parent::get($id);
  491. $result['web_icon_path'] = api_get_path(WEB_UPLOAD_PATH).'badges/'.$result['icon'];
  492. return $result;
  493. }
  494. /**
  495. * @param int $id
  496. * @return array
  497. */
  498. public function get_skill_info($id)
  499. {
  500. $skill_rel_skill = new SkillRelSkill();
  501. $skill_info = $this->get($id);
  502. if (!empty($skill_info)) {
  503. $skill_info['extra'] = $skill_rel_skill->get_skill_info($id);
  504. $skill_info['gradebooks'] = self::get_gradebooks_by_skill($id);
  505. }
  506. return $skill_info;
  507. }
  508. /**
  509. * @param array $skill_list
  510. * @return array
  511. */
  512. public function get_skills_info($skill_list)
  513. {
  514. $skill_list = array_map('intval', $skill_list);
  515. $skill_list = implode("', '", $skill_list);
  516. $sql = "SELECT * FROM {$this->table} WHERE id IN ('$skill_list') ";
  517. $result = Database::query($sql);
  518. $users = Database::store_result($result, 'ASSOC');
  519. return $users;
  520. }
  521. /**
  522. * @param bool $load_user_data
  523. * @param bool $user_id
  524. * @param int $id
  525. * @param int $parent_id
  526. * @return array
  527. */
  528. public function get_all($load_user_data = false, $user_id = false, $id = null, $parent_id = null)
  529. {
  530. $id_condition = '';
  531. if (!empty($id)) {
  532. $id = intval($id);
  533. $id_condition = " WHERE s.id = $id";
  534. }
  535. if (!empty($parent_id)) {
  536. $parent_id = intval($parent_id);
  537. if (empty($id_condition)) {
  538. $id_condition = " WHERE ss.parent_id = $parent_id";
  539. } else {
  540. $id_condition = " AND ss.parent_id = $parent_id";
  541. }
  542. }
  543. $sql = "SELECT
  544. s.id,
  545. s.name,
  546. s.description,
  547. ss.parent_id,
  548. ss.relation_type,
  549. s.icon,
  550. s.short_code,
  551. s.status
  552. FROM {$this->table} s
  553. INNER JOIN {$this->table_skill_rel_skill} ss
  554. ON (s.id = ss.skill_id) $id_condition
  555. ORDER BY ss.id, ss.parent_id";
  556. $result = Database::query($sql);
  557. $skills = array();
  558. $webPath = api_get_path(WEB_UPLOAD_PATH);
  559. if (Database::num_rows($result)) {
  560. while ($row = Database::fetch_array($result, 'ASSOC')) {
  561. $skill_rel_skill = new SkillRelSkill();
  562. $parents = $skill_rel_skill->get_skill_parents($row['id']);
  563. $row['level'] = count($parents) - 1;
  564. $row['gradebooks'] = self::get_gradebooks_by_skill($row['id']);
  565. $row['web_icon_path'] = $webPath.'badges/'.$row['icon'];
  566. $skills[$row['id']] = $row;
  567. }
  568. }
  569. // Load all children of the parent_id
  570. if (!empty($skills) && !empty($parent_id)) {
  571. foreach ($skills as $skill) {
  572. $children = self::get_all($load_user_data, $user_id, $id, $skill['id']);
  573. if (!empty($children)) {
  574. //$skills = array_merge($skills, $children);
  575. $skills = $skills + $children;
  576. }
  577. }
  578. }
  579. return $skills;
  580. }
  581. /**
  582. * @param int $skill_id
  583. * @return array|resource
  584. */
  585. public function get_gradebooks_by_skill($skill_id)
  586. {
  587. $skill_id = intval($skill_id);
  588. $sql = "SELECT g.* FROM {$this->table_gradebook} g
  589. INNER JOIN {$this->table_skill_rel_gradebook} sg
  590. ON g.id = sg.gradebook_id
  591. WHERE sg.skill_id = $skill_id";
  592. $result = Database::query($sql);
  593. $result = Database::store_result($result, 'ASSOC');
  594. return $result;
  595. }
  596. /**
  597. * Get one level childrens
  598. *
  599. * @param int $skill_id
  600. * @param bool $load_user_data
  601. * @return array
  602. */
  603. public function get_children($skill_id, $load_user_data = false)
  604. {
  605. $skill_rel_skill = new SkillRelSkill();
  606. if ($load_user_data) {
  607. $user_id = api_get_user_id();
  608. $skills = $skill_rel_skill->get_children($skill_id, true, $user_id);
  609. } else {
  610. $skills = $skill_rel_skill->get_children($skill_id);
  611. }
  612. return $skills;
  613. }
  614. /**
  615. * Get all children of the current node (recursive)
  616. * @param int $skillId
  617. * @return array
  618. */
  619. public function get_all_children($skillId)
  620. {
  621. $skill_rel_skill = new SkillRelSkill();
  622. $children = $skill_rel_skill->get_children($skillId);
  623. foreach ($children as $child) {
  624. $subChildren = $this->get_all_children($child['skill_id']);
  625. }
  626. if (!empty($subChildren)) {
  627. $children = array_merge($children, $subChildren);
  628. }
  629. return $children;
  630. }
  631. /**
  632. * Gets all parents from from the wanted skill
  633. */
  634. public function get_parents($skill_id)
  635. {
  636. $skill_rel_skill = new SkillRelSkill();
  637. $skills = $skill_rel_skill->get_skill_parents($skill_id, true);
  638. foreach ($skills as &$skill) {
  639. $skill['data'] = self::get($skill['skill_id']);
  640. }
  641. return $skills;
  642. }
  643. /**
  644. * All direct parents
  645. */
  646. public function get_direct_parents($skill_id)
  647. {
  648. $skill_rel_skill = new SkillRelSkill();
  649. $skills = $skill_rel_skill->get_direct_parents($skill_id, true);
  650. foreach($skills as &$skill) {
  651. $skill['data'] = self::get($skill['skill_id']);
  652. $skill_info2 = $skill_rel_skill->get_skill_info($skill['skill_id']);
  653. $skill['data']['parent_id'] = $skill_info2['parent_id'];
  654. }
  655. return $skills;
  656. }
  657. /**
  658. * Adds a new skill
  659. * @param array $params
  660. * @return bool|null
  661. */
  662. public function add($params)
  663. {
  664. if (!isset($params['parent_id'])) {
  665. $params['parent_id'] = 1;
  666. }
  667. if (!is_array($params['parent_id'])) {
  668. $params['parent_id'] = array($params['parent_id']);
  669. }
  670. $skill_rel_skill = new SkillRelSkill();
  671. $skill_rel_gradebook = new SkillRelGradebook();
  672. //Saving name, description
  673. $skill_id = $this->save($params);
  674. if ($skill_id) {
  675. //Saving skill_rel_skill (parent_id, relation_type)
  676. foreach ($params['parent_id'] as $parent_id) {
  677. $relation_exists = $skill_rel_skill->relation_exists($skill_id, $parent_id);
  678. if (!$relation_exists) {
  679. $attributes = array(
  680. 'skill_id' => $skill_id,
  681. 'parent_id' => $parent_id,
  682. 'relation_type' => (isset($params['relation_type'])?$params['relation_type']:0),
  683. //'level' => $params['level'],
  684. );
  685. $skill_rel_skill->save($attributes);
  686. }
  687. }
  688. if (!empty($params['gradebook_id'])) {
  689. foreach ($params['gradebook_id'] as $gradebook_id) {
  690. $attributes = array();
  691. $attributes['gradebook_id'] = $gradebook_id;
  692. $attributes['skill_id'] = $skill_id;
  693. $skill_rel_gradebook->save($attributes);
  694. }
  695. }
  696. return $skill_id;
  697. }
  698. return null;
  699. }
  700. /**
  701. * @param int $user_id
  702. * @param int $gradebook_id
  703. * @param int $courseId
  704. * @param int $sessionId
  705. */
  706. public function add_skill_to_user($user_id, $gradebook_id, $courseId = 0, $sessionId = 0)
  707. {
  708. $skill_gradebook = new SkillRelGradebook();
  709. $skill_rel_user = new SkillRelUser();
  710. $skill_gradebooks = $skill_gradebook->get_all(array('where' => array('gradebook_id = ?' => $gradebook_id)));
  711. if (!empty($skill_gradebooks)) {
  712. foreach ($skill_gradebooks as $skill_gradebook) {
  713. $user_has_skill = $this->user_has_skill($user_id, $skill_gradebook['skill_id'], $courseId, $sessionId);
  714. if (!$user_has_skill) {
  715. $params = array(
  716. 'user_id' => $user_id,
  717. 'skill_id' => $skill_gradebook['skill_id'],
  718. 'acquired_skill_at' => api_get_utc_datetime(),
  719. 'course_id' => intval($courseId),
  720. 'session_id' => intval($sessionId)
  721. );
  722. $skill_rel_user->save($params);
  723. }
  724. }
  725. }
  726. }
  727. /* Deletes a skill */
  728. public function delete($skill_id)
  729. {
  730. /*$params = array('skill_id' => $skill_id);
  731. $skill_rel_skill = new SkillRelSkill();
  732. $skills = $skill_rel_skill->get_all(array('where'=>array('skill_id = ?' =>$skill_id)));
  733. $skill_rel_profile = new SkillRelProfile();
  734. $skill_rel_gradebook = new SkillRelGradebook();
  735. $skill_rel_user = new SkillRelUser();
  736. $this->delete($skill_id);
  737. $skill_rel_gradebook->delete($params);*/
  738. }
  739. /**
  740. * @param array $params
  741. * @return null
  742. */
  743. public function edit($params)
  744. {
  745. if (!isset($params['parent_id'])) {
  746. $params['parent_id'] = 1;
  747. }
  748. $skill_rel_skill = new SkillRelSkill();
  749. $skill_rel_gradebook = new SkillRelGradebook();
  750. //Saving name, description
  751. $this->update($params);
  752. $skill_id = $params['id'];
  753. if ($skill_id) {
  754. //Saving skill_rel_skill (parent_id, relation_type)
  755. if (!is_array($params['parent_id'])) {
  756. $params['parent_id'] = array($params['parent_id']);
  757. }
  758. foreach ($params['parent_id'] as $parent_id) {
  759. $relation_exists = $skill_rel_skill->relation_exists($skill_id, $parent_id);
  760. if (!$relation_exists) {
  761. $attributes = array(
  762. 'skill_id' => $skill_id,
  763. 'parent_id' => $parent_id,
  764. 'relation_type' => $params['relation_type'],
  765. //'level' => $params['level'],
  766. );
  767. $skill_rel_skill->update_by_skill($attributes);
  768. }
  769. }
  770. $skill_rel_gradebook->update_gradebooks_by_skill($skill_id, $params['gradebook_id']);
  771. return $skill_id;
  772. }
  773. return null;
  774. }
  775. /**
  776. * Get user's skills
  777. *
  778. * @param int $userId User's id
  779. * @param bool $get_skill_data
  780. */
  781. public function get_user_skills($user_id, $get_skill_data = false)
  782. {
  783. $user_id = intval($user_id);
  784. $sql = 'SELECT DISTINCT s.id, s.name, s.icon
  785. FROM '.$this->table_skill_rel_user.' u
  786. INNER JOIN '.$this->table.' s
  787. ON u.skill_id = s.id
  788. WHERE user_id = '.$user_id;
  789. $result = Database::query($sql);
  790. $skills = Database::store_result($result, 'ASSOC');
  791. $uploadPath = api_get_path(WEB_UPLOAD_PATH);
  792. $clean_skill = array();
  793. if (!empty($skills)) {
  794. foreach ($skills as $skill) {
  795. if ($get_skill_data) {
  796. $iconThumb = null;
  797. $iconPath = null;
  798. if (!empty($skill['icon'])) {
  799. $iconThumb = sprintf(
  800. "badges/%s-small.png",
  801. sha1($skill['name'])
  802. );
  803. $iconPath = sprintf(
  804. "badges/%s.png",
  805. sha1($skill['name'])
  806. );
  807. }
  808. $clean_skill[$skill['id']] = array_merge(
  809. $skill,
  810. array(
  811. 'web_icon_thumb_path' => $uploadPath.$iconThumb,
  812. 'web_icon_path' => $uploadPath.$iconPath
  813. )
  814. );
  815. } else {
  816. $clean_skill[$skill['id']] = $skill['id'];
  817. }
  818. }
  819. }
  820. return $clean_skill;
  821. }
  822. /**
  823. * @param int $user_id
  824. * @param int $skill_id
  825. * @param bool $return_flat_array
  826. * @param bool $add_root
  827. * @return array|null
  828. */
  829. public function get_skills_tree($user_id = null, $skill_id = null, $return_flat_array = false, $add_root = false)
  830. {
  831. if ($skill_id == 1) {
  832. $skill_id = 0;
  833. }
  834. if (isset($user_id) && !empty($user_id)) {
  835. $skills = $this->get_all(true, $user_id, null, $skill_id);
  836. } else {
  837. $skills = $this->get_all(false, false, null, $skill_id);
  838. }
  839. $original_skill = $this->list = $skills;
  840. // Show 1 item
  841. if (!empty($skill_id)) {
  842. if ($add_root) {
  843. if (!empty($skill_id)) {
  844. // Default root node
  845. $skills[1] = array(
  846. 'id' => '1',
  847. 'name' => get_lang('Root'),
  848. 'parent_id' => '0'
  849. );
  850. $skill_info = $this->get_skill_info($skill_id);
  851. // 2nd node
  852. $skills[$skill_id] = $skill_info;
  853. // Uncomment code below to hide the searched skill
  854. $skills[$skill_id]['data']['parent_id'] = $skill_info['extra']['parent_id'];
  855. $skills[$skill_id]['parent_id'] = 1;
  856. }
  857. }
  858. }
  859. $refs = array();
  860. $skills_tree = null;
  861. // Create references for all nodes
  862. $flat_array = array();
  863. $family = array();
  864. if (!empty($skills)) {
  865. foreach ($skills as &$skill) {
  866. if ($skill['parent_id'] == 0) {
  867. $skill['parent_id'] = 'root';
  868. }
  869. // because except main keys (id, name, children) others keys
  870. // are not saved while in the space tree
  871. $skill['data'] = array('parent_id' => $skill['parent_id']);
  872. // If a short code was defined, send the short code to replace
  873. // skill name (to shorten the text in the wheel)
  874. if (
  875. !empty($skill['short_code']) &&
  876. api_get_setting('show_full_skill_name_on_skill_wheel') === 'false'
  877. ) {
  878. $skill['data']['short_code'] = $skill['short_code'];
  879. }
  880. $skill['data']['name'] = $skill['name'];
  881. // In order to paint all members of a family with the same color
  882. if (empty($skill_id)) {
  883. if ($skill['parent_id'] == 1) {
  884. $family[$skill['id']] = $this->get_all_children($skill['id']);
  885. }
  886. } else {
  887. if ($skill['parent_id'] == $skill_id) {
  888. $family[$skill['id']] = $this->get_all_children($skill['id']);
  889. }
  890. /*if ($skill_id == $skill['id']) {
  891. $skill['parent_id'] = 1;
  892. }*/
  893. }
  894. if (!isset($skill['data']['real_parent_id'])) {
  895. $skill['data']['real_parent_id'] = $skill['parent_id'];
  896. }
  897. // User achieved the skill (depends in the gradebook with certification)
  898. $skill['data']['achieved'] = false;
  899. if ($user_id) {
  900. $skill['data']['achieved'] = $this->user_has_skill($user_id, $skill['id']);
  901. }
  902. // Check if the skill has related gradebooks
  903. $skill['data']['skill_has_gradebook'] = false;
  904. if (isset($skill['gradebooks']) && !empty($skill['gradebooks'])) {
  905. $skill['data']['skill_has_gradebook'] = true;
  906. }
  907. $refs[$skill['id']] = &$skill;
  908. $flat_array[$skill['id']] = &$skill;
  909. }
  910. // Checking family value
  911. $family_id = 1;
  912. $new_family_array = array();
  913. foreach ($family as $main_family_id => $family_items) {
  914. if (!empty($family_items)) {
  915. foreach ($family_items as $item) {
  916. $new_family_array[$item['skill_id']] = $family_id;
  917. }
  918. }
  919. $new_family_array[$main_family_id] = $family_id;
  920. $family_id++;
  921. }
  922. if (empty($original_skill)) {
  923. $refs['root']['children'][0] = $skills[1];
  924. $skills[$skill_id]['data']['family_id'] = 1;
  925. $refs['root']['children'][0]['children'][0] = $skills[$skill_id];
  926. $flat_array[$skill_id] = $skills[$skill_id];
  927. } else {
  928. // Moving node to the children index of their parents
  929. foreach ($skills as $my_skill_id => &$skill) {
  930. if (isset($new_family_array[$skill['id']])) {
  931. $skill['data']['family_id'] = $new_family_array[$skill['id']];
  932. }
  933. $refs[$skill['parent_id']]['children'][] = &$skill;
  934. $flat_array[$my_skill_id] = $skill;
  935. }
  936. }
  937. $skills_tree = array(
  938. 'name' => get_lang('SkillRootName'),
  939. 'id' => 'root',
  940. 'children' => $refs['root']['children'],
  941. 'data' => array()
  942. );
  943. }
  944. if ($return_flat_array) {
  945. return $flat_array;
  946. }
  947. unset($skills);
  948. return $skills_tree;
  949. }
  950. /**
  951. * Get skills tree as a simplified JSON structure
  952. * @param int user id
  953. * @param int skill id
  954. * @param bool return a flat array or not
  955. * @param int depth of the skills
  956. *
  957. */
  958. public function get_skills_tree_json($user_id = null, $skill_id = null, $return_flat_array = false, $main_depth = 2)
  959. {
  960. $tree = $this->get_skills_tree($user_id, $skill_id, $return_flat_array, true);
  961. $simple_tree = array();
  962. if (!empty($tree['children'])) {
  963. foreach ($tree['children'] as $element) {
  964. $simple_tree[] = array(
  965. 'name' => $element['name'],
  966. 'children' => $this->get_skill_json($element['children'], 1, $main_depth)
  967. );
  968. }
  969. }
  970. return json_encode($simple_tree[0]['children']);
  971. }
  972. /**
  973. * Get JSON element
  974. * @param array $subtree
  975. * @param int $depth
  976. * @param int $max_depth
  977. * @return array|null
  978. */
  979. public function get_skill_json($subtree, $depth = 1, $max_depth = 2)
  980. {
  981. $simple_sub_tree = array();
  982. if (is_array($subtree)) {
  983. $counter = 1;
  984. foreach ($subtree as $elem) {
  985. $tmp = array();
  986. $tmp['name'] = $elem['name'];
  987. $tmp['id'] = $elem['id'];
  988. $tmp['isSearched'] = self::isSearched($elem['id']);
  989. if (isset($elem['children']) && is_array($elem['children'])) {
  990. $tmp['children'] = $this->get_skill_json($elem['children'], $depth + 1, $max_depth);
  991. } else {
  992. //$tmp['colour'] = $this->colours[$depth][rand(0,3)];
  993. }
  994. if ($depth > $max_depth) {
  995. continue;
  996. }
  997. $tmp['depth'] = $depth;
  998. $tmp['counter'] = $counter;
  999. $counter++;
  1000. if (isset($elem['data']) && is_array($elem['data'])) {
  1001. foreach ($elem['data'] as $key => $item) {
  1002. $tmp[$key] = $item;
  1003. }
  1004. }
  1005. $simple_sub_tree[] = $tmp;
  1006. }
  1007. return $simple_sub_tree;
  1008. }
  1009. return null;
  1010. }
  1011. /**
  1012. * @param int $user_id
  1013. * @return bool
  1014. */
  1015. public function get_user_skill_ranking($user_id)
  1016. {
  1017. $user_id = intval($user_id);
  1018. $sql = "SELECT count(skill_id) count FROM {$this->table} s
  1019. INNER JOIN {$this->table_skill_rel_user} su
  1020. ON (s.id = su.skill_id)
  1021. WHERE user_id = $user_id";
  1022. $result = Database::query($sql);
  1023. if (Database::num_rows($result)) {
  1024. $result = Database::fetch_row($result);
  1025. return $result[0];
  1026. }
  1027. return false;
  1028. }
  1029. /**
  1030. * @param $start
  1031. * @param $limit
  1032. * @param $sidx
  1033. * @param $sord
  1034. * @param $where_condition
  1035. * @return array
  1036. */
  1037. public function get_user_list_skill_ranking($start, $limit, $sidx, $sord, $where_condition)
  1038. {
  1039. $start = intval($start);
  1040. $limit = intval($limit);
  1041. /* ORDER BY $sidx $sord */
  1042. $sql = "SELECT *, @rownum:=@rownum+1 rank FROM (
  1043. SELECT u.user_id, firstname, lastname, count(username) skills_acquired
  1044. FROM {$this->table} s INNER JOIN {$this->table_skill_rel_user} su ON (s.id = su.skill_id)
  1045. INNER JOIN {$this->table_user} u ON u.user_id = su.user_id, (SELECT @rownum:=0) r
  1046. WHERE 1=1 $where_condition
  1047. GROUP BY username
  1048. ORDER BY skills_acquired desc
  1049. LIMIT $start , $limit) AS T1, (SELECT @rownum:=0) r";
  1050. $result = Database::query($sql);
  1051. if (Database::num_rows($result)) {
  1052. return Database::store_result($result, 'ASSOC');
  1053. }
  1054. return array();
  1055. }
  1056. /**
  1057. * @return int
  1058. */
  1059. public function get_user_list_skill_ranking_count()
  1060. {
  1061. $sql = "SELECT count(*) FROM (
  1062. SELECT count(distinct 1)
  1063. FROM {$this->table} s
  1064. INNER JOIN {$this->table_skill_rel_user} su
  1065. ON (s.id = su.skill_id)
  1066. INNER JOIN {$this->table_user} u
  1067. ON u.user_id = su.user_id
  1068. GROUP BY username
  1069. ) as T1";
  1070. $result = Database::query($sql);
  1071. if (Database::num_rows($result)) {
  1072. $result = Database::fetch_row($result);
  1073. return $result[0];
  1074. }
  1075. return 0;
  1076. }
  1077. /**
  1078. * @param string $course_code
  1079. * @return int
  1080. */
  1081. public function get_count_skills_by_course($course_code)
  1082. {
  1083. $sql = "SELECT count(skill_id) as count
  1084. FROM {$this->table_gradebook} g
  1085. INNER JOIN {$this->table_skill_rel_gradebook} sg
  1086. ON g.id = sg.gradebook_id
  1087. WHERE course_code = '$course_code'";
  1088. $result = Database::query($sql);
  1089. if (Database::num_rows($result)) {
  1090. $result = Database::fetch_row($result);
  1091. return $result[0];
  1092. }
  1093. return 0;
  1094. }
  1095. /**
  1096. * @param int $skill_id
  1097. * @return array
  1098. */
  1099. public function get_courses_by_skill($skill_id)
  1100. {
  1101. $skill_id = intval($skill_id);
  1102. $sql = "SELECT c.title, c.code
  1103. FROM {$this->table_gradebook} g
  1104. INNER JOIN {$this->table_skill_rel_gradebook} sg
  1105. ON g.id = sg.gradebook_id
  1106. INNER JOIN {$this->table_course} c
  1107. ON c.code = g.course_code
  1108. WHERE sg.skill_id = $skill_id
  1109. AND (g.session_id IS NULL OR g.session_id = 0)";
  1110. $result = Database::query($sql);
  1111. return Database::store_result($result, 'ASSOC');
  1112. }
  1113. /**
  1114. * Check if the user has the skill
  1115. * @param int $userId The user id
  1116. * @param int $skillId The skill id
  1117. * @param int $courseId Optional. The course id
  1118. * @param int $sessionId Optional. The session id
  1119. * @return boolean Whether the user has the skill return true. Otherwise return false
  1120. */
  1121. public function user_has_skill($userId, $skillId, $courseId = 0, $sessionId = 0)
  1122. {
  1123. $courseId = intval($courseId);
  1124. $sessionId = intval($sessionId);
  1125. $whereConditions = array(
  1126. 'user_id = ? ' => intval($userId),
  1127. 'AND skill_id = ? ' => intval($skillId)
  1128. );
  1129. if ($courseId > 0) {
  1130. $whereConditions['AND course_id = ? '] = $courseId;
  1131. $whereConditions['AND session_id = ? '] = $sessionId;
  1132. }
  1133. $result = Database::select(
  1134. 'COUNT(1) AS qty',
  1135. $this->table_skill_rel_user, array(
  1136. 'where' => $whereConditions
  1137. ),
  1138. 'first'
  1139. );
  1140. if ($result != false) {
  1141. if ($result['qty'] > 0) {
  1142. return true;
  1143. }
  1144. }
  1145. return false;
  1146. }
  1147. /**
  1148. * Check if a skill is searched
  1149. * @param int $id The skill id
  1150. * @return boolean Whether el skill is searched return true. Otherwise return false
  1151. */
  1152. public static function isSearched($id)
  1153. {
  1154. $id = intval($id);
  1155. if (empty($id)) {
  1156. return false;
  1157. }
  1158. $skillRelProfileTable = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
  1159. $result = Database::select(
  1160. 'COUNT( DISTINCT `skill_id`) AS qty',
  1161. $skillRelProfileTable,
  1162. array(
  1163. 'where' => array(
  1164. 'skill_id = ?' => $id
  1165. )
  1166. ),
  1167. 'first'
  1168. );
  1169. if ($result === false) {
  1170. return false;
  1171. }
  1172. if ($result['qty'] > 0) {
  1173. return true;
  1174. }
  1175. return false;
  1176. }
  1177. /**
  1178. * Get the achieved skills by course
  1179. * @param int $courseId The course id
  1180. * @return array The skills list
  1181. */
  1182. public function listAchievedByCourse($courseId)
  1183. {
  1184. $courseId = intval($courseId);
  1185. if ($courseId == 0) {
  1186. return array();
  1187. }
  1188. $list = array();
  1189. $sql = "SELECT
  1190. course.id c_id,
  1191. course.title c_name,
  1192. course.directory c_directory,
  1193. user.user_id,
  1194. user.lastname,
  1195. user.firstname,
  1196. user.username,
  1197. skill.id skill_id,
  1198. skill.name skill_name,
  1199. sru.acquired_skill_at
  1200. FROM {$this->table_skill_rel_user} AS sru
  1201. INNER JOIN {$this->table_course}
  1202. ON sru.course_id = course.id
  1203. INNER JOIN {$this->table_user}
  1204. ON sru.user_id = user.user_id
  1205. INNER JOIN {$this->table}
  1206. ON sru.skill_id = skill.id
  1207. WHERE course.id = $courseId";
  1208. $result = Database::query($sql);
  1209. while ($row = Database::fetch_assoc($result)) {
  1210. $list[] = $row;
  1211. }
  1212. return $list;
  1213. }
  1214. /**
  1215. * Get the users list who achieved a skill
  1216. * @param int $skillId The skill id
  1217. *
  1218. * @return array The users list
  1219. */
  1220. public function listUsersWhoAchieved($skillId)
  1221. {
  1222. $skillId = intval($skillId);
  1223. if ($skillId == 0) {
  1224. return array();
  1225. }
  1226. $list = array();
  1227. $sql = "SELECT
  1228. course.id c_id,
  1229. course.title c_name,
  1230. course.directory c_directory,
  1231. user.user_id,
  1232. user.lastname,
  1233. user.firstname,
  1234. user.username,
  1235. skill.id skill_id,
  1236. skill.name skill_name,
  1237. sru.acquired_skill_at
  1238. FROM {$this->table_skill_rel_user} AS sru
  1239. INNER JOIN {$this->table_course}
  1240. ON sru.course_id = course.id
  1241. INNER JOIN {$this->table_user}
  1242. ON sru.user_id = user.user_id
  1243. INNER JOIN {$this->table}
  1244. ON sru.skill_id = skill.id
  1245. WHERE skill.id = $skillId ";
  1246. $result = Database::query($sql);
  1247. while ($row = Database::fetch_assoc($result)) {
  1248. $list[] = $row;
  1249. }
  1250. return $list;
  1251. }
  1252. /**
  1253. * Get the session list where the user can achieve a skill
  1254. * @param int $skillId The skill id
  1255. * @return array
  1256. */
  1257. public function getSessionsBySkill($skillId)
  1258. {
  1259. $skillId = intval($skillId);
  1260. $sql = "SELECT s.id, s.name
  1261. FROM {$this->table_gradebook} g
  1262. INNER JOIN {$this->table_skill_rel_gradebook} sg ON g.id = sg.gradebook_id
  1263. INNER JOIN {$this->sessionTable} s ON g.session_id = s.id
  1264. WHERE sg.skill_id = $skillId
  1265. AND g.session_id > 0";
  1266. $result = Database::query($sql);
  1267. return Database::store_result($result, 'ASSOC');
  1268. }
  1269. }