abstractlink.class.php 17 KB

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