1 <?php
2 defined('BASEPATH') OR exit('No direct script access allowed');
3
4 class User extends CI_Controller {
5 public function __construct()
6 {
7 parent::__construct();
8
9 $this->load->database();
10 $this->load->library('session');
11 /*cache control*/
12 $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
13 $this->output->set_header('Pragma: no-cache');
14
15 if (get_settings('allow_instructor') != 1){
16 redirect(site_url('home'), 'refresh');
17 }
18 }
19
20 public function index() {
21 if ($this->session->userdata('user_login') == true) {
22 $this->courses();
23 }else {
24 redirect(site_url('login'), 'refresh');
25 }
26 }
27
28 public function courses() {
29 if ($this->session->userdata('user_login') != true) {
30 redirect(site_url('login'), 'refresh');
31 }
32 $page_data['selected_category_id'] = isset($_GET['category_id']) ? $_GET['category_id'] : "all";
33 $page_data['selected_instructor_id'] = $this->session->userdata('user_id');
34 $page_data['selected_price'] = isset($_GET['price']) ? $_GET['price'] : "all";
35 $page_data['selected_status'] = isset($_GET['status']) ? $_GET['status'] : "all";
36 $page_data['courses'] = $this->crud_model->filter_course_for_backend($page_data['selected_category_id'], $page_data['selected_instructor_id'], $page_data['selected_price'], $page_data['selected_status']);
37 $page_data['page_name'] = 'courses';
38 $page_data['categories'] = $this->crud_model->get_categories();
39 $page_data['page_title'] = get_phrase('active_courses');
40 $this->load->view('backend/index', $page_data);
41 }
42
43 public function course_actions($param1 = "", $param2 = "") {
44 if ($this->session->userdata('user_login') != true) {
45 redirect(site_url('login'), 'refresh');
46 }
47
48 if ($param1 == "add") {
49 $this->crud_model->add_course();
50 redirect(site_url('user/courses'), 'refresh');
51
52 }
53 elseif ($param1 == "edit") {
54 $this->is_the_course_belongs_to_current_instructor($param2);
55 $this->crud_model->update_course($param2);
56 redirect(site_url('user/courses'), 'refresh');
57
58 }
59 elseif ($param1 == 'delete') {
60 $this->is_the_course_belongs_to_current_instructor($param2);
61 $this->crud_model->delete_course($param2);
62 redirect(site_url('user/courses'), 'refresh');
63 }
64 elseif ($param1 == 'draft') {
65 $this->is_the_course_belongs_to_current_instructor($param2);
66 $this->crud_model->change_course_status('draft', $param2);
67 redirect(site_url('user/courses'), 'refresh');
68 }
69 elseif ($param1 == 'publish') {
70 $this->is_the_course_belongs_to_current_instructor($param2);
71 $this->crud_model->change_course_status('pending', $param2);
72 redirect(site_url('user/courses'), 'refresh');
73 }
74 }
75
76 public function course_form($param1 = "", $param2 = "") {
77
78 if ($this->session->userdata('user_login') != true) {
79 redirect(site_url('login'), 'refresh');
80 }
81
82 if ($param1 == 'add_course') {
83 $page_data['languages'] = $this->get_all_languages();
84 $page_data['categories'] = $this->crud_model->get_categories();
85 $page_data['page_name'] = 'course_add';
86 $page_data['page_title'] = get_phrase('add_course');
87 $this->load->view('backend/index', $page_data);
88
89 }elseif ($param1 == 'course_edit') {
90 $this->is_the_course_belongs_to_current_instructor($param2);
91 $page_data['page_name'] = 'course_edit';
92 $page_data['course_id'] = $param2;
93 $page_data['page_title'] = get_phrase('edit_course');
94 $page_data['languages'] = $this->get_all_languages();
95 $page_data['categories'] = $this->crud_model->get_categories();
96 $this->load->view('backend/index', $page_data);
97 }
98 }
99
100 public function payment_settings($param1 = "") {
101 if ($this->session->userdata('user_login') != true) {
102 redirect(site_url('login'), 'refresh');
103 }
104
105 if ($param1 == 'paypal_settings') {
106 $this->user_model->update_instructor_paypal_settings($this->session->userdata('user_id'));
107 redirect(site_url('user/payment_settings'), 'refresh');
108 }
109 if ($param1 == 'stripe_settings') {
110 $this->user_model->update_instructor_stripe_settings($this->session->userdata('user_id'));
111 redirect(site_url('user/payment_settings'), 'refresh');
112 }
113
114 $page_data['page_name'] = 'payment_settings';
115 $page_data['page_title'] = get_phrase('payment_settings');
116 $this->load->view('backend/index', $page_data);
117 }
118
119 public function instructor_revenue($param1 = "") {
120 if ($this->session->userdata('user_login') != true) {
121 redirect(site_url('login'), 'refresh');
122 }
123
124 if ($param1 != "") {
125 $date_range = $this->input->post('date_range');
126 $date_range = explode(" - ", $date_range);
127 $page_data['timestamp_start'] = strtotime($date_range[0]);
128 $page_data['timestamp_end'] = strtotime($date_range[1]);
129 }else {
130 $page_data['timestamp_start'] = strtotime('-29 days', time());
131 $page_data['timestamp_end'] = strtotime(date("m/d/Y"));
132 }
133 $page_data['payment_history'] = $this->crud_model->get_instructor_revenue($page_data['timestamp_start'], $page_data['timestamp_end']);
134 $page_data['page_name'] = 'instructor_revenue';
135 $page_data['page_title'] = get_phrase('instructor_revenue');
136 $this->load->view('backend/index', $page_data);
137 }
138
139 function get_all_languages() {
140 $language_files = array();
141 $all_files = $this->get_list_of_language_files();
142 foreach ($all_files as $file) {
143 $info = pathinfo($file);
144 if( isset($info['extension']) && strtolower($info['extension']) == 'json') {
145 $file_name = explode('.json', $info['basename']);
146 array_push($language_files, $file_name[0]);
147 }
148 }
149 return $language_files;
150 }
151
152 function get_list_of_language_files($dir = APPPATH.'/language', &$results = array()) {
153 $files = scandir($dir);
154 foreach($files as $key => $value){
155 $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
156 if(!is_dir($path)) {
157 $results[] = $path;
158 } else if($value != "." && $value != "..") {
159 $this->get_list_of_directories_and_files($path, $results);
160 $results[] = $path;
161 }
162 }
163 return $results;
164 }
165
166 function get_list_of_directories_and_files($dir = APPPATH, &$results = array()) {
167 $files = scandir($dir);
168 foreach($files as $key => $value){
169 $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
170 if(!is_dir($path)) {
171 $results[] = $path;
172 } else if($value != "." && $value != "..") {
173 $this->get_list_of_directories_and_files($path, $results);
174 $results[] = $path;
175 }
176 }
177 return $results;
178 }
179
180 public function preview($course_id = '') {
181 if ($this->session->userdata('user_login') != 1)
182 redirect(site_url('login'), 'refresh');
183
184 $this->is_the_course_belongs_to_current_instructor($course_id);
185 if ($course_id > 0) {
186 $courses = $this->crud_model->get_course_by_id($course_id);
187 if ($courses->num_rows() > 0) {
188 $course_details = $courses->row_array();
189 redirect(site_url('home/lesson/'.slugify($course_details['title']).'/'.$course_details['id']), 'refresh');
190 }
191 }
192 redirect(site_url('user/courses'), 'refresh');
193 }
194
195 public function sections($param1 = "", $param2 = "", $param3 = "") {
196 if ($this->session->userdata('user_login') != true) {
197 redirect(site_url('login'), 'refresh');
198 }
199
200 if ($param2 == 'add') {
201 $this->crud_model->add_section($param1);
202 $this->session->set_flashdata('flash_message', get_phrase('section_has_been_added_successfully'));
203 }
204 elseif ($param2 == 'edit') {
205 $this->crud_model->edit_section($param3);
206 $this->session->set_flashdata('flash_message', get_phrase('section_has_been_updated_successfully'));
207 }
208 elseif ($param2 == 'delete') {
209 $this->crud_model->delete_section($param1, $param3);
210 $this->session->set_flashdata('flash_message', get_phrase('section_has_been_deleted_successfully'));
211 }
212 redirect(site_url('user/course_form/course_edit/'.$param1));
213 }
214
215 public function lessons($course_id = "", $param1 = "", $param2 = "") {
216 if ($this->session->userdata('user_login') != true) {
217 redirect(site_url('login'), 'refresh');
218 }
219 if ($param1 == 'add') {
220 $this->crud_model->add_lesson();
221 $this->session->set_flashdata('flash_message', get_phrase('lesson_has_been_added_successfully'));
222 redirect('user/course_form/course_edit/'.$course_id);
223 }
224 elseif ($param1 == 'edit') {
225 $this->crud_model->edit_lesson($param2);
226 $this->session->set_flashdata('flash_message', get_phrase('lesson_has_been_updated_successfully'));
227 redirect('user/course_form/course_edit/'.$course_id);
228 }
229 elseif ($param1 == 'delete') {
230 $this->crud_model->delete_lesson($param2);
231 $this->session->set_flashdata('flash_message', get_phrase('lesson_has_been_deleted_successfully'));
232 redirect('user/course_form/course_edit/'.$course_id);
233 }
234 elseif ($param1 == 'filter') {
235 redirect('user/lessons/'.$this->input->post('course_id'));
236 }
237 $page_data['page_name'] = 'lessons';
238 $page_data['lessons'] = $this->crud_model->get_lessons('course', $course_id);
239 $page_data['course_id'] = $course_id;
240 $page_data['page_title'] = get_phrase('lessons');
241 $this->load->view('backend/index', $page_data);
242 }
243
244 // This function checks if this course belongs to current logged in instructor
245 function is_the_course_belongs_to_current_instructor($course_id) {
246 $course_details = $this->crud_model->get_course_by_id($course_id)->row_array();
247 if ($course_details['user_id'] != $this->session->userdata('user_id')) {
248 $this->session->set_flashdata('error_message', get_phrase('you_do_not_have_right_to_access_this_course'));
249 redirect(site_url('user/courses'), 'refresh');
250 }
251 }
252
253 // Manage Quizes
254 public function quizes($course_id = "", $action = "", $quiz_id = "") {
255 if ($this->session->userdata('user_login') != true) {
256 redirect(site_url('login'), 'refresh');
257 }
258
259 if ($action == 'add') {
260 $this->crud_model->add_quiz($course_id);
261 $this->session->set_flashdata('flash_message', get_phrase('quiz_has_been_added_successfully'));
262 }
263 elseif ($action == 'edit') {
264 $this->crud_model->edit_quiz($quiz_id);
265 $this->session->set_flashdata('flash_message', get_phrase('quiz_has_been_updated_successfully'));
266 }
267 elseif ($action == 'delete') {
268 $this->crud_model->delete_section($course_id, $quiz_id);
269 $this->session->set_flashdata('flash_message', get_phrase('quiz_has_been_deleted_successfully'));
270 }
271 redirect(site_url('user/course_form/course_edit/'.$course_id));
272 }
273
274 // Manage Quize Questions
275 public function quiz_questions($quiz_id = "", $action = "", $question_id = "") {
276 if ($this->session->userdata('user_login') != true) {
277 redirect(site_url('login'), 'refresh');
278 }
279 $quiz_details = $this->crud_model->get_lessons('lesson', $quiz_id)->row_array();
280
281 if ($action == 'add') {
282 $response = $this->crud_model->add_quiz_questions($quiz_id);
283 echo $response;
284 }
285
286 elseif ($action == 'edit') {
287 $response = $this->crud_model->update_quiz_questions($question_id);
288 echo $response;
289 }
290
291 elseif ($action == 'delete') {
292 $response = $this->crud_model->delete_quiz_question($question_id);
293 $this->session->set_flashdata('flash_message', get_phrase('question_has_been_deleted'));
294 redirect(site_url('user/course_form/course_edit/'.$quiz_details['course_id']));
295 }
296 }
297
298 function manage_profile() {
299 redirect(site_url('home/profile/user_profile'), 'refresh');
300 }
301
302 function invoice($payment_id = "") {
303 if ($this->session->userdata('user_login') != true) {
304 redirect(site_url('login'), 'refresh');
305 }
306 $page_data['page_name'] = 'invoice';
307 $page_data['payment_details'] = $this->crud_model->get_payment_details_by_id($payment_id);
308 $page_data['page_title'] = get_phrase('invoice');
309 $this->load->view('backend/index', $page_data);
310 }
311 // Ajax Portion
312 public function ajax_get_video_details() {
313 $video_details = $this->video_model->getVideoDetails($_POST['video_url']);
314 echo $video_details['duration'];
315 }
316
317 // this function is responsible for managing multiple choice question
318 function manage_multiple_choices_options() {
319 $page_data['number_of_options'] = $this->input->post('number_of_options');
320 $this->load->view('backend/user/manage_multiple_choices_options', $page_data);
321 }
322
323 public function ajax_sort_section() {
324 $section_json = $this->input->post('itemJSON');
325 $this->crud_model->sort_section($section_json);
326 }
327 public function ajax_sort_lesson() {
328 $lesson_json = $this->input->post('itemJSON');
329 $this->crud_model->sort_lesson($lesson_json);
330 }
331 public function ajax_sort_question() {
332 $question_json = $this->input->post('itemJSON');
333 $this->crud_model->sort_question($question_json);
334 }
335 }