abstractlink.class.php 16 KB

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