skill.lib.php 47 KB

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