Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 42 | class mobile { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the bigbluebuttonbn course view for the mobile app. |
||
| 46 | * @param $args |
||
| 47 | * @return array HTML, javascript and other data. |
||
| 48 | * @throws \coding_exception |
||
| 49 | * @throws \moodle_exception |
||
| 50 | * @throws \require_login_exception |
||
| 51 | * @throws \required_capability_exception |
||
| 52 | */ |
||
| 53 | public static function mobile_course_view($args) { |
||
| 54 | |||
| 55 | global $OUTPUT, $SESSION, $CFG; |
||
| 56 | |||
| 57 | $args = (object) $args; |
||
| 58 | $viewinstance = bigbluebuttonbn_view_validator($args->cmid, null); |
||
|
|
|||
| 59 | if (!$viewinstance) { |
||
| 60 | $error = get_string('view_error_url_missing_parameters', 'bigbluebuttonbn'); |
||
| 61 | return(self::mobile_print_error($error)); |
||
| 62 | } |
||
| 63 | |||
| 64 | $cm = $viewinstance['cm']; |
||
| 65 | $course = $viewinstance['course']; |
||
| 66 | $bigbluebuttonbn = $viewinstance['bigbluebuttonbn']; |
||
| 67 | $context = context_module::instance($cm->id); |
||
| 68 | |||
| 69 | require_login($course->id, false , $cm, true, true); |
||
| 70 | require_capability('mod/bigbluebuttonbn:join', $context); |
||
| 71 | |||
| 72 | // Add view event. |
||
| 73 | bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['view'], $bigbluebuttonbn); |
||
| 74 | |||
| 75 | // Create array bbbsession with configuration for BBB server. |
||
| 76 | $bbbsession['course'] = $course; |
||
| 77 | $bbbsession['coursename'] = $course->fullname; |
||
| 78 | $bbbsession['cm'] = $cm; |
||
| 79 | $bbbsession['bigbluebuttonbn'] = $bigbluebuttonbn; |
||
| 80 | $bbbsession = \mod_bigbluebuttonbn\locallib\mobileview::bigbluebuttonbn_view_bbbsession_set($context, $bbbsession); |
||
| 81 | |||
| 82 | // Check activity status. |
||
| 83 | $activitystatus = \mod_bigbluebuttonbn\locallib\mobileview::bigbluebuttonbn_view_get_activity_status($bbbsession); |
||
| 84 | if ($activitystatus == 'not_started') { |
||
| 85 | $message = get_string('view_message_conference_not_started', 'bigbluebuttonbn'); |
||
| 86 | |||
| 87 | $notstarted = array(); |
||
| 88 | $notstarted['starts_at'] = ''; |
||
| 89 | $notstarted['ends_at'] = ''; |
||
| 90 | if (!empty($bigbluebuttonbn->openingtime)) { |
||
| 91 | $notstarted['starts_at'] = sprintf( |
||
| 92 | '%s: %s', |
||
| 93 | get_string('mod_form_field_openingtime', 'bigbluebuttonbn'), |
||
| 94 | userdate($bigbluebuttonbn->openingtime) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | if (!empty($bigbluebuttonbn->closingtime)) { |
||
| 98 | $notstarted['ends_at'] = sprintf( |
||
| 99 | '%s: %s', |
||
| 100 | get_string('mod_form_field_closingtime', 'bigbluebuttonbn'), |
||
| 101 | userdate($bigbluebuttonbn->closingtime) |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted)); |
||
| 106 | } |
||
| 107 | if ($activitystatus == 'ended') { |
||
| 108 | $message = get_string('view_message_conference_has_ended', 'bigbluebuttonbn'); |
||
| 109 | return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message)); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Check if the BBB server is working. |
||
| 113 | $serverversion = bigbluebuttonbn_get_server_version(); |
||
| 114 | $bbbsession['serverversion'] = (string) $serverversion; |
||
| 115 | View Code Duplication | if (is_null($serverversion)) { |
|
| 116 | |||
| 117 | if ($bbbsession['administrator']) { |
||
| 118 | $error = get_string('view_error_unable_join', 'bigbluebuttonbn'); |
||
| 119 | } else if ($bbbsession['moderator']) { |
||
| 120 | $error = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn'); |
||
| 121 | } else { |
||
| 122 | $error = get_string('view_error_unable_join_student', 'bigbluebuttonbn'); |
||
| 123 | } |
||
| 124 | |||
| 125 | return(self::mobile_print_error($error)); |
||
| 126 | } |
||
| 127 | |||
| 128 | // Mark viewed by user (if required). |
||
| 129 | $completion = new \completion_info($course); |
||
| 130 | $completion->set_module_viewed($cm); |
||
| 131 | |||
| 132 | // Validate if the user is in a role allowed to join. |
||
| 133 | if (!has_capability('moodle/category:manage', $context) && |
||
| 134 | !has_capability('mod/bigbluebuttonbn:join', $context)) { |
||
| 135 | $error = get_string('view_nojoin', 'bigbluebuttonbn'); |
||
| 136 | return(self::mobile_print_error($error)); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Operation URLs. |
||
| 140 | $bbbsession['bigbluebuttonbnURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/view.php?id=' . $bbbsession['cm']->id; |
||
| 141 | $bbbsession['logoutURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=logout&id='.$args->cmid . |
||
| 142 | '&bn=' . $bbbsession['bigbluebuttonbn']->id; |
||
| 143 | $bbbsession['recordingReadyURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=recording_' . |
||
| 144 | 'ready&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id; |
||
| 145 | $bbbsession['meetingEventsURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=meeting' . |
||
| 146 | '_events&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id; |
||
| 147 | $bbbsession['joinURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=join&id=' . $args->cmid . |
||
| 148 | '&bn=' . $bbbsession['bigbluebuttonbn']->id; |
||
| 149 | |||
| 150 | // Initialize session variable used across views. |
||
| 151 | $SESSION->bigbluebuttonbn_bbbsession = $bbbsession; |
||
| 152 | |||
| 153 | // Logic of bbb_view for join to session. |
||
| 154 | // If user is not administrator nor moderator (user is student) and waiting is required. |
||
| 155 | if (!$bbbsession['administrator'] && !$bbbsession['moderator'] && $bbbsession['wait']) { |
||
| 156 | $message = get_string('view_message_conference_wait_for_moderator', 'bigbluebuttonbn'); |
||
| 157 | return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message)); |
||
| 158 | } |
||
| 159 | |||
| 160 | // See if the BBB session is already in progress. |
||
| 161 | if (!bigbluebuttonbn_is_meeting_running($bbbsession['meetingid'])) { |
||
| 162 | |||
| 163 | // The meeting doesnt exist in BBB server, must be created. |
||
| 164 | $response = bigbluebuttonbn_get_create_meeting_array( |
||
| 165 | \mod_bigbluebuttonbn\locallib\mobileview::bigbluebutton_bbb_view_create_meeting_data($bbbsession), |
||
| 166 | \mod_bigbluebuttonbn\locallib\mobileview::bigbluebutton_bbb_view_create_meeting_metadata($bbbsession), |
||
| 167 | $bbbsession['presentation']['name'], |
||
| 168 | $bbbsession['presentation']['url'] |
||
| 169 | ); |
||
| 170 | |||
| 171 | View Code Duplication | if (empty($response)) { |
|
| 172 | // The BBB server is failing. |
||
| 173 | if ($bbbsession['administrator']) { |
||
| 174 | $e = get_string('view_error_unable_join', 'bigbluebuttonbn'); |
||
| 175 | } else if ($bbbsession['moderator']) { |
||
| 176 | $e = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn'); |
||
| 177 | } else { |
||
| 178 | $e = get_string('view_error_unable_join_student', 'bigbluebuttonbn'); |
||
| 179 | } |
||
| 180 | return(self::mobile_print_error($e)); |
||
| 181 | } |
||
| 182 | if ($response['returncode'] == 'FAILED') { |
||
| 183 | // The meeting could not be created. |
||
| 184 | $errorkey = bigbluebuttonbn_get_error_key($response['messageKey'], 'view_error_create'); |
||
| 185 | $e = get_string($errorkey, 'bigbluebuttonbn'); |
||
| 186 | return(self::mobile_print_error($e)); |
||
| 187 | } |
||
| 188 | if ($response['hasBeenForciblyEnded'] == 'true') { |
||
| 189 | $e = get_string('index_error_forciblyended', 'bigbluebuttonbn'); |
||
| 190 | return(self::mobile_print_error($e)); |
||
| 191 | } |
||
| 192 | |||
| 193 | // Event meeting created. |
||
| 194 | bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_create'], $bigbluebuttonbn); |
||
| 195 | // Insert a record that meeting was created. |
||
| 196 | $overrides = array('meetingid' => $bbbsession['meetingid']); |
||
| 197 | $meta = '{"record":'.($bbbsession['record'] ? 'true' : 'false').'}'; |
||
| 198 | bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_CREATE, $overrides, $meta); |
||
| 199 | } |
||
| 200 | |||
| 201 | // It is part of 'bigbluebutton_bbb_view_join_meeting' in bbb_view. |
||
| 202 | // Update the cache. |
||
| 203 | $meetinginfo = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], BIGBLUEBUTTONBN_UPDATE_CACHE); |
||
| 204 | if ($bbbsession['userlimit'] > 0 && intval($meetinginfo['participantCount']) >= $bbbsession['userlimit']) { |
||
| 205 | // No more users allowed to join. |
||
| 206 | $message = get_string('view_error_userlimit_reached', 'bigbluebuttonbn'); |
||
| 207 | return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message)); |
||
| 208 | } |
||
| 209 | |||
| 210 | // Build final url to BBB. |
||
| 211 | $urltojoin = \mod_bigbluebuttonbn\locallib\mobileview::build_url_join_session($bbbsession); |
||
| 212 | |||
| 213 | $data = array( |
||
| 214 | 'bigbluebuttonbn' => $bigbluebuttonbn, |
||
| 215 | 'bbbsession' => (object) $bbbsession, |
||
| 216 | 'urltojoin' => $urltojoin, |
||
| 217 | 'cmid' => $cm->id, |
||
| 218 | 'courseid' => $args->courseid |
||
| 219 | ); |
||
| 220 | |||
| 221 | return array( |
||
| 222 | 'templates' => array( |
||
| 223 | array( |
||
| 224 | 'id' => 'main', |
||
| 225 | 'html' => $OUTPUT->render_from_template('mod_bigbluebuttonbn/mobile_view_page', $data), |
||
| 226 | ), |
||
| 227 | ), |
||
| 228 | 'javascript' => '', |
||
| 229 | 'otherdata' => '', |
||
| 230 | 'files' => '' |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the view for errors. |
||
| 236 | * @param string $error Error to display. |
||
| 237 | * |
||
| 238 | * @return array HTML, javascript and otherdata |
||
| 239 | */ |
||
| 240 | protected static function mobile_print_error($error) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the view for messages. |
||
| 262 | * @param $bigbluebuttonbn |
||
| 263 | * @param $cm |
||
| 264 | * @param string $message Message to display. |
||
| 265 | * @param array $notstarted Extra messages for not started session. |
||
| 266 | * |
||
| 267 | * @return array HTML, javascript and otherdata |
||
| 268 | */ |
||
| 269 | protected static function mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted = array()) { |
||
| 291 | } |
||
| 292 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: