controller.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Link controller script
  5. * @package chamilo.link
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace Link;
  11. use Security;
  12. use Uri;
  13. use Redirect;
  14. use Chamilo;
  15. use Javascript;
  16. use Header;
  17. /**
  18. * Html controller. Dispatch request and perform required action:
  19. *
  20. * - list
  21. * - add/edit/delete link
  22. * - add/edit/delete category
  23. * - make visible/invisible link
  24. * - go to link target
  25. *
  26. * Note:
  27. * Currently some actions are only implemented in the Ajax controller.
  28. *
  29. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  30. * @license /license.txt
  31. */
  32. class Controller extends \Controller
  33. {
  34. const ACTION_LISTING = 'listing';
  35. const ACTION_VIEW = 'view';
  36. const ACTION_ADD_LINK = 'add_link';
  37. const ACTION_EDIT_LINK = 'edit_link';
  38. const ACTION_DELETE_LINK = 'delete_link';
  39. const ACTION_MAKE_VISIBLE = 'make_visisble';
  40. const ACTION_MAKE_INVISIBLE = 'make_invisible';
  41. const ACTION_ADD_CATEGORY = 'add_category';
  42. const ACTION_EDIT_CATEGORY = 'edit_category';
  43. const ACTION_DELETE_CATEGORY = 'delete_category';
  44. const ACTION_GO = 'go';
  45. const ACTION_IMPORT_CSV = 'import_csv';
  46. const ACTION_EXPORT_CSV = 'export_csv';
  47. const ACTION_DEFAULT = 'listing';
  48. /**
  49. *
  50. * @return \Link\Controller
  51. */
  52. public static function instance()
  53. {
  54. static $result = null;
  55. if (empty($result)) {
  56. $result = new self();
  57. }
  58. return $result;
  59. }
  60. public function is_allowed_to_edit()
  61. {
  62. if (Request::is_student_view()) {
  63. return false;
  64. }
  65. //$c_id = self::params()->get_c_id();
  66. //$id = self::params()->get_id();
  67. $session_id = Request::get_session_id();
  68. if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  69. return false;
  70. }
  71. if (!api_is_allowed_to_edit(false, true, true)) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Action to perform.
  78. * Returns the request parameter.
  79. *
  80. * @return string
  81. */
  82. public function get_action()
  83. {
  84. $result = parent::get_action();
  85. // if (self::params()->is_student_view()) {
  86. // if ($result != self::ACTION_LISTING && $result != self::ACTION_VIEW) {
  87. // return self::ACTION_LISTING;
  88. // }
  89. // }
  90. $result = $result ? $result : self::ACTION_DEFAULT;
  91. return $result;
  92. }
  93. public function prolog()
  94. {
  95. event_access_tool(TOOL_LINK);
  96. //legacy
  97. global $interbreadcrumb;
  98. $interbreadcrumb = array();
  99. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Links'));
  100. global $current_course_tool;
  101. global $this_section;
  102. global $nameTools;
  103. $current_course_tool = TOOL_LINK;
  104. $this_section = SECTION_COURSES;
  105. $nameTools = get_lang('Links');
  106. }
  107. /**
  108. * Whether the call is authorized or not.
  109. *
  110. * @return boolean
  111. */
  112. public function authorize()
  113. {
  114. $authorize = api_protect_course_script();
  115. if (!$authorize) {
  116. return false;
  117. }
  118. $c_id = Request::get_c_id();
  119. if (empty($c_id)) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Javascript used by the controller
  126. *
  127. * @return string
  128. */
  129. public function javascript()
  130. {
  131. $src = Chamilo::url('/main/link/resources/js/main.js');
  132. $result = Javascript::tag($src);
  133. $www = Chamilo::url();
  134. $result .= Javascript::tag_code($code);
  135. return $result;
  136. }
  137. /**
  138. * Returns a url for an action that the controller can process
  139. *
  140. * @param string $action
  141. * @param array $params
  142. * @return string
  143. */
  144. public function url($action, $params = array())
  145. {
  146. $url_params = Uri::course_params();
  147. if ($c_id = Request::get_c_id()) {
  148. $url_params[Request::PARAM_C_ID] = $c_id;
  149. }
  150. if ($id = Request::get_id()) {
  151. $url_params[Request::PARAM_ID] = $id;
  152. }
  153. if($session_id = Request::get_session_id()){
  154. $url_params[Request::PARAM_SESSION_ID] = $session_id;
  155. }
  156. $url_params[Request::PARAM_ACTION] = $action;
  157. foreach ($params as $key => $value) {
  158. $url_params[$key] = $value;
  159. }
  160. $result = Uri::url('/main/link/index.php', $url_params, false);
  161. return $result;
  162. }
  163. public function listing()
  164. {
  165. $c_id = Request::get_c_id();
  166. $session_id = Request::get_session_id();
  167. $root = (object) array();
  168. $root->c_id = $c_id;
  169. $root->id = 0;
  170. $root->session_id = $session_id;
  171. $links = LinkRepository::instance()->find_by_category($root);
  172. $repo = LinkCategory::repository();
  173. $categories = $repo->find_by_course($c_id, $session_id);
  174. //$data = compact('links', 'categories');
  175. $data = (object) array();
  176. $data->categories = $categories;
  177. $data->links = $links;
  178. $this->render('index', $data);
  179. }
  180. public function add_link()
  181. {
  182. if (!$this->is_allowed_to_edit()) {
  183. $this->forbidden();
  184. return;
  185. }
  186. $link = new Link();
  187. $link->id = 0;
  188. $link->c_id = Request::get_c_id();
  189. $link->session_id = Request::get_session_id();
  190. /**
  191. * @todo: ensure session_id is correctly defaulted
  192. */
  193. $action = $this->url(self::ACTION_ADD_LINK);
  194. $form = new LinkForm('link', 'post', $action);
  195. $form->init($link);
  196. if ($form->validate()) {
  197. $form->update_model();
  198. $repo = LinkRepository::instance();
  199. $success = $repo->save($link);
  200. $message = $success ? get_lang('LinkAdded') : get_lang('Error');
  201. $home = $this->url(self::ACTION_DEFAULT);
  202. Redirect::go($home);
  203. }
  204. global $interbreadcrumb;
  205. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddLink'));
  206. $data = (object) array();
  207. $data->form = $form;
  208. $this->render('edit_link', $data);
  209. }
  210. public function import_csv()
  211. {
  212. if (!$this->is_allowed_to_edit()) {
  213. $this->forbidden();
  214. return;
  215. }
  216. $action = $this->url(self::ACTION_IMPORT_CSV);
  217. $form = new UploadFileForm('import_csv', 'post', $action);
  218. $form->init();
  219. if ($form->validate()) {
  220. $file = $form->get_file();
  221. $path = $file['tmp_name'];
  222. $c_id = Request::get_c_id();
  223. $session_id = Request::get_session_id();
  224. $import = new ImportCsv($c_id, $session_id, $path);
  225. $import->run();
  226. //import_csvfile();
  227. $home = $this->url(self::ACTION_DEFAULT);
  228. Redirect::go($home);
  229. }
  230. $data = (object) array();
  231. $data->form = $form;
  232. $this->render('edit_link', $data);
  233. }
  234. public function export_csv()
  235. {
  236. $c_id = Request::get_c_id();
  237. $session_id = Request::get_session_id();
  238. $root = (object) array();
  239. $root->c_id = $c_id;
  240. $root->id = 0;
  241. $root->session_id = $session_id;
  242. $links = LinkRepository::instance()->find_by_category($root);
  243. $repo = LinkCategory::repository();
  244. $categories = $repo->find_by_course($c_id, $session_id);
  245. $temp = Chamilo::temp_file();
  246. $writer = \CsvWriter::create(new \FileWriter($temp));
  247. $headers = array();
  248. $headers[] = 'url';
  249. $headers[] = 'title';
  250. $headers[] = 'description';
  251. $headers[] = 'target';
  252. $headers[] = 'category_title';
  253. $headers[] = 'category_description';
  254. $writer->put($headers);
  255. foreach ($links as $link) {
  256. $data = array();
  257. $data[] = $link->url;
  258. $data[] = $link->title;
  259. $data[] = $link->description;
  260. $data[] = $link->target;
  261. $data[] = '';
  262. $data[] = '';
  263. $writer->put($data);
  264. }
  265. foreach ($categories as $category) {
  266. foreach ($category->links as $link) {
  267. $data = array();
  268. $data[] = $link->url;
  269. $data[] = $link->title;
  270. $data[] = $link->description;
  271. $data[] = $link->target;
  272. $data[] = $category->category_title;
  273. $data[] = $category->description;
  274. $writer->put($data);
  275. }
  276. }
  277. \DocumentManager :: file_send_for_download($temp, true, get_lang('Links').'.csv');
  278. }
  279. public function delete_link()
  280. {
  281. if (!$this->is_allowed_to_edit()) {
  282. $this->forbidden();
  283. return;
  284. }
  285. /**
  286. * See AjaxController
  287. */
  288. $this->missing();
  289. }
  290. public function edit_link()
  291. {
  292. if (!$this->is_allowed_to_edit()) {
  293. $this->forbidden();
  294. return;
  295. }
  296. $id = Request::get_id();
  297. $c_id = Request::get_c_id();
  298. $repo = LinkRepository::instance();
  299. $link = $repo->find_one_by_id($c_id, $id);
  300. $action = $this->url(self::ACTION_EDIT_LINK);
  301. $form = new LinkForm('link', 'post', $action);
  302. $form->init($link);
  303. if ($form->validate()) {
  304. $form->update_model();
  305. $success = $repo->save($link);
  306. $message = $success ? get_lang('LinkUpdated') : get_lang('Error');
  307. $home = $this->url(self::ACTION_DEFAULT);
  308. Redirect::go($home);
  309. }
  310. global $interbreadcrumb;
  311. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditLink'));
  312. $data = (object) array();
  313. $data->form = $form;
  314. $this->render('edit_link', $data);
  315. }
  316. public function make_visible()
  317. {
  318. if (!$this->is_allowed_to_edit()) {
  319. $this->forbidden();
  320. return;
  321. }
  322. /**
  323. * See AjaxController
  324. */
  325. $this->missing();
  326. }
  327. public function make_invisible()
  328. {
  329. if (!$this->is_allowed_to_edit()) {
  330. $this->forbidden();
  331. return;
  332. }
  333. /**
  334. * See AjaxController
  335. */
  336. $this->missing();
  337. }
  338. public function add_category()
  339. {
  340. if (!$this->is_allowed_to_edit()) {
  341. $this->forbidden();
  342. return;
  343. }
  344. $category = (object) array();
  345. $category->id = 0;
  346. $category->c_id = Request::get_c_id();
  347. $category->session_id = Request::get_session_id();
  348. $category->category_title = '';
  349. $category->description = '';
  350. $category->display_order = 0;
  351. $action = $this->url(self::ACTION_ADD_CATEGORY);
  352. $form = new CategoryForm('category', 'post', $action);
  353. $form->init($category);
  354. if ($form->validate()) {
  355. $form->update_model();
  356. $repo = LinkCategoryRepository::instance();
  357. $success = $repo->save($category);
  358. $message = $success ? get_lang('CategoryAdded') : get_lang('Error');
  359. $home = $this->url(self::ACTION_DEFAULT);
  360. Redirect::go($home);
  361. }
  362. global $interbreadcrumb;
  363. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddCategory'));
  364. $data = (object) array();
  365. $data->form = $form;
  366. $this->render('edit_category', $data);
  367. }
  368. public function edit_category()
  369. {
  370. if (!$this->is_allowed_to_edit()) {
  371. $this->forbidden();
  372. return;
  373. }
  374. $c_id = Request::get_c_id();
  375. $id = Request::get_id();
  376. $repo = LinkCategoryRepository::instance();
  377. $category = $repo->find_one_by_id($c_id, $id);
  378. $action = $this->url(self::ACTION_EDIT_CATEGORY);
  379. $form = new CategoryForm('category', 'post', $action);
  380. $form->init($category);
  381. if ($form->validate()) {
  382. $form->update_model();
  383. $repo = LinkCategoryRepository::instance();
  384. $success = $repo->save($category);
  385. $message = $success ? get_lang('CategorySaved') : get_lang('Error');
  386. $home = $this->url(self::ACTION_DEFAULT);
  387. Redirect::go($home);
  388. }
  389. global $interbreadcrumb;
  390. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditCategory'));
  391. $data = (object) array();
  392. $data->form = $form;
  393. $this->render('edit_category', $data);
  394. }
  395. public function delete_category()
  396. {
  397. if (!$this->is_allowed_to_edit()) {
  398. $this->forbidden();
  399. return;
  400. }
  401. $category = (object) array();
  402. $category->id = Request::get_id();
  403. $category->c_id = Request::get_c_id();
  404. $success = $repo = CategoryRepo::instance()->remove($category);
  405. $message = $success ? get_lang('CategoryRemoved') : get_lang('Error');
  406. $home = $this->url(self::ACTION_DEFAULT);
  407. Redirect::go($home);
  408. }
  409. public function go()
  410. {
  411. $id = Request::get_id();
  412. $c_id = Request::get_c_id();
  413. $repo = LinkRepository::instance();
  414. $link = $repo->find_one_by_id($c_id, $id);
  415. $url = $link->url;
  416. event_link($id);
  417. Header::cache_control('no-store, no-cache, must-revalidate');
  418. Header::pragma('no-cache');
  419. Redirect::go($url);
  420. }
  421. /**
  422. * Render a template using data. Adds a few common parameters to the data array.
  423. *
  424. * @see /main/template/default/course_description/
  425. * @param string $template
  426. * @param array $data
  427. */
  428. protected function render($template, $data)
  429. {
  430. $data = $data ? $data : (object) array();
  431. $_user = api_get_user_info();
  432. $session_id = Request::get_session_id();
  433. $data->session_image = api_get_session_image($session_id, $_user);
  434. $sec_token = Security::get_token();
  435. $data->sec_token = $sec_token;
  436. $context = Uri::course_params();
  437. $data->root = Uri::url('/main/link/index.php', $context);
  438. $data->session_id = $session_id;
  439. $data->c_id = Request::get_c_id();
  440. $data->is_allowed_to_edit = $this->is_allowed_to_edit();
  441. parent::render("link/$template.tpl", $data);
  442. }
  443. }