add_eval.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package plugin.ims_lti
  5. */
  6. use Chamilo\CoreBundle\Entity\GradebookEvaluation;
  7. use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
  8. require_once __DIR__.'/../../../main/inc/global.inc.php';
  9. $current_course_tool = TOOL_GRADEBOOK;
  10. api_protect_course_script(true);
  11. api_block_anonymous_users();
  12. GradebookUtils::block_students();
  13. $select_cat = isset($_GET['selectcat']) ? (int) $_GET['selectcat'] : 0;
  14. $is_allowedToEdit = $is_courseAdmin;
  15. $em = Database::getManager();
  16. /** @var \Chamilo\CoreBundle\Entity\Course $course */
  17. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  18. $ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
  19. $categories = Category::load(null, null, $course->getCode());
  20. if (empty($categories)) {
  21. $message = Display::return_message(
  22. get_plugin_lang('GradebookNotSetWarning', 'ImsLtiPlugin'),
  23. 'warning'
  24. );
  25. api_not_allowed(true, $message);
  26. }
  27. $evaladd = new Evaluation();
  28. $evaladd->set_user_id($_user['user_id']);
  29. if (!empty($select_cat)) {
  30. $evaladd->set_category_id($_GET['selectcat']);
  31. $cat = Category::load($_GET['selectcat']);
  32. $evaladd->set_course_code($cat[0]->get_course_code());
  33. } else {
  34. $evaladd->set_category_id(0);
  35. }
  36. $form = new EvalForm(
  37. EvalForm::TYPE_ADD,
  38. $evaladd,
  39. null,
  40. 'add_eval_form',
  41. null,
  42. api_get_self().'?selectcat='.$select_cat.'&'.api_get_cidreq()
  43. );
  44. $form->removeElement('name');
  45. $form->removeElement('addresult');
  46. $slcLtiTools = $form->createElement('select', 'name', get_lang('Tool'));
  47. $form->insertElementBefore($slcLtiTools, 'hid_category_id');
  48. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  49. $ltiTools = $ltiToolRepo->findBy(['course' => $course, 'gradebookEval' => null]);
  50. /** @var ImsLtiTool $ltiTool */
  51. foreach ($ltiTools as $ltiTool) {
  52. $slcLtiTools->addOption($ltiTool->getName(), $ltiTool->getId());
  53. }
  54. if ($form->validate()) {
  55. $values = $form->exportValues();
  56. /** @var ImsLtiTool $ltiTool */
  57. $ltiTool = $ltiToolRepo->find($values['name']);
  58. if (!$ltiTool) {
  59. api_not_allowed();
  60. }
  61. $eval = new Evaluation();
  62. $eval->set_name($ltiTool->getName());
  63. $eval->set_description($values['description']);
  64. $eval->set_user_id($values['hid_user_id']);
  65. if (!empty($values['hid_course_code'])) {
  66. $eval->set_course_code($values['hid_course_code']);
  67. }
  68. //Always add the gradebook to the course
  69. $eval->set_course_code(api_get_course_id());
  70. $eval->set_category_id($values['hid_category_id']);
  71. $parent_cat = Category::load($values['hid_category_id']);
  72. $global_weight = $cat[0]->get_weight();
  73. //$values['weight'] = $values['weight_mask']/$global_weight*$parent_cat[0]->get_weight();
  74. $values['weight'] = $values['weight_mask'];
  75. $eval->set_weight($values['weight']);
  76. $eval->set_max($values['max']);
  77. $eval->set_visible(empty($values['visible']) ? 0 : 1);
  78. $eval->add();
  79. /** @var GradebookEvaluation $gradebookEval */
  80. $gradebookEval = $em->find('ChamiloCoreBundle:GradebookEvaluation', $eval->get_id());
  81. $ltiTool->setGradebookEval($gradebookEval);
  82. $em->persist($ltiTool);
  83. $em->flush();
  84. header('Location: '.Category::getUrl().'selectcat='.$eval->get_category_id());
  85. exit;
  86. }
  87. $interbreadcrumb[] = [
  88. 'url' => Category::getUrl().'selectcat='.$select_cat,
  89. 'name' => get_lang('Gradebook'),
  90. ];
  91. $this_section = SECTION_COURSES;
  92. $htmlHeadXtra[] = '<script>
  93. $(document).ready( function() {
  94. $("#hid_category_id").change(function() {
  95. $("#hid_category_id option:selected").each(function () {
  96. var cat_id = $(this).val();
  97. $.ajax({
  98. url: "'.api_get_path(WEB_AJAX_PATH).'gradebook.ajax.php?a=get_gradebook_weight",
  99. data: "cat_id="+cat_id,
  100. success: function(return_value) {
  101. if (return_value != 0 ) {
  102. $("#max_weight").html(return_value);
  103. }
  104. }
  105. });
  106. });
  107. });
  108. });
  109. </script>';
  110. Display::display_header(get_lang('NewEvaluation'));
  111. $form->display();
  112. Display::display_footer();