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