grade_model.lib.php 11 KB

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