grade_model.lib.php 10 KB

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