skill.lib.php 47 KB

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