PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/course/lp-course-functions.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 457 lines | 258 code | 55 blank | 144 comment | 55 complexity | 79515e75612a02b4d887fc8c909ab1ee MD5 | raw file
  1. <?php
  2. /**
  3. * Common functions to manipulate with course, lesson, quiz, questions, etc...
  4. *
  5. * @author ThimPress
  6. * @package LearnPress/Functions
  7. * @version 1.0
  8. */
  9. if ( !defined( 'ABSPATH' ) ) {
  10. exit; // Exit if accessed directly
  11. }
  12. /**
  13. * @param $the_course
  14. *
  15. * @return LP_Course|mixed
  16. */
  17. function learn_press_get_course( $the_course ) {
  18. return LP_Course::get_course( $the_course );
  19. }
  20. function learn_press_get_quiz( $the_quiz ) {
  21. return LP_Quiz::get_quiz( $the_quiz );
  22. }
  23. /**
  24. * print out class for quiz body
  25. *
  26. * @param null $class
  27. *
  28. * @return bool
  29. */
  30. function learn_press_quiz_class( $class = null ) {
  31. $quiz = LP()->quiz;
  32. $user = LP()->user;
  33. if ( !$quiz ) {
  34. return false;
  35. }
  36. if ( $class && is_string( $class ) ) {
  37. $class = explode( ' ', $class );
  38. } elseif ( !$class ) {
  39. $class = array();
  40. }
  41. $class[] = "single-quiz";
  42. if ( $status = $user->get_quiz_status( $quiz->id ) ) {
  43. $class[] = 'quiz-' . $status;
  44. }
  45. if ( $quiz->has( 'questions' ) ) {
  46. $class[] = 'has-questions';
  47. }
  48. $class[] = 'clearfix';
  49. $class = array_unique( $class );
  50. post_class( join( ' ', $class ) );
  51. }
  52. /**
  53. * Get the courses that a item is assigned to
  54. *
  55. * @param $item
  56. *
  57. * @return mixed
  58. */
  59. function learn_press_get_item_courses( $item ) {
  60. global $wpdb;
  61. $query = $wpdb->prepare( "
  62. SELECT c.*
  63. FROM {$wpdb->posts} c
  64. INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id
  65. INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id
  66. WHERE si.item_id = %d
  67. ", $item );
  68. return $wpdb->get_results( $query );
  69. }
  70. /**
  71. * Get the quizzes that a question is assigned to
  72. *
  73. * @param $question_id
  74. *
  75. * @return mixed
  76. */
  77. function learn_press_get_question_quizzes( $question_id ) {
  78. global $wpdb;
  79. $query = $wpdb->prepare( "
  80. SELECT q.*
  81. FROM {$wpdb->posts} q
  82. INNER JOIN {$wpdb->prefix}learnpress_quiz_questions qq ON q.ID = qq.quiz_id
  83. WHERE qq.question_id = %d
  84. ", $question_id );
  85. return $wpdb->get_results( $query );
  86. }
  87. function learn_press_course_post_type_link( $permalink, $post ) {
  88. if ( $post->post_type !== 'lp_course' ) {
  89. return $permalink;
  90. }
  91. // Abort early if the placeholder rewrite tag isn't in the generated URL
  92. if ( false === strpos( $permalink, '%' ) ) {
  93. return $permalink;
  94. }
  95. // Get the custom taxonomy terms in use by this post
  96. $terms = get_the_terms( $post->ID, 'course_category' );
  97. if ( !empty( $terms ) ) {
  98. usort( $terms, '_usort_terms_by_ID' ); // order by ID
  99. $category_object = apply_filters( 'learn_press_course_post_type_link_course_category', $terms[0], $terms, $post );
  100. $category_object = get_term( $category_object, 'course_category' );
  101. $course_category = $category_object->slug;
  102. if ( $parent = $category_object->parent ) {
  103. $ancestors = get_ancestors( $category_object->term_id, 'course_category' );
  104. foreach ( $ancestors as $ancestor ) {
  105. $ancestor_object = get_term( $ancestor, 'course_category' );
  106. $course_category = $ancestor_object->slug . '/' . $course_category;
  107. }
  108. }
  109. } else {
  110. // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
  111. $course_category = _x( 'uncategorized', 'slug', 'learnpress' );
  112. }
  113. $find = array(
  114. '%year%',
  115. '%monthnum%',
  116. '%day%',
  117. '%hour%',
  118. '%minute%',
  119. '%second%',
  120. '%post_id%',
  121. '%category%',
  122. '%course_category%'
  123. );
  124. $replace = array(
  125. date_i18n( 'Y', strtotime( $post->post_date ) ),
  126. date_i18n( 'm', strtotime( $post->post_date ) ),
  127. date_i18n( 'd', strtotime( $post->post_date ) ),
  128. date_i18n( 'H', strtotime( $post->post_date ) ),
  129. date_i18n( 'i', strtotime( $post->post_date ) ),
  130. date_i18n( 's', strtotime( $post->post_date ) ),
  131. $post->ID,
  132. $course_category,
  133. $course_category
  134. );
  135. $permalink = str_replace( $find, $replace, $permalink );
  136. return $permalink;
  137. }
  138. add_filter( 'post_type_link', 'learn_press_course_post_type_link', 10, 2 );
  139. /**
  140. * Get the final quiz for a course if it is existing
  141. *
  142. * @param $course_id
  143. *
  144. * @return mixed
  145. * @throws Exception
  146. */
  147. function learn_press_get_final_quiz( $course_id ) {
  148. $course = LP_Course::get_course( $course_id );
  149. if ( !$course ) {
  150. throw new Exception( sprintf( __( 'The course %d does not exists', 'learnpress' ), $course_id ) );
  151. }
  152. $course_items = $course->get_curriculum_items();
  153. $final = false;
  154. if ( $course_items ) {
  155. $end = end( $course_items );
  156. if ( $end->post_type == LP()->quiz_post_type ) {
  157. $final = $end->ID;
  158. }
  159. }
  160. return apply_filters( 'learn_press_course_final_quiz', $final, $course_id );
  161. }
  162. function learn_press_item_meta_format( $item, $nonce = '' ) {
  163. if ( current_theme_supports( 'post-formats' ) ) {
  164. $format = get_post_format( $item );
  165. if ( false === $format ) {
  166. $format = 'standard';
  167. }
  168. //return false to hide post format
  169. if ( $format = apply_filters( 'learn_press_course_item_format', $format, $item ) ) {
  170. //printf( '<span class="lp-label lp-label-format lp-label-format-%s">%s</span>', $format, ucfirst( $format ) );
  171. printf( '<label for="post-format-0" class="post-format-icon post-format-%s" title="%s"></label>', $format, ucfirst( $format ) );
  172. } else {
  173. echo $nonce;
  174. }
  175. }
  176. }
  177. function learn_press_course_item_format_exclude( $format, $item ) {
  178. if ( get_post_type( $item ) != LP()->lesson_post_type || ( $format == 'standard' ) ) {
  179. $format = false;
  180. }
  181. return $format;
  182. }
  183. //add_filter( 'learn_press_course_item_format', 'learn_press_course_item_format_exclude', 5, 2 );
  184. /*******************************************************/
  185. /*******************************************************/
  186. /**
  187. * Get curriculum of a course
  188. *
  189. * @version 1.0
  190. *
  191. * @param $course_id
  192. *
  193. * @return mixed
  194. */
  195. function learn_press_get_course_curriculum( $course_id ) {
  196. $course = LP_Course::get_course( $course_id );
  197. return $course->get_curriculum();
  198. }
  199. /**
  200. * Verify course access
  201. *
  202. * @param int $course_id
  203. * @param int $user_id
  204. *
  205. * @return boolean
  206. */
  207. function learn_press_is_enrolled_course( $course_id = null, $user_id = null ) {
  208. //_deprecated_function( __FUNCTION__, '1.0', 'LP_User -> has_enrolled_course');
  209. if ( $course = LP_Course::get_course( $course_id ) && $user = learn_press_get_user( $user_id ) ) {
  210. return $user->has_enrolled_course( $course_id );
  211. }
  212. return false;
  213. }
  214. /**
  215. * Detect if a course is free or not
  216. *
  217. * @param null $course_id
  218. *
  219. * @return bool
  220. */
  221. function learn_press_is_free_course( $course_id = null ) {
  222. if ( !$course_id ) {
  223. $course_id = get_the_ID();
  224. }
  225. return learn_press_get_course( $course_id )->is_free();
  226. }
  227. /**
  228. * get current status of user's course
  229. *
  230. * @author Tunn
  231. *
  232. * @param int $user_id
  233. * @param int $course_id
  234. *
  235. * @return string
  236. */
  237. function learn_press_get_user_course_status( $user_id = null, $course_id = null ) {
  238. //_deprecated_function( __FUNCTION__, '1.0', 'LP_User() -> get_course_status');
  239. if ( $course = LP_Course::get_course( $course_id ) && $user = learn_press_get_user( $user_id ) ) {
  240. return $user->get_course_status( $course_id );
  241. }
  242. return false;
  243. }
  244. /**
  245. * Check to see if user can view a lesson or not
  246. *
  247. * @since 0.9.5
  248. *
  249. * @param int $lesson_id
  250. * @param int $course_id
  251. * @param int $user_id
  252. *
  253. * @return boolean
  254. */
  255. function learn_press_user_can_view_lesson( $lesson_id, $course_id = 0, $user_id = null ) {
  256. if ( $user_id ) {
  257. $user = learn_press_get_user( $user_id );
  258. } else {
  259. $user = LP()->user;
  260. }
  261. return $user ? $user->can( 'view-lesson', $lesson_id, $course_id ) : false;
  262. }
  263. /**
  264. * Check to see if user can view a quiz or not
  265. *
  266. * @param int $quiz_id
  267. * @param int $course_id
  268. * @param int $user_id
  269. *
  270. * @return boolean
  271. */
  272. function learn_press_user_can_view_quiz( $quiz_id = null, $course_id = 0, $user_id = null ) {
  273. if ( $user_id ) {
  274. $user = learn_press_get_user( $user_id );
  275. } else {
  276. $user = LP()->user;
  277. }
  278. return $user ? $user->can( 'view-quiz', $quiz_id, $course_id ) : false;
  279. }
  280. /**
  281. * Get course setting is enroll required or public
  282. *
  283. * @since 0.9.5
  284. *
  285. * @param int $course_id
  286. *
  287. * @return boolean
  288. */
  289. function learn_press_course_enroll_required( $course_id = null ) {
  290. $course_id = learn_press_get_course_id( $course_id );
  291. $required = ( 'yes' == get_post_meta( $course_id, '_lpr_course_enrolled_require', true ) );
  292. return apply_filters( 'learn_press_course_enroll_required', $required, $course_id );
  293. }
  294. /**
  295. * Checks to see that an user has finished a lesson or not yet
  296. * Function return the ID of a course if the user has completed a lesson
  297. * Otherwise, return false
  298. *
  299. * @author TuNguyen
  300. *
  301. * @param null $lesson_id
  302. * @param null $user_id
  303. *
  304. * @return mixed
  305. */
  306. function learn_press_user_has_completed_lesson( $lesson_id = null, $user_id = null ) {
  307. _deprecated_function( __FUNCTION__, '1.0', 'LP_User() -> has_completed_quiz' );
  308. if ( $user = learn_press_get_user( $user_id ) ) {
  309. return $user->has_completed_quiz( $lesson_id );
  310. }
  311. return false;
  312. $lesson_id = learn_press_get_lesson_id( $lesson_id );
  313. if ( !$user_id ) $user_id = get_current_user_id();
  314. $completed_lessons = get_user_meta( $user_id, '_lpr_lesson_completed', true );
  315. if ( !$completed_lessons ) return false;
  316. foreach ( $completed_lessons as $courses ) {
  317. if ( is_array( $courses ) && in_array( $lesson_id, $courses ) ) {
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. /**
  324. * Short function to check if a lesson id is not passed to a function
  325. * then try to get it from $_REQUEST
  326. *
  327. * @param null $lesson_id
  328. *
  329. * @return int|null
  330. */
  331. function learn_press_get_lesson_id( $lesson_id = null ) {
  332. if ( !$lesson_id ) {
  333. $lesson_id = !empty( $_REQUEST['lesson'] ) ? $_REQUEST['lesson'] : 0;
  334. }
  335. return $lesson_id;
  336. }
  337. function learn_press_get_all_courses( $args = array() ) {
  338. $term = '';
  339. $exclude = '';
  340. is_array( $args ) && extract( $args );
  341. $args = array(
  342. 'post_type' => array( 'lp_course' ),
  343. 'post_status' => 'publish',
  344. 'posts_per_page' => - 1,
  345. 's' => $term,
  346. 'fields' => 'ids',
  347. 'exclude' => $exclude
  348. );
  349. $args = apply_filters( 'learn_press_get_courses_args', $args );
  350. $posts = get_posts( $args );
  351. return apply_filters( 'learn_press_get_courses', $posts, $args );
  352. }
  353. function learn_press_search_post_excerpt( $where = '' ) {
  354. global $wp_the_query, $wpdb;
  355. if ( empty( $wp_the_query->query_vars['s'] ) )
  356. return $where;
  357. $where = preg_replace(
  358. "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
  359. "post_title LIKE $1) OR ({$wpdb->posts}.post_excerpt LIKE $1", $where );
  360. return $where;
  361. }
  362. //add_filter( 'posts_where', 'learn_press_search_post_excerpt' );
  363. /**
  364. * Return true if a course is required review before submit
  365. *
  366. * @param null $course_id
  367. * @param null $user_id
  368. *
  369. * @return bool
  370. */
  371. function learn_press_course_is_required_review( $course_id = null, $user_id = null ) {
  372. if ( !$user_id ) {
  373. $user_id = get_current_user_id();
  374. }
  375. if ( !$course_id ) {
  376. global $post;
  377. $course_id = $post->ID;
  378. }
  379. if ( get_post_type( $course_id ) != 'lp_course' ) {
  380. return false;
  381. }
  382. $user = learn_press_get_user( $user_id );
  383. if ( $user->is_admin() || ( ( $user_course = learn_press_get_user( get_post_field( 'post_author', $course_id ) ) ) && $user_course->is_admin() ) ) {
  384. return false;
  385. }
  386. $required_review = LP()->settings->get( 'required_review' ) == 'yes';
  387. $enable_edit_published = LP()->settings->get( 'enable_edit_published' ) == 'yes';
  388. $is_publish = get_post_status( $course_id ) == 'publish';
  389. return !( ( !$required_review ) || ( $required_review && $enable_edit_published && $is_publish ) );
  390. }
  391. function learn_press_get_course_user( $course_id = null ) {
  392. if ( !$course_id ) {
  393. $course_id = get_the_ID();
  394. }
  395. return learn_press_get_user( get_post_field( 'post_author', $course_id ) );
  396. }
  397. /////////////////////////
  398. function need_to_updating() {
  399. ob_start();
  400. learn_press_display_message( 'This function need to updating' );
  401. return ob_get_clean();
  402. }