changed git call from https to git readonly
[atutor.git] / mods / certify / certify_certificate.php
1 <?php
2 define('AT_INCLUDE_PATH', '../../include/');
3 require (AT_INCLUDE_PATH.'vitals.inc.php');
4 authenticate(AT_PRIV_CERTIFY);
5
6 $certify_id = '';
7 if (isset($_POST['certify_id'])) {
8     $certify_id = $addslashes($_POST['certify_id']);
9 } else if (isset($_GET['certify_id'])) {
10     $certify_id = $addslashes($_GET['certify_id']);
11 }
12
13 $templatefile = AT_CONTENT_DIR .'certify/template_'.$certify_id.'.pdf';
14 $templatepresent = file_exists($templatefile);
15
16 function let_to_num($v){ //This function transforms the php.ini notation for numbers (like '2M') to an integer (2*1024*1024 in this case)
17     $l = substr($v, -1);
18     $ret = substr($v, 0, -1);
19     switch(strtoupper($l)){
20     case 'P':
21         $ret *= 1024;
22     case 'T':
23         $ret *= 1024;
24     case 'G':
25         $ret *= 1024;
26     case 'M':
27         $ret *= 1024;
28     case 'K':
29         $ret *= 1024;
30         break;
31     }
32     return $ret;
33 }
34 $max_upload_size = min(let_to_num(ini_get('post_max_size')), let_to_num(ini_get('upload_max_filesize')));
35
36 $certify_title = '';
37 $certify_description = '';
38
39 if (isset($_POST['submit'])) { // Incoming changes
40         
41         $certify_title = $addslashes($_POST['certify_title']);
42         $certify_description = $addslashes($_POST['certify_description']);
43
44         if (strlen($certify_id) > 0) {
45                 
46                 // COMMIT CHANGES
47
48                 $sql = "UPDATE ".TABLE_PREFIX."certify
49                                 SET
50                                         title = '$certify_title',
51                                         description = '$certify_description'
52                                 WHERE
53                                         certify_id = $certify_id
54                                 ";
55
56                 $result = mysql_query($sql, $db) or die(mysql_error());
57
58                 if (file_exists($templatefile))
59                         unlink($templatefile);
60                 if ($_FILES['certify_file']['size'] > 0 && $_FILES['certify_file']['error'] == 0) {
61                         if (move_uploaded_file($_FILES['certify_file']['tmp_name'], $templatefile)) {
62                                 // File ok
63                         } else {
64                                 unlink($templatefile);
65                         }
66                 }
67                 $templatepresent = file_exists($templatefile);
68
69                 //write_to_log(AT_ADMIN_LOG_UPDATE, 'certify', mysql_affected_rows($db), $sql);
70
71                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
72         
73                 header('Location: index_instructor.php');
74                 exit;
75
76         } else {
77
78                 // COMMIT NEW
79         
80                 $sql = "INSERT INTO ".TABLE_PREFIX."certify
81                                 (course_id, 
82                                  title,
83                                  description) 
84                                         VALUES (". $_SESSION['course_id'] .", 
85                                                         '". $certify_title ."',
86                                                         '". $certify_description ."')";
87                                                         
88                 $result = mysql_query($sql, $db) or die(mysql_error());
89
90                 if ($_FILES['certify_file']['size'] > 0 && $_FILES['certify_file']['error'] == 0) {
91                         if (move_uploaded_file($_FILES['certify_file']['tmp_name'], $templatefile)) {
92                                 // File ok
93                         } else {
94                                 unlink($templatefile);
95                         }
96                 }
97                 $templatepresent = file_exists($templatefile);
98
99                 $certify_id = mysql_insert_id($db);
100                 write_to_log(AT_ADMIN_LOG_INSERT, 'certify', mysql_affected_rows($db), $sql);
101         
102                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
103         
104                 header('Location: index_instructor.php');
105                 exit;
106                 
107         }
108
109
110 } else if (isset($_POST['cancel'])) { // Cancelled
111         
112         // CANCEL
113
114         $msg->addFeedback('CANCELLED');
115         header('Location: index_instructor.php');
116         exit;
117
118 } else if (strlen($certify_id) > 0) {
119         
120         // EDIT EXISTING
121
122         // Fetch basic data
123         $sql = "SELECT * from ".TABLE_PREFIX."certify where certify_id=".$certify_id;
124         $result = mysql_query($sql, $db) or die(mysql_error());
125         $row = mysql_fetch_assoc($result);
126
127         if (!$row)
128                 exit; // TODO: Invalid id - how to handle?
129                 
130         $certify_title = $row['title'];
131         $certify_description = $row['description'];
132
133 }
134
135 require(AT_INCLUDE_PATH.'header.inc.php'); 
136 $msg->printAll();
137
138 ?>
139
140 <p>For instructor to add new certificate
141
142
143 <form enctype="multipart/form-data" name="certifydetails" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
144 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_upload_size; ?>"> <!-- We have no real need to restrict the upload here -->
145 <?php if (strlen($certify_id) > 0) { ?>
146 <input type="hidden" name="certify_id" value="<?php echo $certify_id; ?>">
147 <?php } ?>
148 <dl>
149 <dt><label for="certify_title"><?php echo _AT('certify_title'); ?></label></dt>
150 <dd><input type="text" name="certify_title" maxlength="60" value="<?php echo $certify_title; ?>"></dd>
151 <dt><label for="certify_description"><?php echo _AT('certify_description'); ?></label></dt>
152 <dd><textarea name="certify_description" cols="40" rows="5"><?php echo $certify_description; ?></textarea></dd>
153 <dt><label for="certify_file"><?php echo _AT('certify_file'); ?></label></dt>
154 <dd><input type="file" name="certify_file"></dd>
155 </dl>
156 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>">
157 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
158 </form>
159
160 <!-- TODO: Download link for existing template -->
161
162
163 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>