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