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. $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. $gradebook['name'] = $gradebook['course_code'];
  77. }
  78. }
  79. return $gradebooks;
  80. }
  81. /**
  82. * @param array $params
  83. *
  84. * @return bool
  85. */
  86. public function update($params)
  87. {
  88. return parent::update($params);
  89. }
  90. /**
  91. * @param int $gradebook_id
  92. * @param array $skill_list
  93. * @return bool
  94. */
  95. public function update_skills_to_gradebook(
  96. $gradebook_id,
  97. $skill_list,
  98. $deleteSkillNotInList = true
  99. ) {
  100. if (!empty($skill_list)) {
  101. //Cleaning skills
  102. $skill_list = array_map('intval', $skill_list);
  103. $skill_list = array_filter($skill_list);
  104. $skill_gradebook = new SkillRelGradebook();
  105. $skill_gradebooks_source = $skill_gradebook->get_all(
  106. array('where' => array('gradebook_id = ?' => $gradebook_id))
  107. );
  108. $clean_gradebook = array();
  109. if (!empty($skill_gradebooks_source)) {
  110. foreach($skill_gradebooks_source as $source) {
  111. $clean_gradebook[]= $source['skill_id'];
  112. }
  113. }
  114. if (!empty($clean_gradebook)) {
  115. $skill_to_remove = array_diff($clean_gradebook, $skill_list);
  116. }
  117. foreach ($skill_list as $skill_id) {
  118. $params = array();
  119. $params['gradebook_id'] = $gradebook_id;
  120. $params['skill_id'] = $skill_id;
  121. if (!$skill_gradebook->exists_gradebook_skill($gradebook_id, $skill_id)) {
  122. $skill_gradebook->save($params);
  123. }
  124. }
  125. if ($deleteSkillNotInList) {
  126. if (!empty($skill_to_remove)) {
  127. foreach ($skill_to_remove as $remove) {
  128. $skill_item = $skill_gradebook->get_skill_info(
  129. $remove,
  130. $gradebook_id
  131. );
  132. $skill_gradebook->delete($skill_item['id']);
  133. }
  134. }
  135. }
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Returns a Form validator Obj
  142. * @todo the form should be auto generated
  143. * @param string url
  144. * @param string action add, edit
  145. * @return obj form validator obj
  146. */
  147. public function show_skill_form($gradebook_id, $url, $header = null)
  148. {
  149. $form = new FormValidator('gradebook_add_skill', 'POST', $url);
  150. // Setting the form elements
  151. if (!isset($header)) {
  152. $header = get_lang('Add');
  153. }
  154. $form->addElement('header', '', $header);
  155. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  156. $form->addElement('hidden', 'id', $id);
  157. $skill = new Skill();
  158. $skills = $skill->get_all();
  159. $clean_skill_list = array();
  160. foreach ($skills as $skill) {
  161. $clean_skill_list[$skill['id']] = $skill['name'];
  162. }
  163. $form->addElement(
  164. 'select',
  165. 'skill',
  166. get_lang('Skills'),
  167. $clean_skill_list,
  168. array(
  169. 'width' => '450px',
  170. 'class' => 'chzn-select',
  171. 'multiple' => 'multiple'
  172. )
  173. );
  174. $selected_skills = self::get_skills_by_gradebook($gradebook_id);
  175. $clean_selected_skills = array();
  176. if (!empty($selected_skills)) {
  177. foreach($selected_skills as $skill) {
  178. $clean_selected_skills[] = $skill['id'];
  179. }
  180. }
  181. $form->addButtonCreate(get_lang('Add'), 'submit');
  182. $form->setDefaults(array('skill'=>$clean_selected_skills));
  183. return $form;
  184. }
  185. /**
  186. * @param int $gradebook_id
  187. * @return array|resource
  188. */
  189. function get_skills_by_gradebook($gradebook_id)
  190. {
  191. $gradebook_id = intval($gradebook_id);
  192. $sql = "SELECT skill.id, skill.name FROM {$this->table_skill} skill
  193. INNER JOIN {$this->table_skill_rel_gradebook} skill_rel_gradebook
  194. ON skill.id = skill_rel_gradebook.skill_id
  195. WHERE skill_rel_gradebook.gradebook_id = $gradebook_id";
  196. $result = Database::query($sql);
  197. $result = Database::store_result($result, 'ASSOC');
  198. return $result;
  199. }
  200. /**
  201. * Displays the title + grid
  202. */
  203. public function display()
  204. {
  205. // action links
  206. echo Display::grid_html('gradebooks');
  207. }
  208. }