/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* This is a model. It holds the logic.
* It's loaded like this in the controllers: $this->load->model("path/to/model");
* This specific model is: $this->load->model("admin/admin_model")
*
* This model is the logic behind the admin panel. It let's you manage courses, pages etc
*/
class Admin_model extends CI_Model {

	function __construct() {
		$this->load->database();
	}

	function getCourses() {
		$query = $this->db->get('courses');
		return $query->result();
	}

	function addCourse($cid, $name){
		$data = array(
			'courseID' => $cid ,
			'courseName' => $name
			);

		$this->db->insert('courses', $data); 
		mkdir("../courses/".$cid);

	}

	function getExamples($cid) {
		$query = $this->db->get_where('examples', array("Courses_courseID" => $cid));
		return $query->result();
	}

	function addExample($cid, $example, $description){
		$data = array(
			'Courses_courseID' => $cid ,
			'Name' => $example,
			'description' => $description
			);

		$this->db->insert('examples', $data); 
		mkdir("../courses/".$cid."/".$example);

	}

	function getPages($cid, $example) {
		$query = $this->db->get_where('pages', array("Examples_Courses_courseID" => $cid, "Examples_Name" => $example));
		return $query->result();
	}

	function addPage($cid, $example, $page){
		$data = array(
			'Examples_Courses_courseID' => $cid ,
			'Examples_Name' => $example,
			'name' => $page
			);

		$this->db->insert('pages', $data); 
		mkdir("../courses/".$cid."/".$example."/".$page);

		$data = array(
			'fileName' => $cid."/".$example."/".$page."/documentation",
			'language' => $file->lang,
			'fileType' => $file->type,
			'dataBlob' => $output
			);

		$this->db->insert('files', $data); 

		$data = array(
			'Files_fileName' =>  $cid."/".$example."/".$page."/documentation"  ,
			'Pages_name' => $page,
			'Pages_Examples_Name' => $example,
			'Pages_Examples_Courses_courseID' => $cid
			);

		$this->db->insert('pagesfiles', $data); 

	}

	function updatePage($cid, $example, $page, $documentation, $files){

		$filearr = json_decode($files);

		foreach ($filearr as $file) {
			$output = "";
			$handle = @fopen("../courses/".$cid."/".$example."/".$page."/".$file->filename, "r");
			if ($handle) {

				while (($buffer = fgets($handle, 4096)) !== false) {

					$buffer = str_replace("&", "&amp;", $buffer);
					$buffer = str_replace("<", "&lt;", $buffer);
					$buffer = str_replace(">", "&gt;", $buffer);

					$output .= $buffer;
				}
				if (!feof($handle)) {
					$output .= "Error: unexpected fgets() fail\n";
				}
				fclose($handle);
			}
			
			$data = array(
				'fileName' => $file->filename ,
				'language' => $file->lang,
				'fileType' => $file->type,
				'dataBlob' => $output
				);

			$this->db->insert('files', $data); 

			$data = array(
				'Files_fileName' =>  $file->filename  ,
				'Pages_name' => $page,
				'Pages_Examples_Name' => $example,
				'Pages_Examples_Courses_courseID' => $cid
				);

			$this->db->insert('pagesfiles', $data); 
		}

		$data = array(
			'dataBlob' => $documentation
			);

		$this->db->where("fileName",$cid."/".$example."/".$page."/"."documentation" );
		$this->db->update('files', $data); 
		

	}

	function uploadFile($files, $cid, $example, $page){
		if(move_uploaded_file($files['tmp_name'], "../courses/".$cid."/".$example."/".$page."/".$files['name'])){
			echo json_encode(array('status'=>'File was uploaded successfuly!'));
		}	
	}
}