Эх сурвалжийг харах

Fix work notifications see BT#13533

- Fix wrong URL for students
- Fix missing get_lang in template
- Add optional sms parameters, in order to use the SMS plugins.
- Format code
- Validate user using api_get_user_info()
- Change function parameter order
jmontoyaa 7 жил өмнө
parent
commit
61e12ac27b

+ 18 - 6
main/inc/lib/message.lib.php

@@ -269,6 +269,7 @@ class MessageManager
      * @param int $sender_id
      * @param bool $directMessage
      * @param int $forwardId
+     * @param array $smsParameters
      *
      * @return bool
      */
@@ -284,7 +285,8 @@ class MessageManager
         $topic_id = 0,
         $sender_id = null,
         $directMessage = false,
-        $forwardId = 0
+        $forwardId = 0,
+        $smsParameters = []
     ) {
         $table_message = Database::get_main_table(TABLE_MESSAGE);
         $group_id = intval($group_id);
@@ -493,6 +495,8 @@ class MessageManager
      * @param int $sender_id
      * @param bool $sendCopyToDrhUsers send copy to related DRH users
      * @param bool $directMessage
+     * @param bool $smsParameters
+     * @param bool $uploadFiles Do not upload files using the MessageManager class
      *
      * @return bool
      */
@@ -502,20 +506,28 @@ class MessageManager
         $message,
         $sender_id = null,
         $sendCopyToDrhUsers = false,
-        $directMessage = false
+        $directMessage = false,
+        $smsParameters = [],
+        $uploadFiles = true
     ) {
+        $files = $_FILES ? $_FILES : [];
+        if ($uploadFiles === false) {
+            $files = [];
+        }
         $result = self::send_message(
             $receiver_user_id,
             $subject,
             $message,
-            $_FILES ? $_FILES : [],
+            $files,
             [],
             null,
             null,
             null,
             null,
             $sender_id,
-            $directMessage
+            $directMessage,
+            0,
+            $smsParameters
         );
 
         if ($sendCopyToDrhUsers) {
@@ -677,7 +689,7 @@ class MessageManager
         // user's file name
         $file_name = $file_attach['name'];
         if (!filter_extension($new_file_name)) {
-            echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
+            Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'));
         } else {
             $new_file_name = uniqid('');
             if (!empty($receiver_user_id)) {
@@ -699,11 +711,11 @@ class MessageManager
             }
 
             $path_message_attach = $path_user_info['dir'].'message_attachments/';
-
             // If this directory does not exist - we create it.
             if (!file_exists($path_message_attach)) {
                 @mkdir($path_message_attach, api_get_permissions_for_new_directories(), true);
             }
+
             $new_path = $path_message_attach.$new_file_name;
             if (is_uploaded_file($file_attach['tmp_name'])) {
                 @copy($file_attach['tmp_name'], $new_path);

+ 7 - 4
main/inc/lib/notification.lib.php

@@ -221,6 +221,7 @@ class Notification extends Model
      * @param string $content
      * @param array $senderInfo result of api_get_user_info() or GroupPortalManager:get_group_data()
      * @param array $attachments
+     * @param array $smsParameters
      *
      */
     public function save_notification(
@@ -228,8 +229,9 @@ class Notification extends Model
         $userList,
         $title,
         $content,
-        $senderInfo = array(),
-        $attachments = array()
+        $senderInfo = [],
+        $attachments = [],
+        $smsParameters = []
     ) {
         $this->type = intval($type);
         $content = $this->formatContent($content, $senderInfo);
@@ -297,7 +299,6 @@ class Notification extends Model
                     case self::NOTIFY_INVITATION_AT_ONCE:
                     case self::NOTIFY_GROUP_AT_ONCE:
                         $extraHeaders = [];
-
                         if (isset($senderInfo['email'])) {
                             $extraHeaders = array(
                                 'reply_to' => array(
@@ -316,7 +317,9 @@ class Notification extends Model
                                 $this->adminName,
                                 $this->adminEmail,
                                 $extraHeaders,
-                                $attachments
+                                $attachments,
+                                false,
+                                $smsParameters
                             );
                         }
                         $sendDate = api_get_utc_datetime();

+ 1 - 1
main/template/default/mail/new_work_alert_hrm.tpl

@@ -1,5 +1,5 @@
 <p>{{ 'DearXComma'|get_lang|format(hrm_name) }}</p>
-<p>{{ 'AsHRMYouReceiveThisMailToAlertThatXHasNewWorkInCourseY'|get_lang }}</p>
+<p>{{ 'AsHRMYouReceiveThisMailToAlertThatXHasNewWorkInCourseY'|get_lang | format(student.complete_name, course.name) }}</p>
 <dl>
     <dt>{{ 'Student'|get_lang }}</dt>
     <dd>{{ student.complete_name }}</dd>

+ 35 - 45
main/work/work.lib.php

@@ -2381,7 +2381,7 @@ function send_reminder_users_without_publication($task_data)
     foreach ($list_users as $user) {
         $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
         $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]).", \n\n";
-        $body      = $dear_line.$content;
+        $body = $dear_line.$content;
         MessageManager::send_message($user[3], $subject, $body);
         $mails_sent_to[] = $name_user;
     }
@@ -2389,18 +2389,18 @@ function send_reminder_users_without_publication($task_data)
 }
 
 /**
- * @param int $courseId The course ID
  * @param int $workId The work ID
+ * @param int $courseId The course ID
  * @param int $sessionId Optional. The session ID
  */
-function sendEmailToDrhOnHomeworkCreation($courseId, $workId, $sessionId = 0)
+function sendEmailToDrhOnHomeworkCreation($workId, $courseId, $sessionId = 0)
 {
     $courseInfo = api_get_course_info_by_id($courseId);
     $assignment = get_work_assignment_by_id($workId, $courseId);
     $work = get_work_data_by_id($workId, $courseId, $sessionId);
     $workInfo = array_merge($assignment, $work);
 
-    if (empty($session_id)) {
+    if (empty($sessionId)) {
         $students = CourseManager::get_student_list_from_course_code($courseInfo['code']);
     } else {
         $students = CourseManager::get_student_list_from_course_code($courseInfo['code'], true, $sessionId);
@@ -2409,14 +2409,17 @@ function sendEmailToDrhOnHomeworkCreation($courseId, $workId, $sessionId = 0)
     $bodyView = new Template(null, false, false, false, false, false);
 
     foreach ($students as $student) {
-        $student['complete_name'] = api_get_person_name($student["firstname"], $student["lastname"]);
-        $hrms = UserManager::getDrhListFromUser($student['id']);
+        $studentInfo = api_get_user_info($student['user_id']);
+        if (empty($studentInfo)) {
+            continue;
+        }
 
+        $hrms = UserManager::getDrhListFromUser($student['id']);
         foreach ($hrms as $hrm) {
             $hrmName = api_get_person_name($hrm['firstname'], $hrm['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
 
             $bodyView->assign('hrm_name', $hrmName);
-            $bodyView->assign('student', $student);
+            $bodyView->assign('student', $studentInfo);
             $bodyView->assign('course', $courseInfo);
             $bodyView->assign('course_link', api_get_course_url($courseInfo['code'], $sessionId));
             $bodyView->assign('work', $workInfo);
@@ -2439,20 +2442,19 @@ function sendEmailToDrhOnHomeworkCreation($courseId, $workId, $sessionId = 0)
 /**
  * Sends an email to the students of a course when a homework is created
  *
- * @param int $courseId course_id
- * @param int $sessionId session_id
- * @param int $workId work_id
- *
+ * @param int $workId
+ * @param int $courseId
+ * @param int $sessionId
  *
  * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  */
-function sendEmailToStudentsOnHomeworkCreation($courseId, $sessionId = 0, $workId)
+function sendEmailToStudentsOnHomeworkCreation($workId, $courseId, $sessionId = 0)
 {
     $courseInfo = api_get_course_info_by_id($courseId);
     $courseCode = $courseInfo['code'];
     // Get the students of the course
-    if (empty($session_id)) {
+    if (empty($sessionId)) {
         $students = CourseManager::get_student_list_from_course_code($courseCode);
     } else {
         $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
@@ -2461,43 +2463,30 @@ function sendEmailToStudentsOnHomeworkCreation($courseId, $sessionId = 0, $workI
     $currentUser = api_get_user_info(api_get_user_id());
     if (!empty($students)) {
         foreach ($students as $student) {
-            $user_info = api_get_user_info($student["user_id"]);
-            if (!empty($user_info["mail"])) {
-                $name_user = api_get_person_name(
-                    $user_info["firstname"],
-                    $user_info["lastname"],
-                    null,
-                    PERSON_NAME_EMAIL_ADDRESS
-                );
-                $link = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId;
-                $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
+            $user_info = api_get_user_info($student['user_id']);
+            if (!empty($user_info)) {
+                $link = api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId;
+                $emailbody = get_lang('Dear')." ".$user_info['complete_name'].",\n\n";
                 $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$courseCode.". "."\n\n".
                     '<a href="'.$link.'">'.get_lang('PleaseCheckHomeworkPage').'</a>';
-                $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
+                $emailbody .= "\n\n".$currentUser['complete_name'];
 
                 $additionalParameters = array(
                     'smsType' => SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE,
-                    'userId' => $student["user_id"],
+                    'userId' => $student['user_id'],
                     'courseTitle' => $courseCode,
                     'link' => $link
                 );
 
-                api_mail_html(
-                    $name_user,
-                    $user_info["mail"],
+                MessageManager::send_message_simple(
+                    $student['user_id'],
                     $emailsubject,
                     $emailbody,
-                    api_get_person_name(
-                        $currentUser["firstname"],
-                        $currentUser["lastname"],
-                        null,
-                        PERSON_NAME_EMAIL_ADDRESS
-                    ),
-                    $currentUser["mail"],
-                    null,
                     null,
-                    null,
-                    $additionalParameters
+                    false,
+                    false,
+                    $additionalParameters,
+                    false
                 );
             }
         }
@@ -4112,7 +4101,7 @@ function processWorkForm(
  * @param int $user_id
  * @param array $courseInfo
  * @param int $groupId
- * @param int $session_id
+ * @param int $sessionId
  * @return bool|int
  * @note $params can have the following elements, but should at least have the 2 first ones: (
  *       'new_dir' => 'some-name',
@@ -4122,7 +4111,7 @@ function processWorkForm(
  *       'allow_text_assignment' => 0/1/2,
  * @todo Rename createAssignment or createWork, or something like that
  */
-function addDir($formValues, $user_id, $courseInfo, $groupId, $session_id)
+function addDir($formValues, $user_id, $courseInfo, $groupId, $sessionId = 0)
 {
     $em = Database::getManager();
 
@@ -4135,7 +4124,8 @@ function addDir($formValues, $user_id, $courseInfo, $groupId, $session_id)
         $groupInfo = GroupManager::get_group_properties($groupId);
         $groupIid = $groupInfo['iid'];
     }
-    $session = $em->find('ChamiloCoreBundle:Session', $session_id);
+    $sessionId = (int) $sessionId;
+    $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
 
     $base_work_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work';
     $course_id = $courseInfo['real_id'];
@@ -4224,16 +4214,16 @@ function addDir($formValues, $user_id, $courseInfo, $groupId, $session_id)
     switch ($sendEmailAlert) {
         case 1:
             sendEmailToStudentsOnHomeworkCreation(
+                $workTable->getIid(),
                 $course_id,
-                $session ? $session->getId() : 0,
-                $workTable->getIid()
+                $sessionId
             );
             //no break
         case 2:
             sendEmailToDrhOnHomeworkCreation(
-                $course_id,
                 $workTable->getIid(),
-                $session ? $session->getId() : 0
+                $course_id,
+                $sessionId
             );
             break;
     }