PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/custom-post-types/question.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 406 lines | 313 code | 42 blank | 51 comment | 38 complexity | 3e0cc226b4eb78f0410015d6240540d0 MD5 | raw file
  1. <?php
  2. if ( !defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( !class_exists( 'LP_Question_Post_Type' ) ) {
  6. // Base class for custom post type to extends
  7. learn_press_include( 'custom-post-types/abstract.php' );
  8. // class LP_Question_Post_Type
  9. class LP_Question_Post_Type extends LP_Abstract_Post_Type {
  10. function __construct() {
  11. add_action( 'admin_head', array( $this, 'init' ) );
  12. //add_action( 'init', array( $this, 'register_post_type' ) );
  13. //add_action( 'admin_head', array( $this, 'enqueue_script' ) );
  14. //add_action( 'admin_init', array( $this, 'add_meta_boxes' ), 5 );
  15. add_filter( 'manage_lp_question_posts_columns', array( $this, 'columns_head' ) );
  16. add_action( 'manage_lp_question_posts_custom_column', array( $this, 'columns_content' ), 10, 2 );
  17. add_filter( 'posts_join_paged', array( $this, 'posts_join_paged' ) );
  18. add_action( 'before_delete_post', array( $this, 'delete_question_answers' ) );
  19. //add_filter( 'posts_fields', array( $this, 'posts_fields' ) );
  20. add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ) );
  21. add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) );
  22. add_filter( 'manage_edit-lp_question_sortable_columns', array( $this, 'columns_sortable' ) );
  23. parent::__construct();
  24. }
  25. function init() {
  26. global $pagenow, $post_type;
  27. $hidden = get_user_meta( get_current_user_id(), 'manageedit-lp_questioncolumnshidden', true );
  28. if ( !is_array( $hidden ) && empty( $hidden ) ) {
  29. update_user_meta( get_current_user_id(), 'manageedit-lp_questioncolumnshidden', array( 'taxonomy-question-tag' ) );
  30. }
  31. }
  32. /**
  33. * Delete all answers assign to question being deleted
  34. *
  35. * @param $post_id
  36. */
  37. function delete_question_answers( $post_id ) {
  38. global $wpdb;
  39. $query = $wpdb->prepare( "
  40. DELETE FROM {$wpdb->prefix}learnpress_question_answers
  41. WHERE question_id = %d
  42. ", $post_id );
  43. $wpdb->query( $query );
  44. learn_press_reset_auto_increment( 'learnpress_question_answers' );
  45. // also, delete question from quiz
  46. $wpdb->query(
  47. $wpdb->prepare( "
  48. DELETE FROM {$wpdb->prefix}learnpress_quiz_questions
  49. WHERE question_id = %d
  50. ", $post_id )
  51. );
  52. learn_press_reset_auto_increment( 'learnpress_quiz_questions' );
  53. }
  54. /**
  55. * Register question post type
  56. */
  57. static function register_post_type() {
  58. register_post_type( LP_QUESTION_CPT,
  59. array(
  60. 'labels' => array(
  61. 'name' => __( 'Question Bank', 'learnpress' ),
  62. 'menu_name' => __( 'Question Bank', 'learnpress' ),
  63. 'singular_name' => __( 'Question', 'learnpress' ),
  64. 'all_items' => __( 'Questions', 'learnpress' ),
  65. 'view_item' => __( 'View Question', 'learnpress' ),
  66. 'add_new_item' => __( 'Add New Question', 'learnpress' ),
  67. 'add_new' => __( 'Add New', 'learnpress' ),
  68. 'edit_item' => __( 'Edit Question', 'learnpress' ),
  69. 'update_item' => __( 'Update Question', 'learnpress' ),
  70. 'search_items' => __( 'Search Questions', 'learnpress' ),
  71. 'not_found' => __( 'No question found', 'learnpress' ),
  72. 'not_found_in_trash' => __( 'No question found in trash', 'learnpress' ),
  73. ),
  74. 'public' => false, // disable access directly via permalink url
  75. 'publicly_queryable' => false,
  76. 'show_ui' => true,
  77. 'has_archive' => false,
  78. 'capability_type' => LP_LESSON_CPT,
  79. 'map_meta_cap' => true,
  80. 'show_in_menu' => 'learn_press',
  81. 'show_in_admin_bar' => true,
  82. 'show_in_nav_menus' => true,
  83. 'supports' => array( 'title', 'editor', 'revisions' ),
  84. 'hierarchical' => true,
  85. 'rewrite' => array( 'slug' => 'questions', 'hierarchical' => true, 'with_front' => false )
  86. )
  87. );
  88. register_taxonomy( 'question_tag', array( LP_QUESTION_CPT ),
  89. array(
  90. 'labels' => array(
  91. 'name' => __( 'Question Tag', 'learnpress' ),
  92. 'menu_name' => __( 'Tag', 'learnpress' ),
  93. 'singular_name' => __( 'Tag', 'learnpress' ),
  94. 'add_new_item' => __( 'Add New Tag', 'learnpress' ),
  95. 'all_items' => __( 'All Tags', 'learnpress' )
  96. ),
  97. 'public' => true,
  98. 'hierarchical' => false,
  99. 'show_ui' => true,
  100. 'show_admin_column' => 'true',
  101. 'show_in_nav_menus' => true,
  102. 'rewrite' => array(
  103. 'slug' => 'question-tag',
  104. 'hierarchical' => false,
  105. 'with_front' => false
  106. ),
  107. )
  108. );
  109. add_post_type_support( 'question', 'comments' );
  110. }
  111. static function add_meta_boxes() {
  112. new RW_Meta_Box(
  113. array(
  114. 'id' => 'question_answer',
  115. 'title' => __( 'Answer', 'learnpress' ),
  116. 'pages' => array( LP()->question_post_type ),
  117. 'fields' => array(
  118. array(
  119. 'name' => __( '', 'learnpress' ),
  120. 'id' => "_lp_question_answer",
  121. 'type' => 'question'
  122. )
  123. )
  124. )
  125. );
  126. $meta_box = self::settings_meta_box();
  127. new RW_Meta_Box( $meta_box );
  128. }
  129. static function settings_meta_box() {
  130. $prefix = '_lp_';
  131. $meta_box = array(
  132. 'id' => 'question_settings',
  133. 'title' => __( 'Settings', 'learnpress' ),
  134. 'pages' => array( LP()->question_post_type ),
  135. 'fields' => array(
  136. array(
  137. 'name' => __( 'Mark for this question', 'learnpress' ),
  138. 'id' => "{$prefix}mark",
  139. 'type' => 'number',
  140. 'clone' => false,
  141. 'desc' => __( 'Mark for choosing the right answer', 'learnpress' ),
  142. 'min' => 1,
  143. 'std' => 1
  144. ),
  145. array(
  146. 'name' => __( 'Question explanation', 'learnpress' ),
  147. 'id' => "{$prefix}explanation",
  148. 'type' => 'textarea',
  149. 'desc' => __( 'Explanation why an option is true and other is false', 'learnpress' ),
  150. 'std' => null
  151. ),
  152. array(
  153. 'name' => __( 'Question hint', 'learnpress' ),
  154. 'id' => "{$prefix}hint",
  155. 'type' => 'textarea',
  156. 'desc' => __( 'Instruction for user select the right answer', 'learnpress' ),
  157. 'std' => null
  158. )
  159. )
  160. );
  161. return apply_filters( 'learn_press_question_meta_box_args', $meta_box );
  162. }
  163. /**
  164. * Enqueue scripts
  165. */
  166. static function admin_scripts() {
  167. if ( !in_array( get_post_type(), array( LP()->question_post_type ) ) ) return;
  168. ob_start();
  169. ?>
  170. <script>
  171. var form = $('#post');
  172. form.submit(function (evt) {
  173. var $title = $('#title'),
  174. is_error = false;
  175. if (0 == $title.val().length) {
  176. alert('<?php _e( 'Please enter the title of the question', 'learnpress' );?>');
  177. $title.focus();
  178. is_error = true;
  179. } else if ($('.lpr-question-types').length && ( 0 == $('.lpr-question-types').val().length )) {
  180. alert('<?php _e( 'Please a type of question', 'learnpress' );?>');
  181. $('.lpr-question-types').focus();
  182. is_error = true;
  183. }
  184. if (is_error) {
  185. evt.preventDefault();
  186. return false;
  187. }
  188. });
  189. </script>
  190. <?php
  191. $script = ob_get_clean();
  192. $script = preg_replace( '!</?script>!', '', $script );
  193. learn_press_enqueue_script( $script );
  194. ob_start();
  195. ?>
  196. <script type="text/html" id="tmpl-form-quick-add-question">
  197. <div id="lpr-form-quick-add-question" class="lpr-quick-add-form">
  198. <input type="text">
  199. <select class="lpr-question-types lpr-select2" name="lpr_question[type]" id="lpr-quiz-question-type">
  200. <?php if ( $questions = learn_press_question_types() ): ?>
  201. <?php foreach ( $questions as $type => $name ): ?>
  202. <option value="<?php echo $type; ?>"><?php echo $name; ?></option>
  203. <?php endforeach; ?>
  204. <?php endif; ?>
  205. </select>
  206. <button class="button" data-action="add" type="button"><?php _e( 'Add [Enter]', 'learnpress' ); ?></button>
  207. <button data-action="cancel" class="button" type="button"><?php _e( 'Cancel [ESC]', 'learnpress' ); ?></button>
  208. <span class="lpr-ajaxload">...</span>
  209. </div>
  210. </script>
  211. <?php
  212. $js_template = ob_get_clean();
  213. learn_press_enqueue_script( $js_template, true );
  214. }
  215. /**
  216. * Add columns to admin manage question page
  217. *
  218. * @param array $columns
  219. *
  220. * @return array
  221. */
  222. function columns_head( $columns ) {
  223. $pos = array_search( 'title', array_keys( $columns ) );
  224. $new_columns = array(
  225. LP()->quiz_post_type => __( 'Quiz', 'learnpress' ),
  226. 'type' => __( 'Type', 'learnpress' )
  227. );
  228. if ( false !== $pos && !array_key_exists( LP()->quiz_post_type, $columns ) ) {
  229. $columns = array_merge(
  230. array_slice( $columns, 0, $pos + 1 ),
  231. $new_columns,
  232. array_slice( $columns, $pos + 1 )
  233. );
  234. }
  235. $user = wp_get_current_user();
  236. if ( in_array( LP()->teacher_role, $user->roles ) ) {
  237. unset( $columns['author'] );
  238. }
  239. return $columns;
  240. }
  241. /**
  242. * Displaying the content of extra columns
  243. *
  244. * @param $name
  245. * @param $post_id
  246. */
  247. function columns_content( $name, $post_id ) {
  248. switch ( $name ) {
  249. case LP()->quiz_post_type:
  250. $quizzes = learn_press_get_question_quizzes( $post_id );
  251. if ( $quizzes ) {
  252. foreach ( $quizzes as $quiz ) {
  253. echo '<div><a href="' . esc_url( add_query_arg( array( 'filter_quiz' => $quiz->ID ) ) ) . '">' . get_the_title( $quiz->ID ) . '</a>';
  254. echo '<div class="row-actions">';
  255. printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $quiz->ID ) ), __( 'Edit', 'learnpress' ) );
  256. echo "&nbsp;|&nbsp;";
  257. printf( '<a href="%s">%s</a>', get_the_permalink( $quiz->ID ), __( 'View', 'learnpress' ) );
  258. echo "&nbsp;|&nbsp;";
  259. if ( $quiz_id = learn_press_get_request( 'filter_quiz' ) ) {
  260. printf( '<a href="%s">%s</a>', remove_query_arg( 'filter_quiz' ), __( 'Remove Filter', 'learnpress' ) );
  261. } else {
  262. printf( '<a href="%s">%s</a>', add_query_arg( 'filter_quiz', $quiz->ID ), __( 'Filter', 'learnpress' ) );
  263. }
  264. echo '</div></div>';
  265. }
  266. } else {
  267. _e( 'Not assigned yet', 'learnpress' );
  268. }
  269. break;
  270. case 'type':
  271. echo learn_press_question_name_from_slug( get_post_meta( $post_id, '_lp_type', true ) );
  272. }
  273. }
  274. /**
  275. * @param $join
  276. *
  277. * @return string
  278. */
  279. function posts_join_paged( $join ) {
  280. if ( !$this->_is_archive() ) {
  281. return $join;
  282. }
  283. global $wpdb;
  284. if ( $quiz_id = $this->_filter_quiz() || ( $this->_get_orderby() == 'quiz-name' ) ) {
  285. $join .= " LEFT JOIN {$wpdb->prefix}learnpress_quiz_questions qq ON {$wpdb->posts}.ID = qq.question_id";
  286. $join .= " LEFT JOIN {$wpdb->posts} q ON q.ID = qq.quiz_id";
  287. }
  288. return $join;
  289. }
  290. /**
  291. * @param $where
  292. *
  293. * @return mixed|string
  294. */
  295. function posts_where_paged( $where ) {
  296. if ( !$this->_is_archive() ) {
  297. return $where;
  298. }
  299. global $wpdb;
  300. if ( $quiz_id = $this->_filter_quiz() ) {
  301. $where .= $wpdb->prepare( " AND (q.ID = %d)", $quiz_id );
  302. }
  303. return $where;
  304. if ( isset( $_GET['s'] ) ) {
  305. $s = $_GET['s'];
  306. if ( !empty( $s ) ) {
  307. $where = preg_replace(
  308. "/\.post_content\s+LIKE\s*(\'[^\']+\')\s*\)/",
  309. " .post_content LIKE '%$s%' ) OR ({$wpdb->posts}.post_title LIKE '%$s%' )", $where
  310. );
  311. }
  312. }
  313. return $where;
  314. }
  315. /**
  316. * @param $order_by_statement
  317. *
  318. * @return string
  319. */
  320. function posts_orderby( $order_by_statement ) {
  321. if ( !$this->_is_archive() ) {
  322. return $order_by_statement;
  323. }
  324. if ( isset ( $_GET['orderby'] ) && isset ( $_GET['order'] ) ) {
  325. switch ( $_GET['orderby'] ) {
  326. case 'quiz-name':
  327. $order_by_statement = "q.post_title {$_GET['order']}";
  328. break;
  329. }
  330. }
  331. return $order_by_statement;
  332. }
  333. /**
  334. * @param $columns
  335. *
  336. * @return mixed
  337. */
  338. function columns_sortable( $columns ) {
  339. $columns[LP()->quiz_post_type] = 'quiz-name';
  340. return $columns;
  341. }
  342. static function admin_styles() {
  343. }
  344. static function admin_params() {
  345. }
  346. function save() {
  347. $this->save_post();
  348. }
  349. private function _is_archive() {
  350. global $pagenow, $post_type;
  351. if ( !is_admin() || ( $pagenow != 'edit.php' ) || ( LP()->question_post_type != $post_type ) ) {
  352. return false;
  353. }
  354. return true;
  355. }
  356. private function _filter_quiz() {
  357. return !empty( $_REQUEST['filter_quiz'] ) ? absint( $_REQUEST['filter_quiz'] ) : false;
  358. }
  359. private function _get_orderby() {
  360. return isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
  361. }
  362. } // end LP_Question_Post_Type
  363. }
  364. new LP_Question_Post_Type();