/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() {
16
		$query = $this->db->get('courses');
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 ,
23
			'courseName' => $name
24
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
25
26
		$this->db->insert('courses', $data); 
27
		mkdir("../courses/".$cid);
28
29
	}
30
31
	function getExamples($cid) {
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
32
		$query = $this->db->get_where('examples', array("Courses_courseID" => $cid));
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
33
		return $query->result();
34
	}
35
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
36
	function addExample($cid, $example, $description){
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
37
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
38
			'Courses_courseID' => $cid ,
39
			'Name' => $example,
40
			'description' => $description
41
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
42
43
		$this->db->insert('examples', $data); 
44
		mkdir("../courses/".$cid."/".$example);
45
46
	}
47
48
	function getPages($cid, $example) {
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
49
		$query = $this->db->get_where('pages', array("Examples_Courses_courseID" => $cid, "Examples_Name" => $example));
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
50
		return $query->result();
51
	}
52
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
53
	function addPage($cid, $example, $page){
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
54
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
55
			'Examples_Courses_courseID' => $cid ,
56
			'Examples_Name' => $example,
57
			'name' => $page
58
			);
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
59
60
		$this->db->insert('pages', $data); 
61
		mkdir("../courses/".$cid."/".$example."/".$page);
62
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
63
		$data = array(
64
			'fileName' => $cid."/".$example."/".$page."/documentation",
65
			'language' => $file->lang,
66
			'fileType' => $file->type,
67
			'dataBlob' => $output
68
			);
69
70
		$this->db->insert('files', $data); 
71
72
		$data = array(
73
			'Files_fileName' =>  $cid."/".$example."/".$page."/documentation"  ,
74
			'Pages_name' => $page,
75
			'Pages_Examples_Name' => $example,
76
			'Pages_Examples_Courses_courseID' => $cid
77
			);
78
79
		$this->db->insert('pagesfiles', $data); 
80
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
81
	}
82
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
83
	function updatePage($cid, $example, $page, $documentation, $files){
84
85
		$filearr = json_decode($files);
86
87
		foreach ($filearr as $file) {
88
			$output = "";
89
			$handle = @fopen("../courses/".$cid."/".$example."/".$page."/".$file->filename, "r");
90
			if ($handle) {
91
92
				while (($buffer = fgets($handle, 4096)) !== false) {
93
94
					$buffer = str_replace("&", "&amp;", $buffer);
95
					$buffer = str_replace("<", "&lt;", $buffer);
96
					$buffer = str_replace(">", "&gt;", $buffer);
97
98
					$output .= $buffer;
99
				}
100
				if (!feof($handle)) {
101
					$output .= "Error: unexpected fgets() fail\n";
102
				}
103
				fclose($handle);
104
			}
105
			
106
			$data = array(
107
				'fileName' => $file->filename ,
108
				'language' => $file->lang,
109
				'fileType' => $file->type,
110
				'dataBlob' => $output
111
				);
112
113
			$this->db->insert('files', $data); 
114
115
			$data = array(
116
				'Files_fileName' =>  $file->filename  ,
117
				'Pages_name' => $page,
118
				'Pages_Examples_Name' => $example,
119
				'Pages_Examples_Courses_courseID' => $cid
120
				);
121
122
			$this->db->insert('pagesfiles', $data); 
123
		}
124
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
125
		$data = array(
23.1.2 by galaxyAbstractor
Fixed database stuff, added files to database.
126
			'dataBlob' => $documentation
127
			);
128
129
		$this->db->where("fileName",$cid."/".$example."/".$page."/"."documentation" );
130
		$this->db->update('files', $data); 
131
		
132
20.1.1 by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff
133
	}
134
135
	function uploadFile($files, $cid, $example, $page){
136
		if(move_uploaded_file($files['tmp_name'], "../courses/".$cid."/".$example."/".$page."/".$files['name'])){
137
			echo json_encode(array('status'=>'File was uploaded successfuly!'));
138
		}	
139
	}
140
}