ActionBoxGenerator.php
1 year ago
AnnouncementsGenerator.php
1 year ago
CourseMetaGenerator.php
1 year ago
CourseThumbnailGenerator.php
1 year ago
ElementGenerator.php
1 year ago
GradebookGenerator.php
1 year ago
InstructorGenerator.php
1 year ago
LessonGenerator.php
1 year ago
Preview.php
1 year ago
QnAGenerator.php
1 year ago
RatingGenerator.php
1 year ago
ResourcesGenerator.php
1 year ago
ShareGenerator.php
1 year ago
TopicsGenerator.php
1 year ago
WishListGenerator.php
1 year ago
RatingGenerator.php
207 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Preview script for html markup generator |
| 4 | * |
| 5 | * @package tutor-droip-elements |
| 6 | */ |
| 7 | |
| 8 | namespace TutorLMSDroip\ElementGenerator; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Class Rating genrator |
| 16 | * |
| 17 | * @package TutorLMSDroip\ElementGenerator |
| 18 | */ |
| 19 | trait RatingGenerator { |
| 20 | |
| 21 | /** |
| 22 | * Generate Rating elements |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | private function generate_rating_elements() { |
| 27 | $ele_name = $this->element['name']; |
| 28 | $course_id = isset( $this->options['post'] ) ? $this->options['post']->ID : get_the_ID(); |
| 29 | $show_course_ratings = apply_filters( 'tutor_show_course_ratings', true, $course_id ); |
| 30 | if ( ! $show_course_ratings ) { |
| 31 | return ''; |
| 32 | } |
| 33 | |
| 34 | switch ( $this->element['name'] ) { |
| 35 | case TDE_APP_PREFIX . '-rating-active-icon': |
| 36 | $current_rating = 0; |
| 37 | if ( isset( $this->options['my_rating_input_generator'] ) && $this->options['my_rating_input_generator'] === true ) { |
| 38 | $current_rating = 5; |
| 39 | } elseif ( isset( $this->options['componentType'], $this->options['comment'] ) && $this->options['componentType'] === 'comments' ) { |
| 40 | $current_rating = get_comment_meta( $this->options['comment']->comment_ID, 'tutor_rating', true ); |
| 41 | } else { |
| 42 | $course_rating = tutor_utils()->get_course_rating( $course_id ); |
| 43 | $current_rating = number_format( $course_rating->rating_avg, 2, '.', '' ); |
| 44 | } |
| 45 | |
| 46 | $html = ''; |
| 47 | for ( $i = 1; $i <= 5; $i++ ) { |
| 48 | if ( $i <= round( (int) $current_rating ) ) { |
| 49 | $html .= $this->generate_common_element(); |
| 50 | } |
| 51 | } |
| 52 | return $html; |
| 53 | |
| 54 | case TDE_APP_PREFIX . '-rating-inactive-icon': |
| 55 | $current_rating = 0; |
| 56 | if ( isset( $this->options['my_rating_input_generator'] ) && $this->options['my_rating_input_generator'] === true ) { |
| 57 | $current_rating = 0; |
| 58 | } elseif ( isset( $this->options['componentType'], $this->options['comment'] ) && $this->options['componentType'] === 'comments' ) { |
| 59 | $current_rating = get_comment_meta( $this->options['comment']->comment_ID, 'tutor_rating', true ); |
| 60 | } else { |
| 61 | $course_rating = tutor_utils()->get_course_rating( $course_id ); |
| 62 | $current_rating = number_format( $course_rating->rating_avg, 2, '.', '' ); |
| 63 | } |
| 64 | |
| 65 | $html = ''; |
| 66 | for ( $i = 1; $i <= 5; $i++ ) { |
| 67 | if ( $i > round( (int) $current_rating ) ) { |
| 68 | $html .= $this->generate_common_element(); |
| 69 | } |
| 70 | } |
| 71 | return $html; |
| 72 | |
| 73 | case TDE_APP_PREFIX . '-rating-avarage-count': |
| 74 | if ( isset( $this->options['tutor_review_avg_count'] ) ) { |
| 75 | $settings = isset( $this->properties['settings'] ) ? $this->properties['settings'] : array(); |
| 76 | $template = isset( $settings['template'] ) ? $settings['template'] : '{{avarage}}'; |
| 77 | $html = str_replace( '{{avarage}}', $this->options['tutor_review_avg_count'], $template ); |
| 78 | |
| 79 | return $this->generate_common_element( false, $html ); |
| 80 | } else { |
| 81 | $course_id = isset( $this->options['post'] ) ? $this->options['post']->ID : get_the_ID(); |
| 82 | $course_rating = tutor_utils()->get_course_rating( $course_id ); |
| 83 | $settings = isset( $this->properties['settings'] ) ? $this->properties['settings'] : array(); |
| 84 | $template = isset( $settings['template'] ) ? $settings['template'] : '{{avarage}}'; |
| 85 | $html = str_replace( '{{avarage}}', $course_rating->rating_avg, $template ); |
| 86 | return $this->generate_common_element( false, $html ); |
| 87 | } |
| 88 | |
| 89 | case TDE_APP_PREFIX . '-rating-total-count': |
| 90 | if ( isset( $this->options['tutor_review_summery_value'] ) ) { |
| 91 | $settings = isset( $this->properties['settings'] ) ? $this->properties['settings'] : array(); |
| 92 | $template = isset( $settings['template'] ) ? $settings['template'] : '{{avarage}}'; |
| 93 | $html = str_replace( '{{count}}', $this->options['tutor_review_summery_value'], $template ); |
| 94 | |
| 95 | return $this->generate_common_element( false, $html ); |
| 96 | } else { |
| 97 | $course_rating = tutor_utils()->get_course_rating( $course_id ); |
| 98 | $settings = isset( $this->properties['settings'] ) ? $this->properties['settings'] : array(); |
| 99 | $template = isset( $settings['template'] ) ? $settings['template'] : '{{avarage}}'; |
| 100 | $html = str_replace( '{{count}}', $course_rating->rating_count, $template ); |
| 101 | return $this->generate_common_element( false, $html ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Generate Review elements |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | private function generate_review_elements() { |
| 112 | $ele_name = $this->element['name']; |
| 113 | $course_id = isset( $this->options['post'] ) ? $this->options['post']->ID : get_the_ID(); |
| 114 | $show_course_ratings = apply_filters( 'tutor_show_course_ratings', true, $course_id ); |
| 115 | if ( ! $show_course_ratings ) { |
| 116 | return ''; |
| 117 | } |
| 118 | switch ( $this->element['name'] ) { |
| 119 | case TDE_APP_PREFIX . '-rating-empty': |
| 120 | $reviews = $this->get_tutor_reviews(); |
| 121 | if ( ! is_array( $reviews ) || ! count( $reviews ) ) { |
| 122 | return $this->generate_common_element(); |
| 123 | } else { |
| 124 | return ''; |
| 125 | } |
| 126 | |
| 127 | case TDE_APP_PREFIX . '-rating-not-empty': |
| 128 | $reviews = $this->get_tutor_reviews(); |
| 129 | if ( ! is_array( $reviews ) || ! count( $reviews ) ) { |
| 130 | return ''; |
| 131 | } else { |
| 132 | // $this->options['tutor_reviews'] = $reviews; |
| 133 | return $this->generate_common_element(); |
| 134 | } |
| 135 | |
| 136 | case TDE_APP_PREFIX . '-rating-summery': |
| 137 | $course_rating = tutor_utils()->get_course_rating( $course_id ); |
| 138 | |
| 139 | $html = ''; |
| 140 | $child = $this->element['children'][0]; |
| 141 | foreach ( $course_rating->count_by_value as $key => $value ) { |
| 142 | $rating_count_percent = ( $value > 0 ) ? ( $value * 100 ) / $course_rating->rating_count : 0; |
| 143 | $html .= call_user_func( |
| 144 | $this->generate_child_element_with_new_id, |
| 145 | $this->elements, |
| 146 | $this->style_blocks, |
| 147 | $child, |
| 148 | array_merge( |
| 149 | $this->options, |
| 150 | array( |
| 151 | 'tutor_review_summery_index' => $key, |
| 152 | 'tutor_review_summery_value' => $value, |
| 153 | 'tutor_review_count_percent' => $rating_count_percent, |
| 154 | 'tutor_review_avg_count' => $value, // TODO: need to recheck avarage count. |
| 155 | ) |
| 156 | ) |
| 157 | ); |
| 158 | } |
| 159 | return $this->generate_common_element( false, $html ); |
| 160 | |
| 161 | case TDE_APP_PREFIX . '-rating-summery-item-index': |
| 162 | return $this->generate_common_element( false, $this->options['tutor_review_summery_index'] ); |
| 163 | |
| 164 | case TDE_APP_PREFIX . '-rating-summery-item-progress': |
| 165 | $id = $this->element['id']; |
| 166 | $per = $this->options['tutor_review_count_percent']; |
| 167 | $html = "<style>[data-droip='$id']{width: $per% !important;}</style>" . $this->generate_common_element( false ); |
| 168 | return $html; |
| 169 | |
| 170 | case TDE_APP_PREFIX . '-review-add-edit': |
| 171 | // TODO: check user can review or not. if not return "";. |
| 172 | $my_rating = tutor_utils()->get_reviews_by_user( 0, 0, 150, false, $course_id, array( 'approved', 'hold' ) ); |
| 173 | if ( ! $my_rating || !$course_id) { |
| 174 | return ''; |
| 175 | } |
| 176 | $this->options['my_rating'] = $my_rating; |
| 177 | $this->options['my_rating_input_generator'] = true; |
| 178 | $children_html = $this->generate_child_elements(); |
| 179 | $active_rating = $my_rating ? $my_rating->rating : 0; |
| 180 | $review_id = $my_rating ? $my_rating->comment_ID : null; |
| 181 | $children_html .= "<input type='hidden' name='course_id' value='$course_id' />"; |
| 182 | $children_html .= "<input type='hidden' name='review_id' value='$review_id' />"; |
| 183 | return "<div $this->attributes data-ele_name='$ele_name' data-active_rating='$active_rating'>$children_html</div>"; |
| 184 | |
| 185 | case TDE_APP_PREFIX . '-review-text-input': |
| 186 | $my_rating = $this->options['my_rating']; |
| 187 | $html = stripslashes( $my_rating ? $my_rating->comment_content : '' ); |
| 188 | return $this->generate_common_element( false, $html ); |
| 189 | |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get Tutor Reviews |
| 195 | * |
| 196 | * @return array |
| 197 | */ |
| 198 | private function get_tutor_reviews() { |
| 199 | $per_page = tutor_utils()->get_option( 'pagination_per_page', 10 ); |
| 200 | $offset = 0;// TODO: need to check tutor functionality. |
| 201 | $current_user_id = get_current_user_id(); |
| 202 | $course_id = isset( $this->options['post'] ) ? $this->options['post']->ID : get_the_ID(); |
| 203 | $reviews = tutor_utils()->get_course_reviews( $course_id, $offset, $per_page, false, array( 'approved' ), $current_user_id ); |
| 204 | return $reviews; |
| 205 | } |
| 206 | } |
| 207 |