gradebook.lib.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. class Gradebook extends Model
  9. {
  10. public $columns = [
  11. 'id',
  12. 'name',
  13. 'description',
  14. 'course_code',
  15. 'parent_id',
  16. 'grade_model_id',
  17. 'session_id',
  18. 'weight',
  19. 'user_id',
  20. ];
  21. /**
  22. * Constructor.
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  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. *
  37. * @return bool
  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 : [];
  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 = [])
  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 int $gradebook_id
  84. * @param array $skill_list
  85. * @param bool $deleteSkillNotInList
  86. *
  87. * @return bool
  88. */
  89. public function updateSkillsToGradeBook(
  90. $gradebook_id,
  91. $skill_list,
  92. $deleteSkillNotInList = true
  93. ) {
  94. $skill_gradebook = new SkillRelGradebook();
  95. $skill_gradebooks_source = $skill_gradebook->get_all(
  96. ['where' => ['gradebook_id = ?' => $gradebook_id]]
  97. );
  98. $clean_gradebook = [];
  99. if (!empty($skill_gradebooks_source)) {
  100. foreach ($skill_gradebooks_source as $source) {
  101. $clean_gradebook[] = $source['skill_id'];
  102. }
  103. }
  104. //Cleaning skills
  105. if (!empty($skill_list)) {
  106. $skill_list = array_map('intval', $skill_list);
  107. $skill_list = array_filter($skill_list);
  108. }
  109. if (!empty($skill_list)) {
  110. if (!empty($clean_gradebook)) {
  111. $skill_to_remove = array_diff($clean_gradebook, $skill_list);
  112. }
  113. foreach ($skill_list as $skill_id) {
  114. $params = [];
  115. $params['gradebook_id'] = $gradebook_id;
  116. $params['skill_id'] = $skill_id;
  117. if (!$skill_gradebook->existsGradeBookSkill($gradebook_id, $skill_id)) {
  118. $skill_gradebook->save($params);
  119. }
  120. }
  121. } else {
  122. $skill_to_remove = $clean_gradebook;
  123. }
  124. if ($deleteSkillNotInList) {
  125. if (!empty($skill_to_remove)) {
  126. foreach ($skill_to_remove as $remove) {
  127. $skill_item = $skill_gradebook->getSkillInfo(
  128. $remove,
  129. $gradebook_id
  130. );
  131. $skill_gradebook->delete($skill_item['id']);
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Returns a Form validator Obj.
  139. *
  140. * @todo the form should be auto generated
  141. *
  142. * @param string url
  143. * @param string action add, edit
  144. *
  145. * @return FormValidator
  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. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  155. $form->addHeader($header);
  156. $form->addElement('hidden', 'id', $id);
  157. $skill = new Skill();
  158. $skills = $skill->get_all();
  159. $clean_skill_list = [];
  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. [
  169. 'multiple' => 'multiple',
  170. ]
  171. );
  172. $selected_skills = self::getSkillsByGradebook($gradebook_id);
  173. $clean_selected_skills = [];
  174. if (!empty($selected_skills)) {
  175. foreach ($selected_skills as $skill) {
  176. $clean_selected_skills[] = $skill['id'];
  177. }
  178. }
  179. $form->addButtonCreate(get_lang('Add'), 'submit');
  180. $form->setDefaults(['skill' => $clean_selected_skills]);
  181. return $form;
  182. }
  183. /**
  184. * @param int $gradebook_id
  185. *
  186. * @return array|resource
  187. */
  188. public function getSkillsByGradebook($gradebook_id)
  189. {
  190. $gradebook_id = intval($gradebook_id);
  191. $sql = "SELECT skill.id, skill.name
  192. 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 self::returnGrid();
  207. }
  208. /**
  209. * Displays the title + grid.
  210. */
  211. public function returnGrid()
  212. {
  213. // action links
  214. return Display::grid_html('gradebooks');
  215. }
  216. }