controller.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace Notebook;
  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 notebook. Dispatch request and peform required action.
  14. *
  15. * - list notes for course
  16. * - add/edit notes 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 \Noteboook\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('Notebook'));
  78. global $this_section;
  79. $this_section = SECTION_COURSES;
  80. global $current_course_tool;
  81. $current_course_tool = TOOL_NOTEBOOK;
  82. // Tracking
  83. event_access_tool(TOOL_NOTEBOOK);
  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/notebook/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. $user_id = api_get_user_id();
  122. $column = Request::get_sort_column();
  123. $direction = Request::get_sort_direction();
  124. $order_by = $column ? "$column $direction" : '';
  125. $repo = Notebook::repository();
  126. $items = $repo->find_by_course_and_user($course, $user_id, $order_by);
  127. $data = (object) array();
  128. $data->items = $items;
  129. $data->sort = $sort;
  130. $data->sort_direction = $direction;
  131. $data->sort_column = $column;
  132. $this->render('index', $data);
  133. }
  134. /**
  135. * Performs the edit action.
  136. */
  137. public function edit()
  138. {
  139. if (!$this->is_allowed_to_edit()) {
  140. $this->forbidden();
  141. return;
  142. }
  143. $id = Request::get_id();
  144. $c_id = Request::get_c_id();
  145. $repo = Notebook::repository();
  146. $item = $repo->find_one_by_id($c_id, $id);
  147. $action = $this->url(self::ACTION_EDIT);
  148. $form = NotebookForm::create($action, $item);
  149. if ($form->validate()) {
  150. $success = $repo->save($item);
  151. $message = $success ? get_lang('NotebookUpdated') : get_lang('Error');
  152. $home = $this->url(self::ACTION_DEFAULT);
  153. Redirect::go($home);
  154. }
  155. $data = (object) array();
  156. $data->form = $form;
  157. $this->render('edit', $data);
  158. }
  159. /**
  160. * Perform the add action
  161. */
  162. public function add()
  163. {
  164. if (!$this->is_allowed_to_edit()) {
  165. $this->forbidden();
  166. return;
  167. }
  168. $c_id = Request::get_c_id();
  169. $session_id = Request::get_session_id();
  170. $item = Notebook::create();
  171. $item->c_id = $c_id;
  172. $item->session_id = $session_id;
  173. $action = $this->url(self::ACTION_ADD);
  174. $form = NotebookForm::create($action, $item);
  175. if ($form->validate()) {
  176. $repo = Notebook::repository();
  177. $success = $repo->save($item);
  178. $message = $success ? get_lang('NotebookAdded') : get_lang('Error');
  179. $home = $this->url();
  180. Redirect::go($home);
  181. }
  182. $data = (object) array();
  183. $data->type = $type;
  184. $data->form = $form;
  185. $this->render('edit', $data);
  186. }
  187. /**
  188. * Performs the delete action.
  189. *
  190. * @see AjaxController
  191. */
  192. public function delete()
  193. {
  194. if (!$this->is_allowed_to_edit()) {
  195. $this->forbidden();
  196. return;
  197. }
  198. $this->missing();
  199. }
  200. public function export_csv()
  201. {
  202. $course = Request::get_course_key();
  203. $items = Notebook::repository()->find_by_course($course);
  204. $writer = CsvWriter::create();
  205. $writer->add($items);
  206. $path = $writer->get_path();
  207. \DocumentManager :: file_send_for_download($path, true, get_lang('Notebook') . '.csv');
  208. }
  209. public function import_csv()
  210. {
  211. if (!$this->is_allowed_to_edit()) {
  212. $this->forbidden();
  213. return;
  214. }
  215. $action = $this->url(self::ACTION_IMPORT_CSV);
  216. $form = UploadFileForm::create($action);
  217. $form->init();
  218. if ($form->validate()) {
  219. // $delete_all = $form->get_delete_all();
  220. // if ($delete_all) {
  221. // $course = Request::get_course_key();
  222. // $repo = Notebook::repository();
  223. // $repo->remove_by_course($course);
  224. // }
  225. $file = $form->get_file();
  226. $path = $file->tmp_name;
  227. $reader = new CsvReader($path);
  228. $items = $reader->get_items();
  229. $course = Request::get_course_key();
  230. $import = new CourseImport($course);
  231. $import->add($items);
  232. $home = $this->url(self::ACTION_DEFAULT);
  233. Redirect::go($home);
  234. }
  235. $data = (object) array();
  236. $data->form = $form;
  237. $this->render('upload', $data);
  238. }
  239. /**
  240. * Render a template using data. Adds a few common parameters to the data array.
  241. *
  242. * @see /main/template/default/course_description/
  243. * @param string $template
  244. * @param array $data
  245. */
  246. protected function render($template, $data)
  247. {
  248. $data = $data ? $data : (object) array();
  249. $_user = api_get_user_info();
  250. $session_id = Request::get_session_id();
  251. $data->session_image = api_get_session_image($session_id, $_user);
  252. $data->sec_token = $this->access()->get_token();
  253. $data->root = $this->url('');
  254. $data->session_id = $session_id;
  255. $data->c_id = Request::get_c_id();
  256. $data->is_allowed_to_edit = $this->is_allowed_to_edit();
  257. parent::render("notebook/$template.tpl", $data);
  258. }
  259. }