/wp-content/plugins/watupro/controllers/xapi.php

https://github.com/livinglab/openlab · PHP · 131 lines · 97 code · 21 blank · 13 comment · 12 complexity · 555b7fe237389aeac5c3968b3f790ee9 MD5 · raw file

  1. <?php
  2. // integrates WatuPRO with the WP Experience API plugin
  3. // https://wordpress.org/plugins/wp-experience-api/
  4. class WatuPROXAPI {
  5. static function options() {
  6. if(!empty($_POST['ok']) and check_admin_referer('watupro_xapi')) {
  7. $options = array(
  8. 'passed_exam' => empty($_POST['passed_exam']) ? 0 : 1,
  9. 'failed_exam' => empty($_POST['failed_exam']) ? 0 : 1,
  10. );
  11. update_option('watupro_xapi', $options);
  12. }
  13. $options = get_option('watupro_xapi');
  14. include(WATUPRO_PATH . '/views/xapi-options.html.php');
  15. }
  16. static function register_triggers() {
  17. if(!class_exists('WP_Experience_API')) return false;
  18. // make all conditional, i.e. hook only if chosen so in WatuPRO xAPI Settings page
  19. $options = get_option('watupro_xapi');
  20. // completed exam
  21. if(!empty($options['passed_exam']) or !empty($options['failed_exam'])) {
  22. WP_Experience_API::register( 'completed_exam', array(
  23. 'hooks' => array( 'watupro_completed_exam' ),
  24. 'num_args' => array( 'watupro_completed_exam' => 1 ),
  25. 'process' => function( $hook, $args ) {
  26. global $wpdb;
  27. $options = get_option('watupro_xapi');
  28. // args parameter should return $user_id, $achievement_id, $this_trigger, $site_id, $args
  29. $taking_id = empty($args[0]) ? 0 : intval($args[0]);
  30. // select taking
  31. $taking = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".WATUPRO_TAKEN_EXAMS." WHERE ID=%d", $taking_id));
  32. // if no student ID and taking email, return
  33. if(empty($taking->user_id) and empty($taking->email)) return false;
  34. $exam = $wpdb->get_row($wpdb->prepare("SELECT ID, name, advanced_settings FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $taking->exam_id));
  35. // define if passed or failed
  36. $passed = false;
  37. $advanced_settings = unserialize(stripslashes($exam->advanced_settings));
  38. if(empty($advanced_settings['completion_criteria']) or $advanced_settings['completion_criteria'] == 'taken') $passed = true;
  39. else {
  40. // passed only if completion criteria are met
  41. $grids = explode('|', $advanced_settings['completion_grades']);
  42. $grids = array_filter($grids);
  43. if(in_array($taking->grade_id, $grids)) $passed = true;
  44. }
  45. if($passed and empty($options['passed_exam'])) return false;
  46. if(!$passed and empty($options['failed_exam'])) return false;
  47. $verb = $passed ? 'passed' : 'failed';
  48. $student_id = $taking->user_id ? $taking->user_id : 0;
  49. // construct statement
  50. $statement = array(
  51. 'verb' => array(
  52. 'id' => 'http://adlnet.gov/expapi/verbs/'.$verb,
  53. 'display' => array( 'en-US' => $verb ),
  54. ),
  55. 'object' => array(
  56. 'id' => WTPExam :: get_permalink($exam->ID),
  57. 'definition' => array(
  58. 'name' => array(
  59. 'en-US' => stripslashes($exam->name),
  60. ),
  61. 'description' => array(
  62. 'en-US' => stripslashes($exam->name),
  63. ),
  64. 'type' => 'http://adlnet.gov/expapi/activities/assessment',
  65. )
  66. ),
  67. 'context_raw' => self :: context_raw(),
  68. 'timestamp_raw' => date( 'c' ),
  69. 'user' => intval($student_id),
  70. );
  71. $statement = self :: add_author($taking->user_id, $statement, $taking);
  72. return $statement;
  73. } // end process
  74. ) ); // end enrolled course
  75. }
  76. }
  77. // adds author to statement
  78. static function add_author($student_id, $statement, $taking = null) {
  79. if($student_id) {
  80. $student = get_userdata($student_id);
  81. $user = array(
  82. 'objectType' => 'Agent',
  83. 'name' => $student->display_name,
  84. 'mbox' => $student->user_email,
  85. );
  86. $statement = array_merge( $statement, array( 'actor' => $user ) );
  87. }
  88. else {
  89. // get data from taking
  90. $user = array(
  91. 'objectType' => 'Agent',
  92. 'name' => ($taking->name ? $taking->name : __('Guest', 'watupro')),
  93. 'mbox' => $taking->email,
  94. );
  95. $statement = array_merge( $statement, array( 'actor_raw' => $user ) );
  96. }
  97. return $statement;
  98. }
  99. // return raw context
  100. static function context_raw() {
  101. $context = array(
  102. 'extensions' => array(
  103. 'http://id.tincanapi.com/extension/browser-info' => array( 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ),
  104. 'http://nextsoftwaresolutions.com/xapi/extensions/referer' => @$_SERVER['HTTP_REFERER'],
  105. ),
  106. 'platform' => defined( 'CTLT_PLATFORM' ) ? constant( 'CTLT_PLATFORM' ) : 'unknown'
  107. );
  108. return $context;
  109. } // end context_rar
  110. }