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