gradebook_display_certificate.php 11 KB

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