abstractlink.class.php 16 KB

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