gradebook.lib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the notebook management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Code
  10. */
  11. class Gradebook extends Model
  12. {
  13. public $columns = array(
  14. 'id',
  15. 'name',
  16. 'description',
  17. 'course_code',
  18. 'parent_id',
  19. 'grade_model_id',
  20. 'session_id',
  21. 'weight',
  22. 'user_id'
  23. );
  24. public function __construct()
  25. {
  26. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  27. $this->table_skill = Database::get_main_table(TABLE_MAIN_SKILL);
  28. $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  29. }
  30. /**
  31. * Returns true if the gradebook is active and visible in a course, false
  32. * otherwise.
  33. *
  34. * @param int $c_id Course integer id, defaults to the current course
  35. * @return boolean
  36. */
  37. public static function is_active($c_id = null)
  38. {
  39. $name = 'gradebook';
  40. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  41. $sql = "SELECT * from $table WHERE variable='course_hide_tools' AND subkey='$name'";
  42. $setting = ResultSet::create($sql)->first();
  43. $setting = $setting ? $setting : array();
  44. $inactive = isset($setting['selected_value']) && $setting['selected_value'] == 'true';
  45. if ($inactive) {
  46. return false;
  47. }
  48. $c_id = $c_id ? intval($c_id) : api_get_course_int_id();
  49. $table = Database::get_course_table(TABLE_TOOL_LIST);
  50. $sql = "SELECT * from $table WHERE c_id = $c_id and name='$name'";
  51. $item = ResultSet::create($sql)->first();
  52. if (empty($item)) {
  53. return true;
  54. }
  55. return $item['visibility'] == '1';
  56. }
  57. public function get_all($options = array())
  58. {
  59. $gradebooks = parent::get_all($options);
  60. foreach ($gradebooks as &$gradebook) {
  61. if (empty($gradebook['name'])) {
  62. $gradebook['name'] = $gradebook['course_code'];
  63. }
  64. }
  65. return $gradebooks;
  66. }
  67. public function update($params)
  68. {
  69. return parent::update($params);
  70. }
  71. public function update_skills_to_gradebook($gradebook_id, $skill_list)
  72. {
  73. if (!empty($skill_list)) {
  74. //Cleaning skills
  75. $skill_list = array_map('intval', $skill_list);
  76. $skill_list = array_filter($skill_list);
  77. $skill_gradebook = new SkillRelGradebook();
  78. $skill_gradebooks_source = $skill_gradebook->get_all(
  79. array('where' => array('gradebook_id = ?' => $gradebook_id))
  80. );
  81. $clean_gradebook = array();
  82. if (!empty($skill_gradebooks_source)) {
  83. foreach ($skill_gradebooks_source as $source) {
  84. $clean_gradebook[] = $source['skill_id'];
  85. }
  86. }
  87. if (!empty($clean_gradebook)) {
  88. $skill_to_remove = array_diff($clean_gradebook, $skill_list);
  89. }
  90. foreach ($skill_list as $skill_id) {
  91. $params = array();
  92. $params['gradebook_id'] = $gradebook_id;
  93. $params['skill_id'] = $skill_id;
  94. if (!$skill_gradebook->exists_gradebook_skill($gradebook_id, $skill_id)) {
  95. $skill_gradebook->save($params);
  96. }
  97. }
  98. if (!empty($skill_to_remove)) {
  99. foreach ($skill_to_remove as $remove) {
  100. $skill_item = $skill_gradebook->get_skill_info($remove, $gradebook_id);
  101. $skill_gradebook->delete($skill_item['id']);
  102. }
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Returns a Form validator Obj
  110. * @todo the form should be auto generated
  111. * @param string url
  112. * @param string action add, edit
  113. * @return obj form validator obj
  114. */
  115. public function show_skill_form($gradebook_id, $url, $header = null)
  116. {
  117. $form = new FormValidator('gradebook_add_skill', 'POST', $url);
  118. // Settting the form elements
  119. if (!isset($header)) {
  120. $header = get_lang('Add');
  121. }
  122. $form->addElement('header', '', $header);
  123. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  124. $form->addElement('hidden', 'id', $id);
  125. $skill = new Skill();
  126. $skills = $skill->get_all();
  127. $clean_skill_list = array();
  128. foreach ($skills as $skill) {
  129. $clean_skill_list[$skill['id']] = $skill['name'];
  130. }
  131. $form->addElement(
  132. 'select',
  133. 'skill',
  134. get_lang('Skills'),
  135. $clean_skill_list,
  136. array('width' => '450px', 'class' => 'chzn-select', 'multiple' => 'multiple')
  137. );
  138. $selected_skills = self::get_skills_by_gradebook($gradebook_id);
  139. $clean_selected_skills = array();
  140. if (!empty($selected_skills)) {
  141. foreach ($selected_skills as $skill) {
  142. $clean_selected_skills[] = $skill['id'];
  143. }
  144. }
  145. $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="save"');
  146. $form->setDefaults(array('skill' => $clean_selected_skills));
  147. return $form;
  148. }
  149. function get_skills_by_gradebook($gradebook_id)
  150. {
  151. $gradebook_id = intval($gradebook_id);
  152. $sql = "SELECT skill.id, skill.name FROM {$this->table_skill} skill INNER JOIN {$this->table_skill_rel_gradebook} skill_rel_gradebook
  153. ON skill.id = skill_rel_gradebook.skill_id
  154. WHERE skill_rel_gradebook.gradebook_id = $gradebook_id";
  155. $result = Database::query($sql);
  156. $result = Database::store_result($result, 'ASSOC');
  157. return $result;
  158. }
  159. /**
  160. * Displays the title + grid
  161. */
  162. public function display()
  163. {
  164. // action links
  165. echo Display::grid_html('gradebooks');
  166. }
  167. }