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  | 
            ||
| 37 | class mobileview { | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * Return standard array with configurations required for BBB server.  | 
            ||
| 41 | * @param $context  | 
            ||
| 42 | * @param $session  | 
            ||
| 43 | * @return mixed  | 
            ||
| 44 | * @throws \coding_exception  | 
            ||
| 45 | * @throws \dml_exception  | 
            ||
| 46 | */  | 
            ||
| 47 | View Code Duplication |     public static function bigbluebuttonbn_view_bbbsession_set($context, &$session) { | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 48 | |||
| 49 | global $CFG, $USER;  | 
            ||
| 50 | |||
| 51 | $session['username'] = fullname($USER);  | 
            ||
| 52 | $session['userID'] = $USER->id;  | 
            ||
| 53 | $session['administrator'] = is_siteadmin($session['userID']);  | 
            ||
| 54 | $participantlist = bigbluebuttonbn_get_participant_list($session['bigbluebuttonbn'], $context);  | 
            ||
| 55 | $session['moderator'] = bigbluebuttonbn_is_moderator($context, $participantlist);  | 
            ||
| 56 | $session['managerecordings'] = ($session['administrator']  | 
            ||
| 57 |             || has_capability('mod/bigbluebuttonbn:managerecordings', $context)); | 
            ||
| 58 | $session['importrecordings'] = ($session['managerecordings']);  | 
            ||
| 59 | $session['modPW'] = $session['bigbluebuttonbn']->moderatorpass;  | 
            ||
| 60 | $session['viewerPW'] = $session['bigbluebuttonbn']->viewerpass;  | 
            ||
| 61 | $session['meetingid'] = $session['bigbluebuttonbn']->meetingid.'-'.$session['course']->id.'-'.  | 
            ||
| 62 | $session['bigbluebuttonbn']->id;  | 
            ||
| 63 | $session['meetingname'] = $session['bigbluebuttonbn']->name;  | 
            ||
| 64 | $session['meetingdescription'] = $session['bigbluebuttonbn']->intro;  | 
            ||
| 65 |         $session['userlimit'] = intval((int)\mod_bigbluebuttonbn\locallib\config::get('userlimit_default')); | 
            ||
| 66 |         if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('userlimit_editable')) { | 
            ||
| 67 | $session['userlimit'] = intval($session['bigbluebuttonbn']->userlimit);  | 
            ||
| 68 | }  | 
            ||
| 69 | $session['voicebridge'] = $session['bigbluebuttonbn']->voicebridge;  | 
            ||
| 70 |         if ($session['bigbluebuttonbn']->voicebridge > 0) { | 
            ||
| 71 | $session['voicebridge'] = 70000 + $session['bigbluebuttonbn']->voicebridge;  | 
            ||
| 72 | }  | 
            ||
| 73 | $session['wait'] = $session['bigbluebuttonbn']->wait;  | 
            ||
| 74 | $session['record'] = $session['bigbluebuttonbn']->record;  | 
            ||
| 75 | $session['welcome'] = $session['bigbluebuttonbn']->welcome;  | 
            ||
| 76 |         if (!isset($session['welcome']) || $session['welcome'] == '') { | 
            ||
| 77 |             $session['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn'); | 
            ||
| 78 | }  | 
            ||
| 79 |         if ($session['bigbluebuttonbn']->record) { | 
            ||
| 80 |             $session['welcome'] .= '<br><br>'.get_string('bbbrecordwarning', 'bigbluebuttonbn'); | 
            ||
| 81 | }  | 
            ||
| 82 | $session['openingtime'] = $session['bigbluebuttonbn']->openingtime;  | 
            ||
| 83 | $session['closingtime'] = $session['bigbluebuttonbn']->closingtime;  | 
            ||
| 84 | $session['context'] = $context;  | 
            ||
| 85 | $session['origin'] = 'Moodle';  | 
            ||
| 86 | $session['originVersion'] = $CFG->release;  | 
            ||
| 87 | $parsedurl = parse_url($CFG->wwwroot);  | 
            ||
| 88 | $session['originServerName'] = $parsedurl['host'];  | 
            ||
| 89 | $session['originServerUrl'] = $CFG->wwwroot;  | 
            ||
| 90 | $session['originServerCommonName'] = '';  | 
            ||
| 91 |         $session['originTag'] = 'moodle-mod_bigbluebuttonbn ('.get_config('mod_bigbluebuttonbn', 'version').')'; | 
            ||
| 92 | $session['bnserver'] = bigbluebuttonbn_is_bn_server();  | 
            ||
| 93 |         $session['clienttype'] = \mod_bigbluebuttonbn\locallib\config::get('clienttype_default'); | 
            ||
| 94 | |||
| 95 |         if (\mod_bigbluebuttonbn\locallib\config::get('clienttype_editable')) { | 
            ||
| 96 | $session['clienttype'] = $session['bigbluebuttonbn']->clienttype;  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 |         if (!\mod_bigbluebuttonbn\locallib\config::clienttype_enabled()) { | 
            ||
| 100 | $session['clienttype'] = BIGBLUEBUTTON_CLIENTTYPE_FLASH;  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | return($session);  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * Build url for join to session.  | 
            ||
| 108 | * This method is similar to "bigbluebutton_bbb_view_join_meeting()" in bbb_view.  | 
            ||
| 109 | * @param $bbbsession  | 
            ||
| 110 | * @return string  | 
            ||
| 111 | */  | 
            ||
| 112 |     public static function build_url_join_session($bbbsession) { | 
            ||
| 113 | $password = $bbbsession['viewerPW'];  | 
            ||
| 114 |         if ($bbbsession['administrator'] || $bbbsession['moderator']) { | 
            ||
| 115 | $password = $bbbsession['modPW'];  | 
            ||
| 116 | }  | 
            ||
| 117 | $joinurl = bigbluebuttonbn_get_join_url($bbbsession['meetingid'], $bbbsession['username'],  | 
            ||
| 118 | $password, $bbbsession['logoutURL'], null, $bbbsession['userID'], $bbbsession['clienttype']);  | 
            ||
| 119 | |||
| 120 | return($joinurl);  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * Return the status of an activity [open|not_started|ended].  | 
            ||
| 125 | *  | 
            ||
| 126 | * @param array $bbbsession  | 
            ||
| 127 | * @return string  | 
            ||
| 128 | */  | 
            ||
| 129 |     public static function bigbluebuttonbn_view_get_activity_status(&$bbbsession) { | 
            ||
| 130 | $now = time();  | 
            ||
| 131 | View Code Duplication |         if (!empty($bbbsession['bigbluebuttonbn']->openingtime) && $now < $bbbsession['bigbluebuttonbn']->openingtime) { | 
            |
| 132 | // The activity has not been opened.  | 
            ||
| 133 | return 'not_started';  | 
            ||
| 134 | }  | 
            ||
| 135 |         if (!empty($bbbsession['bigbluebuttonbn']->closingtime) && $now > $bbbsession['bigbluebuttonbn']->closingtime) { | 
            ||
| 136 | // The activity has been closed.  | 
            ||
| 137 | return 'ended';  | 
            ||
| 138 | }  | 
            ||
| 139 | // The activity is open.  | 
            ||
| 140 | return 'open';  | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * Helper for preparing metadata used while creating the meeting.  | 
            ||
| 145 | *  | 
            ||
| 146 | * @param array $bbbsession  | 
            ||
| 147 | * @return array  | 
            ||
| 148 | */  | 
            ||
| 149 | View Code Duplication |     public static function bigbluebutton_bbb_view_create_meeting_metadata(&$bbbsession) { | 
            |
| 150 | |||
| 151 | global $USER;  | 
            ||
| 152 | // Create standard metadata.  | 
            ||
| 153 | $metadatabbb = [  | 
            ||
| 154 | 'bbb-origin' => $bbbsession['origin'],  | 
            ||
| 155 | 'bbb-origin-version' => $bbbsession['originVersion'],  | 
            ||
| 156 | 'bbb-origin-server-name' => $bbbsession['originServerName'],  | 
            ||
| 157 | 'bbb-origin-server-common-name' => $bbbsession['originServerCommonName'],  | 
            ||
| 158 | 'bbb-origin-tag' => $bbbsession['originTag'],  | 
            ||
| 159 | 'bbb-context' => $bbbsession['course']->fullname,  | 
            ||
| 160 | 'bbb-recording-name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),  | 
            ||
| 161 | 'bbb-recording-description' => bigbluebuttonbn_html2text($bbbsession['meetingdescription'], 64),  | 
            ||
| 162 | 'bbb-recording-tags' => bigbluebuttonbn_get_tags($bbbsession['cm']->id), // Same as $id.  | 
            ||
| 163 | ];  | 
            ||
| 164 | // Check recording status.  | 
            ||
| 165 |         if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingstatus_enabled')) { | 
            ||
| 166 | $metadatabbb["bn-recording-status"] = json_encode(  | 
            ||
| 167 | array(  | 
            ||
| 168 |                     'email' => array('"' . fullname($USER) . '" <' . $USER->email . '>'), | 
            ||
| 169 | 'context' => $bbbsession['bigbluebuttonbnURL']  | 
            ||
| 170 | )  | 
            ||
| 171 | );  | 
            ||
| 172 | }  | 
            ||
| 173 |         if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingready_enabled')) { | 
            ||
| 174 | $metadatabbb['bn-recording-ready-url'] = $bbbsession['recordingReadyURL'];  | 
            ||
| 175 | }  | 
            ||
| 176 |         if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('meetingevents_enabled')) { | 
            ||
| 177 | $metadatabbb['bn-meeting-events-url'] = $bbbsession['meetingEventsURL'];  | 
            ||
| 178 | }  | 
            ||
| 179 | return $metadatabbb;  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Helper to prepare data used for create meeting.  | 
            ||
| 184 | * @param $bbbsession  | 
            ||
| 185 | * @return array  | 
            ||
| 186 | * @throws \coding_exception  | 
            ||
| 187 | */  | 
            ||
| 188 | View Code Duplication |     public static function bigbluebutton_bbb_view_create_meeting_data(&$bbbsession) { | 
            |
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * Helper for returning the flag to know if the meeting is recorded.  | 
            ||
| 221 | *  | 
            ||
| 222 | * @param boolean $record  | 
            ||
| 223 | * @return string  | 
            ||
| 224 | */  | 
            ||
| 225 |     public static function bigbluebutton_bbb_view_create_meeting_data_record($record) { | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * Helper for returning the duration expected for the meeting.  | 
            ||
| 234 | *  | 
            ||
| 235 | * @param string $closingtime  | 
            ||
| 236 | * @return integer  | 
            ||
| 237 | */  | 
            ||
| 238 |     public static function bigbluebutton_bbb_view_create_meeting_data_duration($closingtime) { | 
            ||
| 244 | }  | 
            ||
| 245 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.