grade_model.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GradeModel.
  5. */
  6. class GradeModel extends Model
  7. {
  8. public $table;
  9. public $columns = ['id', 'name', 'description'];
  10. /**
  11. * Constructor.
  12. */
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->table = Database::get_main_table(TABLE_GRADE_MODEL);
  17. }
  18. /**
  19. * @param array $where_conditions
  20. *
  21. * @return array
  22. */
  23. public function get_all($where_conditions = [])
  24. {
  25. return Database::select(
  26. '*',
  27. $this->table,
  28. ['where' => $where_conditions, 'order' => 'name ASC']
  29. );
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function get_count()
  35. {
  36. $row = Database::select(
  37. 'count(*) as count',
  38. $this->table,
  39. [],
  40. 'first'
  41. );
  42. return $row['count'];
  43. }
  44. /**
  45. * Displays the title + grid.
  46. */
  47. public function display()
  48. {
  49. // action links
  50. echo '<div class="actions" style="margin-bottom:20px">';
  51. echo '<a href="grade_models.php">'.
  52. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  53. echo '<a href="'.api_get_self().'?action=add">'.
  54. Display::return_icon('add.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
  55. echo '</div>';
  56. echo Display::grid_html('grade_model');
  57. }
  58. /**
  59. * Returns a Form validator Obj.
  60. *
  61. * @todo the form should be auto generated
  62. *
  63. * @param string $url
  64. * @param string $action add, edit
  65. *
  66. * @return FormValidator form validator obj
  67. */
  68. public function return_form($url, $action)
  69. {
  70. $form = new FormValidator('grades', 'post', $url);
  71. // Setting the form elements
  72. $header = get_lang('Add');
  73. if ($action == 'edit') {
  74. $header = get_lang('Edit');
  75. }
  76. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  77. $form->addHeader($header);
  78. $form->addHidden('id', $id);
  79. $form->addText('name', get_lang('Name'));
  80. $form->addHtmlEditor(
  81. 'description',
  82. get_lang('Description'),
  83. false,
  84. false,
  85. [
  86. 'ToolbarSet' => 'careers',
  87. 'Width' => '100%',
  88. 'Height' => '250',
  89. ]
  90. );
  91. $form->addLabel(get_lang('Components'), '');
  92. // Get components
  93. $nr_items = 2;
  94. $max = 10;
  95. // Setting the defaults
  96. $defaults = $this->get($id);
  97. if ($defaults) {
  98. $components = $this->get_components($defaults['id']);
  99. }
  100. if ($action == 'edit') {
  101. if (!empty($components)) {
  102. $nr_items = count($components);
  103. }
  104. }
  105. $form->addElement('hidden', 'maxvalue', '100');
  106. $form->addElement('hidden', 'minvalue', '0');
  107. $renderer = &$form->defaultRenderer();
  108. $component_array = [];
  109. for ($i = 0; $i <= $max; $i++) {
  110. $counter = $i;
  111. $form->addElement('text', 'components['.$i.'][percentage]', null);
  112. $form->addElement('text', 'components['.$i.'][acronym]', null);
  113. $form->addElement('text', 'components['.$i.'][title]', null);
  114. $form->addElement('hidden', 'components['.$i.'][id]', null);
  115. $template_percentage =
  116. '<div id='.$i.' style="display: '.(($i <= $nr_items) ? 'inline' : 'none').';" class="form-group">
  117. <label for="" class="col-sm-2 control-label">
  118. {label}
  119. </label>
  120. <div class="col-sm-8">
  121. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->
  122. {element} <!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --> % = ';
  123. $template_acronym = '
  124. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->
  125. {element} {label} <!-- BEGIN error --><span class="form_error">{error}</span> <!-- END error -->';
  126. $template_title =
  127. '&nbsp{element} <!-- BEGIN error --> <span class="form_error">{error}</span><!-- END error -->
  128. <a href="javascript:plusItem('.($counter + 1).')">
  129. '.Display::return_icon('add.png', get_lang('Add'), ['id' => 'plus-'.($counter + 1), 'style' => 'display: '.(($counter >= $nr_items) ? 'inline' : 'none')]).'
  130. </a>
  131. <a href="javascript:minItem('.($counter).')">
  132. '.Display::return_icon('delete.png', get_lang('Delete'), ['id' => 'min-'.($counter), 'style' => 'display: '.(($counter >= $nr_items) ? 'inline' : 'none')]).'
  133. </a>
  134. </div></div>';
  135. $renderer->setElementTemplate($template_title, 'components['.$i.'][title]');
  136. $renderer->setElementTemplate($template_percentage, 'components['.$i.'][percentage]');
  137. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][acronym]');
  138. if ($i == 0) {
  139. $form->addRule('components['.$i.'][percentage]', get_lang('Required field'), 'required');
  140. $form->addRule('components['.$i.'][acronym]', get_lang('Required field'), 'required');
  141. $form->addRule('components['.$i.'][title]', get_lang('Required field'), 'required');
  142. }
  143. $form->addRule('components['.$i.'][percentage]', get_lang('Only numbers'), 'numeric');
  144. $form->addRule(['components['.$i.'][percentage]', 'maxvalue'], get_lang('Over 100'), 'compare', '<=');
  145. $form->addRule(['components['.$i.'][percentage]', 'minvalue'], get_lang('Under the minimum.'), 'compare', '>=');
  146. $component_array[] = 'components['.$i.'][percentage]';
  147. }
  148. //New rule added in the formvalidator compare_fields that filters a group of fields in order to compare with the wanted value
  149. $form->addRule($component_array, get_lang('The sum of all values must be 100'), 'compare_fields', '==@100');
  150. $form->addElement('label', '', get_lang('The sum of all values must be 100'));
  151. if ($action == 'edit') {
  152. $form->addButtonUpdate(get_lang('Edit'));
  153. } else {
  154. $form->addButtonCreate(get_lang('Add'));
  155. }
  156. if (!empty($components)) {
  157. $counter = 0;
  158. foreach ($components as $component) {
  159. foreach ($component as $key => $value) {
  160. $defaults['components['.$counter.']['.$key.']'] = $value;
  161. }
  162. $counter++;
  163. }
  164. }
  165. $form->setDefaults($defaults);
  166. // Setting the rules
  167. $form->addRule('name', get_lang('Required field'), 'required');
  168. return $form;
  169. }
  170. /**
  171. * @param $id
  172. *
  173. * @return array|null
  174. */
  175. public function get_components($id)
  176. {
  177. $obj = new GradeModelComponents();
  178. if (!empty($id)) {
  179. return $obj->get_all(['where' => ['grade_model_id = ?' => $id]]);
  180. }
  181. return null;
  182. }
  183. /**
  184. * @param array $params
  185. * @param bool $show_query
  186. *
  187. * @return bool
  188. */
  189. public function save($params, $show_query = false)
  190. {
  191. $id = parent::save($params, $show_query);
  192. if (!empty($id)) {
  193. foreach ($params['components'] as $component) {
  194. if (!empty($component['title']) && !empty($component['percentage']) && !empty($component['acronym'])) {
  195. $obj = new GradeModelComponents();
  196. $component['grade_model_id'] = $id;
  197. $obj->save($component);
  198. }
  199. }
  200. }
  201. //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  202. return $id;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function update($params, $showQuery = false)
  208. {
  209. parent::update($params, $showQuery);
  210. if (!empty($params['id'])) {
  211. foreach ($params['components'] as $component) {
  212. $obj = new GradeModelComponents();
  213. $component['grade_model_id'] = $params['id'];
  214. if (empty($component['title']) && empty($component['percentage']) && empty($component['acronym'])) {
  215. $obj->delete($component['id']);
  216. } else {
  217. $obj->update($component);
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * @param int $id
  224. */
  225. public function delete($id)
  226. {
  227. parent::delete($id);
  228. }
  229. /**
  230. * @param $form
  231. * @param string $name
  232. * @param null $default_value
  233. *
  234. * @return bool
  235. */
  236. public function fill_grade_model_select_in_form(&$form, $name = 'gradebook_model_id', $default_value = null)
  237. {
  238. if (api_get_setting('gradebook_enable_grade_model') === 'false') {
  239. return false;
  240. }
  241. if (api_get_setting('teachers_can_change_grade_model_settings') === 'true' || api_is_platform_admin()) {
  242. $grade_models = $this->get_all();
  243. $grade_model_options = ['-1' => get_lang('none')];
  244. if (!empty($grade_models)) {
  245. foreach ($grade_models as $item) {
  246. $grade_model_options[$item['id']] = $item['name'];
  247. }
  248. }
  249. $form->addElement('select', $name, get_lang('Grading model'), $grade_model_options);
  250. $default_platform_setting = api_get_setting('gradebook_default_grade_model_id');
  251. $default = -1;
  252. if ($default_platform_setting == -1) {
  253. if (!empty($default_value)) {
  254. $default = $default_value;
  255. }
  256. } else {
  257. $default = $default_platform_setting;
  258. }
  259. if (!empty($default) && $default != '-1') {
  260. $form->setDefaults([$name => $default]);
  261. }
  262. }
  263. }
  264. }
  265. /**
  266. * Class GradeModelComponents.
  267. */
  268. class GradeModelComponents extends Model
  269. {
  270. public $table;
  271. public $columns = ['id', 'title', 'percentage', 'acronym', 'grade_model_id'];
  272. /**
  273. * GradeModelComponents constructor.
  274. */
  275. public function __construct()
  276. {
  277. parent::__construct();
  278. $this->table = Database::get_main_table(TABLE_GRADE_MODEL_COMPONENTS);
  279. }
  280. /**
  281. * @param array $params
  282. * @param bool $showQuery
  283. *
  284. * @return bool
  285. */
  286. public function save($params, $showQuery = false)
  287. {
  288. $id = parent::save($params, $showQuery);
  289. return $id;
  290. }
  291. }