-
Notifications
You must be signed in to change notification settings - Fork 3
/
rps_result.php
executable file
·608 lines (483 loc) · 22.2 KB
/
rps_result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/*
Plugin Name: Easy Student Results
Plugin URI: https://www.nurul.me/
Description: Result Management System for School, College and University. Use [esr_results] to display result and [esr_students] to display student list.
Text Domain: easy-student-results
Version: 2.2.8
Author: Nurul Umbhiya
Author URI: https://www.nurul.me/
*/
if( !defined( 'WPINC' ) ) {
die();
}
if( !class_exists( 'RPS_Result_Management' ) ) {
final class RPS_Result_Management {
private static $instance;
private $dir, $url;
const VER = 2.7;
//do not change below constant
const DS = '/';
const PLUGIN_SLUG = 'rps_result';
const TD = 'easy-student-results';
const TABLE_PREFIX = 'rps_';
const FILE = __FILE__;
//Post type constant
const STUDENT = 'rps_result_student';
const COURSE = 'rps_result_course';
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new self;
self::$instance->dir = dirname(__FILE__);
self::$instance->url = plugins_url() . self::DS . basename(self::$instance->dir);
self::$instance->autoload();
self::$instance->setDbTables();
self::$instance->actions();
} else {
throw new BadFunctionCallException(sprintf('Plugin %s already instantiated', __CLASS__));
}
return self::$instance;
}
/**
* Get path to plagin dir relative to wordpress root
* @param bool[optional] $noForwardSlash Whether path should be returned withot forwarding slash
* @return string
*/
public function getRelativePath($noForwardSlash = false) {
$wp_root = str_replace('\\', '/', ABSPATH);
return ($noForwardSlash ? '' : '/') . str_replace($wp_root, '', self::$instance->dir);
}
/**
* Check whether plugin is activated as network one
* @return bool
*/
public function isNetwork() {
if ( !is_multisite() )
return false;
$plugins = get_site_option('active_sitewide_plugins');
if (isset($plugins[plugin_basename(self::FILE)]))
return true;
return false;
}
/**
* Check whether permalinks is enabled
* @return bool
*/
public function isPermalinks() {
global $wp_rewrite;
return $wp_rewrite->using_permalinks();
}
/**
* Return prefix for plugin database tables
* @return string
*/
public static function getTablePrefix() {
global $wpdb;
return $wpdb->prefix . self::TABLE_PREFIX;
}
/**
* Return prefix for wordpress database tables
* @return string
*/
public function getWPPrefix() {
global $wpdb;
return ($this->isNetwork() ? $wpdb->base_prefix : $wpdb->prefix);
}
private function __construct() {
;
}
public function autoload() {
require_once ( $this->dir . self::DS . "RPS" . self::DS . "Autoloader" . self::DS . "LoaderClass.php" );
// Add the Autoloader
$loader = new RPS_Autoloader_LoaderClass( "RPS", dirname( __FILE__ ) );
$loader->register( );
}
private function setDbTables () {
global $wpdb;
$wpdb->rps_department = self::getTablePrefix() . 'departments';
$wpdb->rps_batch = self::getTablePrefix() . 'batches';
$wpdb->rps_exam = self::getTablePrefix() . 'exams';
$wpdb->rps_grade = self::getTablePrefix() . 'grade';
$wpdb->rps_exam_record = self::getTablePrefix() . 'exam_records';
$wpdb->rps_exam_record_meta = self::getTablePrefix() . 'exam_record_meta';
$wpdb->rps_marks = self::getTablePrefix() . 'marks';
}
/**
* call all actions/filters here
*/
private function actions() {
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
//this is for api access
add_action( 'rest_api_init', array( $this, 'api' ) );
//multisite install tables after adding a new blog
add_action( 'wpmu_new_blog', array( $this, 'onCreateBlog'), 10, 6 );
//delete tables after blog is deleted
add_filter( 'wpmu_drop_tables', array($this, 'onDeleteBlog'), 10, 2 );
if ( is_admin() ) {
//register and localize all script/stylesheet for admin panel
add_action( 'admin_enqueue_scripts', array( $this, 'adminRegisterScripts' ) , 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'adminRegisterStyles' ) , 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'adminLocalizeScripts' ) , 1 );
//register menus
RPS_Admin_Menu_Main::getInstance();
//load hooks
RPS_Admin_Hooks::getInstance();
add_action( 'admin_init', array($this, 'actionAdminInit') );
//menu fix
add_filter( 'parent_file', array($this,'semesterMenu') );
//settings page
RPS_Admin_Menu_Settings::getInstance();
}
if ( !is_admin() ) {
//register and localize all scripts/stylesheet for frontend
add_action( 'wp_enqueue_scripts', array($this, 'adminRegisterScripts'), 1 );
add_action( 'wp_enqueue_scripts', array($this, 'adminRegisterStyles'), 1 );
}
//load plugin text domain
add_action( 'plugins_loaded', array($this,'loadTextdomain') );
//load all post type here
RPS_Admin_Init_PostType::getInstance();
//load all taxonomies here
RPS_Admin_Init_Taxonomi::getInstance();
//shortcodes
add_shortcode( 'esr_students', array($this,'studentShortcode') );
add_shortcode( 'esr_results', array($this,'resultShortcode') );
add_shortcode( 'esr_results2', array($this,'resultShortcode2') );
//load styles if shortcode exist
add_action( 'wp_enqueue_scripts', array( $this, 'loadShortcodeScripts' ), 100 );
//load ajax hooks
if ( defined('DOING_AJAX') && DOING_AJAX ) {
RPS_Helper_Ajax::getInstance();
}
add_action( 'init', array($this, 'custom_post_status'), 0 );
if ( is_admin() ) {
//check for updates
$this->check_for_updates();
}
//advanced custom field support
add_action(RPS_Result_Management::PLUGIN_SLUG . '_student_section_meta_single_print', array($this, 'esr_student_section_meta_single_print'), 10, 2 );
add_filter(RPS_Result_Management::PLUGIN_SLUG . '_result_student_section_meta_single', array($this, 'esr_result_student_section_meta_single'), 10, 3);
}
function esr_student_section_meta_single_print($student_id, $meta_key) {
if ( function_exists('get_fields') ) {
$all_fields = get_fields($student_id);
if ( is_array($all_fields) && array_key_exists($meta_key, $all_fields) ) {
echo esc_attr($all_fields[$meta_key]);
}
}
}
function esr_result_student_section_meta_single($value, $student_id, $meta_key) {
if ( function_exists('get_fields') ) {
$all_fields = get_fields($student_id);
if ( is_array($all_fields) && array_key_exists($meta_key, $all_fields) ) {
$value = esc_attr($all_fields[$meta_key]);
}
}
return $value;
}
private function check_for_updates() {
$version = get_option( self::PLUGIN_SLUG . '_version', '' );
if ( $version == '' ) {
//delete cache
RPS_Helper_Function::delete_transient();
//this is version 1.8, run db updates
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
//set default options
$general_option = get_option(RPS_Result_Management::PLUGIN_SLUG .'_basics');
if ( !array_key_exists('marks_js', $general_option) ) {
$general_option['marks_js'] = 'on';
}
if ( !array_key_exists('percentage_js', $general_option) ) {
$general_option['percentage_js'] = 'on';
}
update_option(RPS_Result_Management::PLUGIN_SLUG .'_basics', $general_option);
//update current version
$version = '1.9';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '1.9' ) {
$general_option = get_option(RPS_Result_Management::PLUGIN_SLUG .'_basics');
if ( !array_key_exists('show_picture', $general_option) ) {
$general_option['show_picture'] = 'off';
}
//delete transient cache
define(RPS_Result_Management::PLUGIN_SLUG . '_delete_transient', true);
RPS_Helper_Function::delete_transient();
//update current version
$version = '2.0';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.0' ) {
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
//update current version
$version = '2.1';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.1' ) {
$version = '2.2';
update_option(self::PLUGIN_SLUG . '_version', '2.2');
}
if ( $version == '2.2' ) {
//delete cache
RPS_Helper_Function::delete_transient();
//update current version
$version = '2.2.1';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.2.1' ) {
//delete cache
RPS_Helper_Function::delete_transient();
//update current version
$version = '2.2.2';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.2.2' ) {
//delete cache
RPS_Helper_Function::delete_transient();
//update current version
$version = '2.2.3';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.2.3' ) {
//delete cache
RPS_Helper_Function::delete_transient();
//update current version
$version = '2.2.4';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
if ( $version == '2.2.4') {
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
//update current version
$version = '2.2.7';
update_option(self::PLUGIN_SLUG . '_version', $version);
}
}
// Register Custom Status
public function custom_post_status() {
$args = array(
'label' => _x( 'Promote', 'Status General Name', 'easy-student-results' ),
'label_count' => _n_noop( 'Promote (%s)', 'Promoted (%s)', 'easy-student-results' ),
'public' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
'exclude_from_search' => true,
);
register_post_status( 'promoted', $args );
}
public function api() {
//check api enabled or not
$options = get_option( RPS_Result_Management::PLUGIN_SLUG . '_api', array() );
if ( isset($options['enable_api']) && $options['enable_api'] == 'on' ) {
if ( class_exists('WP_REST_Controller') ) {
$api = new RPS_API();
$api->register_routes();
}
}
}
public function resultShortcode( $atts ) {
ob_start();
new RPS_Shortcodes_Result($atts);
return ob_get_clean();
}
public function studentShortcode( $atts ) {
ob_start();
new RPS_Shortcodes_StudentList( $atts );
return ob_get_clean();
}
public function resultShortcode2( $atts ) {
ob_start();
new RPS_Shortcodes_Result2( $atts );
return ob_get_clean();
}
/**
* This function will return RPS Result as menu
* @param $file
*
* @return string
*/
public function semesterMenu($file) {
global $wpdb;
$screen = get_current_screen();
//echo "<pre>"; print_r($screen);
//echo $file; die;
//echo RPS_Admin_Menu_Main::getSlug('department');
//die;
//rps-result_page_rps_result_department
if( 'edit-tags' === $screen->base ) {
$query = "SELECT slug FROM `{$wpdb->rps_department}`";
$results = $wpdb->get_results($query,ARRAY_A);
if($results !== NULL) {
foreach ($results as $row):
$slug = RPS_Result_Management::PLUGIN_SLUG . '_' . $row['slug'];
if ( $slug === $screen->taxonomy ) {
$file = 'rps_result';
add_action( 'admin_footer', array( $this, 'wpFooter' ) );
break;
}
endforeach;
}
}
return $file;
}
public function wpFooter() {
?>
<script>
var url = '<?php echo 'admin.php?page=' . RPS_Result_Management::PLUGIN_SLUG . '_department' ?>';
jQuery('a[href="'+url+'"]').addClass('current').parent().addClass('current');
</script>
<?php
}
public function actionAdminInit() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
// try to pick it up from the quick edit AJAX post
elseif (!empty($_POST['post_ID'])) {
$post = get_post($_POST['post_ID']);
$typenow = $post->post_type;
}
}
// check for one of our custom post types,
// and start admin handling for that type
switch ($typenow) {
case self::STUDENT:
RPS_Admin_Init_PostFilters_Student::getInstance();
RPS_Admin_Init_Metaboxes_Student::getInstance();
break;
case self::COURSE:
RPS_Admin_Init_PostFilters_Course::getInstance();
RPS_Admin_Init_Metaboxes_Course::getInstance();
break;
}
}
public function adminRegisterStyles() {
wp_register_style( 'jquery-ui', $this->url .'/assets/jquery-ui/jquery-ui-1.10.4.custom.min.css', array(), '1.10.4' );
wp_register_style( 'rps_bootstrap', $this->url . '/assets/bootstrap-3.3.5/css/bootstrap.css', array(), '3.3.5' );
wp_register_style( 'rps_bootstrap4', $this->url . '/assets/bootstrap-4/css/bootstrap.min.css', array(), '4.0' );
wp_register_style( 'rps_sc_student_list', $this->url . '/assets/css/sc_student_list.css', null, self::VER);
wp_register_style( 'rps_bootstrap4', $this->url . '/assets/bootstrap-4/css/bootstrap.css', array(), '4.0' );
}
public function adminRegisterScripts() {
//register bootstrap
wp_register_script( 'rps_bootstrap', $this->url . '/assets/bootstrap-3.3.5/js/bootstrap.min.js', array( 'jquery' ), '3.3.5', true);
//register sortcode script
wp_register_script( 'rps_sc_student_list', $this->url . '/assets/js/sc_student_list.js', array('jquery'), self::VER, true);
wp_register_script( 'rps_sc_result', $this->url . '/assets/js/sc_result.js', array('jquery'), self::VER, true);
wp_register_script( 'rps_sc_result2', $this->url . '/assets/js/sc_result2.js', array('jquery'), self::VER, true);
//register admin scripts
wp_register_script( 'rps_marks', $this->url . '/assets/js/marks.js', array('jquery'), self::VER, true);
wp_register_script( 'rps_percentage', $this->url . '/assets/js/percentage.js', array('jquery'), self::VER, true);
wp_register_script( 'rps_result', $this->url . '/assets/js/result.js', array('jquery'), self::VER, true );
//bootstrap 4
wp_register_script( 'rps_popper_js', $this->url . '/assets/bootstrap-4/js/popper.min.js', array( 'jquery' ), '1.11.0');
wp_register_script( 'rps_bootstrap4', $this->url . '/assets/bootstrap-4/js/bootstrap.min.js', array( 'jquery', 'rps_popper_js' ), '4.0', true);
wp_localize_script(
'rps_sc_result', "result",
array (
'nonce' => wp_create_nonce('rps_print_student_result'),
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
wp_register_script( 'jquery_print', $this->url . '/assets/js/jQuery.print.js', array('jquery'), '1.5.1', true);
}
function loadShortcodeScripts() {
global $post;
if( is_a( $post, 'WP_Post' ) ) {
$flag = false;
if ( has_shortcode( $post->post_content, 'esr_students') ) {
wp_enqueue_style('rps_sc_student_list');
$flag = true;
}
if ( has_shortcode( $post->post_content, 'esr_results') ) {
$flag = true;
}
if ( has_shortcode( $post->post_content, 'esr_results2') ) {
$flag = true;
}
if ( $flag ) {
$general = get_option( RPS_Result_Management::PLUGIN_SLUG . '_basics', array() );
if ( $general['bootstrap_css'] != 'on') {
wp_enqueue_style('rps_bootstrap');
}
}
}
}
public function adminLocalizeScripts() {
//this function is for localize scripts
}
public function loadTextdomain() {
//http://codex.wordpress.org/I18n_for_WordPress_Developers
load_plugin_textdomain( self::TD, false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
public function activate( $network_wide ) {
// uncaught exception doesn't prevent plugin from being activated, therefore replace it with fatal error so it does
set_exception_handler(create_function('$e', 'trigger_error($e->getMessage(), E_USER_ERROR);'));
global $wpdb;
if ( is_multisite() && $network_wide ) {
// Get all blogs in the network and activate plugin on each one
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
//install/update all required database
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
$db_class->insertGPA();
//predefine all options
$option_class = RPS_DefaultOptions::getInstance();
$option_class->createOptions();
restore_current_blog();
}
} else {
//install/update all required database
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
$db_class->insertGPA();
//predefine all options
$option_class = RPS_DefaultOptions::getInstance();
$option_class->createOptions();
}
}
public function deactivate() {
}
public function onCreateBlog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
$plugin = plugin_basename( __FILE__ );
if ( is_plugin_active_for_network( $plugin ) ) {
switch_to_blog( $blog_id );
//install/update all required database
$db_class = RPS_InstallDb::getInstance();
$db_class->createDB();
$db_class->insertGPA();
//predefine all options
$option_class = RPS_DefaultOptions::getInstance();
$option_class->createOptions();
restore_current_blog();
}
}
function onDeleteBlog( $tables, $blog_id ) {
$tables[] = self::getTablePrefix() . 'departments';
$tables[] = self::getTablePrefix() . 'batches';
$tables[] = self::getTablePrefix() . 'exams';
$tables[] = self::getTablePrefix() . 'grade';
$tables[] = self::getTablePrefix() . 'exam_records';
$tables[] = self::getTablePrefix() . 'exam_record_meta';
$tables[] = self::getTablePrefix() . 'marks';
return $tables;
}
public static function DIR() {
return self::$instance->dir;
}
public static function URL() {
return self::$instance->url;
}
}
RPS_Result_Management::getInstance();
}