abstractlink.class.php 16 KB

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