function __construct($course_id)
 {
     global $CFG, $DB;
     $global_config = get_config('block_notifications');
     $Course = new Course();
     if (!$Course->is_registered($course_id) or !$Course->uses_notifications_block($course_id)) {
         echo get_string('rss_not_enabled', 'block_notifications');
         return;
     }
     $User = new User();
     $teacher = $User->get_professor($course_id);
     // if no teacher then add a dummy mail address
     if (empty($teacher)) {
         $teacher = new \stdClass();
         $teacher->firstname = "No";
         $teacher->lastname = "Teacher";
         $teacher->email = "*****@*****.**";
     }
     $course_info = $Course->get_course_info($course_id);
     $course_registration = $Course->get_registration($course_id);
     if ($course_registration->notify_by_rss != 1) {
         return;
         // the rss is not active in the course
     }
     $now = date('r');
     $course_name = $this->standardize($course_info->fullname);
     $course_summary = $this->standardize($course_info->summary);
     $output = "<?xml version=\"1.0\"?>\n\t\t\t\t\t<rss version=\"2.0\">\n\t\t\t\t\t<channel>\n\t\t\t\t\t<title>{$course_name}</title>\n\t\t\t\t\t<link>{$CFG->wwwroot}/course/view.php?id={$course_id}</link>\n\t\t\t\t\t<description>{$course_summary}</description>\n\t\t\t\t\t<language>en-us</language>\n\t\t\t\t\t<pubDate>{$now}</pubDate>\n\t\t\t\t\t<lastBuildDate>{$now}</lastBuildDate>\n\t\t\t\t\t<docs>{$CFG->wwwroot}/course/view.php?id={$course_id}</docs>\n\t\t\t\t\t<managingEditor>{$teacher->email} ({$teacher->firstname} {$teacher->lastname})</managingEditor>\n\t\t\t\t\t<webMaster>helpdesk@elearninglab.org (Helpdesk eLab)</webMaster>";
     $logs = $Course->get_logs($course_id, $global_config->history_length);
     $events = report_eventlist_list_generator::get_all_events_list();
     if (!isset($logs) or !is_array($logs) or count($logs) == 0) {
         $output .= "<item>";
         $output .= '<title>' . get_string('rss_empty_title', 'block_notifications') . '</title>';
         $output .= '<description>' . get_string('rss_empty_description', 'block_notifications') . '</description>';
         $output .= "</item>";
     } else {
         $separator = ' - ';
         foreach ($logs as $log) {
             $output .= "<item>";
             $output .= '<title>' . get_string($log->module, 'block_notifications') . ': ' . $this->standardize($log->name) . '</title>';
             if (preg_match('/deleted/', $log->event)) {
                 $output .= "<link></link>";
             } else {
                 $output .= "<link>" . $this->extract_url($log) . "</link>";
             }
             $output .= "<description>";
             $output .= $this->standardize(preg_replace('/\\\\.*$/', '', $events[$log->event]['raweventname']) . ' on ' . date("r", $log->time_created));
             $output .= "</description>";
             $output .= "</item>";
         }
     }
     $output .= "</channel></rss>";
     header("Content-Type: application/rss+xml");
     echo $output;
 }
 function get_content()
 {
     if ($this->content !== NULL) {
         return $this->content;
     }
     global $COURSE;
     global $USER;
     global $CFG;
     $this->content = new stdClass();
     $Course = new Course();
     $global_config = get_config('block_notifications');
     $course_registration = $Course->get_registration($COURSE->id);
     if ($global_config->email_channel != 1 and $global_config->sms_channel != 1 and $global_config->rss_channel != 1 or $course_registration->notify_by_email == 0 and $course_registration->notify_by_sms == 0 and $course_registration->notify_by_rss == 0) {
         $this->content->text = get_string('configuration_comment', 'block_notifications');
     } else {
         // last notification info
         $this->content->text = "<span style='font-size: 12px'>";
         $this->content->text .= get_string('last_notification', 'block_notifications');
         $this->content->text .= ": " . date("j M Y G:i:s", $course_registration->last_notification_time);
         $this->content->text .= "</span><br />";
         if ($global_config->email_channel == 1 and $course_registration->notify_by_email == 1) {
             $this->content->text .= "<img src='{$CFG->wwwroot}/blocks/notifications/images/Mail-icon.png' ";
             $this->content->text .= "alt='e-mail icon' ";
             $this->content->text .= "title='" . get_string('email_icon_tooltip', 'block_notifications') . "' />";
             //$this->content->text.= '<br />';
         }
         if ($global_config->sms_channel == 1 and $course_registration->notify_by_sms == 1 and class_exists('block_notifications\\SMS')) {
             if (empty($USER->phone2)) {
                 //$this->content->text.= "<a target='_blank' href='$CFG->wwwroot/help.php?module=plugin&file=../blocks/notifications/lang/en_utf8/help/prova.html'>";
                 $this->content->text .= "<a target='_blank' href='{$CFG->wwwroot}/blocks/notifications/help.php'>";
                 $this->content->text .= "<img src='{$CFG->wwwroot}/blocks/notifications/images/SMS-icon_warning.png' ";
                 $this->content->text .= "alt='sms warning icon' ";
                 $this->content->text .= "title='" . get_string('sms_icon_phone_number_missing_tooltip', 'block_notifications') . "' />";
                 $this->content->text .= "</a>";
             } else {
                 $this->content->text .= "<img src='{$CFG->wwwroot}/blocks/notifications/images/SMS-icon.png' ";
                 $this->content->text .= "alt='sms icon' ";
                 $this->content->text .= "title='" . get_string('sms_icon_tooltip', 'block_notifications') . "' />";
             }
             //$this->content->text.= '<br />';
         }
         if ($global_config->rss_channel == 1 and $course_registration->notify_by_rss == 1) {
             if (isset($course_registration->rss_shortname_url_param) and $course_registration->rss_shortname_url_param == 1) {
                 $this->content->text .= "<a target='_blank' href='{$CFG->wwwroot}/blocks/notifications/classes/RSS.php?shortname=" . urlencode($COURSE->shortname) . "'>";
             } else {
                 $this->content->text .= "<a target='_blank' href='{$CFG->wwwroot}/blocks/notifications/classes/RSS.php?id={$COURSE->id}'>";
             }
             $this->content->text .= "<img src='{$CFG->wwwroot}/blocks/notifications/images/RSS-icon.png' ";
             $this->content->text .= "alt='rss icon' ";
             $this->content->text .= "title='" . get_string('rss_icon_tooltip', 'block_notifications') . "' />";
             $this->content->text .= "</a>";
         }
     }
     $this->content->text .= $this->personal_settings($course_registration);
     $this->content->footer = '';
     return $this->content;
 }
 public function execute()
 {
     global $CFG;
     echo "\n\n****** notifications :: begin ******";
     $User = new User();
     // clean deleted users data
     $User->collect_garbage();
     $Course = new Course();
     // clean deleted courses data
     $Course->collect_garbage();
     // get the list of courses that are using this block
     $courses = $Course->get_all_courses_using_notifications_block();
     // if no courses are using this block exit
     if (!is_array($courses) or count($courses) < 1) {
         echo "\n--> None course is using notifications plugin.";
         echo "\n****** notifications :: end ******\n\n";
         return;
     }
     $global_config = get_config('block_notifications');
     echo "\n\nNumber of courses using the block: " . count($courses);
     foreach ($courses as $course) {
         // if course is not visible then skip
         if ($course->visible == 0) {
             continue;
         }
         // if the course has not been registered so far then register
         echo "\n--> Processing course: {$course->fullname}";
         if (!$Course->is_registered($course->id)) {
             $Course->register($course->id, time());
         }
         // get the course registration
         $course_registration = $Course->get_registration($course->id);
         // initialize user preferences and check for new enrolled users in this course
         $enrolled_users = $User->get_all_users_enrolled_in_the_course($course->id);
         foreach ($enrolled_users as $user) {
             // check if the user has preferences
             $user_preferences = $User->get_preferences($user->id, $course->id);
             // if the user has not preferences than set the default
             if (is_null($user_preferences)) {
                 $user_preferences = new \Object();
                 $user_preferences->user_id = $user->id;
                 $user_preferences->course_id = $course->id;
                 $user_preferences->notify_by_email = $course_registration->email_notification_preset;
                 $user_preferences->notify_by_sms = $course_registration->sms_notification_preset;
                 $User->initialize_preferences($user_preferences->user_id, $user_preferences->course_id, $user_preferences->notify_by_email, $user_preferences->notify_by_sms);
             }
         }
         // if course log entry does not exist
         // or the last notification time is older than two days
         // then reinitialize course log
         if (!$Course->log_exists($course->id) or $course_registration->last_notification_time + 48 * 3600 < time()) {
             $Course->initialize_log($course->id);
         }
         $Course->update_log($course->id);
         // check if the course has something new or not
         $changelist = $Course->get_recent_activities($course->id);
         // update the last notification time
         $Course->update_last_notification_time($course->id, time());
         if (empty($changelist)) {
             continue;
         }
         // check the next course. No new items in this one.
         foreach ($enrolled_users as $user) {
             // get user preferences
             $user_preferences = $User->get_preferences($user->id, $course->id);
             // if the email notification is enabled in the course
             // and if the user has set the emailing notification in preferences
             // then send a notification by email
             if ($global_config->email_channel == 1 and $course_registration->notify_by_email == 1 and $user_preferences->notify_by_email == 1) {
                 $eMail = new eMail();
                 $eMail->notify($changelist, $user, $course);
             }
             // if the sms notification is enabled in the course
             // and if the user has set the sms notification in preferences
             // and if the user has set the mobile phone number
             // then send a notification by sms
             if (class_exists('block_notifications\\SMS') and $global_config->sms_channel == 1 and $course_registration->notify_by_sms == 1 and $user_preferences->notify_by_sms == 1 and !empty($user->phone2)) {
                 $sms = new SMS();
                 $sms->notify($changelist, $user, $course);
             }
         }
     }
     echo "\n****** notifications :: end ******\n\n";
 }