PluginProbe
Tutor LMS – eLearning and online course solution / 1.0.7
Tutor LMS – eLearning and online course solution v1.0.7
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / init.php
init.php
430 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 class init{
8 public $version = TUTOR_VERSION;
9 public $path;
10 public $url;
11 public $basename;
12
13 //Components
14 public $utils;
15 public $admin;
16 public $ajax;
17 public $options;
18 public $shortcode;
19
20 private $post_types;
21 private $assets;
22 private $course;
23 private $lesson;
24 private $rewrite_rules;
25 private $template;
26 private $instructor;
27 private $student;
28 private $q_and_a;
29 private $quiz;
30 private $question;
31 private $tools;
32 private $user;
33 private $theme_compatibility;
34 private $gutenberg;
35
36 function __construct() {
37
38 $this->path = plugin_dir_path(TUTOR_FILE);
39 $this->url = plugin_dir_url(TUTOR_FILE);
40 $this->basename = plugin_basename(TUTOR_FILE);
41
42 /**
43 * Include Files
44 */
45 add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
46
47 /**
48 * Loading Autoloader
49 */
50
51 spl_autoload_register(array($this, 'loader'));
52
53 do_action('tutor_before_load');
54
55 $this->post_types = new Post_types();
56 $this->assets = new Assets();
57 $this->admin = new Admin();
58 $this->ajax = new Ajax();
59 $this->options = new Options();
60 $this->shortcode = new Shortcode();
61 $this->course = new Course();
62 $this->lesson = new Lesson();
63 $this->rewrite_rules = new Rewrite_Rules();
64 $this->template = new Template();
65 $this->instructor = new Instructor();
66 $this->student = new Student();
67 $this->q_and_a = new Q_and_A();
68 $this->quiz = new Quiz();
69 $this->question = new Question();
70 $this->tools = new Tools();
71 $this->user = new User();
72 $this->theme_compatibility = new Theme_Compatibility();
73 $this->gutenberg = new Gutenberg();
74
75 do_action('tutor_loaded');
76 }
77 /**
78 * @param $className
79 *
80 * Auto Load class and the files
81 */
82 private function loader($className) {
83 if ( ! class_exists($className)){
84 $className = preg_replace(
85 array('/([a-z])([A-Z])/', '/\\\/'),
86 array('$1-$2', DIRECTORY_SEPARATOR),
87 $className
88 );
89
90 $className = str_replace('TUTOR/', 'classes/', $className);
91 $file_name = $this->path.$className.'.php';
92
93 if (file_exists($file_name) ) {
94 require_once $file_name;
95 }
96 }
97 }
98
99 public function include_template_functions(){
100 include tutor()->path.'includes/tutor-template-functions.php';
101 include tutor()->path.'includes/tutor-template-hook.php';
102 }
103
104 //Run the TUTOR right now
105 public function run(){
106 do_action('tutor_before_run');
107
108 register_activation_hook( TUTOR_FILE, array( $this, 'tutor_activate' ) );
109 register_deactivation_hook(TUTOR_FILE, array($this, 'tutor_deactivation'));
110
111 do_action('tutor_after_run');
112 }
113
114 /**
115 * Do some task during plugin activation
116 */
117 public function tutor_activate(){
118 $version = get_option('tutor_version');
119 //Save Option
120 if ( ! $version){
121 //Create Database
122 $this->create_database();
123
124 $options = self::default_options();
125 update_option('tutor_option', $options);
126
127 //Rewrite Flush
128 update_option('required_rewrite_flush', time());
129 self::manage_tutor_roles_and_permissions();
130
131 self::save_data();//Save initial Page
132 update_option('tutor_version', TUTOR_VERSION);
133 }
134
135 //Set Schedule
136 if (! wp_next_scheduled ( 'tutor_once_in_day_run_schedule' )) {
137 wp_schedule_event(time(), 'twicedaily', 'tutor_once_in_day_run_schedule');
138 }
139
140 /**
141 * backward / Alpha version compatibility
142 * todo: should remove in version 1.1.0
143 */
144 if (version_compare(get_option('TUTOR_VERSION'), '1.0.0', '<')){
145 //Create Database
146 $this->create_database();
147 update_option('tutor_version', '1.0.0');
148 }
149 /**
150 * backward / v.1.0.0 compatibility
151 * todo: should remove in version 1.1.0
152 */
153 if (get_option('TUTOR_VERSION') == '1.0.0' && version_compare(get_option('TUTOR_VERSION'), '1.0.1', '<')){
154 //Adding column course_id in prefix_tutor_quiz_attempts
155 $this->upgrading_db_1_0_1();
156 update_option('tutor_version', '1.0.1');
157 }
158
159 }
160
161 //Run task on deactivation
162 public function tutor_deactivation() {
163 wp_clear_scheduled_hook('tutor_once_in_day_run_schedule');
164 }
165
166 public function create_database(){
167 global $wpdb;
168
169 $charset_collate = $wpdb->get_charset_collate();
170
171 /**
172 * Table SQL
173 *
174 * {$wpdb->prefix}tutor_quiz_attempts
175 * {$wpdb->prefix}tutor_quiz_attempt_answers
176 * {$wpdb->prefix}tutor_quiz_questions
177 * {$wpdb->prefix}tutor_quiz_question_answers
178 *
179 * @since v.1.0.0
180 */
181 $quiz_attempts_sql = "CREATE TABLE {$wpdb->prefix}tutor_quiz_attempts (
182 attempt_id int(11) NOT NULL AUTO_INCREMENT,
183 course_id int(11) DEFAULT NULL,
184 quiz_id int(11) DEFAULT NULL,
185 user_id int(11) DEFAULT NULL,
186 total_questions int(11) DEFAULT NULL,
187 total_answered_questions int(11) DEFAULT NULL,
188 total_marks decimal(9,2) DEFAULT NULL,
189 earned_marks decimal(9,2) DEFAULT NULL,
190 attempt_info text,
191 attempt_status varchar(50) DEFAULT NULL,
192 attempt_ip varchar(250) DEFAULT NULL,
193 attempt_started_at datetime DEFAULT NULL,
194 attempt_ended_at datetime DEFAULT NULL,
195 is_manually_reviewed int(1) DEFAULT NULL,
196 manually_reviewed_at datetime DEFAULT NULL,
197 PRIMARY KEY (attempt_id)
198 ) $charset_collate;";
199
200 $quiz_attempt_answers = "CREATE TABLE {$wpdb->prefix}tutor_quiz_attempt_answers (
201 attempt_answer_id int(11) NOT NULL AUTO_INCREMENT,
202 user_id int(11) DEFAULT NULL,
203 quiz_id int(11) DEFAULT NULL,
204 question_id int(11) DEFAULT NULL,
205 quiz_attempt_id int(11) DEFAULT NULL,
206 given_answer longtext,
207 question_mark decimal(8,2) DEFAULT NULL,
208 achieved_mark decimal(8,2) DEFAULT NULL,
209 minus_mark decimal(8,2) DEFAULT NULL,
210 is_correct tinyint(4) DEFAULT NULL,
211 PRIMARY KEY (attempt_answer_id)
212 ) $charset_collate;";
213
214 $tutor_quiz_questions = "CREATE TABLE {$wpdb->prefix}tutor_quiz_questions (
215 question_id int(11) NOT NULL AUTO_INCREMENT,
216 quiz_id int(11) DEFAULT NULL,
217 question_title text,
218 question_description longtext,
219 question_type varchar(50) DEFAULT NULL,
220 question_mark decimal(9,2) DEFAULT NULL,
221 question_settings longtext,
222 question_order int(11) DEFAULT NULL,
223 PRIMARY KEY (question_id)
224 ) $charset_collate;";
225
226 $tutor_quiz_question_answers = "CREATE TABLE {$wpdb->prefix}tutor_quiz_question_answers (
227 answer_id int(11) NOT NULL AUTO_INCREMENT,
228 belongs_question_id int(11) DEFAULT NULL,
229 belongs_question_type varchar(250) DEFAULT NULL,
230 answer_title text,
231 is_correct tinyint(4) DEFAULT NULL,
232 image_id int(11) DEFAULT NULL,
233 answer_two_gap_match text,
234 answer_view_format varchar(250) DEFAULT NULL,
235 answer_settings text,
236 answer_order int(11) DEFAULT '0',
237 PRIMARY KEY (answer_id)
238 ) $charset_collate;";
239
240 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
241 dbDelta( $quiz_attempts_sql );
242 dbDelta( $quiz_attempt_answers );
243 dbDelta( $tutor_quiz_questions );
244 dbDelta( $tutor_quiz_question_answers );
245 }
246
247 /**
248 * upgrading quiz_attempts_database adding course_id
249 * @since v.1.0.1
250 */
251 public function upgrading_db_1_0_1(){
252 global $wpdb;
253 /**
254 * Adding course_id column in tutor_quiz_attempts table
255 */
256 $sql = "ALTER TABLE {$wpdb->prefix}tutor_quiz_attempts ADD course_id INT NULL DEFAULT NULL AFTER attempt_id;";
257 $wpdb->query($sql);
258 /**
259 * Setting Course_id column data;
260 */
261 $attempts = $wpdb->get_results("SELECT * from {$wpdb->prefix}tutor_quiz_attempts;");
262 if (is_array($attempts) && count($attempts)){
263 foreach ($attempts as $attempt){
264 $course = tutor_utils()->get_course_by_quiz($attempt->quiz_id);
265 $wpdb->update($wpdb->prefix."tutor_quiz_attempts", array('course_id' => $course->ID), array('attempt_id' => $attempt->attempt_id));
266 }
267 }
268 }
269
270 public static function manage_tutor_roles_and_permissions(){
271 /**
272 * Add role for instructor
273 */
274 $instructor_role = tutor()->instructor_role;
275
276 remove_role($instructor_role);
277 add_role( $instructor_role, __('Tutor Instructor', 'tutor'), array() );
278
279 $custom_post_type_permission = array(
280 //Manage Instructor
281 'manage_tutor_instructor',
282
283 //Tutor Posts Type Permission
284 'edit_tutor_course',
285 'read_tutor_course',
286 'delete_tutor_course',
287 'delete_tutor_courses',
288 'edit_tutor_courses',
289 'edit_others_tutor_courses',
290 'read_private_tutor_courses',
291 'edit_tutor_courses',
292
293 'edit_tutor_lesson',
294 'read_tutor_lesson',
295 'delete_tutor_lesson',
296 'delete_tutor_lessons',
297 'edit_tutor_lessons',
298 'edit_others_tutor_lessons',
299 'read_private_tutor_lessons',
300 'edit_tutor_lessons',
301 'publish_tutor_lessons',
302
303 'edit_tutor_quiz',
304 'read_tutor_quiz',
305 'delete_tutor_quiz',
306 'delete_tutor_quizzes',
307 'edit_tutor_quizzes',
308 'edit_others_tutor_quizzes',
309 'read_private_tutor_quizzes',
310 'edit_tutor_quizzes',
311 'publish_tutor_quizzes',
312
313 'edit_tutor_question',
314 'read_tutor_question',
315 'delete_tutor_question',
316 'delete_tutor_questions',
317 'edit_tutor_questions',
318 'edit_others_tutor_questions',
319 'publish_tutor_questions',
320 'read_private_tutor_questions',
321 'edit_tutor_questions',
322 );
323
324 $instructor = get_role( $instructor_role );
325 if ( $instructor ) {
326 $instructor_cap = array (
327 'edit_posts',
328 'read',
329 'upload_files',
330 );
331
332 $instructor_cap = array_merge($instructor_cap, $custom_post_type_permission);
333
334 $can_publish_course = (bool) tutor_utils()->get_option('instructor_can_publish_course');
335 if ($can_publish_course){
336 $instructor_cap[] = 'publish_tutor_courses';
337 }
338
339 foreach ($instructor_cap as $cap){
340 $instructor->add_cap( $cap );
341 }
342 }
343
344 $administrator = get_role( 'administrator' );
345 if ( $administrator ) {
346 $administrator_cap = array (
347 'manage_tutor',
348 );
349 $administrator_cap = array_merge($administrator_cap, $custom_post_type_permission);
350 $administrator_cap[] = 'publish_tutor_courses';
351
352 foreach ($administrator_cap as $cap){
353 $administrator->add_cap( $cap );
354 }
355 }
356 }
357
358 /**
359 * Save data like page
360 */
361 public static function save_data(){
362 $student_dashboard_args = array(
363 'post_title' => __('Student Dashboard', 'tutor'),
364 'post_content' => '[tutor_student_dashboard]',
365 'post_type' => 'page',
366 'post_status' => 'publish',
367 );
368 $student_dashboard_page_id = wp_insert_post( $student_dashboard_args );
369 tutor_utils()->update_option('student_dashboard', $student_dashboard_page_id);
370
371 $student_registration_args = array(
372 'post_title' => __('Student Registration', 'tutor'),
373 'post_content' => '[tutor_student_registration_form]',
374 'post_type' => 'page',
375 'post_status' => 'publish',
376 );
377 $student_register_page_id = wp_insert_post( $student_registration_args );
378 tutor_utils()->update_option('student_register_page', $student_register_page_id);
379
380 $instructor_registration_args = array(
381 'post_title' => __('Instructor Registration', 'tutor'),
382 'post_content' => '[tutor_instructor_registration_form]',
383 'post_type' => 'page',
384 'post_status' => 'publish',
385 );
386 $instructor_registration_id = wp_insert_post( $instructor_registration_args );
387 tutor_utils()->update_option('instructor_register_page', $instructor_registration_id);
388 }
389
390 public static function default_options(){
391 $options = array (
392 'load_tutor_css' => '1',
393 'load_tutor_js' => '1',
394 'course_allow_upload_private_files' => '1',
395 'display_course_instructors' => '1',
396 'enable_q_and_a_on_course' => '1',
397 'courses_col_per_row' => '3',
398 'courses_per_page' => '3',
399 'lesson_permalink_base' => 'lesson',
400 'quiz_time_limit' =>
401 array (
402 'value' => '0',
403 'time' => 'minutes',
404 ),
405 'quiz_when_time_expires' => 'autosubmit',
406 'quiz_attempts_allowed' => '10',
407 'quiz_grade_method' => 'highest_grade',
408 'enable_public_profile' => '1',
409 'email_to_students' =>
410 array (
411 'quiz_completed' => '1',
412 'completed_course' => '1',
413 ),
414 'email_to_instructors' =>
415 array (
416 'a_student_enrolled_in_course' => '1',
417 'a_student_completed_course' => '1',
418 'a_student_completed_lesson' => '1',
419 'a_student_placed_question' => '1',
420 ),
421 'email_from_name' => get_option('blogname'),
422 'email_from_address' => get_option('admin_email'),
423 'email_footer_text' => '',
424 'enable_course_sell_by_woocommerce' => '1',
425 );
426 return $options;
427 }
428
429
430 }