Lifter.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Integrations\Lifter; |
| 4 | |
| 5 | use PrestoPlayer\Models\Player; |
| 6 | |
| 7 | /** |
| 8 | * Lifter LMS integration. |
| 9 | */ |
| 10 | class Lifter { |
| 11 | |
| 12 | /** |
| 13 | * Register actions and filters. |
| 14 | * |
| 15 | * @return void |
| 16 | */ |
| 17 | public function register() { |
| 18 | // wait for plugin to be loaded. |
| 19 | add_action( 'plugins_loaded', array( $this, 'addFilters' ) ); |
| 20 | add_filter( 'presto_player_load_js', array( $this, 'maybeLoadScripts' ) ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Load scripts if it is a LifterLMS ( lesson,Quiz,Course ) page. |
| 25 | */ |
| 26 | public function maybeLoadScripts( $has_player ) { |
| 27 | if ( function_exists( 'is_lifterlms' ) && is_lifterlms() ) { |
| 28 | return true; |
| 29 | } |
| 30 | return $has_player; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Add filters needed for LifterLMS |
| 35 | */ |
| 36 | public function addFilters() { |
| 37 | // plugins are not active |
| 38 | if ( ! $this->lifterPluginsActive() ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // display video progress on student table. |
| 43 | add_filter( 'llms_table_get_data_student-course', array( $this, 'displayVideoCompletionData' ), 11, 5 ); |
| 44 | |
| 45 | // adding islesson attribute to our script. |
| 46 | add_filter( 'presto-settings-block-js-options', array( $this, 'addLifterIsLesson' ) ); |
| 47 | |
| 48 | // turn on autoplay and checks if it on lesson page or not. |
| 49 | add_filter( 'presto_player/block/default_attributes', array( $this, 'maybeTurnOnAutoplay' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Are the lifter plugins active? |
| 54 | * |
| 55 | * @return boolean |
| 56 | */ |
| 57 | public function lifterPluginsActive() { |
| 58 | return is_plugin_active( 'lifterlms-advanced-videos/lifterlms-advanced-videos.php' ) && is_plugin_active( 'lifterlms/lifterlms.php' ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Is the lifterLMS autoplay on? |
| 63 | * |
| 64 | * @return boolean |
| 65 | */ |
| 66 | public function isLifterAutoplayOn() { |
| 67 | return 'no' !== get_option( 'llms_av_prog_auto_play', 'no' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Are we on a lesson page. |
| 72 | * |
| 73 | * @return boolean |
| 74 | */ |
| 75 | public function isLessonPage() { |
| 76 | return function_exists( 'is_lesson' ) && is_lesson(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Override our player to turn on autoplay |
| 81 | * if enabled on LifterLMS settings. |
| 82 | * |
| 83 | * @param array $attributes Block attributes. |
| 84 | * @return array |
| 85 | */ |
| 86 | public function maybeTurnOnAutoplay( $attributes ) { |
| 87 | if ( $this->isLessonPage() && $this->isLifterAutoplayOn() ) { |
| 88 | $attributes['autoplay'] = true; |
| 89 | } |
| 90 | |
| 91 | return $attributes; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Send whether it's a lifter lesson |
| 96 | * |
| 97 | * @param array $attributes Block attributes. |
| 98 | * @return array |
| 99 | */ |
| 100 | public function addLifterIsLesson( $attributes ) { |
| 101 | if ( $this->isLessonPage() ) { |
| 102 | $attributes['lifter']['isLesson'] = true; |
| 103 | } |
| 104 | |
| 105 | return $attributes; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Does the post have a player? |
| 110 | * |
| 111 | * @param integer $id Post id. |
| 112 | * @return boolean |
| 113 | */ |
| 114 | public function hasPlayer( $id ) { |
| 115 | return player::postHasPlayer( $id ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the student id. |
| 120 | * |
| 121 | * @return integer |
| 122 | */ |
| 123 | public function getStudentId() { |
| 124 | return llms_filter_input( INPUT_GET, 'student_id', FILTER_SANITIZE_NUMBER_INT ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Render the progress bar |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function renderProgressBar( $table, $value ) { |
| 133 | return $table->get_progress_bar_html( $value ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Has the student completed the video? |
| 138 | * |
| 139 | * @param integer $student_id Student ID |
| 140 | * @param integer $lesson_id Lesson ID |
| 141 | * @return boolean |
| 142 | */ |
| 143 | public function hasStudentHasCompletedVideo( $student_id, $lesson_id ) { |
| 144 | return (bool) llms_av_has_user_completed_video( $student_id, $lesson_id ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the student's current progress. |
| 149 | * |
| 150 | * @param integer $student_id Student ID |
| 151 | * @param integer $lesson_id Lesson ID |
| 152 | * @return number |
| 153 | */ |
| 154 | public function getStudentProgress( $student_id, $lesson_id ) { |
| 155 | $event = llms_av_get_last_user_video_progress_event( $student_id, $lesson_id ); |
| 156 | if ( $event ) { |
| 157 | $duration = $event->get_meta( 'duration' ); |
| 158 | if ( $duration = $event->get_meta( 'duration' ) ) { |
| 159 | $ts = $event->get_meta( 'ts' ) ? $event->get_meta( 'ts' ) : 0; |
| 160 | $value = LLMS()->grades()->round( ( $ts / $duration ) ) * 100; |
| 161 | return $value; |
| 162 | } |
| 163 | } |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Displays video completion data on the |
| 169 | * student table. |
| 170 | * |
| 171 | * @param string $value Value to display. |
| 172 | * @param string $key Column name |
| 173 | * @param \LLMS_Lesson $lesson LifterLMS Lesson Object |
| 174 | * @param mixed $context Context. |
| 175 | * @param \LLMS_Table_Student_Course $table Table object. |
| 176 | * @return void |
| 177 | */ |
| 178 | public function displayVideoCompletionData( $value, $key, $lesson, $context, $table ) { |
| 179 | // bail if not the video column. |
| 180 | if ( 'video' !== $key ) { |
| 181 | return $value; |
| 182 | } |
| 183 | |
| 184 | if ( ! isset( $lesson ) ) { |
| 185 | return $value; |
| 186 | } |
| 187 | |
| 188 | // get the lesson id. |
| 189 | $lesson_id = $lesson->post->ID; |
| 190 | |
| 191 | if ( empty( $lesson_id ) ) { |
| 192 | return $value; |
| 193 | } |
| 194 | |
| 195 | // Bail if lesson does not contain a Presto Player. |
| 196 | if ( ! $this->hasPlayer( $lesson_id ) ) { |
| 197 | return $value; |
| 198 | } |
| 199 | |
| 200 | // get the student id. |
| 201 | if ( ! $student_id = $this->getStudentId() ) { |
| 202 | return $value; |
| 203 | } |
| 204 | |
| 205 | // show the progress bar at 100% is user completed video. |
| 206 | if ( $this->hasStudentHasCompletedVideo( $student_id, $lesson_id ) ) { |
| 207 | return $this->renderProgressBar( $table, 100 ); |
| 208 | } |
| 209 | |
| 210 | // show progress bar if user has progress. |
| 211 | if ( $progress = $this->getStudentProgress( $student_id, $lesson_id ) ) { |
| 212 | return $this->renderProgressBar( $table, $progress ); |
| 213 | } |
| 214 | |
| 215 | // return default value. |
| 216 | return $value; |
| 217 | } |
| 218 | } |
| 219 |