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