123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Course description's ajax controller class definition
- * @package chamilo.course_description
- */
- /**
- * Init
- */
- namespace CourseDescription;
- use \Display;
- use \Template;
- use \FormValidator;
- use \Security;
- use \Uri;
- use Header;
- /**
- * Ajax controller. Dispatch request and perform required action.
- *
- * - delete category/link
- *
- * Usage:
- *
- * $controller = AjaxController::instance();
- * $controller->run();
- *
- * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
- * @license /license.txt
- */
- class AjaxController extends \Controller
- {
- const ACTION_DELETE = 'delete';
- const ACTION_DELETE_BY_COURSE = 'delete_by_course';
- /**
- * Return the instance of the controller.
- *
- * @return \CourseDescription\AjaxController
- */
- public static function instance()
- {
- static $result = null;
- if (empty($result)) {
- $result = new self();
- }
- return $result;
- }
- protected function __construct()
- {
-
- }
- /**
- * Prepare the environment. Set up breadcrumps and raise tracking event.
- */
- protected function prolog()
- {
- event_access_tool(TOOL_COURSE_DESCRIPTION);
- }
- public function is_allowed_to_edit()
- {
- if (Request::is_student_view()) {
- return false;
- }
- $session_id = Request::get_session_id();
- if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
- return false;
- }
- if (!api_is_allowed_to_edit(false, true, true)) {
- return false;
- }
- return true;
- }
-
- public function authorize()
- {
- $authorize = api_protect_course_script();
- if (!$authorize) {
- return false;
- }
- $c_id = Request::get_c_id();
- if (empty($c_id)) {
- return false;
- }
- if (Request::is_student_view()) {
- return false;
- }
- if (!$this->is_allowed_to_edit()) {
- return false;
- }
- return true;
- }
- /**
- *
- */
- public function delete()
- {
- if (!$this->is_allowed_to_edit()) {
- $this->forbidden();
- return;
- }
- $description = (object) array();
- $description->c_id = Request::get_c_id();
- $description->id = Request::get_id();
- $success = CourseDescription::repository()->remove($description);
- $this->response($success);
- }
- /**
- *
- */
- public function delete_by_course()
- {
- if (!$this->is_allowed_to_edit()) {
- $this->forbidden();
- return;
- }
-
- $course = (object) array();
- $course->c_id = Request::get_c_id();
- $course->session_id = Request::get_session_id();
-
- $success = CourseDescription::repository()->remove_by_course($course);
- $this->response($success);
- }
- function forbidden()
- {
- $this->response(false, get_lang('YouAreNotAuthorized'));
- }
- public function unknown()
- {
- $this->response(false, get_lang('UnknownAction'));
- }
- /**
- * Action exists but implementation is missing.
- */
- public function missing()
- {
- $this->response(false, get_lang('NoImplementation'));
- }
- /**
- * Display a standard json responce.
- *
- * @param bool $success
- * @param string $message
- * @param object $data
- */
- public function response($success = false, $message = '', $data = null)
- {
- $message = trim($message);
- $response = (object) array();
- $response->success = $success;
- if ($message) {
- $response->message = Display::return_message($message, $success ? 'normal' : 'error');
- } else {
- $response->message = '';
- }
- $response->data = $data;
- $this->render_json($response);
- }
- }
|