Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.00 KB | None | 0 0
  1. add_action('wp_ajax_get_batch_course_stats',array($this,'get_batch_course_stats'));
  2.  
  3. <?php
  4.  
  5.  
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9.  
  10. if ( ! defined( 'WPLMS_BATCH_STATISITCS_SLUG' ) )
  11. define( 'WPLMS_BATCH_STATISITCS_SLUG', 'stats' );
  12.  
  13. if ( class_exists( 'BP_Group_Extension' ) ) :
  14.  
  15. class BP_Group_Course_Batch_Statistics extends BP_Group_Extension {
  16.  
  17. var $message;
  18.  
  19. function __construct() {
  20.  
  21. $args = array(
  22. 'slug' => WPLMS_BATCH_STATISITCS_SLUG,
  23. 'name' => __( 'Statistics', 'wplms-batches' ),
  24. 'visibility' => 'private',
  25. 'nav_item_position' => 10,
  26. 'enable_nav_item' => false,
  27. 'screens' => array(
  28. 'admin' => array(
  29. 'metabox_context' => 'side',
  30. 'metabox_priority' => 'core'
  31. ),
  32. 'create' => array(
  33. 'enabled' => false,
  34. ),
  35. 'edit' => array(
  36. 'enabled' => false,
  37. ),
  38. )
  39. );
  40. $group_id = bp_get_group_id();
  41. global $wpdb, $bp;
  42. if(isset($bp->groups) && is_object($bp->groups->current_group)){
  43. $enable = groups_get_groupmeta($bp->groups->current_group->id, 'course_batch' );
  44. }else{
  45. $enable = 0;
  46. }
  47.  
  48. if(!empty($enable)){
  49. $args['enable_nav_item'] = true;
  50. }
  51.  
  52. $flag = 1;
  53. if(isset($bp->groups) && is_object($bp->groups->current_group)){
  54. $batch_stats_visibility = groups_get_groupmeta($bp->groups->current_group->id,'batch_stats_visibility');
  55. }
  56.  
  57.  
  58. if(!empty($batch_stats_visibility)){
  59. $user_id = get_current_user_ID();
  60. if($batch_stats_visibility == 'mods'){
  61. if(!groups_is_user_mod($user_id,$bp->groups->current_group->id)){
  62. $flag = 0;
  63. }
  64. }else if($batch_stats_visibility == 'admins'){
  65. if(!groups_is_user_admin($user_id,$bp->groups->current_group->id)){
  66. $flag = 0;
  67. }
  68. }
  69. }
  70.  
  71. if($flag){
  72. parent::init( $args );
  73. add_action('wplms_batch_statistics',array($this,'batch_statistics'),10,1);
  74. add_action('wplms_batch_moderator_statistics',array($this,'moderator_statistics'),10,1);
  75. add_action('wp_ajax_get_batch_course_stats', array($this,'get_batch_course_stats'));
  76. add_action('bp_enqueue_scripts', array( $this, 'cssjs' ) );
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83. }
  84.  
  85.  
  86.  
  87. function cssjs(){
  88. if( bp_is_single_item() && bp_is_groups_component() && bp_is_current_action(WPLMS_BATCH_STATISITCS_SLUG) ){
  89. wp_enqueue_script('datatables-js',plugins_url( '/assets/js/jquery.dataTables.min.js', __FILE__ ),array('jquery'));
  90. wp_enqueue_script('bootstrap-datatables-js',plugins_url( '/assets/js/dataTables.bootstrap.min.js', __FILE__ ));
  91. }
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /**
  98. * display() contains the markup that will be displayed on the main
  99. * plugin tab
  100. */
  101. function display( $group_id = NULL ) {
  102.  
  103. if(empty($group_id))
  104. $group_id = bp_get_group_id();
  105.  
  106.  
  107. do_action('wplms_batch_statistics',$group_id);
  108. if(bp_current_user_can( 'bp_moderate' )){
  109. do_action('wplms_batch_moderator_statistics',$group_id);
  110. }else{
  111. do_action('wplms_batch_student_statistics',$group_id);
  112. }
  113.  
  114. $leaderboard = groups_get_groupmeta($group_id,'batch_leaderboard');
  115. if(bp_current_user_can( 'bp_moderate' ) || !empty($leaderboard)){
  116. //echo '<h3 class="heading"><span>'._x('Leaderboard','Batch Leaderboard','wplms-batches').'</span></h3>';
  117. $members = BP_Groups_Member::get_group_member_ids($group_id);
  118. $courses = groups_get_groupmeta($group_id,'batch_course',false);
  119. if(empty($courses)){
  120. echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
  121. return;
  122. }
  123. $member_ids = implode(',',$members);
  124. $course_ids = implode(',',$courses);
  125. $course_status_string = '';
  126. foreach($courses as $c){
  127. $course_status_string .= "'course_status".$c."',";
  128. }
  129. $course_status_string =trim($course_status_string, ",");
  130. global $wpdb,$bp;
  131. $results = $wpdb->get_results($wpdb->prepare("SELECT p.meta_key as uid,sum(p.meta_value) as sum
  132. FROM {$wpdb->postmeta} as p
  133. LEFT JOIN {$wpdb->usermeta} as u
  134. ON p.meta_key = u.user_id
  135. WHERE p.meta_key IN ($member_ids)
  136. AND p.post_id IN ($course_ids)
  137. AND u.meta_key IN ($course_status_string)
  138. AND u.meta_value > %d
  139. GROUP BY uid ORDER BY sum DESC",3));
  140. ?>
  141.  
  142. <?php
  143.  
  144. }
  145. }
  146.  
  147.  
  148. function batch_statistics($group_id){
  149. $courses = groups_get_groupmeta($group_id,'batch_course',false);
  150. if(empty($courses)){
  151. echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
  152. return;
  153. }
  154. $members = BP_Groups_Member::get_group_member_ids($group_id);
  155. $admins = groups_get_group_admins( $group_id );
  156. $mods = groups_get_group_mods( $group_id );
  157.  
  158.  
  159. global $wpdb,$bp;
  160. $batch_average = $batch_badge_count = $batch_certificate_count = 0;
  161. $batch_average = groups_get_groupmeta($group_id,'batch_average');
  162. $batch_badge_count = groups_get_groupmeta($group_id,'batch_badge_count');
  163. $batch_certificate_count = groups_get_groupmeta($group_id,'batch_certificate_count');
  164. global $wpdb, $bp;
  165. if(isset($_GET['recalculate'])){
  166. $batch_badge_count = $batch_certificate_count = 0;
  167. $total_marks = 0;$total_denom = 1;
  168. foreach($members as $member_id){
  169. foreach($courses as $course_id){
  170. $status = bp_course_get_user_course_status($member_id,$course_id);
  171. if($status > 3){
  172. $marks = get_post_meta($course_id,$member_id,true);
  173. $total_marks += $marks;
  174. $total_denom++;
  175. }
  176. }
  177. $certificates = get_user_meta($member_id,'certificates',true);
  178. $badges = get_user_meta($member_id,'badges',true);
  179. if(!empty($certificates)){
  180. foreach($certificates as $certificate_course_id){
  181. if(in_array($certificate_course_id,$courses)){
  182. $batch_certificate_count++;
  183. }
  184. }
  185. }
  186. if(!empty($badges)){
  187. foreach($badges as $badges_course_id){
  188. if(in_array($badges_course_id,$courses)){
  189. $batch_badge_count++;
  190. }
  191. }
  192. }
  193. }
  194. if($total_denom > 1){
  195. $total_denom--;
  196. }
  197. $batch_average = round($total_marks/$total_denom,2);
  198. groups_update_groupmeta($group_id,'batch_average',$batch_average);
  199. groups_update_groupmeta($group_id,'batch_badge_count',$batch_badge_count);
  200. groups_update_groupmeta($group_id,'batch_certificate_count',$batch_certificate_count);
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. ?>
  208. <?php /*
  209. ?>
  210. <div class="row">
  211. <div class="col-md-12">
  212. <div class="course_stats_form">
  213. <h3 class="heading"><span><?php _e('Course Specific Statistics','wplms-batches'); ?></span></h3>
  214. <select id="batch_course">
  215. <?php
  216. foreach($courses as $course){
  217. echo '<option value="'.$course.'">'.get_the_title($course).'</option>';
  218. }
  219. ?>
  220. </select>
  221. <?php wp_nonce_field('batch_course_security'.$group_id,'batch_course_security');
  222. echo '<input type="hidden" id="course_batch_id" value="'.$group_id.'" />'; ?>
  223. <a id="get_batch_course_stats" class="button">
  224. <?php _e('Get Stats','wplms-batches'); ?>
  225. </a>
  226. </div>
  227. </div>
  228.  
  229.  
  230. <?php
  231. }
  232. function moderator_statistics($group_id){
  233. global $wpdb, $bp;
  234. $courses = groups_get_groupmeta($group_id,'batch_course',false);
  235. if(empty($courses)){
  236. echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
  237. return;
  238. }
  239.  
  240. ?>
  241.  
  242. <?php
  243. $members = BP_Groups_Member::get_group_member_ids($group_id);
  244.  
  245. }
  246.  
  247. function get_batch_course_stats(){
  248.  
  249.  
  250. $course_id = $_POST['course_id'];
  251. $batch_id = $_POST['batch_id'];
  252. $user_id = get_current_user_ID();
  253. global $wpdb, $bp;
  254. if ( !isset($_POST['security']) || !wp_verify_nonce($_POST['security'],'batch_course_security'.$batch_id) || !is_numeric($course_id) || !is_numeric($batch_id)){
  255. _e('Security check Failed. Contact Administrator.','wplms-batches');
  256. die();
  257. }
  258.  
  259. echo '<h5>'.sprintf(__('Statistics for Course %s','wplms-batches'),'<a href="'.get_permalink($course_id).'" target="_blank">'.get_the_title($course_id)).'</a></h5>';
  260.  
  261. if(function_exists('bp_course_get_curriculum_quizes')){
  262. global $wpdb, $bp;
  263. $quiz_ids = bp_course_get_curriculum_quizes($course_id);
  264.  
  265. // Sort Quiz id
  266. sort($quiz_ids);
  267. $tlength = count($quiz_ids);
  268. for($x = 0; $x < $tlength; $x++);
  269.  
  270. /*
  271. $flag = 0;
  272. if(!empty($quiz_ids)){
  273. $flag++;
  274. }
  275. */
  276.  
  277. ?>
  278.  
  279. <table class="course_leaderboard table table-hover table-bordered" id="table_course">
  280. <thead>
  281. <tr>
  282. <th>#</th>
  283. <th valign="top">
  284. <?php _e('Student','wplms-batches') ?> <br>
  285. </th>
  286. <th>
  287. Progression (%)
  288. </th>
  289. <th>
  290. QCM terminés
  291. </th>
  292. <th>
  293. Sessions terminées
  294. </th>
  295. <?php
  296.  
  297. //if($flag >= 1){
  298. foreach($quiz_ids as $quiz_id){
  299. echo '<th><a href="'.get_permalink($quiz_id).'" target="_blank">'.get_the_title($quiz_id).'</a></th>';
  300. }
  301. //}
  302.  
  303. ?>
  304. </tr>
  305. </thead>
  306. <tbody>
  307.  
  308. <?php
  309. $members = BP_Groups_Member::get_group_member_ids($batch_id);
  310.  
  311. $i=1;
  312. foreach($members as $member_id){
  313.  
  314.  
  315. foreach ($courses as $course_id)
  316. global $wpdb, $bp;
  317. $progress = bp_course_get_user_progress($member_id,$course_id);
  318.  
  319. /*
  320. * Process for QCM Value
  321. */
  322.  
  323. global $wpdb, $bp;
  324. $marks=$wpdb->get_results(sprintf("
  325. SELECT rel.post_id as id, rel.meta_value as val
  326. FROM {$wpdb->posts} AS posts
  327. LEFT JOIN {$wpdb->postmeta} AS rel ON posts.ID = rel.post_id
  328. WHERE posts.post_type = 'quiz'
  329. AND posts.post_title LIKE '%%QCM'
  330. AND posts.post_status = 'publish'
  331. AND rel.meta_key = %d
  332. AND rel.meta_value >= 0
  333. ", $member_id)
  334. );
  335. if (is_array($marks)) {
  336. foreach ($marks as $k=>$mark) {
  337. $user_marks[] = $mark->val;
  338. }
  339. }
  340. else {
  341. $user_marks = array();
  342. }
  343. if (is_array($marks))
  344. $value = count($marks);
  345.  
  346. if ( $title )
  347. $label = $title;
  348. else
  349. $label = __('Quizzes Completed', 'wplms-dashboard');
  350.  
  351. if (is_array($user_marks)) {
  352. foreach ($user_marks as $i=>$mark){
  353. if ($i<11) {
  354. if (!$i)
  355. $marks_string = $mark;
  356. else
  357. $marks_string .= ','.$mark;
  358. }
  359. }
  360. }
  361.  
  362. if (!is_numeric($value))
  363. $value = 0;
  364.  
  365.  
  366. /*
  367. * Process for Personality Value
  368. */
  369.  
  370.  
  371. global $wpdb;
  372. $marks=$wpdb->get_results(sprintf("
  373. SELECT rel.post_id as id, rel.meta_value as val
  374. FROM {$wpdb->posts} AS posts
  375. LEFT JOIN {$wpdb->postmeta} AS rel ON posts.ID = rel.post_id
  376. WHERE posts.post_type = 'quiz'
  377. AND posts.post_title LIKE '%%Test de Personnalité'
  378. AND posts.post_status = 'publish'
  379. AND rel.meta_key = %d
  380. AND rel.meta_value >= 0
  381. ", $member_id)
  382. );
  383.  
  384.  
  385. if (is_array($marks)) {
  386. foreach ($marks as $k=>$mark) {
  387. $user_marks[] = $mark->val;
  388. }
  389. }
  390. else {
  391. $user_marks = array();
  392. }
  393.  
  394. if ( $title )
  395. $label2 = $title;
  396. else
  397. $label2 = __('Personality Tests Completed', 'wplms-dashboard');
  398.  
  399. if (is_array($marks))
  400. $value2 = count($marks);
  401.  
  402. if (is_array($user_marks)) {
  403. foreach ($user_marks as $i=>$mark){
  404. if ($i<11) {
  405. if (!$i)
  406. $marks_string = $mark;
  407. else
  408. $marks_string .= ','.$mark;
  409. }
  410. }
  411. }
  412.  
  413.  
  414. if (!is_numeric($value2))
  415. $value2 = 0;
  416.  
  417.  
  418. ?>
  419.  
  420.  
  421. <tr>
  422. <td>
  423. <?php echo $i ?>
  424. </td>
  425. <td>
  426. <?php echo bp_core_get_user_displayname($member_id) ?>
  427. </td>
  428. <td>
  429. <?php echo $progress ?> %
  430. </td>
  431. <td>
  432. <?php echo $value ?>
  433. </td>
  434. <td>
  435. <?php echo $value2 ?>
  436. </td>
  437.  
  438. <?php
  439. //if($flag >= 1){
  440. foreach($quiz_ids as $quiz_id){
  441. echo '<td>'.get_post_meta($quiz_id,$member_id,true).'</td>';
  442. }
  443. //}
  444. $sum=0;
  445. $status = bp_course_get_user_course_status($member_id,$course_id);
  446. if($status > 3){
  447. $marks = get_post_meta($course_id,$member_id, true);
  448. $sum += $marks;
  449. echo '<td>'.$marks.'</td>';
  450. }else{
  451. echo '<td>'._x('N.A','Marks not available in batch statisitcs leaderboard','wplms-batches').'</td>';
  452. }
  453. echo '</tr>';
  454. $i++;
  455. }
  456. ?>
  457. </tr>
  458.  
  459. <?php }?>
  460. </tbody>
  461. </table>
  462.  
  463. <!-- END Custom Code KVDC -->
  464. <?php
  465. die();
  466. }
  467.  
  468.  
  469. } // END of Class
  470.  
  471. add_action( 'bp_init','wplms_batch_statistics_register_group_extension');
  472. function wplms_batch_statistics_register_group_extension(){
  473. bp_register_group_extension( 'BP_Group_Course_Batch_Statistics' );
  474. }
  475. endif;
  476.  
  477. $sum += $marks;
  478.  
  479. $sum->marks;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement