diff --git a/main/announcements/announcements.inc.php b/main/announcements/announcements.inc.php index bd5928cdd2..44d9aed4ec 100755 --- a/main/announcements/announcements.inc.php +++ b/main/announcements/announcements.inc.php @@ -136,1003 +136,966 @@ class AnnouncementManager { echo "$content"; echo ""; } -} - -/** - * Store an announcement in the database (including its attached file if any) - * @param string Announcement title (pure text) - * @param string Content of the announcement (can be HTML) - * @param int Display order in the list of announcements - * @param array Array of users and groups to send the announcement to - * @param array uploaded file $_FILES - * @param string Comment describing the attachment - * @return int false on failure, ID of the announcement on success - */ -function store_advalvas_item($emailTitle, $newContent, $order, $to, $file = array(), $file_comment='') { - global $_course; - - $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); - $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); - - // filter data - $emailTitle = Database::escape_string($emailTitle); - $newContent = Database::escape_string($newContent); - $order = intval($order); - - // store in the table announcement - $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".api_get_session_id(); - $result = Database::query($sql); - if ($result === false) { - return false; - } else { - //Store the attach file + + + /** + * Store an announcement in the database (including its attached file if any) + * @param string Announcement title (pure text) + * @param string Content of the announcement (can be HTML) + * @param int Display order in the list of announcements + * @param array Array of users and groups to send the announcement to + * @param array uploaded file $_FILES + * @param string Comment describing the attachment + * @return int false on failure, ID of the announcement on success + */ + public static function add_announcement($emailTitle, $newContent, $order, $to, $file = array(), $file_comment='') { + global $_course; + + $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); + $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); + + // filter data + $emailTitle = Database::escape_string($emailTitle); + $newContent = Database::escape_string($newContent); + $order = intval($order); + + // store in the table announcement + $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".api_get_session_id(); + $result = Database::query($sql); + if ($result === false) { + return false; + } else { + //Store the attach file + $last_id = Database::insert_id(); + if (!empty($file)) { + $save_attachment = self::add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']); + } + + // store in item_property (first the groups, then the users + + if (!is_null($to)) { + // !is_null($to): when no user is selected we send it to everyone + $send_to = separate_users_groups($to); + // storing the selected groups + if (is_array($send_to['groups'])) { + foreach ($send_to['groups'] as $group) { + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), $group); + } + } + + // storing the selected users + if (is_array($send_to['users'])) { + foreach ($send_to['users'] as $user) { + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '', $user); + } + } + } else { + // the message is sent to everyone, so we set the group to 0 + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '0'); + } + return $last_id; + } + } + + /* + STORE ANNOUNCEMENT GROUP ITEM + */ + public static function add_group_announcement($emailTitle,$newContent, $order, $to, $to_users, $file = array(), $file_comment='') { + global $_course; + + // database definitions + $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); + $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); + + $emailTitle = Database::escape_string($emailTitle); + $newContent = Database::escape_string($newContent); + $order = intval($order); + + // store in the table announcement + $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".api_get_session_id(); + $result = Database::query($sql); + if ($result === false) { + return false; + } + + //store the attach file $last_id = Database::insert_id(); - if (!empty($file)) { - $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']); + if (empty($file)) { + $save_attachment = self::add_announcement_attachment_file($last_id, $file_comment, $file); } // store in item_property (first the groups, then the users - - if (!is_null($to)) { - // !is_null($to): when no user is selected we send it to everyone - $send_to = separate_users_groups($to); + if (!isset($to_users)) // !isset($to): when no user is selected we send it to everyone + { + $send_to=self::separate_users_groups($to); // storing the selected groups if (is_array($send_to['groups'])) { foreach ($send_to['groups'] as $group) { api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), $group); } } - + } + else // the message is sent to everyone, so we set the group to 0 + { // storing the selected users - if (is_array($send_to['users'])) { - foreach ($send_to['users'] as $user) { + if (is_array($to_users)) { + foreach ($to_users as $user) { api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '', $user); } } - } else { - // the message is sent to everyone, so we set the group to 0 - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '0'); } return $last_id; } -} - -/* - STORE ANNOUNCEMENT GROUP ITEM -*/ -function store_advalvas_group_item($emailTitle,$newContent, $order, $to, $to_users, $file = array(), $file_comment='') { - global $_course; - - // database definitions - $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); - $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); - $emailTitle = Database::escape_string($emailTitle); - $newContent = Database::escape_string($newContent); - $order = intval($order); - - // store in the table announcement - $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".api_get_session_id(); - $result = Database::query($sql); - if ($result === false) { - return false; - } - - //store the attach file - $last_id = Database::insert_id(); - if (empty($file)) { - $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $file); - } - - // store in item_property (first the groups, then the users - if (!isset($to_users)) // !isset($to): when no user is selected we send it to everyone - { - $send_to=separate_users_groups($to); - // storing the selected groups - if (is_array($send_to['groups'])) - { - foreach ($send_to['groups'] as $group) - { - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), $group); + + /* + EDIT ANNOUNCEMENT + */ + /** + * This function stores the announcement item in the announcement table + * and updates the item_property table + * + * @param int id of the announcement + * @param string email + * @param string content + * @param array users that will receive the announcement + * @param mixed attachment + * @param string file comment + * + */ + public static function edit_announcement($id, $emailTitle, $newContent, $to, $file = array(), $file_comment='') { + global $_course; + $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); + $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); + + $emailTitle = Database::escape_string($emailTitle); + $newContent = Database::escape_string($newContent); + $id = intval($id); + + // store the modifications in the table announcement + $sql = "UPDATE $tbl_announcement SET content='$newContent', title = '$emailTitle' WHERE id='$id'"; + $result = Database::query($sql); + + // save attachment file + $row_attach = self::get_attachment($id); + $id_attach = intval($row_attach['id']); + + if (!empty($file)) { + if (empty($id_attach)) { + self::add_announcement_attachment_file($id,$file_comment,$file); + } else { + self::edit_announcement_attachment_file($id_attach,$file,$file_comment); } } - } - else // the message is sent to everyone, so we set the group to 0 - { - // storing the selected users - if (is_array($to_users)) - { - foreach ($to_users as $user) - { - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '', $user); + + // we remove everything from item_property for this + $sql_delete="DELETE FROM $tbl_item_property WHERE ref='$id' AND tool='announcement'"; + $result = Database::query($sql_delete); + + // store in item_property (first the groups, then the users + + if (!is_null($to)) { + // !is_null($to): when no user is selected we send it to everyone + + $send_to = self::separate_users_groups($to); + + // storing the selected groups + if (is_array($send_to['groups'])) { + foreach ($send_to['groups'] as $group) { + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), $group); + } + } + // storing the selected users + if (is_array($send_to['users'])) { + foreach ($send_to['users'] as $user) { + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), 0, $user); + } } + } else { + // the message is sent to everyone, so we set the group to 0 + api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), '0'); } } - return $last_id; -} - - -/* - EDIT ANNOUNCEMENT -*/ -/** -* This function stores the announcement item in the announcement table -* and updates the item_property table -* -* @param int id of the announcement -* @param string email -* @param string content -* @param array users that will receive the announcement -* @param mixed attachment -* @param string file comment -* -*/ -function edit_advalvas_item($id, $emailTitle, $newContent, $to, $file = array(), $file_comment='') { - global $_course; - $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); - $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); - - $emailTitle = Database::escape_string($emailTitle); - $newContent = Database::escape_string($newContent); - $id = intval($id); + /* + MAIL FUNCTIONS + */ - // store the modifications in the table announcement - $sql = "UPDATE $tbl_announcement SET content='$newContent', title = '$emailTitle' WHERE id='$id'"; - $result = Database::query($sql); - - // save attachment file - $row_attach = get_attachment($id); - $id_attach = intval($row_attach['id']); - - if (!empty($file)) { - if (empty($id_attach)) { - add_announcement_attachment_file($id,$file_comment,$file); - } else { - edit_announcement_attachment_file($id_attach,$file,$file_comment); + /** + * Sends an announcement by email to a list of users. + * Emails are sent one by one to try to avoid antispam. + * @todo seems not used in Chamilo 1.8.7 RC2 + */ + public static function send_announcement_email($user_list, $course_code, $_course, $mail_title, $mail_content) { + global $_user; + foreach ($user_list as $this_user) { + + $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title; + $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n"; + $mail_body .= api_get_person_name($this_user['firstname'], $this_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).' <'.$this_user["email"]."> \n\n".stripslashes($mail_title)."\n\n".trim(stripslashes(api_html_entity_decode(strip_tags(str_replace(array('

