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