controller.class.php 7.9 KB

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