','

','
'),array('',"\n","\n"),$mail_content)), ENT_QUOTES, api_get_system_encoding())))." \n\n-- \n"; + $mail_body .= api_get_person_name($_user['firstname'], $_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).' '; + $mail_body .= '<'.$_user['mail'].">\n"; + $mail_body .= $_course['official_code'].' '.$_course['name']; + + @api_mail(api_get_person_name($this_user['firstname'], $this_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $this_user['email'], $mail_subject, $mail_body, api_get_person_name($_SESSION['_user']['firstname'], $_SESSION['_user']['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $_SESSION['_user']['mail']); } } - - // we remove everything from item_property for this - $sql_delete="DELETE FROM $tbl_item_property WHERE ref='$id' AND tool='announcement'"; - $result = Database::query($sql_delete); - - // store in item_property (first the groups, then the users - - if (!is_null($to)) { - // !is_null($to): when no user is selected we send it to everyone - - $send_to = separate_users_groups($to); - - // storing the selected groups - if (is_array($send_to['groups'])) { - foreach ($send_to['groups'] as $group) { - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), $group); - } + + public static function update_mail_sent($insert_id) { + $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); + if ($insert_id != strval(intval($insert_id))) { return false; } + $insert_id = Database::escape_string($insert_id); + // store the modifications in the table tbl_annoucement + $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'"; + Database::query($sql); + } + + /** + * Gets all announcements from a user by course + * @param string course db + * @param int user id + * @return array html with the content and count of announcements or false otherwise + */ + public static function get_all_annoucement_by_user_course($course_db, $user_id) { + if (empty($course_db) || empty($user_id)) { + return false; } - // storing the selected users - if (is_array($send_to['users'])) { - foreach ($send_to['users'] as $user) { - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), 0, $user); + $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db); + $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db); + if (!empty($user_id) && is_numeric($user_id)) { + $user_id = intval($user_id); + $sql="SELECT DISTINCT announcement.title, announcement.content + FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties + WHERE announcement.id = toolitemproperties.ref + AND toolitemproperties.tool='announcement' + AND (toolitemproperties.insert_user_id='$user_id' AND (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null)) + AND toolitemproperties.visibility='1' + AND announcement.session_id = 0 + ORDER BY display_order DESC"; + $rs = Database::query($sql); + $num_rows = Database::num_rows($rs); + $content = ''; + $i=0; + $result = array(); + if ($num_rows>0) { + while ($myrow = Database::fetch_array($rs)) { + $content.= ''.$myrow['title'].'

