This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
course.php
179 lines (175 loc) · 5.41 KB
/
course.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
include_once __DIR__ . '/generic_object.php';
include_once __DIR__ . '/category.php';
include_once __DIR__ . '/db/DB.php';
class Course extends GenericObject {
// ERROR CODES
const EXISTS = 1000;
const NOT_EXISTS = 1001;
const NO_DATA = 1002;
// Languages
const UK = 1;
const RU = 2;
const EN = 3;
// Formats
const ONLINE = 1;
const WEBCAST = 2;
const OFFLINE = 3;
public $languages = array (
1 => 'Українська',
2 => 'Русский',
3 => 'English'
);
public $formats = array (
1 => 'Online',
2 => 'Webcast',
3 => 'Offline'
);
protected $tablename = 'courses';
protected $checker = array (
'active' => 'activeCheck',
'category_id' => 'categoryCheck',
'search' => 'search'
);
function add($data) {
if (! isset ( $data ['name'] ) || ! isset ( $data ['category_id'] ) || ! isset ( $data ['format_id'] ) || ! isset ( $data ['language'] ) || ! isset ( $data ['describe'] )) {
throw new Exception ( "No data", self::NO_DATA );
}
$db = new DB ();
$db->select ( "SELECT * FROM {$this->tablename} WHERE name=$1 AND category_id=$2 AND language=$3", array (
$data ['name'],
$data ['category_id'],
$data ['language']
) );
if ($db->valid ())
throw new Exception ( "Course exists", self::EXISTS );
$data ['created'] = date ( 'Y-m-d H:i:s', time () );
$data ['author_id'] = $_SESSION ['user_id'];
$data ['category_name'] = $this->getCategoryName($data ['category_id']);
return parent::add ( $data );
}
function invert($id) {
$db = new DB ();
$db->select ( "SELECT * FROM {$this->tablename} WHERE id=$1", array (
$id
) );
if ($db->valid ()) {
$data = $db->current ();
$data ['active'] = $data ['active'] == 't' ? 'f' : 't';
$this->update ( $id, array (
'active' => $data ['active']
) );
}
}
function get($cond) {
try {
return parent::get ( $cond );
} catch ( Exception $e ) {
throw new Exception ( "Course is not exists", self::NOT_EXISTS );
}
}
function upload($id) {
if ($_FILES ['file'] ['size'] == 0)
return;
switch ($_FILES ['file'] ['type']) {
case 'application/x-zip-compressed' :
case 'application/zip' :
$zip = new ZipArchive ();
$zip->open ( $_FILES ['file'] ['tmp_name'] );
$dir = __DIR__ . '/../../files/courses/' . $id;
if (! file_exists ( $dir ))
mkdir ( $dir );
$zip->extractTo ( $dir );
break;
default :
throw new Exception ( "Not zip or rar archive" );
}
}
function download($id) {
$dir = __DIR__ . '/../../files/courses/' . $id . '/';
if (file_exists ( $dir )) {
$data = $this->get ( array (
'id' => $id
) );
$i = 0;
$data ['name'] = preg_replace ( '/\/|\\|\?|\%|\*|\:\||\"|\<|\>|\+/', '', $data ['name'] );
$filename = '/var/tmp/' . $data ['name'];
if (file_exists ( $filename . '.zip' ))
while ( file_exists ( $filename . "($i).zip" ) ) {
$i ++;
if ($i > 10)
break;
}
if ($i > 0)
$filename .= "($i).zip";
else
$filename .= '.zip';
Zip::packDir ( $dir, $filename );
if (file_exists ( $filename )) {
header ( 'Content-Description: File Transfer' );
header ( 'Content-Type: application/zip' );
header ( 'Content-Disposition: attachment; filename="' . basename ( $filename ) . '"' );
header ( 'Expires: 0' );
header ( 'Cache-Control: must-revalidate' );
header ( 'Pragma: public' );
header ( 'Content-Length: ' . filesize ( $filename ) );
readfile ( $filename );
unlink ( $filename );
}
return true;
}
}
function search($val) {
return " (courses.name ILIKE '%" . pg_escape_string ( $val ) . "%' ".
"OR courses.describe ILIKE '%" . pg_escape_string ( $val ) . "%' ".
"OR courses.category_name ILIKE '%" . pg_escape_string ( $val ) . "%') ";
}
function activeCheck($val) {
return 'active = ' . ($val ? 'true' : 'false');
}
function categoryCheck($val) {
return 'category_id = ' . intval ( $val );
}
function getCategoryName($category_id=''){
$category = new Category ();
$enum = $category->enumerate ( null );
if ($enum) {
foreach ( $enum as $key => $val ) {
if ($val ['id'] != 0)
$items [$val ['id']] = $val;
}
$categories = $this->makeCategoryList ( $items, $category_id );
}
return $categories;
}
function makeCategoryList(&$items, $category_id) {
$category = $items [$category_id] ['name'];
if ($items [$category_id] ['parent_id'] != 0)
$category = $this->makeCategoryList ( &$items, $items [$category_id] ['parent_id'] ) . " > " . $category;
return $category;
}
function getCategoriesForSelect($category_id){
$category = new Category ();
$enum = $category->enumerate ( null );
if ($enum) {
foreach ( $enum as $key => $val ) {
if ($val ['id'] != 0)
$items [$val ['parent_id']] [] = $val;
}
foreach ( $items as $key => $val ) {
asort ( $items [$key] );
}
asort ( $items );
$categories = '<option></option>' . Course::makeFilterCategoryList ( $items, 0, $category_id );
}
return $categories;
}
function makeFilterCategoryList(&$items, $id, $category_id, $space = '') {
foreach ( $items [$id] as $key => $val ) {
$categories .= "<option value=\"$val[id]\"" . ($val ['id'] == $category_id ? ' selected' : '') . ">" . $space . htmlspecialchars ( $val ['name'], ENT_QUOTES ) . "</option>" . NL;
if (isset ( $items [$val ['id']] ))
$categories .= Course::makeFilterCategoryList ( $items, $val ['id'], $category_id, $space . ' ' );
}
return $categories;
}
}