gradebook_display_certificate.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $current_course_tool = TOOL_GRADEBOOK;
  9. if (!api_is_student_boss()) {
  10. api_protect_course_script(true);
  11. }
  12. set_time_limit(0);
  13. ini_set('max_execution_time', 0);
  14. //extra javascript functions for in html head:
  15. $htmlHeadXtra[] = "<script>
  16. function confirmation() {
  17. if (confirm(\" " . trim(get_lang('AreYouSureToDelete'))." ?\")) {
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23. </script>";
  24. api_block_anonymous_users();
  25. if (!api_is_allowed_to_edit() && !api_is_student_boss()) {
  26. api_not_allowed(true);
  27. }
  28. $cat_id = isset($_GET['cat_id']) ? (int) $_GET['cat_id'] : null;
  29. $action = isset($_GET['action']) && $_GET['action'] ? $_GET['action'] : null;
  30. $filterOfficialCode = isset($_POST['filter']) ? Security::remove_XSS($_POST['filter']) : null;
  31. $filterOfficialCodeGet = isset($_GET['filter']) ? Security::remove_XSS($_GET['filter']) : null;
  32. $url = api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id.'&filter='.$filterOfficialCode;
  33. $courseInfo = api_get_course_info();
  34. $filter = api_get_setting('certificate_filter_by_official_code');
  35. $userList = array();
  36. $filterForm = null;
  37. $certificate_list = array();
  38. if ($filter === 'true') {
  39. $options = UserManager::getOfficialCodeGrouped();
  40. $options = array_merge(array('all' => get_lang('All')), $options);
  41. $form = new FormValidator(
  42. 'official_code_filter',
  43. 'POST',
  44. api_get_self().'?'.api_get_cidreq().'&cat_id='.$cat_id
  45. );
  46. $form->addElement('select', 'filter', get_lang('OfficialCode'), $options);
  47. $form->addButton('submit', get_lang('Submit'));
  48. $filterForm = '<br />'.$form->returnForm();
  49. if ($form->validate()) {
  50. $officialCode = $form->getSubmitValue('filter');
  51. if ($officialCode == 'all') {
  52. $certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
  53. } else {
  54. $userList = UserManager::getUsersByOfficialCode($officialCode);
  55. if (!empty($userList)) {
  56. $certificate_list = GradebookUtils::get_list_users_certificates(
  57. $cat_id,
  58. $userList
  59. );
  60. }
  61. }
  62. } else {
  63. $certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
  64. }
  65. } else {
  66. $certificate_list = GradebookUtils::get_list_users_certificates($cat_id);
  67. }
  68. $content = '';
  69. $tags = Certificate::notificationTags();
  70. switch ($action) {
  71. case 'send_notifications':
  72. $currentUserInfo = api_get_user_info();
  73. $message = isset($_POST['message']) ? $_POST['message'] : '';
  74. $subject = get_lang('NotificationCertificateSubject');
  75. if (!empty($originalMessage)) {
  76. foreach ($certificate_list as $index => $value) {
  77. $userInfo = api_get_user_info($value['user_id']);
  78. if (empty($userInfo)) {
  79. continue;
  80. }
  81. $list = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  82. $value['user_id'],
  83. $cat_id
  84. );
  85. foreach ($list as $valueCertificate) {
  86. Certificate::sendNotification(
  87. $subject,
  88. $message,
  89. $userInfo,
  90. $courseInfo,
  91. $valueCertificate
  92. );
  93. }
  94. }
  95. Display::addFlash(Display::return_message(get_lang('Sent')));
  96. }
  97. header('Location: '.$url);
  98. exit;
  99. break;
  100. case 'show_notification_form':
  101. $form = new FormValidator('notification', 'post', $url.'&action=send_notifications');
  102. $form->addHeader(get_lang('SendNotification'));
  103. $form->addHtmlEditor('message', get_lang('Message'));
  104. $form->addLabel(
  105. get_lang('Tags'),
  106. Display::return_message(implode('<br />', $tags), 'normal', false)
  107. );
  108. $form->addButtonSend(get_lang('Send'));
  109. $form->setDefaults(
  110. ['message' => nl2br(get_lang('NotificationCertificateTemplate'))]
  111. );
  112. $content = $form->returnForm();
  113. break;
  114. case 'export_all_certificates':
  115. if (api_is_student_boss()) {
  116. $userGroup = new UserGroup();
  117. $userList = $userGroup->getGroupUsersByUser(api_get_user_id());
  118. } else {
  119. $userList = array();
  120. if (!empty($filterOfficialCodeGet)) {
  121. $userList = UserManager::getUsersByOfficialCode($filterOfficialCodeGet);
  122. }
  123. }
  124. Category::exportAllCertificates($cat_id, $userList);
  125. break;
  126. case 'generate_all_certificates':
  127. $userList = CourseManager::get_user_list_from_course_code(
  128. api_get_course_id(),
  129. api_get_session_id()
  130. );
  131. if (!empty($userList)) {
  132. foreach ($userList as $userInfo) {
  133. if ($userInfo['status'] == INVITEE) {
  134. continue;
  135. }
  136. Category::generateUserCertificate($cat_id, $userInfo['user_id']);
  137. }
  138. }
  139. break;
  140. case 'delete_all_certificates':
  141. Category::deleteAllCertificates($cat_id);
  142. break;
  143. }
  144. $interbreadcrumb[] = array(
  145. 'url' => Category::getUrl(),
  146. 'name' => get_lang('Gradebook'),
  147. );
  148. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('GradebookListOfStudentsCertificates'));
  149. $this_section = SECTION_COURSES;
  150. Display::display_header('');
  151. if (isset($_GET['action']) && $_GET['action'] == 'delete') {
  152. $check = Security::check_token('get');
  153. if ($check) {
  154. $certificate = new Certificate($_GET['certificate_id']);
  155. $result = $certificate->delete(true);
  156. Security::clear_token();
  157. if ($result == true) {
  158. echo Display::return_message(get_lang('CertificateRemoved'), 'confirmation');
  159. } else {
  160. echo Display::return_message(get_lang('CertificateNotRemoved'), 'error');
  161. }
  162. }
  163. }
  164. $token = Security::get_token();
  165. echo Display::page_header(get_lang('GradebookListOfStudentsCertificates'));
  166. if (!empty($content)) {
  167. echo $content;
  168. }
  169. //@todo replace all this code with something like get_total_weight()
  170. $cats = Category::load($cat_id, null, null, null, null, null, false);
  171. if (!empty($cats)) {
  172. //with this fix the teacher only can view 1 gradebook
  173. if (api_is_platform_admin()) {
  174. $stud_id = (api_is_allowed_to_edit() ? null : api_get_user_id());
  175. } else {
  176. $stud_id = api_get_user_id();
  177. }
  178. $total_weight = $cats[0]->get_weight();
  179. $allcat = $cats[0]->get_subcategories(
  180. $stud_id,
  181. api_get_course_id(),
  182. api_get_session_id()
  183. );
  184. $alleval = $cats[0]->get_evaluations($stud_id);
  185. $alllink = $cats[0]->get_links($stud_id);
  186. $datagen = new GradebookDataGenerator($allcat, $alleval, $alllink);
  187. $total_resource_weight = 0;
  188. if (!empty($datagen)) {
  189. $data_array = $datagen->get_data(
  190. 0,
  191. 0,
  192. null,
  193. true
  194. );
  195. if (!empty($data_array)) {
  196. $newarray = array();
  197. foreach ($data_array as $data) {
  198. $newarray[] = array_slice($data, 1);
  199. }
  200. foreach ($newarray as $item) {
  201. $total_resource_weight = $total_resource_weight + $item['2'];
  202. }
  203. }
  204. }
  205. if ($total_resource_weight != $total_weight) {
  206. echo Display::return_message(
  207. get_lang('SumOfActivitiesWeightMustBeEqualToTotalWeight'),
  208. 'warning'
  209. );
  210. }
  211. }
  212. echo '<div class="btn-group">';
  213. echo Display::url(
  214. get_lang('GenerateCertificates'),
  215. $url.'&action=generate_all_certificates',
  216. array('class' => 'btn btn-default')
  217. );
  218. echo Display::url(
  219. get_lang('DeleteAllCertificates'),
  220. $url.'&action=delete_all_certificates',
  221. array('class' => 'btn btn-default')
  222. );
  223. $hideCertificateExport = api_get_setting('hide_certificate_export_link');
  224. if (count($certificate_list) > 0 && $hideCertificateExport !== 'true') {
  225. echo Display::url(
  226. get_lang('ExportAllCertificatesToPDF'),
  227. $url.'&action=export_all_certificates',
  228. array('class' => 'btn btn-default')
  229. );
  230. echo Display::url(
  231. get_lang('SendCertificateNotifications'),
  232. $url.'&action=show_notification_form',
  233. array('class' => 'btn btn-default')
  234. );
  235. }
  236. echo '</div>';
  237. echo $filterForm;
  238. if (count($certificate_list) == 0) {
  239. echo Display::return_message(get_lang('NoResultsAvailable'), 'warning');
  240. } else {
  241. echo '<br /><br /><table class="data_table">';
  242. foreach ($certificate_list as $index => $value) {
  243. echo '<tr>
  244. <td width="100%" class="actions">'.get_lang('Student').' : '.api_get_person_name($value['firstname'], $value['lastname']).' ('.$value['username'].')</td>';
  245. echo '</tr>';
  246. echo '<tr><td>
  247. <table class="data_table">';
  248. $list = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  249. $value['user_id'],
  250. $cat_id
  251. );
  252. foreach ($list as $valueCertificate) {
  253. echo '<tr>';
  254. echo '<td width="50%">'.get_lang('Score').' : '.$valueCertificate['score_certificate'].'</td>';
  255. echo '<td width="30%">'.get_lang('Date').' : '.api_convert_and_format_date($valueCertificate['created_at']).'</td>';
  256. echo '<td width="20%">';
  257. $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$valueCertificate['id'];
  258. $certificates = Display::url(
  259. get_lang('Certificate'),
  260. $url,
  261. array('target' => '_blank', 'class' => 'btn btn-default')
  262. );
  263. echo $certificates;
  264. echo '<a onclick="return confirmation();" href="gradebook_display_certificate.php?sec_token='.$token.'&'.api_get_cidreq().'&action=delete&cat_id='.$cat_id.'&certificate_id='.$valueCertificate['id'].'">
  265. '.Display::return_icon('delete.png', get_lang('Delete')).'
  266. </a>';
  267. echo '</td></tr>';
  268. }
  269. echo '</table>';
  270. echo '</td></tr>';
  271. }
  272. echo '</table>';
  273. }
  274. Display::display_footer();