gradebook.lib.php 6.7 KB

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