skill.lib.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. }