abstractlink.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  302. $paramcount++;
  303. }
  304. if (isset($category_id)) {
  305. if ($paramcount != 0) {
  306. $sql .= ' AND';
  307. } else {
  308. $sql .= ' WHERE';
  309. }
  310. $sql .= ' category_id = '.intval($category_id);
  311. $paramcount++;
  312. }
  313. if (isset($visible)) {
  314. if ($paramcount != 0) {
  315. $sql .= ' AND';
  316. } else {
  317. $sql .= ' WHERE';
  318. }
  319. $sql .= ' visible = '.intval($visible);
  320. }
  321. $result = Database::query($sql);
  322. $links = self::create_objects_from_sql_result($result);
  323. return $links;
  324. }
  325. /**
  326. * Insert this link into the database.
  327. */
  328. public function add()
  329. {
  330. $this->add_linked_data();
  331. if (isset($this->type) &&
  332. isset($this->ref_id) &&
  333. isset($this->user_id) &&
  334. isset($this->course_code) &&
  335. isset($this->category) &&
  336. isset($this->weight) &&
  337. isset($this->visible)
  338. ) {
  339. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  340. $sql = "SELECT count(*) count FROM $table
  341. WHERE
  342. ref_id = ".$this->get_ref_id()." AND
  343. category_id = ".$this->category->get_id()." AND
  344. course_code = '".$this->course_code."' AND
  345. type = ".$this->type;
  346. $result = Database::query($sql);
  347. $row = Database::fetch_array($result, 'ASSOC');
  348. if ($row['count'] == 0) {
  349. $params = [
  350. 'type' => $this->get_type(),
  351. 'ref_id' => $this->get_ref_id(),
  352. 'user_id' => $this->get_user_id(),
  353. 'course_code' => $this->get_course_code(),
  354. 'category_id' => $this->get_category_id(),
  355. 'weight' => $this->get_weight(),
  356. 'visible' => $this->is_visible(),
  357. 'created_at' => api_get_utc_datetime(),
  358. 'locked' => 0,
  359. ];
  360. $id = Database::insert($table, $params);
  361. $this->set_id($id);
  362. return $id;
  363. }
  364. }
  365. return false;
  366. }
  367. /**
  368. * Update the properties of this link in the database.
  369. */
  370. public function save()
  371. {
  372. $em = Database::getManager();
  373. $link = $em->find('ChamiloCoreBundle:GradebookLink', $this->id);
  374. if (!$link) {
  375. return;
  376. }
  377. self::add_link_log($this->id);
  378. $this->save_linked_data();
  379. $link
  380. ->setType($this->get_type())
  381. ->setRefId($this->get_ref_id())
  382. ->setUserId($this->get_user_id())
  383. ->setCourseCode($this->get_course_code())
  384. ->setCategoryId($this->get_category_id())
  385. ->setWeight($this->get_weight())
  386. ->setVisible($this->is_visible());
  387. $em->merge($link);
  388. $em->flush();
  389. }
  390. /**
  391. * @param int $evaluationId
  392. */
  393. public static function add_link_log($evaluationId, $nameLog = null)
  394. {
  395. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  396. $dateobject = self::load($evaluationId, null, null, null, null);
  397. $now = api_get_utc_datetime();
  398. $arreval = get_object_vars($dateobject[0]);
  399. $description_log = isset($arreval['description']) ? $arreval['description'] : '';
  400. if (empty($nameLog)) {
  401. if (isset($_POST['name_link'])) {
  402. $name_log = isset($_POST['name_link']) ? $_POST['name_link'] : $arreval['course_code'];
  403. } elseif (isset($_POST['link_'.$evaluationId]) && $_POST['link_'.$evaluationId]) {
  404. $name_log = $_POST['link_'.$evaluationId];
  405. } else {
  406. $name_log = $arreval['course_code'];
  407. }
  408. } else {
  409. $name_log = $nameLog;
  410. }
  411. $params = [
  412. 'id_linkeval_log' => $arreval['id'],
  413. 'name' => $name_log,
  414. 'description' => $description_log,
  415. 'created_at' => $now,
  416. 'weight' => $arreval['weight'],
  417. 'visible' => $arreval['visible'],
  418. 'type' => 'Link',
  419. 'user_id_log' => api_get_user_id(),
  420. ];
  421. Database::insert($table, $params);
  422. }
  423. /**
  424. * Delete this link from the database.
  425. */
  426. public function delete()
  427. {
  428. $this->delete_linked_data();
  429. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  430. $sql = 'DELETE FROM '.$table.'
  431. WHERE id = '.intval($this->id);
  432. Database::query($sql);
  433. }
  434. /**
  435. * Generate an array of possible categories where this link can be moved to.
  436. * Notice: its own parent will be included in the list: it's up to the frontend
  437. * to disable this element.
  438. *
  439. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  440. */
  441. public function get_target_categories()
  442. {
  443. // links can only be moved to categories inside this course
  444. $targets = [];
  445. $level = 0;
  446. $categories = Category::load(null, null, $this->get_course_code(), 0);
  447. foreach ($categories as $cat) {
  448. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  449. $targets = $this->addTargetSubcategories(
  450. $targets,
  451. $level + 1,
  452. $cat->get_id()
  453. );
  454. }
  455. return $targets;
  456. }
  457. /**
  458. * Move this link to the given category.
  459. * If this link moves to outside a course, delete it.
  460. */
  461. public function move_to_cat($cat)
  462. {
  463. if ($this->get_course_code() != $cat->get_course_code()) {
  464. $this->delete();
  465. } else {
  466. $this->set_category_id($cat->get_id());
  467. $this->save();
  468. }
  469. }
  470. /**
  471. * Find links by name
  472. * To keep consistency, do not call this method but LinkFactory::find_links instead.
  473. *
  474. * @todo can be written more efficiently using a new (but very complex) sql query
  475. *
  476. * @param string $name_mask
  477. *
  478. * @return array
  479. */
  480. public function find_links($name_mask, $selectcat)
  481. {
  482. $rootcat = Category::load($selectcat);
  483. $links = $rootcat[0]->get_links((api_is_allowed_to_edit() ? null : api_get_user_id()), true);
  484. $foundlinks = [];
  485. foreach ($links as $link) {
  486. if (!(api_strpos(api_strtolower($link->get_name()), api_strtolower($name_mask)) === false)) {
  487. $foundlinks[] = $link;
  488. }
  489. }
  490. return $foundlinks;
  491. }
  492. /**
  493. * @return string
  494. */
  495. public function get_item_type()
  496. {
  497. return 'L';
  498. }
  499. /**
  500. * @return string
  501. */
  502. public function get_icon_name()
  503. {
  504. return 'link';
  505. }
  506. public function get_all_links()
  507. {
  508. return [];
  509. }
  510. public function add_linked_data()
  511. {
  512. }
  513. public function save_linked_data()
  514. {
  515. }
  516. public function delete_linked_data()
  517. {
  518. }
  519. /**
  520. * @param string $name
  521. */
  522. public function set_name($name)
  523. {
  524. }
  525. /**
  526. * @param string $description
  527. */
  528. public function set_description($description)
  529. {
  530. }
  531. /**
  532. * @param int $max
  533. */
  534. public function set_max($max)
  535. {
  536. }
  537. public function get_view_url($stud_id)
  538. {
  539. return null;
  540. }
  541. /**
  542. * Locks a link.
  543. *
  544. * @param int $locked 1 or unlocked 0
  545. *
  546. * */
  547. public function lock($locked)
  548. {
  549. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  550. $sql = "UPDATE $table SET locked = '".intval($locked)."'
  551. WHERE id='".$this->id."'";
  552. Database::query($sql);
  553. }
  554. /**
  555. * Get current user ranking.
  556. *
  557. * @param int $userId
  558. * @param array $studentList Array with user id and scores
  559. * Example: [1 => 5.00, 2 => 8.00]
  560. *
  561. * @return array
  562. */
  563. public static function getCurrentUserRanking($userId, $studentList)
  564. {
  565. $ranking = null;
  566. $currentUserId = $userId;
  567. if (!empty($studentList) && !empty($currentUserId)) {
  568. $studentList = array_map('floatval', $studentList);
  569. asort($studentList);
  570. $ranking = $count = count($studentList);
  571. foreach ($studentList as $userId => $position) {
  572. if ($currentUserId == $userId) {
  573. break;
  574. }
  575. $ranking--;
  576. }
  577. // If no ranking was detected.
  578. if ($ranking == 0) {
  579. return [];
  580. }
  581. return [$ranking, $count];
  582. }
  583. return [];
  584. }
  585. /**
  586. * @return string
  587. */
  588. public function getSkillsFromItem()
  589. {
  590. $toolType = '';
  591. switch ($this->type) {
  592. case LINK_ATTENDANCE:
  593. $toolType = ITEM_TYPE_ATTENDANCE;
  594. break;
  595. case LINK_EXERCISE:
  596. $toolType = ITEM_TYPE_EXERCISE;
  597. break;
  598. case LINK_FORUM_THREAD:
  599. $toolType = ITEM_TYPE_FORUM_THREAD;
  600. break;
  601. case LINK_LEARNPATH:
  602. $toolType = ITEM_TYPE_LEARNPATH;
  603. break;
  604. case LINK_HOTPOTATOES:
  605. $toolType = ITEM_TYPE_HOTPOTATOES;
  606. break;
  607. case LINK_STUDENTPUBLICATION:
  608. $toolType = ITEM_TYPE_STUDENT_PUBLICATION;
  609. break;
  610. case LINK_SURVEY:
  611. $toolType = ITEM_TYPE_SURVEY;
  612. break;
  613. }
  614. $skillToString = Skill::getSkillRelItemsToString($toolType, $this->get_ref_id());
  615. return $skillToString;
  616. }
  617. /**
  618. * @param int $itemId
  619. * @param int $linkType
  620. * @param string $courseCode
  621. * @param int $sessionId
  622. *
  623. * @return array|bool|\Doctrine\DBAL\Driver\Statement
  624. */
  625. public static function getGradebookLinksFromItem($itemId, $linkType, $courseCode, $sessionId = 0)
  626. {
  627. if (empty($courseCode) || empty($itemId) || empty($linkType)) {
  628. return false;
  629. }
  630. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  631. $tableCategory = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  632. $itemId = (int) $itemId;
  633. $linkType = (int) $linkType;
  634. $sessionId = (int) $sessionId;
  635. $sessionCondition = api_get_session_condition($sessionId, true, false, 'c.session_id');
  636. $courseCode = Database::escape_string($courseCode);
  637. $sql = "SELECT DISTINCT l.*
  638. FROM $table l INNER JOIN $tableCategory c
  639. ON (c.course_code = l.course_code AND c.id = l.category_id)
  640. WHERE
  641. ref_id = $itemId AND
  642. type = $linkType AND
  643. l.course_code = '$courseCode'
  644. $sessionCondition ";
  645. $result = Database::query($sql);
  646. if (Database::num_rows($result)) {
  647. $result = Database::store_result($result);
  648. return $result;
  649. }
  650. return false;
  651. }
  652. /**
  653. * @param Doctrine\DBAL\Driver\Statement|null $result
  654. *
  655. * @return array
  656. */
  657. private static function create_objects_from_sql_result($result)
  658. {
  659. $links = [];
  660. $allow = api_get_configuration_value('allow_gradebook_stats');
  661. if ($allow) {
  662. $em = Database::getManager();
  663. $repo = $em->getRepository('ChamiloCoreBundle:GradebookLink');
  664. }
  665. while ($data = Database::fetch_array($result)) {
  666. $link = LinkFactory::create($data['type']);
  667. $link->set_id($data['id']);
  668. $link->set_type($data['type']);
  669. $link->set_ref_id($data['ref_id']);
  670. $link->set_user_id($data['user_id']);
  671. $link->set_course_code($data['course_code']);
  672. $link->set_category_id($data['category_id']);
  673. $link->set_date($data['created_at']);
  674. $link->set_weight($data['weight']);
  675. $link->set_visible($data['visible']);
  676. $link->set_locked($data['locked']);
  677. //session id should depend of the category --> $data['category_id']
  678. $session_id = api_get_session_id();
  679. $link->set_session_id($session_id);
  680. if ($allow) {
  681. $link->entity = $repo->find($data['id']);
  682. }
  683. $links[] = $link;
  684. }
  685. return $links;
  686. }
  687. /**
  688. * Internal function used by get_target_categories().
  689. *
  690. * @param array $targets
  691. * @param int $level
  692. * @param int $catid
  693. *
  694. * @return array
  695. */
  696. private function addTargetSubcategories($targets, $level, $catid)
  697. {
  698. $subcats = Category::load(null, null, null, $catid);
  699. foreach ($subcats as $cat) {
  700. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  701. $targets = $this->addTargetSubcategories(
  702. $targets,
  703. $level + 1,
  704. $cat->get_id()
  705. );
  706. }
  707. return $targets;
  708. }
  709. }