abstractlink.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\GradebookLink;
  4. /**
  5. * Class AbstractLink
  6. * Defines a gradebook AbstractLink object.
  7. * To implement specific links,
  8. * extend this class and define a type in LinkFactory.
  9. * Use the methods in LinkFactory to create link objects.
  10. *
  11. * @author Bert Steppé
  12. * @author Julio Montoya <gugli100@gmail.com> security improvements
  13. *
  14. * @package chamilo.gradebook
  15. */
  16. abstract class AbstractLink implements GradebookItem
  17. {
  18. public $course_id;
  19. public $studentList;
  20. /** @var GradebookLink */
  21. public $entity;
  22. protected $id;
  23. protected $type;
  24. protected $ref_id;
  25. protected $user_id;
  26. protected $course_code;
  27. /** @var Category */
  28. protected $category;
  29. protected $created_at;
  30. protected $weight;
  31. protected $visible;
  32. protected $session_id;
  33. /**
  34. * Constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->course_id = api_get_course_int_id();
  39. }
  40. /**
  41. * @return bool
  42. */
  43. abstract public function has_results();
  44. /**
  45. * @return string
  46. */
  47. abstract public function get_link();
  48. /**
  49. * @return bool
  50. */
  51. abstract public function is_valid_link();
  52. /**
  53. * @return string
  54. */
  55. abstract public function get_type_name();
  56. /**
  57. * @return bool
  58. */
  59. abstract public function needs_name_and_description();
  60. /**
  61. * @return bool
  62. */
  63. abstract public function needs_max();
  64. /**
  65. * @return bool
  66. */
  67. abstract public function needs_results();
  68. /**
  69. * @return bool
  70. */
  71. abstract public function is_allowed_to_change_name();
  72. /**
  73. * @return int
  74. */
  75. public function get_id()
  76. {
  77. return $this->id;
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function get_type()
  83. {
  84. return $this->type;
  85. }
  86. /**
  87. * @return int
  88. */
  89. public function get_ref_id()
  90. {
  91. return (int) $this->ref_id;
  92. }
  93. /**
  94. * @return int
  95. */
  96. public function get_session_id()
  97. {
  98. return (int) $this->session_id;
  99. }
  100. /**
  101. * @return int
  102. */
  103. public function get_user_id()
  104. {
  105. return $this->user_id;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function get_course_code()
  111. {
  112. return $this->course_code;
  113. }
  114. /**
  115. * @return Category
  116. */
  117. public function getCategory()
  118. {
  119. return $this->category;
  120. }
  121. /**
  122. * @param Category $category
  123. */
  124. public function setCategory($category)
  125. {
  126. $this->category = $category;
  127. }
  128. /**
  129. * @return int
  130. */
  131. public function get_category_id()
  132. {
  133. return $this->category->get_id();
  134. }
  135. /**
  136. * @param int $category_id
  137. */
  138. public function set_category_id($category_id)
  139. {
  140. $categories = Category::load($category_id);
  141. if (isset($categories[0])) {
  142. $this->setCategory($categories[0]);
  143. }
  144. }
  145. public function get_date()
  146. {
  147. return $this->created_at;
  148. }
  149. public function get_weight()
  150. {
  151. return $this->weight;
  152. }
  153. public function is_locked()
  154. {
  155. return isset($this->locked) && $this->locked == 1 ? true : false;
  156. }
  157. public function is_visible()
  158. {
  159. return $this->visible;
  160. }
  161. public function set_id($id)
  162. {
  163. $this->id = $id;
  164. }
  165. public function set_type($type)
  166. {
  167. $this->type = $type;
  168. }
  169. public function set_ref_id($ref_id)
  170. {
  171. $this->ref_id = $ref_id;
  172. }
  173. public function set_user_id($user_id)
  174. {
  175. $this->user_id = $user_id;
  176. }
  177. /**
  178. * @param string $course_code
  179. */
  180. public function set_course_code($course_code)
  181. {
  182. $courseInfo = api_get_course_info($course_code);
  183. if ($courseInfo) {
  184. $this->course_code = $course_code;
  185. $this->course_id = $courseInfo['real_id'];
  186. }
  187. }
  188. /**
  189. * @return array
  190. */
  191. public function getStudentList()
  192. {
  193. if (empty($this->studentList)) {
  194. return [];
  195. }
  196. return $this->studentList;
  197. }
  198. /**
  199. * @param array $list
  200. */
  201. public function setStudentList($list)
  202. {
  203. $this->studentList = $list;
  204. }
  205. public function set_date($date)
  206. {
  207. $this->created_at = $date;
  208. }
  209. public function set_weight($weight)
  210. {
  211. $this->weight = $weight;
  212. }
  213. public function set_visible($visible)
  214. {
  215. $this->visible = $visible;
  216. }
  217. /**
  218. * @param int $id
  219. */
  220. public function set_session_id($id)
  221. {
  222. $this->session_id = $id;
  223. }
  224. /**
  225. * @param $locked
  226. */
  227. public function set_locked($locked)
  228. {
  229. $this->locked = $locked;
  230. }
  231. /**
  232. * @return int
  233. */
  234. public function getCourseId()
  235. {
  236. return (int) $this->course_id;
  237. }
  238. /**
  239. * Retrieve links and return them as an array of extensions of AbstractLink.
  240. * To keep consistency, do not call this method but LinkFactory::load instead.
  241. *
  242. * @param int $id
  243. * @param int $type
  244. * @param int $ref_id
  245. * @param int $user_id
  246. * @param string $course_code
  247. * @param int $category_id
  248. * @param int $visible
  249. *
  250. * @return array
  251. */
  252. public static function load(
  253. $id = null,
  254. $type = null,
  255. $ref_id = null,
  256. $user_id = null,
  257. $course_code = null,
  258. $category_id = null,
  259. $visible = null
  260. ) {
  261. $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  262. $sql = 'SELECT * FROM '.$tbl_grade_links;
  263. $paramcount = 0;
  264. if (isset($id)) {
  265. $sql .= ' WHERE id = '.intval($id);
  266. $paramcount++;
  267. }
  268. if (isset($type)) {
  269. if ($paramcount != 0) {
  270. $sql .= ' AND';
  271. } else {
  272. $sql .= ' WHERE';
  273. }
  274. $sql .= ' type = '.intval($type);
  275. $paramcount++;
  276. }
  277. if (isset($ref_id)) {
  278. if ($paramcount != 0) {
  279. $sql .= ' AND';
  280. } else {
  281. $sql .= ' WHERE';
  282. }
  283. $sql .= ' ref_id = '.intval($ref_id);
  284. $paramcount++;
  285. }
  286. if (isset($user_id)) {
  287. if ($paramcount != 0) {
  288. $sql .= ' AND';
  289. } else {
  290. $sql .= ' WHERE';
  291. }
  292. $sql .= ' user_id = '.intval($user_id);
  293. $paramcount++;
  294. }
  295. if (isset($course_code)) {
  296. if ($paramcount != 0) {
  297. $sql .= ' AND';
  298. } else {
  299. $sql .= ' WHERE';
  300. }
  301. $courseInfo = api_get_course_info($course_code);
  302. if ($courseInfo) {
  303. $sql .= " c_id = '".$courseInfo['real_id']."'";
  304. $paramcount++;
  305. }
  306. }
  307. if (isset($category_id)) {
  308. if ($paramcount != 0) {
  309. $sql .= ' AND';
  310. } else {
  311. $sql .= ' WHERE';
  312. }
  313. $sql .= ' category_id = '.intval($category_id);
  314. $paramcount++;
  315. }
  316. if (isset($visible)) {
  317. if ($paramcount != 0) {
  318. $sql .= ' AND';
  319. } else {
  320. $sql .= ' WHERE';
  321. }
  322. $sql .= ' visible = '.intval($visible);
  323. }
  324. $result = Database::query($sql);
  325. $links = self::create_objects_from_sql_result($result);
  326. return $links;
  327. }
  328. /**
  329. * Insert this link into the database.
  330. */
  331. public function add()
  332. {
  333. $this->add_linked_data();
  334. if (isset($this->type) &&
  335. isset($this->ref_id) &&
  336. isset($this->user_id) &&
  337. isset($this->course_code) &&
  338. isset($this->category) &&
  339. isset($this->weight) &&
  340. isset($this->visible)
  341. ) {
  342. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  343. $sql = "SELECT count(*) count FROM $table
  344. WHERE
  345. ref_id = ".$this->get_ref_id()." AND
  346. category_id = ".$this->category->get_id()." AND
  347. c_id = '".$this->course_id."' AND
  348. type = ".$this->type." ";
  349. $result = Database::query($sql);
  350. $row = Database::fetch_array($result, 'ASSOC');
  351. if ($row['count'] == 0) {
  352. $em = Database::getManager();
  353. $link = new GradebookLink();
  354. $link
  355. ->setType($this->get_type())
  356. ->setVisible($this->is_visible())
  357. ->setWeight(api_float_val($this->get_weight()))
  358. ->setUserId($this->get_user_id())
  359. ->setRefId($this->get_ref_id())
  360. ->setCategoryId($this->get_category_id())
  361. ->setCourse(api_get_course_entity())
  362. ->setCategoryId($this->get_category_id());
  363. $em->persist($link);
  364. $em->flush();
  365. $this->set_id($link->getId());
  366. return $link->getId();
  367. }
  368. }
  369. return false;
  370. }
  371. /**
  372. * Update the properties of this link in the database.
  373. */
  374. public function save()
  375. {
  376. $em = Database::getManager();
  377. $link = $em->find('ChamiloCoreBundle:GradebookLink', $this->id);
  378. if (!$link) {
  379. return;
  380. }
  381. self::add_link_log($this->id);
  382. $this->save_linked_data();
  383. $course = api_get_course_entity($this->getCourseId());
  384. $link
  385. ->setType($this->get_type())
  386. ->setRefId($this->get_ref_id())
  387. ->setUserId($this->get_user_id())
  388. ->setCourse($course)
  389. ->setCategoryId($this->get_category_id())
  390. ->setWeight($this->get_weight())
  391. ->setVisible($this->is_visible());
  392. $em->merge($link);
  393. $em->flush();
  394. }
  395. /**
  396. * @param int $evaluationId
  397. */
  398. public static function add_link_log($evaluationId, $nameLog = null)
  399. {
  400. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  401. $dateobject = self::load($evaluationId, null, null, null, null);
  402. $now = api_get_utc_datetime();
  403. $arreval = get_object_vars($dateobject[0]);
  404. $description_log = isset($arreval['description']) ? $arreval['description'] : '';
  405. if (empty($nameLog)) {
  406. if (isset($_POST['name_link'])) {
  407. $name_log = isset($_POST['name_link']) ? $_POST['name_link'] : $arreval['course_code'];
  408. } elseif (isset($_POST['link_'.$evaluationId]) && $_POST['link_'.$evaluationId]) {
  409. $name_log = $_POST['link_'.$evaluationId];
  410. } else {
  411. $name_log = $arreval['course_code'];
  412. }
  413. } else {
  414. $name_log = $nameLog;
  415. }
  416. $params = [
  417. 'id_linkeval_log' => $arreval['id'],
  418. 'name' => $name_log,
  419. 'description' => $description_log,
  420. 'created_at' => $now,
  421. 'weight' => $arreval['weight'],
  422. 'visible' => $arreval['visible'],
  423. 'type' => 'Link',
  424. 'user_id_log' => api_get_user_id(),
  425. ];
  426. Database::insert($table, $params);
  427. }
  428. /**
  429. * Delete this link from the database.
  430. */
  431. public function delete()
  432. {
  433. $this->delete_linked_data();
  434. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  435. $sql = 'DELETE FROM '.$table.'
  436. WHERE id = '.intval($this->id);
  437. Database::query($sql);
  438. }
  439. /**
  440. * Generate an array of possible categories where this link can be moved to.
  441. * Notice: its own parent will be included in the list: it's up to the frontend
  442. * to disable this element.
  443. *
  444. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  445. */
  446. public function get_target_categories()
  447. {
  448. // links can only be moved to categories inside this course
  449. $targets = [];
  450. $level = 0;
  451. $categories = Category::load(null, null, $this->get_course_code(), 0);
  452. foreach ($categories as $cat) {
  453. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  454. $targets = $this->addTargetSubcategories(
  455. $targets,
  456. $level + 1,
  457. $cat->get_id()
  458. );
  459. }
  460. return $targets;
  461. }
  462. /**
  463. * Move this link to the given category.
  464. * If this link moves to outside a course, delete it.
  465. */
  466. public function move_to_cat($cat)
  467. {
  468. if ($this->get_course_code() != $cat->get_course_code()) {
  469. $this->delete();
  470. } else {
  471. $this->set_category_id($cat->get_id());
  472. $this->save();
  473. }
  474. }
  475. /**
  476. * Find links by name
  477. * To keep consistency, do not call this method but LinkFactory::find_links instead.
  478. *
  479. * @todo can be written more efficiently using a new (but very complex) sql query
  480. *
  481. * @param string $name_mask
  482. *
  483. * @return array
  484. */
  485. public function find_links($name_mask, $selectcat)
  486. {
  487. $rootcat = Category::load($selectcat);
  488. $links = $rootcat[0]->get_links((api_is_allowed_to_edit() ? null : api_get_user_id()), true);
  489. $foundlinks = [];
  490. foreach ($links as $link) {
  491. if (!(api_strpos(api_strtolower($link->get_name()), api_strtolower($name_mask)) === false)) {
  492. $foundlinks[] = $link;
  493. }
  494. }
  495. return $foundlinks;
  496. }
  497. /**
  498. * @return string
  499. */
  500. public function get_item_type()
  501. {
  502. return 'L';
  503. }
  504. /**
  505. * @return string
  506. */
  507. public function get_icon_name()
  508. {
  509. return 'link';
  510. }
  511. public function get_all_links()
  512. {
  513. return [];
  514. }
  515. public function add_linked_data()
  516. {
  517. }
  518. public function save_linked_data()
  519. {
  520. }
  521. public function delete_linked_data()
  522. {
  523. }
  524. /**
  525. * @param string $name
  526. */
  527. public function set_name($name)
  528. {
  529. }
  530. /**
  531. * @param string $description
  532. */
  533. public function set_description($description)
  534. {
  535. }
  536. /**
  537. * @param int $max
  538. */
  539. public function set_max($max)
  540. {
  541. }
  542. public function get_view_url($stud_id)
  543. {
  544. return null;
  545. }
  546. /**
  547. * Locks a link.
  548. *
  549. * @param int $locked 1 or unlocked 0
  550. *
  551. * */
  552. public function lock($locked)
  553. {
  554. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  555. $sql = "UPDATE $table SET locked = '".intval($locked)."'
  556. WHERE id='".$this->id."'";
  557. Database::query($sql);
  558. }
  559. /**
  560. * Get current user ranking.
  561. *
  562. * @param int $userId
  563. * @param array $studentList Array with user id and scores
  564. * Example: [1 => 5.00, 2 => 8.00]
  565. *
  566. * @return array
  567. */
  568. public static function getCurrentUserRanking($userId, $studentList)
  569. {
  570. $ranking = null;
  571. $currentUserId = $userId;
  572. if (!empty($studentList) && !empty($currentUserId)) {
  573. $studentList = array_map('floatval', $studentList);
  574. asort($studentList);
  575. $ranking = $count = count($studentList);
  576. foreach ($studentList as $userId => $position) {
  577. if ($currentUserId == $userId) {
  578. break;
  579. }
  580. $ranking--;
  581. }
  582. // If no ranking was detected.
  583. if ($ranking == 0) {
  584. return [];
  585. }
  586. return [$ranking, $count];
  587. }
  588. return [];
  589. }
  590. /**
  591. * @return string
  592. */
  593. public function getSkillsFromItem()
  594. {
  595. $toolType = '';
  596. switch ($this->type) {
  597. case LINK_ATTENDANCE:
  598. $toolType = ITEM_TYPE_ATTENDANCE;
  599. break;
  600. case LINK_EXERCISE:
  601. $toolType = ITEM_TYPE_EXERCISE;
  602. break;
  603. case LINK_FORUM_THREAD:
  604. $toolType = ITEM_TYPE_FORUM_THREAD;
  605. break;
  606. case LINK_LEARNPATH:
  607. $toolType = ITEM_TYPE_LEARNPATH;
  608. break;
  609. case LINK_HOTPOTATOES:
  610. $toolType = ITEM_TYPE_HOTPOTATOES;
  611. break;
  612. case LINK_STUDENTPUBLICATION:
  613. $toolType = ITEM_TYPE_STUDENT_PUBLICATION;
  614. break;
  615. case LINK_SURVEY:
  616. $toolType = ITEM_TYPE_SURVEY;
  617. break;
  618. }
  619. $skillToString = Skill::getSkillRelItemsToString($toolType, $this->get_ref_id());
  620. return $skillToString;
  621. }
  622. /**
  623. * @param int $itemId
  624. * @param int $linkType
  625. * @param string $courseCode
  626. * @param int $sessionId
  627. *
  628. * @return array|bool|\Doctrine\DBAL\Driver\Statement
  629. */
  630. public static function getGradebookLinksFromItem($itemId, $linkType, $courseCode, $sessionId = 0)
  631. {
  632. if (empty($courseCode) || empty($itemId) || empty($linkType)) {
  633. return false;
  634. }
  635. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  636. $tableCategory = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  637. $itemId = (int) $itemId;
  638. $linkType = (int) $linkType;
  639. $sessionId = (int) $sessionId;
  640. $sessionCondition = api_get_session_condition($sessionId, true, false, 'c.session_id');
  641. $courseCode = Database::escape_string($courseCode);
  642. $sql = "SELECT DISTINCT l.*
  643. FROM $table l INNER JOIN $tableCategory c
  644. ON (c.course_code = l.course_code AND c.id = l.category_id)
  645. WHERE
  646. ref_id = $itemId AND
  647. type = $linkType AND
  648. l.course_code = '$courseCode'
  649. $sessionCondition ";
  650. $result = Database::query($sql);
  651. if (Database::num_rows($result)) {
  652. $result = Database::store_result($result);
  653. return $result;
  654. }
  655. return false;
  656. }
  657. /**
  658. * @param Doctrine\DBAL\Driver\Statement|null $result
  659. *
  660. * @return array
  661. */
  662. private static function create_objects_from_sql_result($result)
  663. {
  664. $links = [];
  665. $allow = api_get_configuration_value('allow_gradebook_stats');
  666. if ($allow) {
  667. $em = Database::getManager();
  668. $repo = $em->getRepository('ChamiloCoreBundle:GradebookLink');
  669. }
  670. while ($data = Database::fetch_array($result)) {
  671. $link = LinkFactory::create($data['type']);
  672. $link->set_id($data['id']);
  673. $link->set_type($data['type']);
  674. $link->set_ref_id($data['ref_id']);
  675. $link->set_user_id($data['user_id']);
  676. $link->set_course_code(api_get_course_id());
  677. $link->set_category_id($data['category_id']);
  678. $link->set_date($data['created_at']);
  679. $link->set_weight($data['weight']);
  680. $link->set_visible($data['visible']);
  681. $link->set_locked($data['locked']);
  682. //session id should depend of the category --> $data['category_id']
  683. $session_id = api_get_session_id();
  684. $link->set_session_id($session_id);
  685. if ($allow) {
  686. $link->entity = $repo->find($data['id']);
  687. }
  688. $links[] = $link;
  689. }
  690. return $links;
  691. }
  692. /**
  693. * Internal function used by get_target_categories().
  694. *
  695. * @param array $targets
  696. * @param int $level
  697. * @param int $catid
  698. *
  699. * @return array
  700. */
  701. private function addTargetSubcategories($targets, $level, $catid)
  702. {
  703. $subcats = Category::load(null, null, null, $catid);
  704. foreach ($subcats as $cat) {
  705. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  706. $targets = $this->addTargetSubcategories(
  707. $targets,
  708. $level + 1,
  709. $cat->get_id()
  710. );
  711. }
  712. return $targets;
  713. }
  714. }