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
QnAGenerator.php
188 lines
| 1 | <?php |
| 2 | /** |
| 3 | * QnA view generator |
| 4 | * |
| 5 | * @package tutor-droip-elements |
| 6 | */ |
| 7 | |
| 8 | namespace TutorLMSDroip\ElementGenerator; |
| 9 | |
| 10 | use Droip\HelperFunctions as DroipHelperFunctions; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class QnAGenerator |
| 18 | * This class is used to define all QnA functions. |
| 19 | */ |
| 20 | trait QnAGenerator { |
| 21 | |
| 22 | /** |
| 23 | * Generate QnA Editor |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | private function generate_qna_editor() { |
| 28 | $comment_id = 0; |
| 29 | if ( ! empty( $this->options ) && ! empty( $this->options['comment']->comment_ID ) ) { |
| 30 | $comment_id = $this->options['comment']->comment_ID; |
| 31 | } |
| 32 | |
| 33 | ob_start(); |
| 34 | wp_editor( |
| 35 | '', |
| 36 | 'tutor_qna_reply_editor_' . $comment_id, |
| 37 | tutor_utils()->text_editor_config( |
| 38 | array( |
| 39 | 'plugins' => 'codesample', |
| 40 | 'tinymce' => array( |
| 41 | 'toolbar1' => 'bold,italic,underline,link,unlink,removeformat,image,bullist,codesample', |
| 42 | 'toolbar2' => '', |
| 43 | 'toolbar3' => '', |
| 44 | ), |
| 45 | ) |
| 46 | ) |
| 47 | ); |
| 48 | $editor_markup = ob_get_contents(); |
| 49 | ob_end_clean(); |
| 50 | |
| 51 | $more_attrs_array = array( |
| 52 | 'name' => $this->element['name'], |
| 53 | ); |
| 54 | |
| 55 | if ( ! empty( $this->options['comment'] ) ) { |
| 56 | $more_attrs_array['comment_parent'] = $this->options['comment']->comment_ID; |
| 57 | } |
| 58 | |
| 59 | $more_attrs = ''; |
| 60 | |
| 61 | foreach ( $more_attrs_array as $key => $value ) { |
| 62 | $more_attrs .= "$key=\"$value\""; |
| 63 | } |
| 64 | |
| 65 | return <<<EOL |
| 66 | <div $this->attributes $more_attrs> |
| 67 | $editor_markup |
| 68 | </div> |
| 69 | EOL; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Generate QnA Reply |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | private function generate_qna_reply() { |
| 78 | $templates = $this->detectReplyTemplateByUser(); |
| 79 | |
| 80 | $non_current_user_reply_template_id = $templates[0]; |
| 81 | $current_user_reply_template_id = $templates[1]; |
| 82 | |
| 83 | $child = ''; |
| 84 | |
| 85 | if ( (int) $this->options['comment']->user_id === get_current_user_ID() ) { |
| 86 | $child .= call_user_func( |
| 87 | $this->generate_child_element, |
| 88 | $current_user_reply_template_id, |
| 89 | $this->options |
| 90 | ); |
| 91 | } else { |
| 92 | $child .= call_user_func( |
| 93 | $this->generate_child_element, |
| 94 | $non_current_user_reply_template_id, |
| 95 | $this->options |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | $name = $this->element['name']; |
| 100 | |
| 101 | return <<<EOL |
| 102 | <div $this->attributes data-ele_name='$name'> |
| 103 | $child |
| 104 | </div> |
| 105 | EOL; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Generate QnA Reply Button |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | private function generate_qna_reply_button() { |
| 114 | $child = ''; |
| 115 | |
| 116 | foreach ( $this->element['children'] as $child_id ) { |
| 117 | $child .= call_user_func( |
| 118 | $this->generate_child_element, |
| 119 | $child_id, |
| 120 | $this->options |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $name = $this->element['name']; |
| 125 | $comment_parent = $this->options['comment']->comment_ID; |
| 126 | $commentItemId = $this->options['commentItemId']; |
| 127 | |
| 128 | return <<<EOL |
| 129 | <a |
| 130 | $this->attributes |
| 131 | data-ele_name='$name' |
| 132 | data-comment_parent='$comment_parent' |
| 133 | data-comment_item_id='$commentItemId' |
| 134 | > |
| 135 | $child |
| 136 | </a> |
| 137 | EOL; |
| 138 | } |
| 139 | |
| 140 | private function detectReplyTemplateByUser() { |
| 141 | |
| 142 | return array( |
| 143 | $this->element['children'][0], |
| 144 | $this->element['children'][1], |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Generate QnA Element |
| 150 | * |
| 151 | * @param object $comment (new comment) |
| 152 | * @param string $elementName (new element name to be generated, this could be comment-item or -qna-reply) |
| 153 | * @param array $collection_data (every collection data which comes with request) |
| 154 | * $collection_data = array( |
| 155 | * 'blocks' => array(), |
| 156 | * 'styles' => array(), |
| 157 | * ); |
| 158 | * |
| 159 | * @return string as html preview element |
| 160 | */ |
| 161 | public static function generateQnAElement( $comment, $elementName, $collection_data ) { |
| 162 | $blocks = $collection_data['blocks']; |
| 163 | $styles = $collection_data['styles']; |
| 164 | |
| 165 | $element_id = ''; |
| 166 | |
| 167 | foreach ( $blocks as $key => $block ) { |
| 168 | if ( $elementName === $block['name'] ) { |
| 169 | $element_id = $key; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | $preview = DroipHelperFunctions::get_html_using_preview_script( |
| 175 | $blocks, |
| 176 | $styles, |
| 177 | $element_id, |
| 178 | null, |
| 179 | array( |
| 180 | 'comment' => $comment, |
| 181 | 'componentType' => 'comments', |
| 182 | ) |
| 183 | ); |
| 184 | |
| 185 | return $preview; |
| 186 | } |
| 187 | } |
| 188 |