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