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