controller.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace CourseDescription;
  3. use \Display;
  4. use \Template;
  5. use \FormValidator;
  6. use \Security;
  7. use Uri;
  8. use Redirect;
  9. use Chamilo;
  10. use Javascript;
  11. /**
  12. * Controller for course description. Dispatch request and peform required action.
  13. *
  14. * - list course description for course
  15. * - add a new course description to a course/session
  16. * - edit a course session
  17. * - delete a course session
  18. *
  19. * Usage:
  20. *
  21. * $controller = CourseDescriptionController::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_LISTING = 'listing';
  35. const ACTION_DEFAULT = 'listing';
  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 CourseDescriptionController
  42. */
  43. public static function instance()
  44. {
  45. static $result = null;
  46. if (empty($result)) {
  47. $result = new self();
  48. }
  49. return $result;
  50. }
  51. protected function __construct()
  52. {
  53. }
  54. /**
  55. * Action to perform.
  56. * Returns the request parameter.
  57. *
  58. * @return string
  59. */
  60. public function get_action()
  61. {
  62. if (Request::is_student_view()) {
  63. return self::ACTION_LISTING;
  64. }
  65. $result = parent::get_action();
  66. $result = $result ? $result : self::ACTION_DEFAULT;
  67. return $result;
  68. }
  69. public function is_allowed_to_edit()
  70. {
  71. if (Request::is_student_view()) {
  72. return false;
  73. }
  74. $session_id = Request::get_session_id();
  75. if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  76. return false;
  77. }
  78. if (!api_is_allowed_to_edit(false, true, true)) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. /**
  84. * Whether the call is authorized or not.
  85. *
  86. * @return boolean
  87. */
  88. public function authorize()
  89. {
  90. $authorize = api_protect_course_script(true);
  91. if (!$authorize) {
  92. return false;
  93. }
  94. $c_id = Request::get_c_id();
  95. if (empty($c_id)) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Prepare the environment. Set up breadcrumps and raise tracking event.
  102. */
  103. protected function prolog()
  104. {
  105. global $interbreadcrumb;
  106. $interbreadcrumb = array();
  107. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('CourseProgram'));
  108. $type_id = Request::get_description_type();
  109. $type = CourseDescriptionType::repository()->find_one_by_id($type_id);
  110. if ($type) {
  111. $interbreadcrumb[] = array('url' => '#', 'name' => $type->get_title());
  112. }
  113. global $this_section;
  114. $this_section = SECTION_COURSES;
  115. global $current_course_tool;
  116. $current_course_tool = TOOL_COURSE_DESCRIPTION;
  117. // Tracking
  118. event_access_tool(TOOL_COURSE_DESCRIPTION);
  119. }
  120. /**
  121. * Javascript used by the controller
  122. *
  123. * @return string
  124. */
  125. public function javascript()
  126. {
  127. $src = Chamilo::url('/main/course_description/resources/js/main.js');
  128. $result = Javascript::tag($src);
  129. $www = Chamilo::url();
  130. $code = "var www = '$www';\n";
  131. $result .= Javascript::tag_code($code);
  132. return $result;
  133. }
  134. /**
  135. * Returns a url for an action that the controller can process
  136. *
  137. * @param string $action
  138. * @param array $params
  139. * @return string
  140. */
  141. public function url($action = '', $params = array())
  142. {
  143. $url_params = Uri::course_params();
  144. if ($c_id = Request::get_c_id()) {
  145. $url_params[Request::PARAM_C_ID] = $c_id;
  146. }
  147. if ($id = Request::get_id()) {
  148. $url_params[Request::PARAM_ID] = $id;
  149. }
  150. if ($session_id = Request::get_session_id()) {
  151. $url_params[Request::PARAM_SESSION_ID] = $session_id;
  152. }
  153. $url_params[Request::PARAM_ACTION] = $action;
  154. foreach ($params as $key => $value) {
  155. $url_params[$key] = $value;
  156. }
  157. $result = Uri::url('/main/course_description/index.php', $url_params, false);
  158. return $result;
  159. }
  160. /**
  161. * List course descriptions.
  162. *
  163. * @param array messages
  164. */
  165. public function listing()
  166. {
  167. $course = (object) array();
  168. $course->c_id = Request::get_c_id();
  169. $course->session_id = Request::get_session_id();
  170. $repo = CourseDescription::repository();
  171. $descriptions = $repo->find_by_course($course);
  172. $data = (object) array();
  173. $data->descriptions = $descriptions;
  174. $this->render('index', $data);
  175. }
  176. /**
  177. * Performs the edit action.
  178. */
  179. public function edit()
  180. {
  181. if (!$this->is_allowed_to_edit()) {
  182. $this->forbidden();
  183. return;
  184. }
  185. $id = Request::get_id();
  186. $c_id = Request::get_c_id();
  187. $repo = CourseDescription::repository();
  188. $description = $repo->find_one_by_id($c_id, $id);
  189. $action = $this->url(self::ACTION_EDIT);
  190. $form = CourseDescriptionForm::create($action, $description);
  191. if ($form->validate()) {
  192. $success = $repo->save($description);
  193. $message = $success ? get_lang('DescriptionUpdated') : get_lang('Error');
  194. $home = $this->url(self::ACTION_DEFAULT);
  195. Redirect::go($home);
  196. }
  197. $data = (object) array();
  198. $data->form = $form;
  199. $this->render('edit', $data);
  200. }
  201. /**
  202. * Perform the add action
  203. */
  204. public function add()
  205. {
  206. if (!$this->is_allowed_to_edit()) {
  207. $this->forbidden();
  208. return;
  209. }
  210. $type_id = Request::get_description_type();
  211. $type = CourseDescriptionType::repository()->find_one_by_id($type_id);
  212. $c_id = Request::get_c_id();
  213. $session_id = Request::get_session_id();
  214. if (empty($type)) {
  215. $home = $this->url(self::ACTION_DEFAULT);
  216. Redirect::go($home);
  217. }
  218. $description = $type->create();
  219. $description->c_id = $c_id;
  220. $description->session_id = $session_id;
  221. $params = array();
  222. $params[Request::PARAM_DESCRIPTION_TYPE] = $type_id;
  223. $action = $this->url(self::ACTION_ADD, $params);
  224. $form = CourseDescriptionForm::create($action, $description);
  225. if ($form->validate()) {
  226. $repo = CourseDescription::repository();
  227. $success = $repo->save($description);
  228. $message = $success ? get_lang('CourseDescriptionAdded') : get_lang('Error');
  229. $home = $this->url();
  230. Redirect::go($home);
  231. }
  232. //$is_valid = !empty($type) && !empty($c_id) && !empty($title) && !empty($content) && Security::check_token();
  233. $data = (object) array();
  234. $data->type = $type;
  235. $data->form = $form;
  236. $this->render('edit', $data);
  237. }
  238. /**
  239. * Performs the delete action.
  240. *
  241. * @todo: could be worth to require a security token in the url and check it. Currently confirmation is done through javascript confirmation only.
  242. */
  243. public function delete()
  244. {
  245. if (!$this->is_allowed_to_edit()) {
  246. $this->forbidden();
  247. return;
  248. }
  249. // $is_valid = Security::check_token();
  250. // if (!$is_valid) {
  251. // $this->listing();
  252. // return false;
  253. // }
  254. $description->c_id = Request::get_c_id();
  255. $description->id = Request::get_id();
  256. $repo = CourseDescription::repository();
  257. $success = $repo->remove($description);
  258. $message = $success ? get_lang('CourseDescriptionDeleted') : get_lang('Error');
  259. $home = $this->url();
  260. Redirect::go($home);
  261. }
  262. public function export_csv()
  263. {
  264. $c_id = Request::get_c_id();
  265. $session_id = Request::get_session_id();
  266. $course = (object) array();
  267. $course->c_id = $c_id;
  268. $course->session_id = $session_id;
  269. $descriptions = CourseDescription::repository()->find_by_course($course);
  270. $writer = new CsvWriter();
  271. $writer->add($descriptions);
  272. $path = $writer->get_path();
  273. \DocumentManager :: file_send_for_download($path, true, get_lang('CourseDescriptions') . '.csv');
  274. }
  275. public function import_csv()
  276. {
  277. if (!$this->is_allowed_to_edit()) {
  278. $this->forbidden();
  279. return;
  280. }
  281. $action = $this->url(self::ACTION_IMPORT_CSV);
  282. $form = new UploadFileForm('import_csv', 'post', $action);
  283. $form->init();
  284. if ($form->validate()) {
  285. $file = $form->get_file();
  286. $path = $file->tmp_name;
  287. $reader = new CsvReader($path);
  288. $descriptions = $reader->get_items();
  289. $c_id = Request::get_c_id();
  290. $session_id = Request::get_session_id();
  291. $course = (object) array();
  292. $course->c_id = $c_id;
  293. $course->session_id = $session_id;
  294. $import = new CourseImport($course);
  295. $import->add($descriptions);
  296. $home = $this->url(self::ACTION_DEFAULT);
  297. Redirect::go($home);
  298. }
  299. $data = (object) array();
  300. $data->form = $form;
  301. $this->render('upload', $data);
  302. }
  303. /**
  304. * Render a template using data. Adds a few common parameters to the data array.
  305. *
  306. * @see /main/template/default/course_description/
  307. * @param string $template
  308. * @param array $data
  309. */
  310. protected function render($template, $data)
  311. {
  312. $data = $data ? $data : (object) array();
  313. $_user = api_get_user_info();
  314. $session_id = Request::get_session_id();
  315. $data->session_image = api_get_session_image($session_id, $_user);
  316. $sec_token = Security::get_token();
  317. $data->sec_token = $sec_token;
  318. $data->root = $this->url('');
  319. $data->types = CourseDescriptionType::repository()->all();
  320. $data->session_id = $session_id;
  321. $data->c_id = Request::get_c_id();
  322. $data->is_allowed_to_edit = $this->is_allowed_to_edit();
  323. parent::render("course_description/$template.tpl", $data);
  324. }
  325. }