grade_model.lib.php 11 KB

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