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