abstractlink.class.php 12 KB

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