/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) {
79
		$query = $this->db->get_where('Categories', array("courseID" => $cid));
80
		return $query->result();
81
	}
82
83
	function addCategory($cid, $categoryName){
84
		$this->db->select_max("orderNr");
85
		$query = $this->db->get_where('Categories', array("courseID" => $cid));
86
87
		$result = $query->result();
88
		$data = array(
89
			'courseID' => $cid ,
90
			'categoryName' => $categoryName,
91
			'orderNr' => $result[0]->orderNr+1
92
			);
93
94
		$this->db->insert('Categories', $data); 
95
		mkdir("../courses/".$cid."/".$categoryName);
96
97
	}
98
99
	function getSubCategories($cid, $categoryName) {
100
		$query = $this->db->get_where('SubCategories', array("courseID" => $cid, "categoryName" => $categoryName));
101
		return $query->result();
102
	}
103
104
	function addSubCategory($cid, $categoryName, $subCategoryName){
105
		$this->db->select_max("orderNr");
106
		$query = $this->db->get_where('SubCategories', array("courseID" => $cid, "categoryName" => $categoryName));
107
108
		$result = $query->result();
109
		$data = array(
110
			'courseID' => $cid ,
111
			'subCategoryName' => $subCategoryName,
112
			'categoryName' => $categoryName,
113
			'orderNr' => $result[0]->orderNr+1
114
			);
115
116
		$this->db->insert('SubCategories', $data); 
117
		mkdir("../courses/".$cid."/".$categoryName."/".$subCategoryName);
118
119
	}
120
121
	function getExamples($cid, $categoryName, $subCategoryName) {
122
		$query = $this->db->get_where('Examples', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
123
		return $query->result();
124
	}
125
126
	function getQuizzes($cid, $categoryName, $subCategoryName) {
127
		$query = $this->db->get_where('Quizzes', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
128
		return $query->result();
129
	}
130
131
	function addExample($cid, $categoryName, $subCategoryName, $example, $description){
132
		$this->db->select_max("orderNr");
133
		$query = $this->db->get_where('Examples', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName));
134
		$result = $query->result();
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
135
		$data = array(
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
136
			'courseID' => $cid ,
137
			'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
138
			'description' => $description,
139
			'categoryName' => $categoryName,
140
			'subCategoryName' => $subCategoryName,
141
			'orderNr' => $result[0]->orderNr+1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
142
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
143
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
144
		$this->db->insert('Examples', $data); 
69.2.1 by galaxyAbstractor
Replaced admin model
145
146
		$data = array(
147
			'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/documentation",
148
			'codeLanguage' => "text",
149
			'fileType' => "text",
150
			'dataBlob' => ""
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
151
			);
152
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
153
		$this->db->insert('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
154
155
		$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
156
			'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/documentation",
157
			'categoryName' => $categoryName,
158
			'subCategoryName' => $subCategoryName,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
159
			'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
160
			'courseID' => $cid,
161
			'columnNr' => 1,
162
			'orderNr' => 1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
163
			);
164
69.2.1 by galaxyAbstractor
Replaced admin model
165
		$this->db->insert('Containers', $data); 
166
		mkdir("../courses/".$cid."/".$categoryName."/".$subCategoryName."/".$example);
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
167
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
168
	}
169
69.2.1 by galaxyAbstractor
Replaced admin model
170
	function updateExample($cid, $categoryName, $subCategoryName, $example,  $documentation, $files){
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
171
172
		$filearr = json_decode($files);
173
174
		foreach ($filearr as $file) {
175
			$output = "";
69.2.1 by galaxyAbstractor
Replaced admin model
176
			$handle = @fopen("../courses/".$cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename, "r");
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
177
			if ($handle) {
178
179
				while (($buffer = fgets($handle, 4096)) !== false) {
180
181
					$buffer = str_replace("&", "&amp;", $buffer);
182
					$buffer = str_replace("<", "&lt;", $buffer);
183
					$buffer = str_replace(">", "&gt;", $buffer);
184
185
					$output .= $buffer;
186
				}
187
				if (!feof($handle)) {
188
					$output .= "Error: unexpected fgets() fail\n";
189
				}
190
				fclose($handle);
191
			}
192
			
193
			$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
194
				'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename ,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
195
				'codeLanguage' => $file->lang,
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
196
				'fileType' => $file->type,
197
				'dataBlob' => $output
198
				);
199
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
200
			$this->db->insert('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
201
69.2.1 by galaxyAbstractor
Replaced admin model
202
			$this->db->select_max("orderNr");
203
			$query = $this->db->get_where('Containers', array("courseID" => $cid, "categoryName" => $categoryName, "subCategoryName" => $subCategoryName, "exampleName" => $example, "columnNr" => $file->columnNr));
204
			$result = $query->result();
205
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
206
			$data = array(
69.2.1 by galaxyAbstractor
Replaced admin model
207
				'fileName' => $cid."/".$categoryName."/".$subCategoryName."/".$example."/".$file->filename ,
208
				'categoryName' => $categoryName,
209
				'subCategoryName' => $subCategoryName,
35.1.1 by Erik Elfstrand
Changed internal table names and attribute names to the correct names
210
				'exampleName' => $example,
69.2.1 by galaxyAbstractor
Replaced admin model
211
				'courseID' => $cid,
212
				'columnNr' => $file->columnNr,
213
				'orderNr' => $result[0]->orderNr+1
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
214
				);
215
69.2.1 by galaxyAbstractor
Replaced admin model
216
			$this->db->insert('Containers', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
217
		}
218
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
219
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
220
			'dataBlob' => $documentation
221
			);
222
69.2.1 by galaxyAbstractor
Replaced admin model
223
		$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
224
		$this->db->update('Files', $data); 
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
225
		
226
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
227
	}
228
69.2.1 by galaxyAbstractor
Replaced admin model
229
	function uploadFile($files, $cid, $categoryName, $subCategoryName, $example){
230
		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
231
			echo json_encode(array('status'=>'File was uploaded successfuly!'));
232
		}	
233
	}
83.2.3 by elof.bigestans at gmail
Working on functionality for managecourses
234
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
235
}