/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
1
<?php
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
2
/**
3
* This is a model. It holds the logic.
4
* It's loaded like this in the controllers: $this->load->model("path/to/model");
5
* This specific model is: $this->load->model("admin/admin_model")
6
*
7
* This model is the logic behind the admin panel. It let's you manage courses, pages etc
8
*/
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
9
class Admin_model extends CI_Model {
10
11
	function __construct() {
12
		$this->load->database();
13
	}
14
15
	function getCourses() {
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
16
		$query = $this->db->get('Courses');
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
17
		return $query->result();
18
	}
19
79.1.1 by a10rolch
- Updated admin_model function addCourse to also include courseData.
20
	function addCourse($cid, $name, $courseData){
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
21
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
22
			'courseID' => $cid ,
79.1.1 by a10rolch
- Updated admin_model function addCourse to also include courseData.
23
			'name' => $name,
24
			'courseData' => $courseData
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
25
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
26
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
27
		$this->db->insert('Courses', $data); 
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
28
		mkdir("../courses/".$cid);
29
30
	}
31
83.2.1 by elof.bigestans at gmail
Fixed various bugs in the ManageCourses function
32
	function editCourse($cid, $name, $courseData) {
33
		$data = array(
34
			'name' => $name,
35
			'courseData' => $courseData
36
		);
37
38
		$this->db->where('courseID', $cid);
39
		$this->db->update('Courses', $data);
40
	}
41
69.2.1 by galaxyAbstractor
Replaced admin model
42
	function getMenu(){
43
		$menu = array();
44
		$courses = $this->db->get("Courses");
45
		foreach ($courses->result() as $course) {
46
			$menu[$course->name] = array();
47
			$menu[$course->name]['CID'] = $course->courseID;
48
			$categories = $this->db->get_where("Categories", array("courseID" => $course->courseID));
49
50
			foreach($categories->result() as $category){
51
				$menu[$course->name][$category->categoryName] = array();
52
53
				$subcategories = $this->db->get_where("SubCategories", array("courseID" => $course->courseID, "categoryName" => $category->categoryName));
54
55
				foreach($subcategories->result() as $subcategory){
56
					$menu[$course->name][$category->categoryName][$subcategory->subCategoryName] = array();
57
58
				}
59
			}
60
		}
61
		
62
		return $menu;
63
64
	}
65
66
	function getCategories($cid) {
67
		$query = $this->db->get_where('Categories', array("courseID" => $cid));
68
		return $query->result();
69
	}
70
71
	function addCategory($cid, $categoryName){
72
		$this->db->select_max("orderNr");
73
		$query = $this->db->get_where('Categories', array("courseID" => $cid));
74
75
		$result = $query->result();
76
		$data = array(
77
			'courseID' => $cid ,
78
			'categoryName' => $categoryName,
79
			'orderNr' => $result[0]->orderNr+1
80
			);
81
82
		$this->db->insert('Categories', $data); 
83
		mkdir("../courses/".$cid."/".$categoryName);
84
85
	}
86
87
	function getSubCategories($cid, $categoryName) {
88
		$query = $this->db->get_where('SubCategories', array("courseID" => $cid, "categoryName" => $categoryName));
89
		return $query->result();
90
	}
91
92
	function addSubCategory($cid, $categoryName, $subCategoryName){
93
		$this->db->select_max("orderNr");
94
		$query = $this->db->get_where('SubCategories', array("courseID" => $cid, "categoryName" => $categoryName));
95
96
		$result = $query->result();
97
		$data = array(
98
			'courseID' => $cid ,
99
			'subCategoryName' => $subCategoryName,
100
			'categoryName' => $categoryName,
101
			'orderNr' => $result[0]->orderNr+1
102
			);
103
104
		$this->db->insert('SubCategories', $data); 
105
		mkdir("../courses/".$cid."/".$categoryName."/".$subCategoryName);
106
107
	}
108
109
	function getExamples($cid, $categoryName, $subCategoryName) {
110
		$query = $this->db->get_where('Examples', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
111
		return $query->result();
112
	}
113
114
	function getQuizzes($cid, $categoryName, $subCategoryName) {
115
		$query = $this->db->get_where('Quizzes', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
116
		return $query->result();
117
	}
118
119
	function addExample($cid, $categoryName, $subCategoryName, $example, $description){
120
		$this->db->select_max("orderNr");
121
		$query = $this->db->get_where('Examples', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
122
		$result = $query->result();
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
123
		$data = array(
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
124
			'courseID' => $cid ,
125
			'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
126
			'description' => $description,
127
			'categoryName' => $categoryName,
128
			'subCategoryName' => $subCategoryName,
129
			'orderNr' => $result[0]->orderNr+1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
130
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
131
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
132
		$this->db->insert('Examples', $data); 
69.2.1 by galaxyAbstractor
Replaced admin model
133
134
		$data = array(
135
			'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/documentation",
136
			'codeLanguage' => "text",
137
			'fileType' => "text",
138
			'dataBlob' => ""
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
139
			);
140
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
141
		$this->db->insert('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
142
143
		$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
144
			'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/documentation",
145
			'categoryName' => $categoryName,
146
			'subCategoryName' => $subCategoryName,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
147
			'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
148
			'courseID' => $cid,
149
			'columnNr' => 1,
150
			'orderNr' => 1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
151
			);
152
69.2.1 by galaxyAbstractor
Replaced admin model
153
		$this->db->insert('Containers', $data); 
154
		mkdir("../courses/".$cid."/".$categoryName."/".$subCategoryName."/".$example);
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
155
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
156
	}
157
69.2.1 by galaxyAbstractor
Replaced admin model
158
	function updateExample($cid, $categoryName, $subCategoryName, $example,  $documentation, $files){
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
159
160
		$filearr = json_decode($files);
161
162
		foreach ($filearr as $file) {
163
			$output = "";
69.2.1 by galaxyAbstractor
Replaced admin model
164
			$handle = @fopen("../courses/".$cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename, "r");
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
165
			if ($handle) {
166
167
				while (($buffer = fgets($handle, 4096)) !== false) {
168
169
					$buffer = str_replace("&", "&amp;", $buffer);
170
					$buffer = str_replace("<", "&lt;", $buffer);
171
					$buffer = str_replace(">", "&gt;", $buffer);
172
173
					$output .= $buffer;
174
				}
175
				if (!feof($handle)) {
176
					$output .= "Error: unexpected fgets() fail\n";
177
				}
178
				fclose($handle);
179
			}
180
			
181
			$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
182
				'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename ,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
183
				'codeLanguage' => $file->lang,
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
184
				'fileType' => $file->type,
185
				'dataBlob' => $output
186
				);
187
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
188
			$this->db->insert('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
189
69.2.1 by galaxyAbstractor
Replaced admin model
190
			$this->db->select_max("orderNr");
191
			$query = $this->db->get_where('Containers', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName, "exampleName" => $example, "columnNr" => $file->columnNr));
192
			$result = $query->result();
193
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
194
			$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
195
				'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename ,
196
				'categoryName' => $categoryName,
197
				'subCategoryName' => $subCategoryName,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
198
				'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
199
				'courseID' => $cid,
200
				'columnNr' => $file->columnNr,
201
				'orderNr' => $result[0]->orderNr+1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
202
				);
203
69.2.1 by galaxyAbstractor
Replaced admin model
204
			$this->db->insert('Containers', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
205
		}
206
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
207
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
208
			'dataBlob' => $documentation
209
			);
210
69.2.1 by galaxyAbstractor
Replaced admin model
211
		$this->db->where("fileName",$cid."/".$categoryName."/".$subCategoryName."/".$example."/documentation");
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
212
		$this->db->update('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
213
		
214
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
215
	}
216
69.2.1 by galaxyAbstractor
Replaced admin model
217
	function uploadFile($files, $cid, $categoryName, $subCategoryName, $example){
218
		if(move_uploaded_file($files['tmp_name'], "../courses/".$cid."/".$categoryName."/".$subCategoryName."/".$example."/".$files['name'])){
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
219
			echo json_encode(array('status'=>'File was uploaded successfuly!'));
220
		}	
221
	}
222
}