| @@ -1749,5 +1749,75 @@ | ||
| 1749 | 1749 | return ( $duration['durationHours'] * HOUR_IN_SECONDS ) |
| 1750 | 1750 | + ( $duration['durationMinutes'] * MINUTE_IN_SECONDS ) |
| 1751 | 1751 | + ( $duration['durationSeconds'] ); |
| 1752 | 1752 | } |
| 1753 | + | |
| 1754 | + /** | |
| 1755 | + * Update the post_author of a course's content (topics, lessons, | |
| 1756 | + * quizzes and assignments) to a given user. | |
| 1757 | + * | |
| 1758 | + * @since 4.0.3 | |
| 1759 | + * | |
| 1760 | + * @param int $course_id course id. | |
| 1761 | + * @param int $author_id new author (user) id. | |
| 1762 | + * | |
| 1763 | + * @return bool | |
| 1764 | + */ | |
| 1765 | + public static function update_course_content_author( int $course_id, int $author_id ): bool { | |
| 1766 | + global $wpdb; | |
| 1767 | + | |
| 1768 | + $content_ids = get_posts( | |
| 1769 | + array( | |
| 1770 | + 'post_parent' => $course_id, | |
| 1771 | + 'post_type' => tutor()->topics_post_type, | |
| 1772 | + 'fields' => 'ids', | |
| 1773 | + 'posts_per_page' => -1, | |
| 1774 | + 'post_status' => 'any', | |
| 1775 | + ) | |
| 1776 | + ); | |
| 1777 | + | |
| 1778 | + $content_ids = is_array( $content_ids ) ? $content_ids : array(); | |
| 1779 | + $default_post_types = array( tutor()->lesson_post_type, tutor()->quiz_post_type ); | |
| 1780 | + $content_post_types = array_unique( apply_filters( 'tutor_course_contents_post_types', $default_post_types ) ); | |
| 1781 | + | |
| 1782 | + $primary_table = 'posts as course'; | |
| 1783 | + $topic_table = 'posts as topic'; | |
| 1784 | + $content_table = 'posts as content'; | |
| 1785 | + | |
| 1786 | + $joined_data = QueryHelper::get_joined_data( | |
| 1787 | + $primary_table, | |
| 1788 | + array( | |
| 1789 | + array( | |
| 1790 | + 'type' => 'INNER', | |
| 1791 | + 'table' => $topic_table, | |
| 1792 | + 'on' => 'course.ID = topic.post_parent', | |
| 1793 | + ), | |
| 1794 | + array( | |
| 1795 | + 'type' => 'INNER', | |
| 1796 | + 'table' => $content_table, | |
| 1797 | + 'on' => 'topic.ID = content.post_parent', | |
| 1798 | + ), | |
| 1799 | + ), | |
| 1800 | + array( 'content.ID' ), | |
| 1801 | + array( | |
| 1802 | + 'course.ID' => $course_id, | |
| 1803 | + 'content.post_type' => array( 'IN', $content_post_types ), | |
| 1804 | + ), | |
| 1805 | + array(), | |
| 1806 | + '', | |
| 1807 | + -1 | |
| 1808 | + ); | |
| 1809 | + | |
| 1810 | + $content_ids = array_merge( $content_ids, wp_list_pluck( $joined_data['results'], 'ID' ) ); | |
| 1811 | + $content_ids = array_filter( array_unique( array_map( 'absint', $content_ids ) ) ); | |
| 1812 | + | |
| 1813 | + if ( empty( $content_ids ) ) { | |
| 1814 | + return false; | |
| 1815 | + } | |
| 1816 | + | |
| 1817 | + return (bool) QueryHelper::update_where_in( | |
| 1818 | + $wpdb->posts, | |
| 1819 | + array( 'post_author' => $author_id ), | |
| 1820 | + implode( ',', $content_ids ) | |
| 1821 | + ); | |
| 1822 | + } | |
| 1753 | 1823 | } |