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
@@ 1564-1686 (lines=123) @@
1561
	/**
1562
	 * Restore announcements
1563
	 */
1564
    public function restore_announcements($sessionId = 0)
1565
    {
1566
		if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) {
1567
            $sessionId = intval($sessionId);
1568
			$table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
1569
			$resources = $this->course->resources;
1570
			foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement) {
1571
1572
				// check resources inside html from ckeditor tool and copy correct urls into recipient course
1573
                $announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
1574
                    $announcement->content,
1575
                    $this->course->code,
1576
                    $this->course->destination_path,
1577
                    $this->course->backup_path,
1578
                    $this->course->info['path']
1579
                );
1580
1581
                $params = [
1582
                    'c_id' => $this->destination_course_id,
1583
                    'title' =>  self::DBUTF8($announcement->title),
1584
                    'content' => self::DBUTF8($announcement->content),
1585
                    'end_date' => $announcement->date,
1586
                    'display_order' => $announcement->display_order,
1587
                    'email_sent' => $announcement->email_sent,
1588
                    'session_id' => $sessionId,
1589
                ];
1590
1591
				$new_announcement_id = Database::insert($table, $params);
1592
1593
                if ($new_announcement_id) {
1594
                    $sql = "UPDATE $table SET id = iid WHERE iid = $new_announcement_id";
1595
                    Database::query($sql);
1596
1597
                    if (!isset($this->course->resources[RESOURCE_ANNOUNCEMENT][$id])) {
1598
                        $this->course->resources[RESOURCE_ANNOUNCEMENT][$id] = new stdClass();
1599
                    }
1600
                    $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = $new_announcement_id;
1601
                }
1602
1603
				$origin_path = $this->course->backup_path.'/upload/announcements/';
1604
				$destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/announcements/';
1605
1606
				// Copy announcement attachment file
1607
				if (!empty($this->course->orig)) {
1608
1609
					$table_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1610
					$sql = 'SELECT path, comment, size, filename
1611
					        FROM '.$table_attachment.'
1612
					        WHERE
1613
					            c_id = '.$this->destination_course_id.' AND
1614
					            announcement_id = '.$id;
1615
					$attachment_event = Database::query($sql);
1616
					$attachment_event = Database::fetch_object($attachment_event);
1617
1618
					if (file_exists($origin_path.$attachment_event->path) &&
1619
                        !is_dir($origin_path.$attachment_event->path)
1620
                    ) {
1621
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1622
                        $copy_result = copy(
1623
                            $origin_path.$attachment_event->path,
1624
                            $destination_path.$new_filename
1625
                        );
1626
1627
						if ($copy_result) {
1628
							$table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1629
1630
                            $params = [
1631
                                'c_id' => $this->destination_course_id,
1632
                                'path' => self::DBUTF8($new_filename),
1633
                                'comment' => self::DBUTF8($attachment_event->comment),
1634
                                'size' => $attachment_event->size,
1635
                                'filename' => $attachment_event->filename,
1636
                                'announcement_id' => $new_announcement_id,
1637
                            ];
1638
1639
                            $attachmentId = Database::insert($table_attachment, $params);
1640
1641
                            if ($attachmentId) {
1642
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $attachmentId";
1643
                                Database::query($sql);
1644
                            }
1645
						}
1646
					}
1647
				} else {
1648
					// get the info of the file
1649
					if (!empty($announcement->attachment_path) &&
1650
                        is_file($origin_path.$announcement->attachment_path) &&
1651
                        is_readable($origin_path.$announcement->attachment_path)
1652
                    ) {
1653
						$new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
1654
						$copy_result = copy($origin_path.$announcement->attachment_path, $destination_path.$new_filename);
1655
1656
						if ($copy_result) {
1657
							$table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
1658
							/*$sql = "INSERT INTO ".$table_attachment." SET
1659
							        c_id = ".$this->destination_course_id." ,
1660
							        path = '".self::DBUTF8escapestring($new_filename)."',
1661
							        comment = '".self::DBUTF8escapestring($announcement->attachment_comment)."',
1662
							        size = '".$announcement->attachment_size."', filename = '".$announcement->attachment_filename."',
1663
							        announcement_id = '".$new_announcement_id."' ";
1664
							Database::query($sql);*/
1665
1666
                            $params = [
1667
                                'c_id' => $this->destination_course_id,
1668
                                'path' => self::DBUTF8($new_filename),
1669
                                'comment' => self::DBUTF8($announcement->attachment_comment),
1670
                                'size' => $announcement->attachment_size,
1671
                                'filename' => $announcement->attachment_filename,
1672
                                'announcement_id' => $new_announcement_id,
1673
                            ];
1674
1675
                            $attachmentId = Database::insert($table_attachment, $params);
1676
1677
                            if ($attachmentId) {
1678
                                $sql = "UPDATE $table_attachment SET id = iid WHERE iid = $attachmentId";
1679
                                Database::query($sql);
1680
                            }
1681
						}
1682
					}
1683
				}
1684
			}
1685
		}
1686
	}
1687
1688
    /**
1689
     * Restore Quiz