'; + $content.= $myrow['content']; + $i++; + } + $result['content'] = $content; + $result['count'] = $i; + return $result; } + return false; } - } else { - // the message is sent to everyone, so we set the group to 0 - api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), '0'); - } -} - - -/* - MAIL FUNCTIONS -*/ - -/** -* Sends an announcement by email to a list of users. -* Emails are sent one by one to try to avoid antispam. -*/ -function send_announcement_email($user_list, $course_code, $_course, $mail_title, $mail_content) { - global $_user; - foreach ($user_list as $this_user) { - - $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title; - $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n"; - $mail_body .= api_get_person_name($this_user['firstname'], $this_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).' <'.$this_user["email"]."> \n\n".stripslashes($mail_title)."\n\n".trim(stripslashes(api_html_entity_decode(strip_tags(str_replace(array('

','

','
'),array('',"\n","\n"),$mail_content)), ENT_QUOTES, api_get_system_encoding())))." \n\n-- \n"; - $mail_body .= api_get_person_name($_user['firstname'], $_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).' '; - $mail_body .= '<'.$_user['mail'].">\n"; - $mail_body .= $_course['official_code'].' '.$_course['name']; - - @api_mail(api_get_person_name($this_user['firstname'], $this_user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $this_user['email'], $mail_subject, $mail_body, api_get_person_name($_SESSION['_user']['firstname'], $_SESSION['_user']['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $_SESSION['_user']['mail']); - } -} - -function update_mail_sent($insert_id) { - $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); - if ($insert_id != strval(intval($insert_id))) { return false; } - $insert_id = Database::escape_string($insert_id); - // store the modifications in the table tbl_annoucement - $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'"; - Database::query($sql); -} - -/** - * Gets all announcements from a user by course - * @param string course db - * @param int user id - * @return array html with the content and count of announcements or false otherwise - */ -function get_all_annoucement_by_user_course($course_db, $user_id) { - if (empty($course_db) || empty($user_id)) { return false; } - $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db); - $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db); - if (!empty($user_id) && is_numeric($user_id)) { - $user_id = intval($user_id); - $sql="SELECT DISTINCT announcement.title, announcement.content - FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties - WHERE announcement.id = toolitemproperties.ref - AND toolitemproperties.tool='announcement' - AND (toolitemproperties.insert_user_id='$user_id' AND (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null)) - AND toolitemproperties.visibility='1' - AND announcement.session_id = 0 - ORDER BY display_order DESC"; - $rs = Database::query($sql); - $num_rows = Database::num_rows($rs); - $content = ''; - $i=0; - $result = array(); - if ($num_rows>0) { - while ($myrow = Database::fetch_array($rs)) { - $content.= ''.$myrow['title'].'

