gradebook.lib.php 6.8 KB

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