grade_model.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. {
  16. public $table;
  17. public $columns = array('id', 'name', 'description');
  18. public function __construct()
  19. {
  20. $this->table = Database::get_main_table(TABLE_GRADE_MODEL);
  21. }
  22. public function get_all($where_conditions = array())
  23. {
  24. return Database::select('*', $this->table, array('where' => $where_conditions, 'order' => 'name ASC'));
  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(
  40. 'add.png',
  41. get_lang('Add'),
  42. '',
  43. '32'
  44. ).'</a>';
  45. echo '</div>';
  46. echo Display::grid_html('grade_model');
  47. }
  48. /**
  49. * Returns a Form validator Obj
  50. * @todo the form should be auto generated
  51. * @param string url
  52. * @param string action add, edit
  53. * @return obj form validator obj
  54. */
  55. public function return_form($url, $action)
  56. {
  57. $oFCKeditor = new FCKeditor('description');
  58. $oFCKeditor->ToolbarSet = 'grade_model';
  59. $oFCKeditor->Width = '100%';
  60. $oFCKeditor->Height = '200';
  61. $oFCKeditor->Value = '';
  62. $oFCKeditor->CreateHtml();
  63. $form = new FormValidator('grades', 'post', $url);
  64. // Settting the form elements
  65. $header = get_lang('Add');
  66. if ($action == 'edit') {
  67. $header = get_lang('Modify');
  68. }
  69. $form->addElement('header', $header);
  70. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  71. $form->addElement('hidden', 'id', $id);
  72. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  73. $form->add_html_editor(
  74. 'description',
  75. get_lang('Description'),
  76. false,
  77. false,
  78. array('ToolbarSet' => 'careers', 'Width' => '100%', 'Height' => '250')
  79. );
  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(
  100. 'text',
  101. 'components['.$i.'][acronym]',
  102. null,
  103. array('class' => 'span1', 'placeholder' => get_lang('Acronym'))
  104. );
  105. $form->addElement(
  106. 'text',
  107. 'components['.$i.'][title]',
  108. null,
  109. array('class' => 'span2', 'placeholder' => get_lang('Title'))
  110. );
  111. $form->addElement(
  112. 'text',
  113. 'components['.$i.'][prefix]',
  114. null,
  115. array('class' => 'span1', 'placeholder' => get_lang('Prefix'))
  116. );
  117. $options = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5);
  118. $form->addElement('select', 'components['.$i.'][count_elements]', null, $options);
  119. $options = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5);
  120. $form->addElement('select', 'components['.$i.'][exclusions]', null, $options);
  121. $form->addElement('hidden', 'components['.$i.'][id]');
  122. $template_percentage =
  123. '<div id='.$i.' style="display: '.(($i <= $nr_items) ? 'inline' : 'none').';" class="control-group">
  124. <p>
  125. <label class="control-label">{label}</label>
  126. <div class="controls">
  127. {element} <!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --> % = ';
  128. $template_acronym = '
  129. <!-- BEGIN required -->
  130. {element} {label} <!-- BEGIN error --><span class="form_error">{error}</span> <!-- END error -->';
  131. $template_title =
  132. '&nbsp{element} <!-- BEGIN error --> <span class="form_error">{error}</span><!-- END error -->
  133. <a href="javascript:plusItem('.($counter + 1).')">
  134. <img style="display: '.(($counter >= $nr_items) ? 'inline' : 'none').';" id="plus-'.($counter + 1).'" src="../img/icons/22/add.png" alt="'.get_lang(
  135. 'Add'
  136. ).'" title="'.get_lang('Add').'"></img>
  137. </a>
  138. <a href="javascript:minItem('.($counter).')">
  139. <img style="display: '.(($counter >= $nr_items) ? 'inline' : 'none').';" id="min-'.$counter.'" src="../img/delete.png" alt="'.get_lang(
  140. 'Delete'
  141. ).'" title="'.get_lang('Delete').'"></img>
  142. </a>
  143. </div></p></div>';
  144. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][title]');
  145. $renderer->setElementTemplate($template_percentage, 'components['.$i.'][percentage]');
  146. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][acronym]');
  147. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][prefix]');
  148. $renderer->setElementTemplate($template_title, 'components['.$i.'][exclusions]');
  149. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][count_elements]');
  150. if ($i == 0) {
  151. $form->addRule('components['.$i.'][percentage]', get_lang('ThisFieldIsRequired'), 'required');
  152. $form->addRule('components['.$i.'][title]', get_lang('ThisFieldIsRequired'), 'required');
  153. $form->addRule('components['.$i.'][acronym]', get_lang('ThisFieldIsRequired'), 'required');
  154. }
  155. $form->addRule('components['.$i.'][percentage]', get_lang('OnlyNumbers'), 'numeric');
  156. $form->addRule(array('components['.$i.'][percentage]', 'maxvalue'), get_lang('Over100'), 'compare', '<=');
  157. $form->addRule(array('components['.$i.'][percentage]', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  158. $component_array[] = 'components['.$i.'][percentage]';
  159. }
  160. //New rule added in the formvalidator compare_fields that filters a group of fields in order to compare with the wanted value
  161. $form->addRule($component_array, get_lang('AllMustWeight100'), 'compare_fields', '==@100');
  162. $form->addElement('advanced_settings', get_lang('AllMustWeight100'));
  163. if ($action == 'edit') {
  164. $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"');
  165. } else {
  166. $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="save"');
  167. }
  168. if (!empty($components)) {
  169. $counter = 0;
  170. foreach ($components as $component) {
  171. foreach ($component as $key => $value) {
  172. $defaults['components['.$counter.']['.$key.']'] = $value;
  173. }
  174. $counter++;
  175. }
  176. }
  177. $form->setDefaults($defaults);
  178. // Setting the rules
  179. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  180. return $form;
  181. }
  182. public function get_components($id)
  183. {
  184. $obj = new GradeModelComponents();
  185. if (!empty($id)) {
  186. return $obj->get_all(array('where' => array('grade_model_id = ?' => $id)));
  187. }
  188. return null;
  189. }
  190. public function save($params, $show_query = false)
  191. {
  192. $id = parent::save($params, $show_query);
  193. if (!empty($id)) {
  194. foreach ($params['components'] as $component) {
  195. if (!empty($component['title']) && !empty($component['percentage']) && !empty($component['acronym'])) {
  196. $obj = new GradeModelComponents();
  197. $component['grade_model_id'] = $id;
  198. $obj->save($component);
  199. }
  200. }
  201. }
  202. //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  203. return $id;
  204. }
  205. public function update($params)
  206. {
  207. parent::update($params);
  208. if (!empty($params['id'])) {
  209. foreach ($params['components'] as $component) {
  210. $obj = new GradeModelComponents();
  211. $component['grade_model_id'] = $params['id'];
  212. if (empty($component['title']) && empty($component['percentage']) && empty($component['acronym'])) {
  213. $obj->delete($component['id']);
  214. } else {
  215. $obj->update($component);
  216. }
  217. }
  218. }
  219. //$params['components']
  220. }
  221. public function delete($id)
  222. {
  223. parent::delete($id);
  224. //event_system(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  225. }
  226. public function fill_grade_model_select_in_form(&$form, $name = 'gradebook_model_id', $default_value = null)
  227. {
  228. if (api_get_setting('gradebook_enable_grade_model') == 'false') {
  229. return false;
  230. }
  231. if (api_get_setting('teachers_can_change_grade_model_settings') == 'true' || api_is_platform_admin()) {
  232. $grade_models = $this->get_all();
  233. $grade_model_options = array('-1' => get_lang('None'));
  234. if (!empty($grade_models)) {
  235. foreach ($grade_models as $item) {
  236. $grade_model_options[$item['id']] = $item['name'];
  237. }
  238. }
  239. $form->addElement('select', $name, get_lang('GradeModel'), $grade_model_options);
  240. $default_platform_setting = api_get_setting('gradebook_default_grade_model_id');
  241. $default = -1;
  242. if ($default_platform_setting == -1) {
  243. if (!empty($default_value)) {
  244. $default = $default_value;
  245. }
  246. } else {
  247. $default = $default_platform_setting;
  248. }
  249. if (!empty($default) && $default != '-1') {
  250. $form->setDefaults(array($name => $default));
  251. }
  252. }
  253. }
  254. }
  255. class GradeModelComponents extends Model
  256. {
  257. var $table;
  258. var $columns = array(
  259. 'id',
  260. 'title',
  261. 'percentage',
  262. 'acronym',
  263. 'grade_model_id',
  264. 'prefix',
  265. 'exclusions',
  266. 'count_elements'
  267. );
  268. public function __construct()
  269. {
  270. $this->table = Database::get_main_table(TABLE_GRADE_MODEL_COMPONENTS);
  271. }
  272. public function save($params, $show_query = false)
  273. {
  274. $id = parent::save($params, $show_query);
  275. return $id;
  276. }
  277. }