grade_model.lib.php 11 KB

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