gradebook.lib.php 6.8 KB

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