grade_model.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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->addElement('header', $header);
  73. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  74. $form->addElement('hidden', 'id', $id);
  75. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  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. $components = $this->get_components($defaults['id']);
  94. if ($action == 'edit') {
  95. if (!empty($components)) {
  96. $nr_items = count($components) -1;
  97. }
  98. }
  99. $form->addElement('hidden', 'maxvalue', '100');
  100. $form->addElement('hidden', 'minvalue', '0');
  101. $renderer = & $form->defaultRenderer();
  102. $component_array = array();
  103. for ($i = 0; $i <= $max; $i++) {
  104. $counter = $i;
  105. $form->addElement('text', 'components['.$i.'][percentage]', null);
  106. $form->addElement('text', 'components['.$i.'][acronym]', null);
  107. $form->addElement('text', 'components['.$i.'][title]', null);
  108. $form->addElement('hidden', 'components['.$i.'][id]', null);
  109. $template_percentage =
  110. '<div id=' . $i . ' style="display: '.(($i<=$nr_items)?'inline':'none').';" class="control-group">
  111. <p>
  112. <label class="control-label">{label}</label>
  113. <div class="controls">
  114. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->
  115. {element} <!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --> % = ';
  116. $template_acronym = '
  117. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->
  118. {element} {label} <!-- BEGIN error --><span class="form_error">{error}</span> <!-- END error -->';
  119. $template_title =
  120. '&nbsp{element} <!-- BEGIN error --> <span class="form_error">{error}</span><!-- END error -->
  121. <a href="javascript:plusItem(' . ($counter+1) . ')">
  122. <img style="display: '.(($counter>=$nr_items)?'inline':'none').';" id="plus-' . ($counter+1) . '" src="'.Display::return_icon('add.png').'" alt="'.get_lang('Add').'" title="'.get_lang('Add').'">
  123. </a>
  124. <a href="javascript:minItem(' . ($counter) . ')">
  125. <img style="display: '.(($counter>=$nr_items)?'inline':'none').';" id="min-' . $counter . '" src="'.Display::return_icon('delete.png').'" alt="'.get_lang('Delete').'" title="'.get_lang('Delete').'">
  126. </a>
  127. </div></p></div>';
  128. $renderer->setElementTemplate($template_title, 'components['.$i.'][title]');
  129. $renderer->setElementTemplate($template_percentage, 'components['.$i.'][percentage]');
  130. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][acronym]');
  131. if ($i == 0) {
  132. $form->addRule('components['.$i.'][percentage]', get_lang('ThisFieldIsRequired'), 'required');
  133. $form->addRule('components['.$i.'][title]', get_lang('ThisFieldIsRequired'), 'required');
  134. $form->addRule('components['.$i.'][acronym]', get_lang('ThisFieldIsRequired'), 'required');
  135. }
  136. $form->addRule('components['.$i.'][percentage]', get_lang('OnlyNumbers'), 'numeric');
  137. $form->addRule(array('components['.$i.'][percentage]', 'maxvalue'), get_lang('Over100'), 'compare', '<=');
  138. $form->addRule(array('components['.$i.'][percentage]', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  139. $component_array[] = 'components['.$i.'][percentage]';
  140. }
  141. //New rule added in the formvalidator compare_fields that filters a group of fields in order to compare with the wanted value
  142. $form->addRule($component_array, get_lang('AllMustWeight100'), 'compare_fields', '==@100');
  143. $form->addElement('label', '', get_lang('AllMustWeight100'));
  144. if ($action == 'edit') {
  145. $form->addButtonUpdate(get_lang('Modify'));
  146. } else {
  147. $form->addButtonCreate(get_lang('Add'));
  148. }
  149. if (!empty($components)) {
  150. $counter = 0;
  151. foreach ($components as $component) {
  152. foreach ($component as $key => $value) {
  153. $defaults['components['.$counter.']['.$key.']'] = $value;
  154. }
  155. $counter++;
  156. }
  157. }
  158. $form->setDefaults($defaults);
  159. // Setting the rules
  160. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  161. return $form;
  162. }
  163. public function get_components($id)
  164. {
  165. $obj = new GradeModelComponents();
  166. if (!empty($id)) {
  167. return $obj->get_all(array('where'=> array('grade_model_id = ?' => $id)));
  168. }
  169. return null;
  170. }
  171. public function save($params, $show_query = false)
  172. {
  173. $id = parent::save($params, $show_query);
  174. if (!empty($id)) {
  175. foreach ($params['components'] as $component) {
  176. if (!empty($component['title']) && !empty($component['percentage']) && !empty($component['acronym'])) {
  177. $obj = new GradeModelComponents();
  178. $component['grade_model_id'] = $id;
  179. $obj->save($component);
  180. }
  181. }
  182. }
  183. //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  184. return $id;
  185. }
  186. /**
  187. * @param array $params
  188. */
  189. public function update($params)
  190. {
  191. parent::update($params);
  192. if (!empty($params['id'])) {
  193. foreach ($params['components'] as $component) {
  194. $obj = new GradeModelComponents();
  195. $component['grade_model_id'] = $params['id'];
  196. if (empty($component['title']) && empty($component['percentage']) && empty($component['acronym'])) {
  197. $obj->delete($component['id']);
  198. } else {
  199. $obj->update($component);
  200. }
  201. }
  202. }
  203. //$params['components']
  204. }
  205. public function delete($id)
  206. {
  207. parent::delete($id);
  208. //event_system(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  209. }
  210. public function fill_grade_model_select_in_form(&$form, $name = 'gradebook_model_id', $default_value = null)
  211. {
  212. if (api_get_setting(
  213. 'gradebook.gradebook_enable_grade_model'
  214. ) == 'false'
  215. ) {
  216. return false;
  217. }
  218. if (api_get_setting(
  219. 'gradebook.teachers_can_change_grade_model_settings'
  220. ) == 'true' || api_is_platform_admin()
  221. ) {
  222. $grade_models = $this->get_all();
  223. $grade_model_options = array('-1' => get_lang('None'));
  224. if (!empty($grade_models)) {
  225. foreach ($grade_models as $item) {
  226. $grade_model_options[$item['id']] = $item['name'];
  227. }
  228. }
  229. $form->addElement('select', $name, get_lang('GradeModel'), $grade_model_options);
  230. $default_platform_setting = api_get_setting(
  231. 'gradebook.gradebook_default_grade_model_id'
  232. );
  233. $default = -1;
  234. if ($default_platform_setting == -1) {
  235. if (!empty($default_value)) {
  236. $default = $default_value;
  237. }
  238. } else {
  239. $default = $default_platform_setting;
  240. }
  241. if (!empty($default) && $default != '-1') {
  242. $form->setDefaults(array($name => $default));
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * Class GradeModelComponents
  249. */
  250. class GradeModelComponents extends Model
  251. {
  252. public $table;
  253. public $columns = array('id', 'title', 'percentage', 'acronym', 'grade_model_id');
  254. public function __construct()
  255. {
  256. $this->table = Database::get_main_table(TABLE_GRADE_MODEL_COMPONENTS);
  257. }
  258. public function save($params, $show_query = false)
  259. {
  260. $id = parent::save($params, $show_query);
  261. return $id;
  262. }
  263. }