abstractlink.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class AbstractLink
  5. * Defines a gradebook AbstractLink object.
  6. * To implement specific links,
  7. * extend this class and define a type in LinkFactory.
  8. * Use the methods in LinkFactory to create link objects.
  9. * @author Bert Steppé
  10. * @author Julio Montoya <gugli100@gmail.com> security improvements
  11. * @package chamilo.gradebook
  12. */
  13. abstract class AbstractLink implements GradebookItem
  14. {
  15. protected $id;
  16. protected $type;
  17. protected $ref_id;
  18. protected $user_id;
  19. protected $course_code;
  20. /** @var Category */
  21. protected $category;
  22. protected $created_at;
  23. protected $weight;
  24. protected $visible;
  25. protected $session_id;
  26. public $course_id;
  27. public $studentList;
  28. /**
  29. * Constructor
  30. */
  31. public function __construct()
  32. {
  33. $this->course_id = api_get_course_int_id();
  34. }
  35. /**
  36. * @return int
  37. */
  38. public function get_id()
  39. {
  40. return $this->id;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function get_type()
  46. {
  47. return $this->type;
  48. }
  49. /**
  50. * @return int
  51. */
  52. public function get_ref_id()
  53. {
  54. return (int) $this->ref_id;
  55. }
  56. /**
  57. * @return int
  58. */
  59. public function get_session_id()
  60. {
  61. return $this->session_id;
  62. }
  63. /**
  64. * @return int
  65. */
  66. public function get_user_id()
  67. {
  68. return $this->user_id;
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function get_course_code()
  74. {
  75. return $this->course_code;
  76. }
  77. /**
  78. * @return Category
  79. */
  80. public function getCategory()
  81. {
  82. return $this->category;
  83. }
  84. /**
  85. * @param Category $category
  86. */
  87. public function setCategory($category)
  88. {
  89. $this->category = $category;
  90. }
  91. /**
  92. * @return int
  93. */
  94. public function get_category_id()
  95. {
  96. return $this->category->get_id();
  97. }
  98. /**
  99. * @param int $category_id
  100. */
  101. public function set_category_id($category_id)
  102. {
  103. $categories = Category::load($category_id);
  104. if (isset($categories[0])) {
  105. $this->setCategory($categories[0]);
  106. }
  107. }
  108. public function get_date()
  109. {
  110. return $this->created_at;
  111. }
  112. public function get_weight()
  113. {
  114. return $this->weight;
  115. }
  116. public function is_locked()
  117. {
  118. return isset($this->locked) && $this->locked == 1 ? true : false;
  119. }
  120. public function is_visible()
  121. {
  122. return $this->visible;
  123. }
  124. public function set_id ($id)
  125. {
  126. $this->id = $id;
  127. }
  128. public function set_type ($type)
  129. {
  130. $this->type = $type;
  131. }
  132. public function set_ref_id ($ref_id)
  133. {
  134. $this->ref_id = $ref_id;
  135. }
  136. public function set_user_id ($user_id)
  137. {
  138. $this->user_id = $user_id;
  139. }
  140. /**
  141. * @param string $course_code
  142. */
  143. public function set_course_code($course_code)
  144. {
  145. $this->course_code = $course_code;
  146. $course_info = api_get_course_info($course_code);
  147. $this->course_id = $course_info['real_id'];
  148. }
  149. public function getStudentList()
  150. {
  151. return $this->studentList;
  152. }
  153. public function setStudentList($list)
  154. {
  155. $this->studentList = $list;
  156. }
  157. public function set_date($date)
  158. {
  159. $this->created_at = $date;
  160. }
  161. public function set_weight($weight)
  162. {
  163. $this->weight = $weight;
  164. }
  165. public function set_visible($visible)
  166. {
  167. $this->visible = $visible;
  168. }
  169. /**
  170. * @param integer $id
  171. */
  172. public function set_session_id($id)
  173. {
  174. $this->session_id = $id;
  175. }
  176. /**
  177. * @param $locked
  178. */
  179. public function set_locked($locked)
  180. {
  181. $this->locked = $locked;
  182. }
  183. /**
  184. * @return int
  185. */
  186. public function getCourseId()
  187. {
  188. return $this->course_id;
  189. }
  190. /**
  191. * Retrieve links and return them as an array of extensions of AbstractLink.
  192. * To keep consistency, do not call this method but LinkFactory::load instead.
  193. * @param integer $id
  194. * @param integer $type
  195. * @param integer $user_id
  196. * @param string $course_code
  197. * @param integer $category_id
  198. * @param integer $visible
  199. */
  200. public static function load(
  201. $id = null,
  202. $type = null,
  203. $ref_id = null,
  204. $user_id = null,
  205. $course_code = null,
  206. $category_id = null,
  207. $visible = null
  208. ) {
  209. $em = Database::getManager();
  210. $qb = $em->createQueryBuilder();
  211. $qb
  212. ->select('gl')
  213. ->from('ChamiloCoreBundle:GradebookLink', 'gl');
  214. if (isset ($id)) {
  215. $id = intval($id);
  216. $qb->andWhere(
  217. $qb->expr()->eq('gl.id', $id)
  218. );
  219. }
  220. if (isset ($type)) {
  221. $type = intval($type);
  222. $qb->andWhere(
  223. $qb->expr()->eq('gl.type', $type)
  224. );
  225. }
  226. if (isset ($ref_id)) {
  227. $ref_id = intval($ref_id);
  228. $qb->andWhere(
  229. $qb->expr()->eq('gl.refId', $ref_id)
  230. );
  231. }
  232. if (isset ($user_id)) {
  233. $user_id = intval($user_id);
  234. $qb->andWhere(
  235. $qb->expr()->eq('gl.userId', $user_id)
  236. );
  237. }
  238. if (isset ($course_code)) {
  239. $course = $em->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $course_code]);
  240. $qb->andWhere(
  241. $qb->expr()->eq('gl.course', $course->getId())
  242. );
  243. }
  244. if (isset ($category_id)) {
  245. $category_id = intval($category_id);
  246. $qb->andWhere(
  247. $qb->expr()->eq('gl.categoryId', $category_id)
  248. );
  249. }
  250. if (isset ($visible)) {
  251. $visible = intval($visible);
  252. $qb->andWhere(
  253. $qb->expr()->eq('gl.visible', $visible)
  254. );
  255. }
  256. $result = $qb->getQuery()->getResult();
  257. $links = AbstractLink::createObjectsFromEntities($result);
  258. return $links;
  259. }
  260. /**
  261. * Create an AbsctrackLink array from GradebookLink \Doctrine\Common\Collections\ArrayCollection or array
  262. * @param \Doctrine\Common\Collections\ArrayCollection|array $entities
  263. * @return array
  264. */
  265. private static function createObjectsFromEntities($entities)
  266. {
  267. $links = [];
  268. foreach ($entities as $gradebookLink) {
  269. $link = LinkFactory::create($gradebookLink->getType());
  270. $link->set_id($gradebookLink->getId());
  271. $link->set_type($gradebookLink->getType());
  272. $link->set_ref_id($gradebookLink->getRefId());
  273. $link->set_user_id($gradebookLink->getUserId());
  274. $link->set_course_code($gradebookLink->getCourse()->getCode());
  275. $link->set_category_id($gradebookLink->getCategoryId());
  276. $link->set_date($gradebookLink->getCreatedAt()->format('Y-m-d h:m:i'));
  277. $link->set_weight($gradebookLink->getWeight());
  278. $link->set_visible($gradebookLink->getVisible());
  279. $link->set_locked($gradebookLink->getLocked());
  280. //session id should depend of the category --> $data['category_id']
  281. $session_id = api_get_session_id();
  282. $link->set_session_id($session_id);
  283. $links[] = $link;
  284. }
  285. return $links;
  286. }
  287. /**
  288. * Insert this link into the database
  289. */
  290. public function add()
  291. {
  292. $this->add_linked_data();
  293. if (isset($this->type) &&
  294. isset($this->ref_id) &&
  295. isset($this->user_id) &&
  296. isset($this->course_code) &&
  297. isset($this->category) &&
  298. isset($this->weight) &&
  299. isset($this->visible)
  300. ) {
  301. $em = Database::getManager();
  302. $row_testing = $em
  303. ->createQuery('
  304. SELECT COUNT(gl) FROM ChamiloCoreBundle:GradebookLink gl
  305. WHERE gl.refId = :reference AND gl.categoryId = :category AND
  306. gl.course = :course AND gl.type = :type
  307. ')
  308. ->setParameters([
  309. 'reference' => $this->get_ref_id(),
  310. 'category' => $this->category->get_id(),
  311. 'course' => $this->course_id,
  312. 'type' => $this->type
  313. ])
  314. ->getSingleScalarResult();
  315. if ($row_testing == 0) {
  316. $createdAt = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  317. $course = $em->find('ChamiloCoreBundle:Course', $this->course_id);
  318. $gradebookLink = new \Chamilo\CoreBundle\Entity\GradebookLink();
  319. $gradebookLink
  320. ->setType($this->get_type())
  321. ->setRefId($this->get_ref_id())
  322. ->setUserId($this->get_user_id())
  323. ->setCourse($course)
  324. ->setCategoryId($this->get_category_id())
  325. ->setWeight($this->get_weight())
  326. ->setVisible($this->is_visible())
  327. ->setCreatedAt($createdAt)
  328. ->setLocked(0);
  329. $em->persist($gradebookLink);
  330. $em->flush();
  331. $inserted_id = $gradebookLink->getId();
  332. $this->set_id($inserted_id);
  333. return $inserted_id;
  334. }
  335. }
  336. return false;
  337. }
  338. /**
  339. * Update the properties of this link in the database
  340. */
  341. public function save()
  342. {
  343. $em = Database::getManager();
  344. $link = $em->find('ChamiloCoreBundle:GradebookLink', $this->id);
  345. if (!$link) {
  346. return;
  347. }
  348. AbstractLink::add_link_log($this->id);
  349. $this->save_linked_data();
  350. $course = $em->find('ChamiloCoreBundle:Course', $this->course_id);
  351. $link
  352. ->setType($this->get_type())
  353. ->setRefId($this->get_ref_id())
  354. ->setUserId($this->get_user_id())
  355. ->setCourse($course)
  356. ->setCategoryId($this->get_category_id())
  357. ->setWeight($this->get_weight())
  358. ->setVisible($this->is_visible());
  359. $em->merge($link);
  360. $em->flush();
  361. }
  362. /**
  363. * @param int $idevaluation
  364. */
  365. public static function add_link_log($idevaluation, $nameLog = null)
  366. {
  367. $table = Database:: get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  368. $dateobject = AbstractLink::load($idevaluation, null, null, null, null);
  369. $current_date_server = api_get_utc_datetime();
  370. $arreval = get_object_vars($dateobject[0]);
  371. $description_log = isset($arreval['description']) ? $arreval['description']:'';
  372. if (empty($nameLog)) {
  373. if (isset($_POST['name_link'])) {
  374. $name_log = isset($_POST['name_link']) ? $_POST['name_link'] : $arreval['course_code'];
  375. } elseif (isset($_POST['link_' . $idevaluation]) && $_POST['link_' . $idevaluation]) {
  376. $name_log = $_POST['link_' . $idevaluation];
  377. } else {
  378. $name_log = $arreval['course_code'];
  379. }
  380. } else {
  381. $name_log = $nameLog;
  382. }
  383. $params = [
  384. 'id_linkeval_log' => $arreval['id'],
  385. 'name' => $name_log,
  386. 'description' => $description_log,
  387. 'created_at' => $current_date_server,
  388. 'weight' => $arreval['weight'],
  389. 'visible' => $arreval['visible'],
  390. 'type' => 'Link',
  391. 'user_id_log' => api_get_user_id(),
  392. ];
  393. Database::insert($table, $params);
  394. }
  395. /**
  396. * Delete this link from the database
  397. */
  398. public function delete()
  399. {
  400. $this->delete_linked_data();
  401. $em = Database::getManager();
  402. $gradebookLink = $em->find('ChamiloCoreBundle:GradebookLink', $this->id);
  403. if ($gradebookLink) {
  404. $em->remove($gradebookLink);
  405. $em->flush();
  406. }
  407. }
  408. /**
  409. * Generate an array of possible categories where this link can be moved to.
  410. * Notice: its own parent will be included in the list: it's up to the frontend
  411. * to disable this element.
  412. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  413. */
  414. public function get_target_categories()
  415. {
  416. // links can only be moved to categories inside this course
  417. $targets = array();
  418. $level = 0;
  419. $crscats = Category::load(null,null,$this->get_course_code(),0);
  420. foreach ($crscats as $cat) {
  421. $targets[] = array($cat->get_id(), $cat->get_name(), $level+1);
  422. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  423. }
  424. return $targets;
  425. }
  426. /**
  427. * Internal function used by get_target_categories()
  428. * @param integer $level
  429. */
  430. private function add_target_subcategories($targets, $level, $catid)
  431. {
  432. $subcats = Category::load(null,null,null,$catid);
  433. foreach ($subcats as $cat) {
  434. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  435. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  436. }
  437. return $targets;
  438. }
  439. /**
  440. * Move this link to the given category.
  441. * If this link moves to outside a course, delete it.
  442. */
  443. public function move_to_cat($cat)
  444. {
  445. if ($this->get_course_code() != $cat->get_course_code()) {
  446. $this->delete();
  447. } else {
  448. $this->set_category_id($cat->get_id());
  449. $this->save();
  450. }
  451. }
  452. /**
  453. * Find links by name
  454. * To keep consistency, do not call this method but LinkFactory::find_links instead.
  455. * @todo can be written more efficiently using a new (but very complex) sql query
  456. * @param string $name_mask
  457. */
  458. public function find_links ($name_mask,$selectcat)
  459. {
  460. $rootcat = Category::load($selectcat);
  461. $links = $rootcat[0]->get_links((api_is_allowed_to_edit() ? null : api_get_user_id()), true);
  462. $foundlinks = array();
  463. foreach ($links as $link) {
  464. if (!(api_strpos(api_strtolower($link->get_name()), api_strtolower($name_mask)) === false)) {
  465. $foundlinks[] = $link;
  466. }
  467. }
  468. return $foundlinks;
  469. }
  470. /**
  471. * @return string
  472. */
  473. public function get_item_type()
  474. {
  475. return 'L';
  476. }
  477. /**
  478. * @return string
  479. */
  480. public function get_icon_name()
  481. {
  482. return 'link';
  483. }
  484. abstract function has_results();
  485. abstract function get_link();
  486. abstract function is_valid_link();
  487. abstract function get_type_name();
  488. abstract function needs_name_and_description();
  489. abstract function needs_max();
  490. abstract function needs_results();
  491. abstract function is_allowed_to_change_name();
  492. /* Seems to be not used anywhere */
  493. public function get_not_created_links()
  494. {
  495. return null;
  496. }
  497. public function get_all_links()
  498. {
  499. return null;
  500. }
  501. public function add_linked_data()
  502. {
  503. }
  504. public function save_linked_data()
  505. {
  506. }
  507. /**
  508. *
  509. */
  510. public function delete_linked_data()
  511. {
  512. }
  513. /**
  514. * @param string $name
  515. */
  516. public function set_name($name)
  517. {
  518. }
  519. /**
  520. * @param string $description
  521. */
  522. public function set_description($description)
  523. {
  524. }
  525. /**
  526. * @param integer $max
  527. */
  528. public function set_max($max)
  529. {
  530. }
  531. public function get_view_url($stud_id)
  532. {
  533. return null;
  534. }
  535. /**
  536. * Locks a link
  537. * @param int $locked 1 or unlocked 0
  538. *
  539. * */
  540. public function lock($locked)
  541. {
  542. $locked = intval($locked);
  543. $em = Database::getManager();
  544. $gradebookLink = $em->find('ChamiloCoreBundle:GradebookLink', $this->id);
  545. if ($gradebookLink) {
  546. $gradebookLink->setLocked($locked);
  547. $em->persist($gradebookLink);
  548. $em->flush();
  549. }
  550. }
  551. /**
  552. * Get current user ranking
  553. *
  554. * @param int $userId
  555. * @param array $studentList Array with user id and scores
  556. * Example: [1 => 5.00, 2 => 8.00]
  557. */
  558. public static function getCurrentUserRanking($userId, $studentList)
  559. {
  560. $ranking = null;
  561. $currentUserId = $userId;
  562. if (!empty($studentList) && !empty($currentUserId)) {
  563. $studentList = array_map('floatval', $studentList);
  564. asort($studentList);
  565. $ranking = $count = count($studentList);
  566. foreach ($studentList as $userId => $position) {
  567. if ($currentUserId == $userId) {
  568. break;
  569. }
  570. $ranking--;
  571. }
  572. // If no ranking was detected.
  573. if ($ranking == 0) {
  574. return [];
  575. }
  576. return array($ranking, $count);
  577. }
  578. return array();
  579. }
  580. }