grade_model.lib.php 10 KB

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