grade_model.lib.php 10 KB

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