Code Duplication    Length = 107-123 lines in 2 locations

main/coursecopy/classes/CourseRestorer.class.php 2 locations

@@ 1406-1512 (lines=107) @@
1403
	/**
1404
	 * Restore events
1405
	 */
1406
    public function restore_events($sessionId = 0)
1407
    {
1408
		if ($this->course->has_resources(RESOURCE_EVENT)) {
1409
            $sessionId = intval($sessionId);
1410
			$table = Database :: get_course_table(TABLE_AGENDA);
1411
			$resources = $this->course->resources;
1412
			foreach ($resources[RESOURCE_EVENT] as $id => $event) {
1413
				// check resources inside html from ckeditor tool and copy correct urls into recipient course
1414
                $event->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
1415
                    $event->content,
1416
                    $this->course->code,
1417
                    $this->course->destination_path,
1418
                    $this->course->backup_path,
1419
                    $this->course->info['path']
1420
                );
1421
1422
                $params = [
1423
                    'c_id' => $this->destination_course_id,
1424
                    'title' => self::DBUTF8($event->title),
1425
                    'content' => self::DBUTF8($event->content),
1426
                    'all_day' => $event->all_day,
1427
                    'start_date' => $event->start_date,
1428
                    'end_date' => $event->end_date,
1429
                    'session_id' => $sessionId,
1430
                ];
1431
				$new_event_id = Database::insert($table, $params);
1432
1433
                if ($new_event_id) {
1434
                    $sql = "UPDATE $table SET id = iid WHERE iid = $new_event_id";
1435
                    Database::query($sql);
1436
1437
                    if (!isset($this->course->resources[RESOURCE_EVENT][$id])) {
1438
                        $this->course->resources[RESOURCE_EVENT][$id] = new stdClass();
1439
                    }
1440
1441
                    $this->course->resources[RESOURCE_EVENT][$id]->destination_id = $new_event_id;
1442
                }
1443
1444
				// Copy event attachment
1445
1446
				$origin_path = $this->course->backup_path.'/upload/calendar/';
1447
				$destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/calendar/';
1448
1449
				if (!empty($this->course->orig)) {
1450
1451
					$table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
1452
					$sql = 'SELECT path, comment, size, filename
1453
					        FROM '.$table_attachment.'
1454
					        WHERE c_id = '.$this->destination_course_id.' AND agenda_id = '.$id;
1455
					$attachment_event = Database::query($sql);
1456
					$attachment_event = Database::fetch_object($attachment_event);
1457
1458
					if (file_exists($origin_path.$attachment_event->path) &&
1459
                        !is_dir($origin_path.$attachment_event->path)
1460
                    ) {
1461
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1462
						$copy_result = copy($origin_path.$attachment_event->path, $destination_path.$new_filename);
1463
						//$copy_result = true;
1464
						if ($copy_result) {
1465
							$table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
1466
1467
							$params = [
1468
                                'c_id' => $this->destination_course_id,
1469
                                'path' => self::DBUTF8($new_filename),
1470
                                'comment' => self::DBUTF8($attachment_event->comment),
1471
                                'size' => $attachment_event->size,
1472
                                'filename' => $attachment_event->filename,
1473
                                'agenda_id' => $new_event_id,
1474
                            ];
1475
                            $id = Database::insert($table_attachment, $params);
1476
                            if ($id) {
1477
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $id";
1478
                                Database::query($sql);
1479
                            }
1480
                        }
1481
					}
1482
				} else {
1483
					// get the info of the file
1484
					if (!empty($event->attachment_path) &&
1485
                        is_file($origin_path.$event->attachment_path) &&
1486
                        is_readable($origin_path.$event->attachment_path)
1487
                    ) {
1488
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1489
						$copy_result = copy($origin_path.$event->attachment_path, $destination_path.$new_filename);
1490
						if ($copy_result) {
1491
							$table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
1492
1493
                            $params = [
1494
                                'c_id' => $this->destination_course_id,
1495
                                'path' => self::DBUTF8($new_filename),
1496
                                'comment' => self::DBUTF8($event->attachment_comment),
1497
                                'size' => $event->size,
1498
                                'filename' => $event->filename,
1499
                                'agenda_id' => $new_event_id,
1500
                            ];
1501
                            $id = Database::insert($table_attachment, $params);
1502
1503
                            if ($id) {
1504
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $id";
1505
                                Database::query($sql);
1506
                            }
1507
						}
1508
					}
1509
				}
1510
			}
1511
		}
1512
	}
1513
1514
	/**
1515
	 * Restore course-description
@@ 1565-1687 (lines=123) @@
1562
	/**
1563
	 * Restore announcements
1564
	 */
1565
    public function restore_announcements($sessionId = 0)
