controller.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace Glossary;
  3. use \ChamiloSession as Session;
  4. use \Display;
  5. use \Template;
  6. use \FormValidator;
  7. use \Security;
  8. use Uri;
  9. use Redirect;
  10. use Chamilo;
  11. use Javascript;
  12. /**
  13. * Controller for glossary. Dispatch request and peform required action.
  14. *
  15. * - list glossary entries for course
  16. * - add/edit glossary entry
  17. * - change view from table to details
  18. *
  19. * Usage:
  20. *
  21. * $controller = Controller::instance();
  22. * $controller->run();
  23. *
  24. * @package chamilo.course_description
  25. * @author Christian Fasanando <christian1827@gmail.com>
  26. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  27. * @license see /license.txt
  28. */
  29. class Controller extends \Controller
  30. {
  31. const ACTION_ADD = 'add';
  32. const ACTION_EDIT = 'edit';
  33. const ACTION_DELETE = 'delete';
  34. const ACTION_INDEX = 'index';
  35. const ACTION_DEFAULT = 'index';
  36. const ACTION_EXPORT_CSV = 'export_csv';
  37. const ACTION_IMPORT_CSV = 'import_csv';
  38. /**
  39. * Return the instance of the controller.
  40. *
  41. * @return \Glossary\Controller
  42. */
  43. public static function instance()
  44. {
  45. static $result = null;
  46. if (empty($result)) {
  47. $result = new self(Access::instance());
  48. }
  49. return $result;
  50. }
  51. /**
  52. * Action to perform.
  53. * Returns the request parameter.
  54. *
  55. * @return string
  56. */
  57. public function get_action()
  58. {
  59. if (Request::is_student_view()) {
  60. return self::ACTION_INDEX;
  61. }
  62. $result = parent::get_action();
  63. $result = $result ? $result : self::ACTION_DEFAULT;
  64. return $result;
  65. }
  66. public function is_allowed_to_edit()
  67. {
  68. return $this->access()->can_edit();
  69. }
  70. /**
  71. * Prepare the environment. Set up breadcrumps and raise tracking event.
  72. */
  73. protected function prolog()
  74. {
  75. global $interbreadcrumb;
  76. $interbreadcrumb = array();
  77. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Glossary'));
  78. global $this_section;
  79. $this_section = SECTION_COURSES;
  80. global $current_course_tool;
  81. $current_course_tool = TOOL_GLOSSARY;
  82. // Tracking
  83. event_access_tool(TOOL_GLOSSARY);
  84. }
  85. /**
  86. * Returns a url for an action that the controller can process
  87. *
  88. * @param string $action
  89. * @param array $params
  90. * @return string
  91. */
  92. public function url($action = '', $params = array())
  93. {
  94. $url_params = Uri::course_params();
  95. if ($c_id = Request::get_c_id()) {
  96. $url_params[Request::PARAM_C_ID] = $c_id;
  97. }
  98. if ($id = Request::get_id()) {
  99. $url_params[Request::PARAM_ID] = $id;
  100. }
  101. if ($session_id = Request::get_session_id()) {
  102. $url_params[Request::PARAM_SESSION_ID] = $session_id;
  103. }
  104. if ($action) {
  105. $url_params[Request::PARAM_ACTION] = $action;
  106. }
  107. foreach ($params as $key => $value) {
  108. $url_params[$key] = $value;
  109. }
  110. $result = Uri::url('/main/glossary/index.php', $url_params, false);
  111. return $result;
  112. }
  113. /**
  114. * List course descriptions.
  115. *
  116. * @param array messages
  117. */
  118. public function index()
  119. {
  120. $course = Request::get_course_key();
  121. $repo = Glossary::repository();
  122. $items = $repo->find_by_course($course);
  123. $view = Request::get_view();
  124. Session::write(Request::PARAM_VIEW, $view);
  125. $data = (object) array();
  126. $data->items = $items;
  127. $data->sort = $sort;
  128. $data->view = $view;
  129. $this->render('index', $data);
  130. }
  131. /**
  132. * Performs the edit action.
  133. */
  134. public function edit()
  135. {
  136. if (!$this->is_allowed_to_edit()) {
  137. $this->forbidden();
  138. return;
  139. }
  140. $id = Request::get_id();
  141. $c_id = Request::get_c_id();
  142. $repo = Glossary::repository();
  143. $item = $repo->find_one_by_id($c_id, $id);
  144. $action = $this->url(self::ACTION_EDIT);
  145. $form = GlossaryForm::create($action, $item);
  146. if ($form->validate()) {
  147. $success = $repo->save($item);
  148. $message = $success ? get_lang('GlossaryTermUpdated') : get_lang('Error');
  149. $home = $this->url(self::ACTION_DEFAULT);
  150. Redirect::go($home);
  151. }
  152. $data = (object) array();
  153. $data->form = $form;
  154. $this->render('edit', $data);
  155. }
  156. /**
  157. * Perform the add action
  158. */
  159. public function add()
  160. {
  161. if (!$this->is_allowed_to_edit()) {
  162. $this->forbidden();
  163. return;
  164. }
  165. $c_id = Request::get_c_id();
  166. $session_id = Request::get_session_id();
  167. $item = Glossary::create();
  168. $item->c_id = $c_id;
  169. $item->session_id = $session_id;
  170. $action = $this->url(self::ACTION_ADD);
  171. $form = GlossaryForm::create($action, $item);
  172. if ($form->validate()) {
  173. $repo = Glossary::repository();
  174. $success = $repo->save($item);
  175. $message = $success ? get_lang('GlossaryAdded') : get_lang('Error');
  176. $home = $this->url();
  177. Redirect::go($home);
  178. }
  179. $data = (object) array();
  180. $data->type = $type;
  181. $data->form = $form;
  182. $this->render('edit', $data);
  183. }
  184. /**
  185. * Performs the delete action.
  186. *
  187. * @see AjaxController
  188. */
  189. public function delete()
  190. {
  191. if (!$this->is_allowed_to_edit()) {
  192. $this->forbidden();
  193. return;
  194. }
  195. $this->missing();
  196. }
  197. public function export_csv()
  198. {
  199. $course = Request::get_course_key();
  200. $items = Glossary::repository()->find_by_course($course);
  201. $writer = CsvWriter::create();
  202. $writer->add($items);
  203. $path = $writer->get_path();
  204. \DocumentManager :: file_send_for_download($path, true, get_lang('Glossary') . '.csv');
  205. }
  206. public function import_csv()
  207. {
  208. if (!$this->is_allowed_to_edit()) {
  209. $this->forbidden();
  210. return;
  211. }
  212. $action = $this->url(self::ACTION_IMPORT_CSV);
  213. $form = UploadFileForm::create($action);
  214. $form->init();
  215. if ($form->validate()) {
  216. $delete_all = $form->get_delete_all();
  217. if ($delete_all) {
  218. $course = Request::get_course_key();
  219. $repo = Glossary::repository();
  220. $repo->remove_by_course($course);
  221. }
  222. $file = $form->get_file();
  223. $path = $file->tmp_name;
  224. $reader = new CsvReader($path);
  225. $items = $reader->get_items();
  226. $course = Request::get_course_key();
  227. $import = new CourseImport($course);
  228. $import->add($items);
  229. $home = $this->url(self::ACTION_DEFAULT);
  230. Redirect::go($home);
  231. }
  232. $data = (object) array();
  233. $data->form = $form;
  234. $this->render('upload', $data);
  235. }
  236. /**
  237. * Render a template using data. Adds a few common parameters to the data array.
  238. *
  239. * @see /main/template/default/course_description/
  240. * @param string $template
  241. * @param array $data
  242. */
  243. protected function render($template, $data)
  244. {
  245. $data = $data ? $data : (object) array();
  246. $_user = api_get_user_info();
  247. $session_id = Request::get_session_id();
  248. $data->session_image = api_get_session_image($session_id, $_user);
  249. $data->sec_token = $this->access()->get_token();
  250. ;
  251. $data->root = $this->url('');
  252. $data->session_id = $session_id;
  253. $data->c_id = Request::get_c_id();
  254. $data->is_allowed_to_edit = $this->is_allowed_to_edit();
  255. parent::render("glossary/$template.tpl", $data);
  256. }
  257. }