abstractlink.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. protected $category;
  22. protected $created_at;
  23. protected $weight;
  24. protected $visible;
  25. protected $session_id;
  26. public $course_id;
  27. public function __construct()
  28. {
  29. $this->course_id = api_get_course_int_id();
  30. }
  31. public function get_id()
  32. {
  33. return $this->id;
  34. }
  35. public function get_type()
  36. {
  37. return $this->type;
  38. }
  39. public function get_ref_id()
  40. {
  41. return $this->ref_id;
  42. }
  43. public function get_session_id()
  44. {
  45. return $this->session_id;
  46. }
  47. public function get_user_id()
  48. {
  49. return $this->user_id;
  50. }
  51. public function get_course_code()
  52. {
  53. return $this->course_code;
  54. }
  55. public function get_category_id()
  56. {
  57. return $this->category;
  58. }
  59. public function get_date()
  60. {
  61. return $this->created_at;
  62. }
  63. public function get_weight()
  64. {
  65. return $this->weight;
  66. }
  67. public function is_locked()
  68. {
  69. return isset($this->locked) && $this->locked == 1 ? true : false ;
  70. }
  71. public function is_visible()
  72. {
  73. return $this->visible;
  74. }
  75. public function set_id ($id)
  76. {
  77. $this->id = $id;
  78. }
  79. public function set_type ($type)
  80. {
  81. $this->type = $type;
  82. }
  83. public function set_ref_id ($ref_id)
  84. {
  85. $this->ref_id = $ref_id;
  86. }
  87. public function set_user_id ($user_id)
  88. {
  89. $this->user_id = $user_id;
  90. }
  91. public function set_course_code ($course_code)
  92. {
  93. $this->course_code = $course_code;
  94. $course_info = api_get_course_info($course_code);
  95. $this->course_id = $course_info['real_id'];
  96. }
  97. public function set_category_id ($category_id)
  98. {
  99. $this->category = $category_id;
  100. }
  101. public function set_date ($date)
  102. {
  103. $this->created_at = $date;
  104. }
  105. public function set_weight ($weight)
  106. {
  107. $this->weight = $weight;
  108. }
  109. public function set_visible ($visible)
  110. {
  111. $this->visible = $visible;
  112. }
  113. public function set_session_id($id)
  114. {
  115. $this->session_id = $id;
  116. }
  117. /**
  118. * @param $locked
  119. */
  120. public function set_locked($locked)
  121. {
  122. $this->locked = $locked;
  123. }
  124. /**
  125. * @return int
  126. */
  127. public function getCourseId()
  128. {
  129. return $this->course_id;
  130. }
  131. /**
  132. * Retrieve links and return them as an array of extensions of AbstractLink.
  133. * To keep consistency, do not call this method but LinkFactory::load instead.
  134. */
  135. public static function load(
  136. $id = null,
  137. $type = null,
  138. $ref_id = null,
  139. $user_id = null,
  140. $course_code = null,
  141. $category_id = null,
  142. $visible = null
  143. )
  144. {
  145. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  146. $sql = 'SELECT * FROM '.$tbl_grade_links;
  147. $paramcount = 0;
  148. if (isset ($id)) {
  149. $sql.= ' WHERE id = '.Database::escape_string($id);
  150. $paramcount ++;
  151. }
  152. if (isset ($type)) {
  153. if ($paramcount != 0) $sql .= ' AND';
  154. else $sql .= ' WHERE';
  155. $sql .= ' type = '.Database::escape_string($type);
  156. $paramcount ++;
  157. }
  158. if (isset ($ref_id)) {
  159. if ($paramcount != 0) $sql .= ' AND';
  160. else $sql .= ' WHERE';
  161. $sql .= ' ref_id = '.intval($ref_id);
  162. $paramcount ++;
  163. }
  164. if (isset ($user_id)) {
  165. if ($paramcount != 0) {
  166. $sql .= ' AND';
  167. }else {
  168. $sql .= ' WHERE';
  169. }
  170. $sql .= ' user_id = '.intval($user_id);
  171. $paramcount ++;
  172. }
  173. if (isset ($course_code)) {
  174. if ($paramcount != 0) {
  175. $sql .= ' AND';
  176. } else {
  177. $sql .= ' WHERE';
  178. }
  179. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  180. $paramcount ++;
  181. }
  182. if (isset ($category_id)) {
  183. if ($paramcount != 0) {
  184. $sql .= ' AND';
  185. }else {
  186. $sql .= ' WHERE';
  187. }
  188. $sql .= ' category_id = '.intval($category_id);
  189. $paramcount ++;
  190. }
  191. if (isset ($visible)) {
  192. if ($paramcount != 0) {
  193. $sql .= ' AND';
  194. } else {
  195. $sql .= ' WHERE';
  196. }
  197. $sql .= ' visible = '.intval($visible);
  198. }
  199. $result = Database::query($sql);
  200. $links = AbstractLink::create_objects_from_sql_result($result);
  201. return $links;
  202. }
  203. /**
  204. * @param $result
  205. * @return array
  206. */
  207. private static function create_objects_from_sql_result($result)
  208. {
  209. $links = array();
  210. while ($data = Database::fetch_array($result)) {
  211. $link = LinkFactory::create($data['type']);
  212. $link->set_id($data['id']);
  213. $link->set_type($data['type']);
  214. $link->set_ref_id($data['ref_id']);
  215. $link->set_user_id($data['user_id']);
  216. $link->set_course_code($data['course_code']);
  217. $link->set_category_id($data['category_id']);
  218. $link->set_date($data['created_at']);
  219. $link->set_weight($data['weight']);
  220. $link->set_visible($data['visible']);
  221. $link->set_locked($data['locked']);
  222. //session id should depend of the category --> $data['category_id']
  223. $session_id = api_get_session_id();
  224. $link->set_session_id($session_id);
  225. $links[]=$link;
  226. }
  227. return $links;
  228. }
  229. /**
  230. * Insert this link into the database
  231. */
  232. public function add()
  233. {
  234. $this->add_linked_data();
  235. if (isset($this->type) && isset($this->ref_id) && isset($this->user_id) && isset($this->course_code) && isset($this->category) && isset($this->weight) && isset($this->visible)) {
  236. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  237. $sql = "SELECT count(*) FROM ".$tbl_grade_links."
  238. WHERE
  239. ref_id=".$this->get_ref_id()." AND
  240. category_id = ".$this->category." AND
  241. course_code = '".$this->course_code."' AND
  242. type = ".$this->type." ";
  243. $result = Database::query($sql);
  244. $row_testing = Database::fetch_array($result);
  245. if ($row_testing[0]==0) {
  246. $sql = 'INSERT INTO '.$tbl_grade_links.' (type, ref_id, user_id, course_code, category_id, weight, visible, created_at) VALUES ('
  247. .intval($this->get_type())
  248. .','.intval($this->get_ref_id())
  249. .','.intval($this->get_user_id())
  250. .",'".Database::escape_string($this->get_course_code())."'"
  251. .','.intval($this->get_category_id())
  252. .",'".Database::escape_string($this->get_weight())."'"
  253. .','.intval($this->is_visible());
  254. $sql .= ','.'"'.$date_current = api_get_local_time().'"';
  255. $sql .= ")";
  256. Database::query($sql);
  257. $inserted_id = Database::insert_id();
  258. $this->set_id($inserted_id);
  259. return $inserted_id;
  260. }
  261. } else {
  262. die('Error in AbstractLink add: required field empty');
  263. }
  264. return false;
  265. }
  266. /**
  267. * Update the properties of this link in the database
  268. */
  269. public function save() {
  270. $this->save_linked_data();
  271. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  272. $sql = "UPDATE $tbl_grade_links SET
  273. type = ".intval($this->get_type()).",
  274. ref_id = ".intval($this->get_ref_id()).",
  275. user_id = ".intval($this->get_user_id()).",
  276. course_code = '".Database::escape_string($this->get_course_code())."',
  277. category_id = ".intval($this->get_category_id()).",
  278. weight = '".Database::escape_string($this->get_weight())."',
  279. visible = ".intval($this->is_visible())."
  280. WHERE id = ".intval($this->id);
  281. AbstractLink::add_link_log($this->id);
  282. Database::query($sql);
  283. }
  284. /**
  285. * @param int $idevaluation
  286. */
  287. public function add_link_log($idevaluation)
  288. {
  289. $tbl_grade_linkeval_log = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  290. $dateobject=AbstractLink::load ($idevaluation,null,null,null,null);
  291. $current_date_server=api_get_utc_datetime();
  292. $arreval=get_object_vars($dateobject[0]);
  293. $description_log=isset($arreval['description'])?$arreval['description']:'';
  294. if (isset($_POST['name_link'])) {
  295. $name_log=isset($_POST['name_link'])?Security::remove_XSS($_POST['name_link']):$arreval['course_code'];
  296. } elseif ($_POST['link_'.$idevaluation]) {
  297. $name_log=$_POST['link_'.$idevaluation];
  298. } else {
  299. $name_log=$arreval['course_code'];
  300. }
  301. $sql="INSERT INTO ".$tbl_grade_linkeval_log."(id_linkeval_log,name,description,created_at,weight,visible,type,user_id_log)
  302. VALUES('".Database::escape_string($arreval['id'])."','".Database::escape_string($name_log)."','".Database::escape_string($description_log)."','".Database::escape_string($current_date_server)."','".Database::escape_string($arreval['weight'])."','".Database::escape_string($arreval['visible'])."','Link',".api_get_user_id().")";
  303. Database::query($sql);
  304. }
  305. /**
  306. * Delete this link from the database
  307. */
  308. public function delete() {
  309. $this->delete_linked_data();
  310. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  311. $sql = 'DELETE FROM '.$tbl_grade_links.' WHERE id = '.intval($this->id);
  312. Database::query($sql);
  313. }
  314. /**
  315. * Generate an array of possible categories where this link can be moved to.
  316. * Notice: its own parent will be included in the list: it's up to the frontend
  317. * to disable this element.
  318. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  319. */
  320. public function get_target_categories()
  321. {
  322. // links can only be moved to categories inside this course
  323. $targets = array();
  324. $level = 0;
  325. $crscats = Category::load(null,null,$this->get_course_code(),0);
  326. foreach ($crscats as $cat) {
  327. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  328. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  329. }
  330. return $targets;
  331. }
  332. /**
  333. * Internal function used by get_target_categories()
  334. */
  335. private function add_target_subcategories($targets, $level, $catid)
  336. {
  337. $subcats = Category::load(null,null,null,$catid);
  338. foreach ($subcats as $cat) {
  339. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  340. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  341. }
  342. return $targets;
  343. }
  344. /**
  345. * Move this link to the given category.
  346. * If this link moves to outside a course, delete it.
  347. */
  348. public function move_to_cat($cat)
  349. {
  350. if ($this->get_course_code() != $cat->get_course_code()) {
  351. $this->delete();
  352. } else {
  353. $this->set_category_id($cat->get_id());
  354. $this->save();
  355. }
  356. }
  357. /**
  358. * Find links by name
  359. * To keep consistency, do not call this method but LinkFactory::find_links instead.
  360. * @todo can be written more efficiently using a new (but very complex) sql query
  361. */
  362. public function find_links ($name_mask,$selectcat)
  363. {
  364. $rootcat = Category::load($selectcat);
  365. $links = $rootcat[0]->get_links((api_is_allowed_to_edit() ? null : api_get_user_id()), true);
  366. $foundlinks = array();
  367. foreach ($links as $link) {
  368. if (!(api_strpos(api_strtolower($link->get_name()), api_strtolower($name_mask)) === false)) {
  369. $foundlinks[] = $link;
  370. }
  371. }
  372. return $foundlinks;
  373. }
  374. // Other methods implementing GradebookItem
  375. public function get_item_type()
  376. {
  377. return 'L';
  378. }
  379. public function get_icon_name() {
  380. return 'link';
  381. }
  382. // ABSTRACT FUNCTIONS - to be implemented by subclass
  383. abstract function has_results();
  384. abstract function get_link();
  385. abstract function is_valid_link();
  386. abstract function get_type_name();
  387. // The following methods are already defined in GradebookItem,
  388. // and must be implemented by the subclass as well !
  389. // abstract function get_name();
  390. // abstract function get_description();
  391. // abstract function calc_score($stud_id = null);
  392. abstract function needs_name_and_description();
  393. abstract function needs_max();
  394. abstract function needs_results();
  395. abstract function is_allowed_to_change_name();
  396. /* TRIVIAL FUNCTIONS - to be overwritten by subclass if needed */
  397. /* Seems to be not used anywhere */
  398. public function get_not_created_links()
  399. {
  400. return null;
  401. }
  402. public function get_all_links()
  403. {
  404. return null;
  405. }
  406. public function add_linked_data()
  407. {
  408. }
  409. public function save_linked_data()
  410. {
  411. }
  412. public function delete_linked_data()
  413. {
  414. }
  415. public function set_name ($name)
  416. {
  417. }
  418. public function set_description ($description)
  419. {
  420. }
  421. public function set_max ($max)
  422. {
  423. }
  424. public function get_view_url ($stud_id)
  425. {
  426. return null;
  427. }
  428. /**
  429. * Locks a link
  430. * @param int locked 1 or unlocked 0
  431. *
  432. * */
  433. function lock($locked)
  434. {
  435. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  436. $sql = "UPDATE $table SET locked = '".intval($locked)."' WHERE id='".$this->id."'";
  437. Database::query($sql);
  438. }
  439. }