PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/certify_certificate.php

https://github.com/atutor/certify
PHP | 166 lines | 117 code | 42 blank | 7 comment | 24 complexity | 7268bb7a849f90ded1c578176382fa69 MD5 | raw file
  1. <?php
  2. define('AT_INCLUDE_PATH', '../../include/');
  3. require (AT_INCLUDE_PATH.'vitals.inc.php');
  4. authenticate(AT_PRIV_CERTIFY);
  5. $certify_id = '';
  6. if (isset($_POST['certify_id'])) {
  7. $certify_id = $addslashes($_POST['certify_id']);
  8. } else if (isset($_GET['certify_id'])) {
  9. $certify_id = $addslashes($_GET['certify_id']);
  10. }
  11. $templatefile = AT_CONTENT_DIR .'certify/template_'.$certify_id.'.pdf';
  12. $templatepresent = file_exists($templatefile);
  13. 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)
  14. $l = substr($v, -1);
  15. $ret = substr($v, 0, -1);
  16. switch(strtoupper($l)){
  17. case 'P':
  18. $ret *= 1024;
  19. case 'T':
  20. $ret *= 1024;
  21. case 'G':
  22. $ret *= 1024;
  23. case 'M':
  24. $ret *= 1024;
  25. case 'K':
  26. $ret *= 1024;
  27. break;
  28. }
  29. return $ret;
  30. }
  31. $max_upload_size = min(let_to_num(ini_get('post_max_size')), let_to_num(ini_get('upload_max_filesize')));
  32. $certify_title = '';
  33. $certify_description = '';
  34. if (isset($_POST['submit'])) { // Incoming changes
  35. $certify_title = $addslashes($_POST['certify_title']);
  36. $certify_description = $addslashes($_POST['certify_description']);
  37. if (strlen($certify_id) > 0) {
  38. // COMMIT CHANGES
  39. $query ="UPDATE %scertify
  40. SET
  41. title = '%s',
  42. description = '%s'
  43. WHERE
  44. certify_id = %d
  45. ";
  46. $certify_updated = queryDB($query, array(TABLE_PREFIX, $certify_title, $certify_description, $certify_id ));
  47. if (file_exists($templatefile))
  48. unlink($templatefile);
  49. if ($_FILES['certify_file']['size'] > 0 && $_FILES['certify_file']['error'] == 0) {
  50. if (move_uploaded_file($_FILES['certify_file']['tmp_name'], $templatefile)) {
  51. // File ok
  52. } else {
  53. unlink($templatefile);
  54. }
  55. }
  56. $templatepresent = file_exists($templatefile);
  57. write_to_log(AT_ADMIN_LOG_UPDATE, 'certify', count($certify_updated), sprintf($query,TABLE_PREFIX, $certify_title, $certify_description, $certify_id) );
  58. $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
  59. header('Location: index_instructor.php');
  60. exit;
  61. } else {
  62. // COMMIT NEW
  63. $query = "INSERT INTO %scertify
  64. (course_id,
  65. title,
  66. description)
  67. VALUES (%d,
  68. '%s',
  69. '%s')";
  70. $certify_inserted = queryDB($query, array(TABLE_PREFIX, $_SESSION['course_id'], $certify_title,$certify_description));
  71. $certify_id = at_insert_id();
  72. $templatefile = AT_CONTENT_DIR .'certify/template_'.$certify_id.'.pdf';
  73. if ($_FILES['certify_file']['size'] > 0 && $_FILES['certify_file']['error'] == 0) {
  74. if (move_uploaded_file($_FILES['certify_file']['tmp_name'], $templatefile)) {
  75. // File ok
  76. } else {
  77. unlink($templatefile);
  78. }
  79. }
  80. $templatepresent = file_exists($templatefile);
  81. write_to_log(AT_ADMIN_LOG_INSERT, 'certify', count($certify_updated), sprintf($query,TABLE_PREFIX, $_SESSION['course_id'], $certify_title,$certify_description));
  82. $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
  83. header('Location: index_instructor.php');
  84. exit;
  85. }
  86. } else if (isset($_POST['cancel'])) { // Cancelled
  87. // CANCEL
  88. $msg->addFeedback('CANCELLED');
  89. header('Location: index_instructor.php');
  90. exit;
  91. } else if (strlen($certify_id) > 0) {
  92. // EDIT EXISTING
  93. // Fetch basic data
  94. $query = "SELECT * from %scertify where certify_id=%d";
  95. $rows = queryDB($query, array(TABLE_PREFIX, $certify_id));
  96. if (!$row)
  97. exit; // TODO: Invalid id - how to handle?
  98. foreach($rows as $row){
  99. $certify_title = $row['title'];
  100. $certify_description = $row['description'];
  101. }
  102. }
  103. require(AT_INCLUDE_PATH.'header.inc.php');
  104. $msg->printAll();
  105. ?>
  106. <p>For instructor to add new certificate
  107. <form enctype="multipart/form-data" name="certifydetails" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  108. <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_upload_size; ?>"> <!-- We have no real need to restrict the upload here -->
  109. <?php if (strlen($certify_id) > 0) { ?>
  110. <input type="hidden" name="certify_id" value="<?php echo $certify_id; ?>">
  111. <?php } ?>
  112. <dl>
  113. <dt><label for="certify_title"><?php echo _AT('certify_title'); ?></label></dt>
  114. <dd><input type="text" name="certify_title" maxlength="60" value="<?php echo $certify_title; ?>"></dd>
  115. <dt><label for="certify_description"><?php echo _AT('certify_description'); ?></label></dt>
  116. <dd><textarea name="certify_description" cols="40" rows="5"><?php echo $certify_description; ?></textarea></dd>
  117. <dt><label for="certify_file"><?php echo _AT('certify_file'); ?></label></dt>
  118. <dd><input type="file" name="certify_file"></dd>
  119. </dl>
  120. <input type="submit" name="submit" value="<?php echo _AT('save'); ?>">
  121. <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
  122. </form>
  123. <!-- TODO: Download link for existing template -->
  124. <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>