gradebook.lib.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 'c_id',
  16. 'parent_id',
  17. 'grade_model_id',
  18. 'session_id',
  19. 'weight',
  20. 'user_id'
  21. );
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  28. $this->table_skill = Database::get_main_table(TABLE_MAIN_SKILL);
  29. $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
  30. }
  31. /**
  32. * Returns true if the gradebook is active and visible in a course, false
  33. * otherwise.
  34. *
  35. * @param int $c_id Course integer id, defaults to the current course
  36. * @return boolean
  37. */
  38. public static function is_active($c_id = null)
  39. {
  40. $name = 'gradebook';
  41. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  42. $sql = "SELECT * from $table
  43. WHERE variable='course_hide_tools' AND subkey='$name'
  44. LIMIT 1";
  45. $result = Database::query($sql);
  46. $setting = Database::store_result($result);
  47. $setting = isset($setting[0]) ? $setting[0] : null;
  48. $setting = $setting ? $setting : array();
  49. $inactive = isset($setting['selected_value']) && $setting['selected_value'] == 'true';
  50. if ($inactive) {
  51. return false;
  52. }
  53. $c_id = $c_id ? intval($c_id) : api_get_course_int_id();
  54. $table = Database::get_course_table(TABLE_TOOL_LIST);
  55. $sql = "SELECT * from $table
  56. WHERE c_id = $c_id and name='$name'
  57. LIMIT 1";
  58. $result = Database::query($sql);
  59. $item = Database::store_result($result, 'ASSOC');
  60. $item = isset($item[0]) ? $item[0] : null;
  61. if (empty($item)) {
  62. return true;
  63. }
  64. return $item['visibility'] == '1';
  65. }
  66. /**
  67. * @param array $options
  68. *
  69. * @return array
  70. */
  71. public function get_all($options = array())
  72. {
  73. $gradebooks = parent::get_all($options);
  74. foreach ($gradebooks as &$gradebook) {
  75. if (!empty($gradebook['name'])) {
  76. continue;
  77. }
  78. $courseInfo = api_get_course_info_by_id($gradebook['c_id']);
  79. $gradebook['name'] = $courseInfo['code'];
  80. }
  81. return $gradebooks;
  82. }
  83. /**
  84. * @param array $params
  85. *
  86. * @return bool
  87. */
  88. public function update($params)
  89. {
  90. return parent::update($params);
  91. }
  92. /**
  93. * @param int $gradebook_id
  94. * @param array $skill_list
  95. * @return bool
  96. */
  97. public function update_skills_to_gradebook(
  98. $gradebook_id,
  99. $skill_list,
  100. $deleteSkillNotInList = true
  101. ) {
  102. $skill_gradebook = new SkillRelGradebook();
  103. $skill_gradebooks_source = $skill_gradebook->get_all(
  104. array('where' => array('gradebook_id = ?' => $gradebook_id))
  105. );
  106. $clean_gradebook = array();
  107. if (!empty($skill_gradebooks_source)) {
  108. foreach($skill_gradebooks_source as $source) {
  109. $clean_gradebook[] = $source['skill_id'];
  110. }
  111. }
  112. //Cleaning skills
  113. $skill_list = array_map('intval', $skill_list);
  114. $skill_list = array_filter($skill_list);
  115. if (!empty($skill_list)) {
  116. if (!empty($clean_gradebook)) {
  117. $skill_to_remove = array_diff($clean_gradebook, $skill_list);
  118. }
  119. foreach ($skill_list as $skill_id) {
  120. $params = array();
  121. $params['gradebook_id'] = $gradebook_id;
  122. $params['skill_id'] = $skill_id;
  123. if (!$skill_gradebook->exists_gradebook_skill($gradebook_id, $skill_id)) {
  124. $skill_gradebook->save($params);
  125. }
  126. }
  127. } else {
  128. $skill_to_remove = $clean_gradebook;
  129. }
  130. if ($deleteSkillNotInList) {
  131. if (!empty($skill_to_remove)) {
  132. foreach ($skill_to_remove as $remove) {
  133. $skill_item = $skill_gradebook->get_skill_info(
  134. $remove,
  135. $gradebook_id
  136. );
  137. $skill_gradebook->delete($skill_item['id']);
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * Returns a Form validator Obj
  145. * @todo the form should be auto generated
  146. * @param string url
  147. * @param string action add, edit
  148. * @return obj form validator obj
  149. */
  150. public function show_skill_form($gradebook_id, $url, $header = null)
  151. {
  152. $form = new FormValidator('gradebook_add_skill', 'POST', $url);
  153. // Setting the form elements
  154. if (!isset($header)) {
  155. $header = get_lang('Add');
  156. }
  157. $form->addElement('header', '', $header);
  158. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  159. $form->addElement('hidden', 'id', $id);
  160. $skill = new Skill();
  161. $skills = $skill->get_all();
  162. $clean_skill_list = array();
  163. foreach ($skills as $skill) {
  164. $clean_skill_list[$skill['id']] = $skill['name'];
  165. }
  166. $form->addElement(
  167. 'select',
  168. 'skill',
  169. get_lang('Skills'),
  170. $clean_skill_list,
  171. array(
  172. 'multiple' => 'multiple'
  173. )
  174. );
  175. $selected_skills = self::get_skills_by_gradebook($gradebook_id);
  176. $clean_selected_skills = array();
  177. if (!empty($selected_skills)) {
  178. foreach($selected_skills as $skill) {
  179. $clean_selected_skills[] = $skill['id'];
  180. }
  181. }
  182. $form->addButtonCreate(get_lang('Add'), 'submit');
  183. $form->setDefaults(array('skill'=>$clean_selected_skills));
  184. return $form;
  185. }
  186. /**
  187. * @param int $gradebook_id
  188. * @return array|resource
  189. */
  190. function get_skills_by_gradebook($gradebook_id)
  191. {
  192. $gradebook_id = intval($gradebook_id);
  193. $sql = "SELECT skill.id, skill.name FROM {$this->table_skill} skill
  194. INNER JOIN {$this->table_skill_rel_gradebook} skill_rel_gradebook
  195. ON skill.id = skill_rel_gradebook.skill_id
  196. WHERE skill_rel_gradebook.gradebook_id = $gradebook_id";
  197. $result = Database::query($sql);
  198. $result = Database::store_result($result, 'ASSOC');
  199. return $result;
  200. }
  201. /**
  202. * Displays the title + grid
  203. */
  204. public function display()
  205. {
  206. // action links
  207. echo Display::grid_html('gradebooks');
  208. }
  209. }