grade_model.lib.php 10 KB

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