1566
    {
1567
		if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) {
1568
            $sessionId = intval($sessionId);
1569
			$table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
1570
			$resources = $this->course->resources;
1571
			foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement) {
1572
1573
				// check resources inside html from ckeditor tool and copy correct urls into recipient course
1574
                $announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
1575
                    $announcement->content,
1576
                    $this->course->code,
1577
                    $this->course->destination_path,
1578
                    $this->course->backup_path,
1579
                    $this->course->info['path']
1580
                );
1581
1582
                $params = [
1583
                    'c_id' => $this->destination_course_id,
1584
                    'title' =>  self::DBUTF8($announcement->title),
1585
                    'content' => self::DBUTF8($announcement->content),
1586
                    'end_date' => $announcement->date,
1587
                    'display_order' => $announcement->display_order,
1588
                    'email_sent' => $announcement->email_sent,
1589
                    'session_id' => $sessionId,
1590
                ];
1591
1592
				$new_announcement_id = Database::insert($table, $params);
1593
1594
                if ($new_announcement_id) {
1595
                    $sql = "UPDATE $table SET id = iid WHERE iid = $new_announcement_id";
1596
                    Database::query($sql);
1597
1598
                    if (!isset($this->course->resources[RESOURCE_ANNOUNCEMENT][$id])) {
1599
                        $this->course->resources[RESOURCE_ANNOUNCEMENT][$id] = new stdClass();
1600
                    }
1601
                    $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = $new_announcement_id;
1602
                }
1603
1604
				$origin_path = $this->course->backup_path.'/upload/announcements/';
1605
				$destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/announcements/';
1606
1607
				// Copy announcement attachment file
1608
				if (!empty($this->course->orig)) {
1609
1610
					$table_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1611
					$sql = 'SELECT path, comment, size, filename
1612
					        FROM '.$table_attachment.'
1613
					        WHERE
1614
					            c_id = '.$this->destination_course_id.' AND
1615
					            announcement_id = '.$id;
1616
					$attachment_event = Database::query($sql);
1617
					$attachment_event = Database::fetch_object($attachment_event);
1618
1619
					if (file_exists($origin_path.$attachment_event->path) &&
1620
                        !is_dir($origin_path.$attachment_event->path)
1621
                    ) {
1622
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1623
                        $copy_result = copy(
1624
                            $origin_path.$attachment_event->path,
1625
                            $destination_path.$new_filename
1626
                        );
1627
1628
						if ($copy_result) {
1629
							$table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1630
1631
                            $params = [
1632
                                'c_id' => $this->destination_course_id,
1633
                                'path' => self::DBUTF8($new_filename),
1634
                                'comment' => self::DBUTF8($attachment_event->comment),
1635
                                'size' => $attachment_event->size,
1636
                                'filename' => $attachment_event->filename,
1637
                                'announcement_id' => $new_announcement_id,
1638
                            ];
1639
1640
                            $attachmentId = Database::insert($table_attachment, $params);
1641
1642
                            if ($attachmentId) {
1643
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $attachmentId";
1644
                                Database::query($sql);
1645
                            }
1646
						}
1647
					}
1648
				} else {
1649
					// get the info of the file
1650
					if (!empty($announcement->attachment_path) &&
1651
                        is_file($origin_path.$announcement->attachment_path) &&
1652
                        is_readable($origin_path.$announcement->attachment_path)
1653
                    ) {
1654
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1655
						$copy_result = copy($origin_path.$announcement->attachment_path, $destination_path.$new_filename);
1656
1657
						if ($copy_result) {
1658
							$table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1659
							/*$sql = "INSERT INTO ".$table_attachment." SET
1660
							        c_id = ".$this->destination_course_id." ,
1661
							        path = '".self::DBUTF8escapestring($new_filename)."',
1662
							        comment = '".self::DBUTF8escapestring($announcement->attachment_comment)."',
1663
							        size = '".$announcement->attachment_size."', filename = '".$announcement->attachment_filename."',
1664
							        announcement_id = '".$new_announcement_id."' ";
1665
							Database::query($sql);*/
1666
1667
                            $params = [
1668
                                'c_id' => $this->destination_course_id,
1669
                                'path' => self::DBUTF8($new_filename),
1670
                                'comment' => self::DBUTF8($announcement->attachment_comment),
1671
                                'size' => $announcement->attachment_size,
1672
                                'filename' => $announcement->attachment_filename,
1673
                                'announcement_id' => $new_announcement_id,
1674
                            ];
1675
1676
                            $attachmentId = Database::insert($table_attachment, $params);
1677
1678
                            if ($attachmentId) {
1679
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $attachmentId";
1680
                                Database::query($sql);
1681
                            }
1682
						}
1683
					}
1684
				}
1685
			}
1686
		}
1687
	}
1688
1689
    /**
1690
     * Restore Quiz