123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class Gradebook
- * This class provides methods for the notebook management.
- * Include/require it in your code to use its features.
- * @package chamilo.library
- */
- class Gradebook extends Model
- {
- public $columns = array(
- 'id',
- 'name',
- 'description',
- 'course_code',
- 'parent_id',
- 'grade_model_id',
- 'session_id',
- 'weight',
- 'user_id'
- );
- public function __construct()
- {
- $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
- $this->table_skill = Database::get_main_table(TABLE_MAIN_SKILL);
- $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
- }
- /**
- * Returns true if the gradebook is active and visible in a course, false
- * otherwise.
- *
- * @param int $c_id Course integer id, defaults to the current course
- * @return boolean
- */
- public static function is_active($c_id = null)
- {
- $name = 'gradebook';
- $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
- $sql = "SELECT * from $table WHERE variable='course_hide_tools' AND subkey='$name' LIMIT 1";
- $result = Database::query($sql);
- $setting = Database::store_result($result);
- $setting = isset($setting[0]) ? $setting[0] : null;
- $setting = $setting ? $setting : array();
- $inactive = isset($setting['selected_value']) && $setting['selected_value'] == 'true';
- if ($inactive) {
- return false;
- }
- $c_id = $c_id ? intval($c_id) : api_get_course_int_id();
- $table = Database::get_course_table(TABLE_TOOL_LIST);
- $sql = "SELECT * from $table WHERE c_id = $c_id and name='$name' LIMIT 1";
- $result = Database::query($sql);
- $item = Database::store_result($result, 'ASSOC');
- $item = isset($item[0]) ? $item[0] : null;
- if (empty($item)) {
- return true;
- }
- return $item['visibility'] == '1';
- }
- /**
- * @param array $options
- * @return array
- */
- public function get_all($options = array())
- {
- $gradebooks = parent::get_all($options);
- foreach ($gradebooks as &$gradebook) {
- if (empty($gradebook['name'])) {
- $gradebook['name'] = $gradebook['course_code'];
- }
- //$gradebook['name'] = $gradebook['course_code'] .' > '.$gradebook['name'];
- }
- return $gradebooks;
- }
- /**
- * @param array $params
- * @return bool
- */
- public function update($params)
- {
- return parent::update($params);
- }
- /**
- * @param int $gradebook_id
- * @param array $skill_list
- * @return bool
- */
- public function update_skills_to_gradebook(
- $gradebook_id,
- $skill_list,
- $deleteSkillNotInList = true
- )
- {
- if (!empty($skill_list)) {
- //Cleaning skills
- $skill_list = array_map('intval', $skill_list);
- $skill_list = array_filter($skill_list);
- $skill_gradebook = new SkillRelGradebook();
- $skill_gradebooks_source = $skill_gradebook->get_all(array('where'=>array('gradebook_id = ?' =>$gradebook_id)));
- $clean_gradebook = array();
- if (!empty($skill_gradebooks_source)) {
- foreach($skill_gradebooks_source as $source) {
- $clean_gradebook[]= $source['skill_id'];
- }
- }
- if (!empty($clean_gradebook)) {
- $skill_to_remove = array_diff($clean_gradebook, $skill_list);
- }
- foreach ($skill_list as $skill_id) {
- $params = array();
- $params['gradebook_id'] = $gradebook_id;
- $params['skill_id'] = $skill_id;
- if (!$skill_gradebook->exists_gradebook_skill($gradebook_id, $skill_id)) {
- $skill_gradebook->save($params);
- }
- }
- if ($deleteSkillNotInList) {
- if (!empty($skill_to_remove)) {
- foreach ($skill_to_remove as $remove) {
- $skill_item = $skill_gradebook->get_skill_info(
- $remove,
- $gradebook_id
- );
- $skill_gradebook->delete($skill_item['id']);
- }
- }
- }
- return true;
- }
- return false;
- }
- /**
- * Returns a Form validator Obj
- * @todo the form should be auto generated
- * @param string url
- * @param string action add, edit
- * @return obj form validator obj
- */
- public function show_skill_form($gradebook_id, $url, $header = null)
- {
- $form = new FormValidator('gradebook_add_skill', 'POST', $url);
- // Setting the form elements
- if (!isset($header)) {
- $header = get_lang('Add');
- }
- $form->addElement('header', '', $header);
- $id = isset($_GET['id']) ? intval($_GET['id']) : '';
- $form->addElement('hidden', 'id', $id);
- $skill = new Skill();
- $skills = $skill->get_all();
- $clean_skill_list = array();
- foreach ($skills as $skill) {
- $clean_skill_list[$skill['id']] = $skill['name'];
- }
- $form->addElement('select', 'skill', get_lang('Skills'), $clean_skill_list, array('width'=>'450px', 'class'=>'chzn-select','multiple' => 'multiple'));
- $selected_skills = self::get_skills_by_gradebook($gradebook_id);
- $clean_selected_skills = array();
- if (!empty($selected_skills)) {
- foreach($selected_skills as $skill) {
- $clean_selected_skills[] = $skill['id'];
- }
- }
- $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="save"');
- $form->setDefaults(array('skill'=>$clean_selected_skills));
- return $form;
- }
- /**
- * @param int $gradebook_id
- * @return array|resource
- */
- function get_skills_by_gradebook($gradebook_id)
- {
- $gradebook_id = intval($gradebook_id);
- $sql = "SELECT skill.id, skill.name FROM {$this->table_skill} skill INNER JOIN {$this->table_skill_rel_gradebook} skill_rel_gradebook
- ON skill.id = skill_rel_gradebook.skill_id
- WHERE skill_rel_gradebook.gradebook_id = $gradebook_id";
- $result = Database::query($sql);
- $result = Database::store_result($result,'ASSOC');
- return $result;
- }
- /**
- * Displays the title + grid
- */
- public function display()
- {
- // action links
- echo Display::grid_html('gradebooks');
- }
- }
|