grade_model.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /**
  23. * @param array $where_conditions
  24. * @return array
  25. */
  26. public function get_all($where_conditions = array())
  27. {
  28. return Database::select('*', $this->table, array('where' => $where_conditions, 'order' => 'name ASC'));
  29. }
  30. /**
  31. * @return mixed
  32. */
  33. public function get_count()
  34. {
  35. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  36. return $row['count'];
  37. }
  38. /**
  39. * Displays the title + grid
  40. */
  41. public function display()
  42. {
  43. // action links
  44. echo '<div class="actions" style="margin-bottom:20px">';
  45. echo '<a href="grade_models.php">'.Display::return_icon('back.png', get_lang('Back'), '', '32').'</a>';
  46. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon(
  47. 'add.png',
  48. get_lang('Add'),
  49. '',
  50. '32'
  51. ).'</a>';
  52. echo '</div>';
  53. echo Display::grid_html('grade_model');
  54. }
  55. /**
  56. * Returns a Form validator Obj
  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. $form = new FormValidator('grades', 'post', $url);
  64. // Setting 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. '.Display::return_icon('add.png', get_lang('Add'), array('style' => 'display: '.(($counter >= $nr_items) ? 'inline' : 'none'), 'id' => 'plus-'.($counter + 1))).'
  135. </a>
  136. <a href="javascript:minItem('.($counter).')">
  137. '.Display::return_icon('delete.png', get_lang('Delete'), array('style' => 'display: '.(($counter >= $nr_items) ? 'inline' : 'none'), 'id' => 'min-'.$counter)).'
  138. </a>
  139. </div></p></div>';
  140. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][title]');
  141. $renderer->setElementTemplate($template_percentage, 'components['.$i.'][percentage]');
  142. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][acronym]');
  143. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][prefix]');
  144. $renderer->setElementTemplate($template_title, 'components['.$i.'][exclusions]');
  145. $renderer->setElementTemplate($template_acronym, 'components['.$i.'][count_elements]');
  146. if ($i == 0) {
  147. $form->addRule('components['.$i.'][percentage]', get_lang('ThisFieldIsRequired'), 'required');
  148. $form->addRule('components['.$i.'][title]', get_lang('ThisFieldIsRequired'), 'required');
  149. $form->addRule('components['.$i.'][acronym]', get_lang('ThisFieldIsRequired'), 'required');
  150. }
  151. $form->addRule('components['.$i.'][percentage]', get_lang('OnlyNumbers'), 'numeric');
  152. $form->addRule(array('components['.$i.'][percentage]', 'maxvalue'), get_lang('Over100'), 'compare', '<=');
  153. $form->addRule(array('components['.$i.'][percentage]', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  154. $component_array[] = 'components['.$i.'][percentage]';
  155. }
  156. //New rule added in the formvalidator compare_fields that filters a group of fields in order to compare with the wanted value
  157. $form->addRule($component_array, get_lang('AllMustWeight100'), 'compare_fields', '==@100');
  158. $form->addElement('label', null, get_lang('AllMustWeight100'));
  159. if ($action == 'edit') {
  160. $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"');
  161. } else {
  162. $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="save"');
  163. }
  164. if (!empty($components)) {
  165. $counter = 0;
  166. foreach ($components as $component) {
  167. foreach ($component as $key => $value) {
  168. $defaults['components['.$counter.']['.$key.']'] = $value;
  169. }
  170. $counter++;
  171. }
  172. }
  173. $form->setDefaults($defaults);
  174. // Setting the rules
  175. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  176. return $form;
  177. }
  178. /**
  179. * @param $id
  180. * @return array|null
  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. /**
  191. * @param $params
  192. * @param bool $show_query
  193. * @return bool
  194. */
  195. public function save($params, $show_query = false)
  196. {
  197. $id = parent::save($params, $show_query);
  198. if (!empty($id)) {
  199. foreach ($params['components'] as $component) {
  200. if (!empty($component['title']) && !empty($component['percentage']) && !empty($component['acronym'])) {
  201. $obj = new GradeModelComponents();
  202. $component['grade_model_id'] = $id;
  203. $obj->save($component);
  204. }
  205. }
  206. }
  207. //Event::addEvent(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  208. return $id;
  209. }
  210. /**
  211. * @param array $params
  212. * @return bool|void
  213. */
  214. public function update($params)
  215. {
  216. parent::update($params);
  217. if (!empty($params['id'])) {
  218. foreach ($params['components'] as $component) {
  219. $obj = new GradeModelComponents();
  220. $component['grade_model_id'] = $params['id'];
  221. if (empty($component['title']) && empty($component['percentage']) && empty($component['acronym'])) {
  222. $obj->delete($component['id']);
  223. } else {
  224. $obj->update($component);
  225. }
  226. }
  227. }
  228. //$params['components']
  229. }
  230. /**
  231. * @param int $id
  232. * @return bool|void
  233. */
  234. public function delete($id)
  235. {
  236. parent::delete($id);
  237. //Event::addEvent(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  238. }
  239. public function fill_grade_model_select_in_form(&$form, $name = 'gradebook_model_id', $default_value = null)
  240. {
  241. if (api_get_setting('gradebook.gradebook_enable_grade_model') ==
  242. 'false') {
  243. return false;
  244. }
  245. if (api_get_setting('gradebook.teachers_can_change_grade_model_settings') == 'true' || api_is_platform_admin()) {
  246. $grade_models = $this->get_all();
  247. $grade_model_options = array('-1' => get_lang('None'));
  248. if (!empty($grade_models)) {
  249. foreach ($grade_models as $item) {
  250. $grade_model_options[$item['id']] = $item['name'];
  251. }
  252. }
  253. $form->addElement('select', $name, get_lang('GradeModel'), $grade_model_options);
  254. $default_platform_setting = api_get_setting('gradebook.gradebook_default_grade_model_id');
  255. $default = -1;
  256. if ($default_platform_setting == -1) {
  257. if (!empty($default_value)) {
  258. $default = $default_value;
  259. }
  260. } else {
  261. $default = $default_platform_setting;
  262. }
  263. if (!empty($default) && $default != '-1') {
  264. $form->setDefaults(array($name => $default));
  265. }
  266. }
  267. }
  268. }
  269. //@todo move in another file
  270. class GradeModelComponents extends Model
  271. {
  272. public $table;
  273. public $columns = array(
  274. 'id',
  275. 'title',
  276. 'percentage',
  277. 'acronym',
  278. 'grade_model_id',
  279. 'prefix',
  280. 'exclusions',
  281. 'count_elements'
  282. );
  283. public function __construct()
  284. {
  285. $this->table = Database::get_main_table(TABLE_GRADE_MODEL_COMPONENTS);
  286. }
  287. public function save($params, $show_query = false)
  288. {
  289. $id = parent::save($params, $show_query);
  290. return $id;
  291. }
  292. }