'; - $content.= $myrow['content']; - $i++; - } - $result['content'] = $content; - $result['count'] = $i; - return $result; + + /* + SHOW_TO_FORM + */ + /** + * this function shows the form for sending a message to a specific group or user. + */ + public static function show_to_form($to_already_selected) { + + $user_list = self::get_course_users(); + $group_list = self::get_course_groups(); + + if ($to_already_selected == '' || $to_already_selected == 'everyone') { + $to_already_selected = array(); } - return false; + + echo ""; + echo ''; + + // the form containing all the groups and all the users of the course + echo '"; + + // the buttons for adding or removing groups/users + echo '"; + + echo ""; + echo ""; + echo "
'; + echo "".get_lang('Users')."
"; + self::construct_not_selected_select_form($group_list,$user_list,$to_already_selected); + echo "
'; + echo ''; + echo '

'; + echo ''; + echo "
"; + + // the form containing the selected groups and users + echo "".get_lang('DestinationUsers')."
"; + self::construct_selected_select_form($group_list,$user_list,$to_already_selected); + echo "
"; } - return false; -} - - -/* - SHOW_TO_FORM -*/ -/** -* this function shows the form for sending a message to a specific group or user. -*/ -function show_to_form($to_already_selected) { - $user_list = get_course_users(); - $group_list = get_course_groups(); - - if ($to_already_selected == '' || $to_already_selected == 'everyone') { - $to_already_selected = array(); + + + /** + * this function shows the form for sending a message to a specific group or user. + */ + public static function show_to_form_group($group_id) { + echo ""; + echo ""; + echo ""; + + // the buttons for adding or removing groups/users + echo ""; + echo ""; + echo ""; + echo "
"; + echo "'; + + echo ""; + echo ''; + echo '

'; + echo ''; + echo "
"; + + echo "'; + + echo "
"; } - - echo ""; - echo ''; - - // the form containing all the groups and all the users of the course - echo '"; - - // the buttons for adding or removing groups/users - echo ""; - echo ""; - echo "
'; - echo "".get_lang('Users')."
"; - construct_not_selected_select_form($group_list,$user_list,$to_already_selected); - echo "
"; - ?> - -

- - "; - echo "
"; - - // the form containing the selected groups and users - echo "".get_lang('DestinationUsers')."
"; - construct_selected_select_form($group_list,$user_list,$to_already_selected); - echo "
"; -} - - -/* - CONSTRUCT_NOT_SELECT_SELECT_FORM -*/ -/** -* this function shows the form for sending a message to a specific group or user. -*/ -function construct_not_selected_select_form($group_list=null, $user_list=null,$to_already_selected) { - - echo ""; + // adding the groups to the select form + if ($group_list){ + foreach($group_list as $this_group) { + if (is_array($to_already_selected)) { + if (!in_array("GROUP:".$this_group['id'],$to_already_selected)) // $to_already_selected is the array containing the groups (and users) that are already selected + { + echo ""; + } } } + // a divider + echo ""; } - // a divider - echo ""; - } - // adding the individual users to the select form - if ($user_list) { - foreach($user_list as $this_user) - { - if (is_array($to_already_selected)) { - if (!in_array("USER:".$this_user['user_id'],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected - { - echo ""; + // adding the individual users to the select form + if ($user_list) { + foreach($user_list as $this_user) { + if (is_array($to_already_selected)) { + if (!in_array("USER:".$this_user['user_id'],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected + { + echo ""; + } } } } + echo ""; } - echo ""; -} - - - -/* - CONSTRUCT_SELECTED_SELECT_FORM -*/ -/** -* this function shows the form for sending a message to a specific group or user. -*/ -function construct_selected_select_form($group_list=null, $user_list=null,$to_already_selected) { - // we separate the $to_already_selected array (containing groups AND users into - // two separate arrays - $groupuser = array(); - if (is_array($to_already_selected)) { - $groupuser=separate_users_groups($to_already_selected); - } - $groups_to_already_selected=$groupuser['groups']; - $users_to_already_selected=$groupuser['users']; - - // we load all the groups and all the users into a reference array that we use to search the name of the group / user - $ref_array_groups=get_course_groups(); - $ref_array_users=get_course_users(); - - // we construct the form of the already selected groups / users - echo ""; + if (is_array($to_already_selected)) { + foreach($to_already_selected as $groupuser) { + list($type,$id)=explode(":",$groupuser); + if ($type=="GROUP") { + echo ""; + } else { + foreach($ref_array_users as $key=>$value){ + if($value['user_id']==$id){ + echo ""; + break; + } } } } - } - } else { - if ($to_already_selected=='everyone') { - // adding the groups to the select form - if (is_array($ref_array_groups)) - { - foreach($ref_array_groups as $this_group) - { - //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]); - if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'],$to_already_selected)) // $to_already_selected is the array containing the groups (and users) that are already selected + } else { + if ($to_already_selected=='everyone') { + // adding the groups to the select form + if (is_array($ref_array_groups)) { + foreach($ref_array_groups as $this_group) { + //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]); + if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'],$to_already_selected)) // $to_already_selected is the array containing the groups (and users) that are already selected + { + echo ""; + } + } + } + // adding the individual users to the select form + foreach($ref_array_users as $this_user) { + if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected { - echo ""; } } } - // adding the individual users to the select form - foreach($ref_array_users as $this_user) - { - if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected - { - echo ""; - } - } } + echo ""; } - echo ""; -} - - -/** -* this function shows the form for sending a message to a specific group or user. -*/ -function show_to_form_group($group_id) { - echo ""; - echo ""; - echo ""; - - // the buttons for adding or removing groups/users - echo ""; - echo ""; - echo "
"; - echo "'; - - echo ""; - /*echo "> \">", - - "

 

