public function update_permission($task_set_id, $task_set_permission_id) { $this->load->library('form_validation'); $this->form_validation->set_rules('task_set_permission[group_id]', 'lang:admin_task_sets_form_field_group_id', 'required'); $task_set_permission_data = $this->input->post('task_set_permission'); $this->form_validation->set_rules('task_set_permission[deadline_notification_emails_handler]', 'lang:admin_task_sets_form_field_deadline_notification_emails_handler', 'required'); if (isset($task_set_permission_data['deadline_notification_emails_handler']) && $task_set_permission_data['deadline_notification_emails_handler'] == 2) { $this->form_validation->set_rules('task_set_permission[deadline_notification_emails]', 'lang:admin_task_sets_form_field_deadline_notification_emails', 'required|valid_emails'); } else { $this->form_validation->set_rules('task_set_permission[deadline_notification_emails]', 'lang:admin_task_sets_form_field_deadline_notification_emails', 'zero_or_more_valid_emails'); } if ($this->form_validation->run()) { $this->_transaction_isolation(); $this->db->trans_begin(); $task_set = new Task_set(); $task_set->get_by_id((int) $task_set_id); $task_set_permission = new Task_set_permission(); $task_set_permission->get_by_id((int) $task_set_permission_id); if ($task_set->exists() && !is_null($task_set->course_id)) { if ($task_set_permission->exists() && $task_set_permission->is_related_to($task_set)) { $task_set_permission_upload_end_time = $task_set_permission->upload_end_time; $task_set_permission->enabled = isset($task_set_permission_data['enabled']) ? 1 : 0; $task_set_permission->group_id = $task_set_permission_data['group_id']; $task_set_permission->room_id = intval($task_set_permission_data['room_id']) > 0 ? intval($task_set_permission_data['room_id']) : NULL; $task_set_permission->publish_start_time = preg_match(Task_sets::REGEXP_PATTERN_DATETYME, $task_set_permission_data['publish_start_time']) ? $task_set_permission_data['publish_start_time'] : NULL; $task_set_permission->upload_end_time = preg_match(Task_sets::REGEXP_PATTERN_DATETYME, $task_set_permission_data['upload_end_time']) ? $task_set_permission_data['upload_end_time'] : NULL; $task_set_permission->deadline_notification_emails = $task_set_permission_data['deadline_notification_emails']; $task_set_permission->deadline_notification_emails_handler = $task_set_permission_data['deadline_notification_emails_handler']; if ($task_set_permission->upload_end_time !== $task_set_permission_upload_end_time) { $task_set_permission->deadline_notified = 0; } if ($task_set_permission->save()) { $task_set_permissions = new Task_set_permission(); $task_set_permissions->where_related($task_set); $task_set_permissions->where('group_id', $task_set_permission->group_id); if ($task_set_permissions->count() > 1) { $this->db->trans_rollback(); $this->messages->add_message('lang:admin_task_set_permissions_error_message_cant_save_for_the_same_group', Messages::MESSAGE_TYPE_ERROR); } else { $this->db->trans_commit(); $this->messages->add_message('lang:admin_task_set_permissions_success_message_saved', Messages::MESSAGE_TYPE_SUCCESS); $this->_action_success(); } } else { $this->db->trans_rollback(); $this->messages->add_message('lang:admin_task_set_permissions_error_message_cant_save', Messages::MESSAGE_TYPE_ERROR); } } else { $this->db->trans_rollback(); $this->messages->add_message('lang:admin_task_set_permissions_error_message_cant_find_task_set_permission_or_is_not_related_to_task_set', Messages::MESSAGE_TYPE_ERROR); } } else { $this->db->trans_rollback(); $this->messages->add_message('lang:admin_task_set_permissions_error_message_cant_find_task_set_or_course', Messages::MESSAGE_TYPE_ERROR); } redirect(create_internal_url('admin_task_set_permissions/edit_permission/' . (int) $task_set_id) . '/' . (int) $task_set_permission_id); } else { $this->edit_permission($task_set_id, $task_set_permission_id); } }
public function send_deadline_notifications($lang_idiom = NULL) { $this->load->database(); $this->load->model('translations'); if (!is_null($lang_idiom)) { $this->lang->reinitialize_for_idiom($lang_idiom); } $translations = $this->translations->get_translations_for_idiom($this->lang->get_current_idiom()); $this->lang->add_custom_translations($translations); $this->lang->load('cli'); $current_time = Date('Y-m-d H:i:s'); $one_day_back_time = Date('Y-m-d H:i:s', strtotime('now -1 day')); $task_sets1 = new Task_set(); $task_sets1->select('id, name, course_id, group_id AS common_group_id, upload_end_time AS common_upload_end_time, deadline_notified AS common_deadline_notified, deadline_notification_emails AS common_deadline_notification_emails, deadline_notification_emails_handler AS common_deadline_notification_emails_handler'); $task_sets1->select('null AS `task_set_permission_id`', FALSE); $task_sets1->where('deadline_notified', 0); $task_sets1->where('deadline_notification_emails_handler >', 0); $task_sets1->group_start(); $task_sets1->not_group_start(); $task_sets1->where('upload_end_time', NULL); $task_sets1->group_end(); $task_sets1->where('upload_end_time <', $current_time); $task_sets1->where('upload_end_time >=', $one_day_back_time); $task_sets1->group_end(); $task_sets1->where_subquery(0, '(SELECT COUNT(`tsp`.`id`) AS `count` FROM `task_set_permissions` tsp WHERE `tsp`.`task_set_id` = `task_sets`.`id` AND `tsp`.`enabled` = 1)'); $task_sets1->where('published', 1); $task_sets2 = new Task_set(); $task_sets2->select('id, name, course_id'); $task_sets2->include_related('task_set_permission', 'group_id', 'common'); $task_sets2->include_related('task_set_permission', 'upload_end_time', 'common'); $task_sets2->include_related('task_set_permission', 'deadline_notified', 'common'); $task_sets2->include_related('task_set_permission', 'deadline_notification_emails', 'common'); $task_sets2->include_related('task_set_permission', 'deadline_notification_emails_handler', 'common'); $task_sets2->include_related('task_set_permission', 'id'); $task_sets2->where_related('task_set_permission', 'enabled', 1); $task_sets2->where_related('task_set_permission', 'deadline_notified', 0); $task_sets2->where_related('task_set_permission', 'deadline_notification_emails_handler >', 0); $task_sets2->group_start(); $task_sets2->not_group_start(); $task_sets2->where_related('task_set_permission', 'upload_end_time', NULL); $task_sets2->group_end(); $task_sets2->where_related('task_set_permission', 'upload_end_time <', $current_time); $task_sets2->where_related('task_set_permission', 'upload_end_time >=', $one_day_back_time); $task_sets2->group_end(); $task_sets2->where('published', 1); $task_sets1->union_iterated($task_sets2, TRUE); $this->load->library('email'); $sent_notifications = 0; foreach ($task_sets1 as $task_set) { if ($task_set->common_deadline_notification_emails_handler > 0) { $emails = trim($task_set->common_deadline_notification_emails) != '' ? explode(',', $task_set->common_deadline_notification_emails) : array(); array_walk($emails, function (&$email, $key) { $email = trim($email); }); if ($task_set->common_deadline_notification_emails_handler == 1) { $groups = new Group(); $groups->where_related('course', 'id', $task_set->course_id); $groups->include_related('room/teacher', '*'); $groups->group_start('NOT'); $groups->where_related('room', 'id', null); $groups->or_where_related('room/teacher', 'id', null); $groups->group_end(); $groups->group_by_related('room/teacher', 'email'); if (!is_null($task_set->common_group_id)) { $groups->where('id', $task_set->common_group_id); } $groups->get_iterated(); foreach ($groups as $teacher) { if (trim($teacher->room_teacher_email) != '') { $email = trim($teacher->room_teacher_email); if (!in_array($email, $emails)) { $emails[] = $email; } } } } $group = new Group(); if (!is_null($task_set->common_group_id)) { $group->get_by_id((int) $task_set->common_group_id); } if (count($emails)) { $this->email->from_system(); $this->email->reply_to_system(); $this->email->build_message_body('file:emails/cli/deadline_notification.tpl', array('task_set' => $task_set, 'group' => $group)); if ($this->config->item('email_multirecipient_batch_mode')) { $this->email->to($emails); $this->email->subject('LIST: ' . $this->lang->line('cli_deadline_notification_subject')); $this->email->send(); } else { foreach ($emails as $email) { $this->email->to($email); $this->email->subject('TEST'); $this->email->send(); } } $sent_notifications++; if (!is_null($task_set->task_set_permission_id)) { $task_set_permission = new Task_set_permission(); $task_set_permission->get_by_id($task_set->task_set_permission_id); if ($task_set_permission->exists()) { $task_set_permission->deadline_notified = 1; $task_set_permission->save(); } } else { $task_set_update = new Task_set(); $task_set_update->get_by_id($task_set->id); if ($task_set_update->exists()) { $task_set_update->deadline_notified = 1; $task_set_update->save(); } } } } } echo "Process finished, {$sent_notifications} notifications were sent ...\n"; }