controller.class.php 14 KB

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