", - - "";*/ - -?> - -

- -"; - echo "
"; - - echo "'; - - echo "
"; -} - - -/* - DATA FUNCTIONS -*/ - -/** -* this function gets all the users of the course, -* including users from linked courses -*/ -function get_course_users() { - //this would return only the users from real courses: - //$user_list = CourseManager::get_user_list_from_course_code(api_get_course_id()); - $session_id = api_get_session_id(); - - if ($session_id != 0) { - $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $session_id); - } else { - $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), false, 0); - } - - return $user_list; -} - -/** -* this function gets all the groups of the course, -* not including linked courses -*/ -function get_course_groups() { - $session_id = api_get_session_id(); - if ($session_id != 0) { - $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), intval($session_id)); - } else { - $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0); - } - return $new_group_list; -} - -/* - * - LOAD_EDIT_USERS -*/ -/** -* This tools loads all the users and all the groups who have received -* a specific item (in this case an announcement item) -*/ -function load_edit_users($tool, $id) { - $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); - - $tool = Database::escape_string($tool); - $id = Database::escape_string($id); - - $sql = "SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='$id'"; - $result = Database::query($sql); - while ($row = Database::fetch_array($result)) { - $to_group=$row['to_group_id']; - switch ($to_group) { - // it was send to one specific user - case null: - $to[]="USER:".$row['to_user_id']; - break; - // it was sent to everyone - case 0: - return "everyone"; - exit; - break; - default: - $to[]="GROUP:".$row['to_group_id']; + + /** + * this function gets all the groups of the course, + * not including linked courses + */ + public static function get_course_groups() { + $session_id = api_get_session_id(); + if ($session_id != 0) { + $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), intval($session_id)); + } else { + $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0); } + return $new_group_list; } - return $to; -} - - - -/* - USER_GROUP_FILTER_JAVASCRIPT -*/ -/** -* returns the javascript for setting a filter -* this goes into the $htmlHeadXtra[] array -*/ -function user_group_filter_javascript() { - return " - "; -} - - -/* - TO_JAVASCRIPT -*/ -/** -* returns all the javascript that is required for easily -* setting the target people/groups -* this goes into the $htmlHeadXtra[] array -*/ -function to_javascript() { - return " + "; + } + + + /* + TO_JAVASCRIPT + */ + /** + * returns all the javascript that is required for easily + * setting the target people/groups + * this goes into the $htmlHeadXtra[] array + */ + public static function to_javascript() { + return ""; -} - - -/* - SENT_TO_FORM -*/ -/** -* constructs the form to display all the groups and users the message has been sent to -* input: $sent_to_array is a 2 dimensional array containing the groups and the users -* the first level is a distinction between groups and users: -* $sent_to_array['groups'] * and $sent_to_array['users'] -* $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array -* containing all the id's of the groups (resp. users) who have received this message. -* @author Patrick Cool -*/ -function sent_to_form($sent_to_array) { - // we find all the names of the groups - $group_names=get_course_groups(); - count($sent_to_array); - - // we count the number of users and the number of groups - if (isset($sent_to_array['users'])) { - $number_users=count($sent_to_array['users']); - } else { - $number_users=0; - } - if (isset($sent_to_array['groups'])) { - $number_groups=count($sent_to_array['groups']); - } else { - $number_groups=0; - } - $total_numbers=$number_users+$number_groups; - - // starting the form if there is more than one user/group - - if ($total_numbers >1) { - $output=""; - } else { - // there is only one user/group - if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) { - $user_info = api_get_user_info($sent_to_array['users'][0]); - echo api_get_person_name($user_info['firstname'], $user_info['lastname']); + $number_users=count($sent_to_array['users']); + } else { + $number_users=0; } - if (isset($sent_to_array['groups']) and is_array($sent_to_array['groups']) and isset($sent_to_array['groups'][0]) and $sent_to_array['groups'][0]!==0) { - $group_id=$sent_to_array['groups'][0]; - echo " ".$group_names[$group_id]['name']; + if (isset($sent_to_array['groups'])) { + $number_groups=count($sent_to_array['groups']); + } else { + $number_groups=0; } - if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) { - echo " ".get_lang('Everybody'); + $total_numbers=$number_users+$number_groups; + + // starting the form if there is more than one user/group + + if ($total_numbers >1) { + $output=""; + } else { + // there is only one user/group + if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) { + $user_info = api_get_user_info($sent_to_array['users'][0]); + echo api_get_person_name($user_info['firstname'], $user_info['lastname']); + } + if (isset($sent_to_array['groups']) and is_array($sent_to_array['groups']) and isset($sent_to_array['groups'][0]) and $sent_to_array['groups'][0]!==0) { + $group_id=$sent_to_array['groups'][0]; + echo " ".$group_names[$group_id]['name']; + } + if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) { + echo " ".get_lang('Everybody'); + } + } + if(!empty($output)) + { + echo $output; } - } - if(!empty($output)) - { - echo $output; } -} - - -/* - SEPARATE_USERS_GROUPS -*/ -/** -* This function separates the users from the groups -* users have a value USER:XXX (with XXX the dokeos id -* groups have a value GROUP:YYY (with YYY the group id) -* @param array Array of strings that define the type and id of each destination -* @return array Array of groups and users (each an array of IDs) -*/ -function separate_users_groups($to) { - foreach($to as $to_item) { - list($type, $id) = explode(':', $to_item); - switch($type) { - case 'GROUP': - $grouplist[] = intval($id); - break; - case 'USER': - $userlist[] = intval($id); - break; + + + /* + SEPARATE_USERS_GROUPS + */ + /** + * This function separates the users from the groups + * users have a value USER:XXX (with XXX the dokeos id + * groups have a value GROUP:YYY (with YYY the group id) + * @param array Array of strings that define the type and id of each destination + * @return array Array of groups and users (each an array of IDs) + */ + public static function separate_users_groups($to) { + foreach($to as $to_item) { + list($type, $id) = explode(':', $to_item); + switch($type) { + case 'GROUP': + $grouplist[] = intval($id); + break; + case 'USER': + $userlist[] = intval($id); + break; + } } + + $send_to['groups']=$grouplist; + $send_to['users']=$userlist; + return $send_to; } - - $send_to['groups']=$grouplist; - $send_to['users']=$userlist; - return $send_to; -} - - - -/* - SENT_TO() -*/ -/** -* Returns all the users and all the groups a specific announcement item -* has been sent to -* @param string The tool (announcement, agenda, ...) -* @param int ID of the element of the corresponding type -* @return array Array of users and groups to whom the element has been sent -*/ -function sent_to($tool, $id) { - global $_course; - global $tbl_item_property; - - $tool = Database::escape_string($tool); - $id = Database::escape_string($id); - - $sent_to_group = array(); - $sent_to = array(); - - $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='".$id."'"; - $result = Database::query($sql); - - - while ($row=Database::fetch_array($result)) { - // if to_group_id is null then it is sent to a specific user - // if to_group_id = 0 then it is sent to everybody - if ($row['to_group_id'] != 0) - { - $sent_to_group[]=$row['to_group_id']; + + + + /* + SENT_TO() + */ + /** + * Returns all the users and all the groups a specific announcement item + * has been sent to + * @param string The tool (announcement, agenda, ...) + * @param int ID of the element of the corresponding type + * @return array Array of users and groups to whom the element has been sent + */ + public static function sent_to($tool, $id) { + global $_course; + global $tbl_item_property; + + $tool = Database::escape_string($tool); + $id = Database::escape_string($id); + + $sent_to_group = array(); + $sent_to = array(); + + $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='".$id."'"; + $result = Database::query($sql); + + + while ($row=Database::fetch_array($result)) { + // if to_group_id is null then it is sent to a specific user + // if to_group_id = 0 then it is sent to everybody + if ($row['to_group_id'] != 0) + { + $sent_to_group[]=$row['to_group_id']; + } + // if to_user_id <> 0 then it is sent to a specific user + if ($row['to_user_id'] <> 0) { + $sent_to_user[]=$row['to_user_id']; + } } - // if to_user_id <> 0 then it is sent to a specific user - if ($row['to_user_id'] <> 0) - { - $sent_to_user[]=$row['to_user_id']; + if (isset($sent_to_group)) { + $sent_to['groups']=$sent_to_group; } + if (isset($sent_to_user)) { + $sent_to['users']=$sent_to_user; + } + return $sent_to; } - if (isset($sent_to_group)) - { - $sent_to['groups']=$sent_to_group; - } - if (isset($sent_to_user)) - { - $sent_to['users']=$sent_to_user; - } - return $sent_to; -} - - -/* - CHANGE_VISIBILITY($tool,$id) -*/ - -/* ATTACHMENT FUNCTIONS */ - -/** - * Show a list with all the attachments according to the post's id - * @param int announcement id - * @return array with the post info - * @author Arthur Portugal - * @version November 2009, dokeos 1.8.6.2 - */ - -function get_attachment($announcement_id) { - $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); - $announcement_id=Database::escape_string($announcement_id); - $row=array(); - $sql = 'SELECT id,path, filename,comment FROM '. $tbl_announcement_attachment.' WHERE announcement_id = '.(int)$announcement_id.''; - $result=Database::query($sql); - if (Database::num_rows($result)!=0) { - $row = Database::fetch_array($result,'ASSOC'); - } - return $row; -} - -/** - * This function add a attachment file into announcement - * @param int announcement id - * @param string file comment - * @param array uploaded file $_FILES - * @return int -1 if failed, 0 if unknown (should not happen), 1 if success - */ - -function add_announcement_attachment_file($announcement_id, $file_comment, $file) { - global $_course; - $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); - $return = 0; - $announcement_id = intval($announcement_id); - - if (is_array($file) && $file['error'] == 0 ) { - $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. - $sys_course_path = api_get_path(SYS_COURSE_PATH); - $updir = $sys_course_path.$courseDir; - - // Try to add an extension to the file if it hasn't one - $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); - // user's file name - $file_name = $file['name']; - - if (!filter_extension($new_file_name)) { - $return = -1; - Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); - } else { - $new_file_name = uniqid(''); - $new_path = $updir.'/'.$new_file_name; - $result = @move_uploaded_file($file['tmp_name'], $new_path); - $safe_file_comment = Database::escape_string($file_comment); - $safe_file_name = Database::escape_string($file_name); - $safe_new_file_name = Database::escape_string($new_file_name); - // Storing the attachments if any - $sql = 'INSERT INTO '.$tbl_announcement_attachment.'(filename, comment, path, announcement_id, size) '. - "VALUES ( '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '".intval($file['size'])."' )"; - $result = Database::query($sql); - $return = 1; + + + /* ATTACHMENT FUNCTIONS */ + + /** + * Show a list with all the attachments according to the post's id + * @param int announcement id + * @return array with the post info + * @author Arthur Portugal + * @version November 2009, dokeos 1.8.6.2 + */ + + public static function get_attachment($announcement_id) { + $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); + $announcement_id=Database::escape_string($announcement_id); + $row=array(); + $sql = 'SELECT id,path, filename,comment FROM '. $tbl_announcement_attachment.' WHERE announcement_id = '.(int)$announcement_id.''; + $result=Database::query($sql); + if (Database::num_rows($result)!=0) { + $row = Database::fetch_array($result,'ASSOC'); } + return $row; } - return $return; -} - -/** - * This function edit a attachment file into announcement - * @param int attach id - * @param array uploaded file $_FILES - * @param string file comment - * @return int - */ -function edit_announcement_attachment_file($id_attach, $file, $file_comment) { - global $_course; - $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); - $return = 0; - - if (is_array($file) && $file['error'] == 0 ) { - $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. - $sys_course_path = api_get_path(SYS_COURSE_PATH); - $updir = $sys_course_path.$courseDir; - - // Try to add an extension to the file if it hasn't one - $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); - // user's file name - $file_name =$file ['name']; - - if (!filter_extension($new_file_name)) { - $return -1; - Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); - } else { - $new_file_name = uniqid(''); - $new_path = $updir.'/'.$new_file_name; - $result = @move_uploaded_file($file['tmp_name'], $new_path); - $safe_file_comment = Database::escape_string($file_comment); - $safe_file_name = Database::escape_string($file_name); - $safe_new_file_name = Database::escape_string($new_file_name); - $id_attach = intval($id_attach); - $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='".intval($file['size'])."' - WHERE id = '$id_attach'"; - $result = Database::query($sql); - if ($result === false) { + + /** + * This function add a attachment file into announcement + * @param int announcement id + * @param string file comment + * @param array uploaded file $_FILES + * @return int -1 if failed, 0 if unknown (should not happen), 1 if success + */ + + public static function add_announcement_attachment_file($announcement_id, $file_comment, $file) { + global $_course; + $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); + $return = 0; + $announcement_id = intval($announcement_id); + + if (is_array($file) && $file['error'] == 0 ) { + $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. + $sys_course_path = api_get_path(SYS_COURSE_PATH); + $updir = $sys_course_path.$courseDir; + + // Try to add an extension to the file if it hasn't one + $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); + // user's file name + $file_name = $file['name']; + + if (!filter_extension($new_file_name)) { $return = -1; - Display :: display_error_message(get_lang('UplUnableToSaveFile')); + Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); } else { - $return = 1; + $new_file_name = uniqid(''); + $new_path = $updir.'/'.$new_file_name; + $result = @move_uploaded_file($file['tmp_name'], $new_path); + $safe_file_comment = Database::escape_string($file_comment); + $safe_file_name = Database::escape_string($file_name); + $safe_new_file_name = Database::escape_string($new_file_name); + // Storing the attachments if any + $sql = 'INSERT INTO '.$tbl_announcement_attachment.'(filename, comment, path, announcement_id, size) '. + "VALUES ( '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '".intval($file['size'])."' )"; + $result = Database::query($sql); + $return = 1; } } + return $return; } - return $return; -} - -/** - * This function delete a attachment file by id - * @param integer attachment file Id - * - */ -function delete_announcement_attachment_file($id) { - global $_course; - $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); - $id=Database::escape_string($id); - $sql="DELETE FROM $tbl_announcement_attachment WHERE id = $id"; - $result=Database::query($sql); - // update item_property - //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id()); -} \ No newline at end of file + + /** + * This function edit a attachment file into announcement + * @param int attach id + * @param array uploaded file $_FILES + * @param string file comment + * @return int + */ + public static function edit_announcement_attachment_file($id_attach, $file, $file_comment) { + global $_course; + $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); + $return = 0; + + if (is_array($file) && $file['error'] == 0 ) { + $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. + $sys_course_path = api_get_path(SYS_COURSE_PATH); + $updir = $sys_course_path.$courseDir; + + // Try to add an extension to the file if it hasn't one + $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); + // user's file name + $file_name =$file ['name']; + + if (!filter_extension($new_file_name)) { + $return -1; + Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); + } else { + $new_file_name = uniqid(''); + $new_path = $updir.'/'.$new_file_name; + $result = @move_uploaded_file($file['tmp_name'], $new_path); + $safe_file_comment = Database::escape_string($file_comment); + $safe_file_name = Database::escape_string($file_name); + $safe_new_file_name = Database::escape_string($new_file_name); + $id_attach = intval($id_attach); + $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='".intval($file['size'])."' + WHERE id = '$id_attach'"; + $result = Database::query($sql); + if ($result === false) { + $return = -1; + Display :: display_error_message(get_lang('UplUnableToSaveFile')); + } else { + $return = 1; + } + } + } + return $return; + } + + /** + * This function delete a attachment file by id + * @param integer attachment file Id + * + */ + public static function delete_announcement_attachment_file($id) { + global $_course; + $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); + $id=Database::escape_string($id); + $sql="DELETE FROM $tbl_announcement_attachment WHERE id = $id"; + $result=Database::query($sql); + // update item_property + //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id()); + } +} //end class \ No newline at end of file diff --git a/main/announcements/announcements.php b/main/announcements/announcements.php index 6fa8a0c083..d6d48aeb33 100755 --- a/main/announcements/announcements.php +++ b/main/announcements/announcements.php @@ -160,14 +160,14 @@ if (((!empty($_GET['action']) && $_GET['action'] == 'add') && $_GET['origin'] == if ((empty($originalresource) || ($originalresource!=='no')) and (!empty($action) && $action=='add')) { $_SESSION['formelements']=null; //unset($_SESSION['formelements']); - unset_session_resources(); + //unset_session_resources(); } /* Javascript */ -$htmlHeadXtra[] = to_javascript(); +$htmlHeadXtra[] = AnnouncementManager::to_javascript(); $htmlHeadXtra[] = ''; //jQuery $htmlHeadXtra[] = '