Total Complexity | 135 |
Total Lines | 799 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CourseSelectForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseSelectForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CourseSelectForm |
||
20 | { |
||
21 | /** |
||
22 | * @return array |
||
23 | */ |
||
24 | public static function getResourceTitleList() |
||
25 | { |
||
26 | $list = []; |
||
27 | $list[RESOURCE_LEARNPATH_CATEGORY] = get_lang('Learnpath').' '.get_lang('Category'); |
||
28 | $list[RESOURCE_ASSET] = get_lang('Assets'); |
||
29 | $list[RESOURCE_GRADEBOOK] = get_lang('Gradebook'); |
||
30 | $list[RESOURCE_EVENT] = get_lang('Events'); |
||
31 | $list[RESOURCE_ANNOUNCEMENT] = get_lang('Announcements'); |
||
32 | $list[RESOURCE_DOCUMENT] = get_lang('Documents'); |
||
33 | $list[RESOURCE_LINK] = get_lang('Links'); |
||
34 | $list[RESOURCE_COURSEDESCRIPTION] = get_lang('CourseDescription'); |
||
35 | $list[RESOURCE_FORUM] = get_lang('Forums'); |
||
36 | $list[RESOURCE_FORUMCATEGORY] = get_lang('ForumCategory'); |
||
37 | $list[RESOURCE_QUIZ] = get_lang('Tests'); |
||
38 | $list[RESOURCE_TEST_CATEGORY] = get_lang('QuestionCategory'); |
||
39 | $list[RESOURCE_LEARNPATH] = get_lang('ToolLearnpath'); |
||
40 | $list[RESOURCE_SCORM] = 'SCORM'; |
||
41 | $list[RESOURCE_TOOL_INTRO] = get_lang('ToolIntro'); |
||
42 | $list[RESOURCE_SURVEY] = get_lang('Survey'); |
||
43 | $list[RESOURCE_GLOSSARY] = get_lang('Glossary'); |
||
44 | $list[RESOURCE_WIKI] = get_lang('Wiki'); |
||
45 | $list[RESOURCE_THEMATIC] = get_lang('Thematic'); |
||
46 | $list[RESOURCE_ATTENDANCE] = get_lang('Attendance'); |
||
47 | $list[RESOURCE_WORK] = get_lang('ToolStudentPublication'); |
||
48 | |||
49 | return $list; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Display the form. |
||
54 | * |
||
55 | * @param array $course |
||
56 | * @param array $hidden_fields hidden fields to add to the form |
||
57 | * @param bool $avoidSerialize the document array will be serialize. |
||
58 | * This is used in the course_copy.php file |
||
59 | * @param bool $avoidCourseInForm |
||
60 | */ |
||
61 | public static function display_form( |
||
62 | $course, |
||
63 | $hidden_fields = null, |
||
64 | $avoidSerialize = false, |
||
65 | $avoidCourseInForm = false |
||
66 | ) { |
||
67 | global $charset; ?> |
||
68 | <script> |
||
69 | function exp(item) { |
||
70 | el = document.getElementById('div_'+item); |
||
71 | if (el.style.display == 'none') { |
||
72 | el.style.display = ''; |
||
73 | $('#img_'+item).removeClass(); |
||
74 | $('#img_'+item).addClass('fa fa-minus-square-o fa-lg'); |
||
75 | |||
76 | } else { |
||
77 | el.style.display = 'none'; |
||
78 | $('#img_'+item).removeClass(); |
||
79 | $('#img_'+item).addClass('fa fa-plus-square-o fa-lg'); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | function setCheckboxForum(type, value, item_id) { |
||
84 | d = document.course_select_form; |
||
85 | for (i = 0; i < d.elements.length; i++) { |
||
86 | if (d.elements[i].type == "checkbox") { |
||
87 | var name = d.elements[i].attributes.getNamedItem('name').nodeValue; |
||
88 | if (name.indexOf(type) > 0 || type == 'all') { |
||
89 | if ($(d.elements[i]).attr('rel') == item_id) { |
||
90 | d.elements[i].checked = value; |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | function setCheckbox(type,value) { |
||
98 | d = document.course_select_form; |
||
99 | for (i = 0; i < d.elements.length; i++) { |
||
100 | if (d.elements[i].type == "checkbox") { |
||
101 | var name = d.elements[i].attributes.getNamedItem('name').nodeValue; |
||
102 | if( name.indexOf(type) > 0 || type == 'all' ){ |
||
103 | d.elements[i].checked = value; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | function checkLearnPath(message){ |
||
110 | d = document.course_select_form; |
||
111 | for (i = 0; i < d.elements.length; i++) { |
||
112 | if (d.elements[i].type == "checkbox") { |
||
113 | var name = d.elements[i].attributes.getNamedItem('name').nodeValue; |
||
114 | if( name.indexOf('learnpath') > 0){ |
||
115 | if(d.elements[i].checked){ |
||
116 | setCheckbox('document',true); |
||
117 | alert(message); |
||
118 | break; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | function check_forum(obj) { |
||
126 | var id = $(obj).attr('rel'); |
||
127 | var my_id = $(obj).attr('my_rel'); |
||
128 | var checked = false; |
||
129 | if ($('#resource_forum_'+my_id).attr('checked')) { |
||
130 | checked = true; |
||
131 | } |
||
132 | setCheckboxForum('thread', checked, my_id); |
||
133 | $('#resource_Forum_Category_'+id).attr('checked','checked'); |
||
134 | } |
||
135 | |||
136 | function check_category(obj) { |
||
137 | var my_id = $(obj).attr('my_rel'); |
||
138 | var checked = false; |
||
139 | if ($('#resource_Forum_Category_'+my_id).attr('checked')) { |
||
140 | checked = true; |
||
141 | } |
||
142 | $('.resource_forum').each(function(index, value) { |
||
143 | if ($(value).attr('rel') == my_id) { |
||
144 | $(value).attr('checked', checked); |
||
145 | } |
||
146 | }); |
||
147 | |||
148 | $('.resource_topic').each(function(index, value) { |
||
149 | if ($(value).attr('cat_id') == my_id) { |
||
150 | $(value).attr('checked', checked); |
||
151 | } |
||
152 | }); |
||
153 | } |
||
154 | |||
155 | function check_topic(obj) { |
||
156 | var my_id = $(obj).attr('cat_id'); |
||
157 | var forum_id = $(obj).attr('forum_id'); |
||
158 | $('#resource_Forum_Category_'+my_id).attr('checked','checked'); |
||
159 | $('#resource_forum_'+forum_id).attr('checked','checked'); |
||
160 | } |
||
161 | </script> |
||
162 | <?php |
||
163 | // get destination course title |
||
164 | if (!empty($hidden_fields['destination_course'])) { |
||
165 | $sessionTitle = !empty($hidden_fields['destination_session']) ? ' ('.api_get_session_name($hidden_fields['destination_session']).')' : null; |
||
166 | $courseInfo = api_get_course_info($hidden_fields['destination_course']); |
||
167 | echo '<h3>'; |
||
168 | echo get_lang('DestinationCourse').' : '.$courseInfo['title'].' ('.$courseInfo['code'].') '.$sessionTitle; |
||
169 | echo '</h3>'; |
||
170 | } |
||
171 | |||
172 | echo '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>'; |
||
173 | echo '<div class="tool-backups-options">'; |
||
174 | echo '<form method="post" id="upload_form" name="course_select_form">'; |
||
175 | echo '<input type="hidden" name="action" value="course_select_form"/>'; |
||
176 | |||
177 | if (!empty($hidden_fields['destination_course']) && |
||
178 | !empty($hidden_fields['origin_course']) && |
||
179 | !empty($hidden_fields['destination_session']) && |
||
180 | !empty($hidden_fields['origin_session']) |
||
181 | ) { |
||
182 | echo '<input type="hidden" name="destination_course" value="'.$hidden_fields['destination_course'].'"/>'; |
||
183 | echo '<input type="hidden" name="origin_course" value="'.$hidden_fields['origin_course'].'"/>'; |
||
184 | echo '<input type="hidden" name="destination_session" value="'.$hidden_fields['destination_session'].'"/>'; |
||
185 | echo '<input type="hidden" name="origin_session" value="'.$hidden_fields['origin_session'].'"/>'; |
||
186 | } |
||
187 | |||
188 | $forum_categories = []; |
||
189 | $forums = []; |
||
190 | $forum_topics = []; |
||
191 | |||
192 | echo '<p>'; |
||
193 | echo get_lang('SelectResources'); |
||
194 | echo '</p>'; |
||
195 | echo Display::return_message(get_lang('DontForgetToSelectTheMediaFilesIfYourResourceNeedIt')); |
||
196 | |||
197 | $resource_titles = self::getResourceTitleList(); |
||
198 | $element_count = self::parseResources($resource_titles, $course->resources, true, true); |
||
199 | |||
200 | // Fixes forum order |
||
201 | if (!empty($forum_categories)) { |
||
202 | $type = RESOURCE_FORUMCATEGORY; |
||
203 | echo '<div class="item-backup" onclick="javascript:exp('."'$type'".');">'; |
||
204 | echo '<em id="img_'.$type.'" class="fa fa-minus-square-o fa-lg"></em>'; |
||
205 | echo '<span class="title">'.$resource_titles[RESOURCE_FORUM].'</span></div>'; |
||
206 | echo '<div class="item-content" id="div_'.$type.'">'; |
||
207 | echo '<ul class="list-backups-options">'; |
||
208 | foreach ($forum_categories as $forum_category_id => $forum_category) { |
||
209 | echo '<li>'; |
||
210 | echo '<label class="checkbox">'; |
||
211 | echo '<input type="checkbox" |
||
212 | id="resource_'.RESOURCE_FORUMCATEGORY.'_'.$forum_category_id.'" |
||
213 | my_rel="'.$forum_category_id.'" |
||
214 | onclick="javascript:check_category(this);" |
||
215 | name="resource['.RESOURCE_FORUMCATEGORY.']['.$forum_category_id.']" /> '; |
||
216 | $forum_category->show(); |
||
217 | echo '</label>'; |
||
218 | echo '</li>'; |
||
219 | |||
220 | if (isset($forums[$forum_category_id])) { |
||
221 | $my_forums = $forums[$forum_category_id]; |
||
222 | echo '<ul>'; |
||
223 | foreach ($my_forums as $forum_id => $forum) { |
||
224 | echo '<li>'; |
||
225 | echo '<label class="checkbox">'; |
||
226 | echo '<input type="checkbox" |
||
227 | class="resource_forum" |
||
228 | id="resource_'.RESOURCE_FORUM.'_'.$forum_id.'" |
||
229 | onclick="javascript:check_forum(this);" |
||
230 | my_rel="'.$forum_id.'" |
||
231 | rel="'.$forum_category_id.'" |
||
232 | name="resource['.RESOURCE_FORUM.']['.$forum_id.']" />'; |
||
233 | $forum->show(); |
||
234 | echo '</label>'; |
||
235 | echo '</li>'; |
||
236 | if (isset($forum_topics[$forum_id])) { |
||
237 | $my_forum_topics = $forum_topics[$forum_id]; |
||
238 | if (!empty($my_forum_topics)) { |
||
239 | echo '<ul>'; |
||
240 | foreach ($my_forum_topics as $topic_id => $topic) { |
||
241 | echo '<li>'; |
||
242 | echo '<label class="checkbox">'; |
||
243 | echo '<input |
||
244 | type="checkbox" |
||
245 | id="resource_'.RESOURCE_FORUMTOPIC.'_'.$topic_id.'" |
||
246 | onclick="javascript:check_topic(this);" class="resource_topic" |
||
247 | forum_id="'.$forum_id.'" |
||
248 | rel="'.$forum_id.'" |
||
249 | cat_id="'.$forum_category_id.'" |
||
250 | name="resource['.RESOURCE_FORUMTOPIC.']['.$topic_id.']" />'; |
||
251 | $topic->show(); |
||
252 | echo '</label>'; |
||
253 | echo '</li>'; |
||
254 | } |
||
255 | echo '</ul>'; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | echo '</ul>'; |
||
260 | } |
||
261 | echo '<hr/>'; |
||
262 | } |
||
263 | echo '</ul>'; |
||
264 | echo '</div>'; |
||
265 | echo '<script language="javascript">exp('."'$type'".')</script>'; |
||
266 | } |
||
267 | |||
268 | if ($avoidSerialize) { |
||
269 | /*Documents are avoided due the huge amount of memory that the serialize php function "eats" |
||
270 | (when there are directories with hundred/thousand of files) */ |
||
271 | // this is a known issue of serialize |
||
272 | $course->resources['document'] = null; |
||
273 | } |
||
274 | |||
275 | if ($avoidCourseInForm === false) { |
||
276 | /** @var Course $course */ |
||
277 | $courseSerialized = base64_encode(Course::serialize($course)); |
||
278 | echo '<input type="hidden" name="course" value="'.$courseSerialized.'"/>'; |
||
279 | } |
||
280 | |||
281 | if (is_array($hidden_fields)) { |
||
282 | foreach ($hidden_fields as $key => $value) { |
||
283 | echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>'; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | $recycleOption = isset($_POST['recycle_option']) ? true : false; |
||
288 | if (empty($element_count)) { |
||
289 | echo Display::return_message(get_lang('NoDataAvailable'), 'warning'); |
||
290 | } else { |
||
291 | if (!empty($hidden_fields['destination_session'])) { |
||
292 | echo '<br /> |
||
293 | <button |
||
294 | class="save" |
||
295 | type="submit" |
||
296 | onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset))."'".')) return false;" >'. |
||
297 | get_lang('Ok').'</button>'; |
||
298 | } else { |
||
299 | if ($recycleOption) { |
||
300 | echo '<br /><button class="save" type="submit">'.get_lang('Ok').'</button>'; |
||
301 | } else { |
||
302 | echo '<br /> |
||
303 | <button |
||
304 | class="save btn btn-primary" |
||
305 | type="submit" |
||
306 | onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'. |
||
307 | get_lang('Ok').'</button>'; |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | self::display_hidden_quiz_questions($course); |
||
313 | self::display_hidden_scorm_directories($course); |
||
314 | echo '</form>'; |
||
315 | echo '</div>'; |
||
316 | echo '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>'; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param array $resource_titles |
||
321 | * @param array $resourceList |
||
322 | * @param bool $showHeader |
||
323 | * @param bool $showItems |
||
324 | * |
||
325 | * @return int |
||
326 | */ |
||
327 | public static function parseResources( |
||
328 | $resource_titles, |
||
329 | $resourceList, |
||
330 | $showHeader = true, |
||
331 | $showItems = true |
||
332 | ) { |
||
333 | $element_count = 0; |
||
334 | foreach ($resourceList as $type => $resources) { |
||
335 | if (count($resources) > 0) { |
||
336 | switch ($type) { |
||
337 | // Resources to avoid |
||
338 | case RESOURCE_FORUMCATEGORY: |
||
339 | foreach ($resources as $id => $resource) { |
||
340 | $forum_categories[$id] = $resource; |
||
341 | } |
||
342 | $element_count++; |
||
343 | break; |
||
344 | case RESOURCE_FORUM: |
||
345 | foreach ($resources as $id => $resource) { |
||
346 | $forums[$resource->obj->forum_category][$id] = $resource; |
||
347 | } |
||
348 | $element_count++; |
||
349 | break; |
||
350 | case RESOURCE_FORUMTOPIC: |
||
351 | foreach ($resources as $id => $resource) { |
||
352 | $forum_topics[$resource->obj->forum_id][$id] = $resource; |
||
353 | } |
||
354 | $element_count++; |
||
355 | break; |
||
356 | case RESOURCE_LINKCATEGORY: |
||
357 | case RESOURCE_FORUMPOST: |
||
358 | case RESOURCE_QUIZQUESTION: |
||
359 | case RESOURCE_SURVEYQUESTION: |
||
360 | case RESOURCE_SURVEYINVITATION: |
||
361 | case RESOURCE_SCORM: |
||
362 | break; |
||
363 | default: |
||
364 | if ($showHeader) { |
||
365 | echo '<div class="item-backup" onclick="javascript:exp('."'$type'".');">'; |
||
366 | echo '<em id="img_'.$type.'" class="fa fa-plus-square-o fa-lg"></em>'; |
||
367 | echo '<span class="title">'.$resource_titles[$type].'</span>'; |
||
368 | echo '</div>'; |
||
369 | echo '<div class="item-content" id="div_'.$type.'">'; |
||
370 | } |
||
371 | |||
372 | if ($type == RESOURCE_LEARNPATH) { |
||
373 | echo Display::return_message( |
||
374 | get_lang( |
||
375 | 'ToExportLearnpathWithQuizYouHaveToSelectQuiz' |
||
376 | ), |
||
377 | 'warning' |
||
378 | ); |
||
379 | echo Display::return_message( |
||
380 | get_lang( |
||
381 | 'IfYourLPsHaveAudioFilesIncludedYouShouldSelectThemFromTheDocuments' |
||
382 | ), |
||
383 | 'warning' |
||
384 | ); |
||
385 | } |
||
386 | |||
387 | if ($type == RESOURCE_DOCUMENT) { |
||
388 | if (api_get_setting('show_glossary_in_documents') != 'none') { |
||
389 | echo Display::return_message( |
||
390 | get_lang( |
||
391 | 'ToExportDocumentsWithGlossaryYouHaveToSelectGlossary' |
||
392 | ), |
||
393 | 'warning' |
||
394 | ); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | if ($showItems) { |
||
399 | echo '<div class="well">'; |
||
400 | echo '<div class="btn-group">'; |
||
401 | echo "<a class=\"btn btn-default\" |
||
402 | href=\"javascript: void(0);\" |
||
403 | onclick=\"javascript: setCheckbox('$type',true);\" >".get_lang('All')."</a>"; |
||
404 | echo "<a class=\"btn btn-default\" |
||
405 | href=\"javascript: void(0);\" |
||
406 | onclick=\"javascript:setCheckbox('$type',false);\" >".get_lang('None')."</a>"; |
||
407 | echo '</div>'; |
||
408 | echo '<ul class="list-backups-options">'; |
||
409 | foreach ($resources as $id => $resource) { |
||
410 | if ($resource) { |
||
411 | echo '<li>'; |
||
412 | // Event obj in 1.9.x in 1.10.x the class is CalendarEvent |
||
413 | Resource::setClassType($resource); |
||
414 | echo '<label class="checkbox">'; |
||
415 | echo '<input |
||
416 | type="checkbox" |
||
417 | name="resource['.$type.']['.$id.']" |
||
418 | id="resource['.$type.']['.$id.']" />'; |
||
419 | $resource->show(); |
||
420 | echo '</label>'; |
||
421 | echo '</li>'; |
||
422 | } |
||
423 | } |
||
424 | echo '</ul>'; |
||
425 | echo '</div>'; |
||
426 | } |
||
427 | |||
428 | if ($showHeader) { |
||
429 | echo '</div>'; |
||
430 | echo '<script language="javascript">exp('."'$type'".')</script>'; |
||
431 | } |
||
432 | $element_count++; |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | |||
437 | return $element_count; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @param $course |
||
442 | */ |
||
443 | public static function display_hidden_quiz_questions($course) |
||
444 | { |
||
445 | if (is_array($course->resources)) { |
||
446 | foreach ($course->resources as $type => $resources) { |
||
447 | if (count($resources) > 0) { |
||
448 | switch ($type) { |
||
449 | case RESOURCE_QUIZQUESTION: |
||
450 | foreach ($resources as $id => $resource) { |
||
451 | echo '<input |
||
452 | type="hidden" |
||
453 | name="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" |
||
454 | id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />'; |
||
455 | } |
||
456 | break; |
||
457 | } |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param $course |
||
465 | */ |
||
466 | public static function display_hidden_scorm_directories($course) |
||
480 | } |
||
481 | } |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Get the posted course. |
||
488 | * |
||
489 | * @param string $from who calls the function? |
||
490 | * It can be copy_course, create_backup, import_backup or recycle_course |
||
491 | * @param int $session_id |
||
492 | * @param string $course_code |
||
493 | * @param Course $postedCourse |
||
494 | * |
||
495 | * @return Course The course-object with all resources selected by the user |
||
496 | * in the form given by display_form(...) |
||
497 | */ |
||
498 | public static function get_posted_course($from = '', $session_id = 0, $course_code = '', $postedCourse = null) |
||
696 | } |
||
697 | |||
698 | /** |
||
699 | * Display the form session export. |
||
700 | * |
||
701 | * @param array $list_course |
||
702 | * @param array $hidden_fields hidden fields to add to the form |
||
703 | * @param bool $avoidSerialize the document array will be serialize. This is used in the course_copy.php file |
||
704 | */ |
||
705 | public static function display_form_session_export( |
||
818 | } |
||
819 | } |
||
820 |