Skip to content
Snippets Groups Projects
Select Git revision
  • 33d9d8bc7a94cbf405c530aec6efd785c9e48318
  • master default protected
  • moodle4.4
  • devm42
  • search_student
  • clean_tracking_table
  • moodle4.3
  • optimizing
  • devm4
  • groupes_select
  • interface_admin
  • pastilles_cliquables
  • moodle4.2
  • fix_cpts
  • ergonomie_tableau
  • activity-filter
  • dev
  • m4-bugfix-retractable
  • v3.x
  • 3-permutation-de-sections
  • 1-bobologie-1
  • v4.4
  • v4.3
  • v4.2.01
  • v4.2
  • v3.11.02
  • v4.0
  • v3.11.01
  • v3.7.02
  • v3.7.01
  • v3.5.4plus.01
31 results

course_format_iena_cron_action.php

Blame
  • course_format_iena_cron_action.php 5.67 KiB
    <?php
    // This file is part of Moodle - http://moodle.org/
    //
    // Moodle is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // Moodle is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    //
    // You should have received a copy of the GNU General Public License
    // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
    /**
     *
     * Class course_format_iena_cron_action
     * This class hide and show section with setting present in table format_iena
     * This class send message with setting present in table format_iena
     *
     * @package    format_iena
     * @copyright  2018 Softia/Université lorraine
     * @author     vrignaud camille / Michaël Lebeau
     * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     */
    
    class course_format_iena_cron_action {
        /**
         * Récupère le nombre de jours avant le rendu/présence pour envoyer une notification
         * @return type
         */
        public function cron_message() {
            global $DB, $USER, $CFG;
    
            $sections = $DB->get_records_sql("SELECT fo1.sectionid, fo1.value daysnotif,
                fo2.value daterendu, fo3.value presence, fo4.value msg
                FROM {course_format_options} fo1
                LEFT JOIN {course_format_options} fo2 ON fo1.sectionid = fo2.sectionid
                LEFT JOIN {course_format_options} fo3 ON fo1.sectionid = fo3.sectionid
                LEFT JOIN {course_format_options} fo4 ON fo1.sectionid = fo4.sectionid
                WHERE fo1.format='iena'
                AND fo1.name ='daysnotif'
                AND (fo1.value IS NOT NULL)
                AND fo1.value != -1
                AND fo2.name='daterendu'
                AND fo3.name='presence'
                AND fo4.name='msg'");
            foreach ($sections as $section) {
                $requete = $DB->get_record('course_sections', array('id' => $section->sectionid));
                $datenotif = $this->is_notif($section);
                if ($datenotif == false) {
                    continue;
                }
                $this->iena_send_message($requete, $section);
            }
        }
    
        /**
         * Check if it's time to send a notif by comparing the date withe the numbers of days before sending one in the db
         * @param $section
         * @return bool true if it's time to send a notif
         */
        private function is_notif($section) {
            // On compare la date d'aujourd'hui avec le timestamp - daysnotifs.
            $datenow = date('Ymd');
            $timenotif = strtotime('-'.$section->daysnotif.' days', $section->daterendu);
            $datenotif = date('Ymd', $timenotif);
            if ($datenotif == $datenow) {
                return true;
            }
            return false;
        }
    
        /**
         * @param $requete
         * @param $section
         * @throws coding_exception
         * @throws dml_exception
         */
        private function iena_send_message($requete, $section) {
            global $DB, $CFG, $USER;
            $coursectx = context_course::instance($requete->course);
    
            if ($section->presence == 2) {
                $modalite = get_string('not_presence', 'format_iena');
            } else if ($section->presence == 1) {
                $modalite = get_string('in_presence', 'format_iena');
            }
            
            // Si le message personnalisé est vide.
            if ((empty($section->msg)) && ($section->presence == 2)) {
                $section->msg = get_config('format_iena', 'msg_dist');
            } else if ((empty($section->msg)) && ($section->presence == 1)) {
                $section->msg = get_config('format_iena', 'msg_pres');
            }
    
            // A vérifier : on récupère tous les utilisateurs, même les invités.
            // 2. $students = get_role_users(5 , $context) (the 5 represents the role-id with role.shortname = 'student').
            $students = get_enrolled_users($coursectx);
            $course = $DB->get_record('course', array('id' => $requete->course), '*', MUST_EXIST);
    
            $messagecontent = false;
    
            $messagecontent .= "<h1>Rappel</h1>";
            $messagecontent .= "<h2>$course->fullname ($modalite) - " . date('d/m/Y H:i', $section->daterendu) . "</h2>";
    
            // 0 : NC
            // 1 : work in the classroom
            // 2 : online work
            $messagecontent .= "$section->msg";
    
            $messagecontent .= "<p>Lien vers le cours : <a href='" . $CFG->wwwroot .
                "/course/view.php?id=" . $course->id . "'>" . $course->fullname . "</a></p>";
    
            // Create message.
            $message = new \core\message\message();
            $message->component = 'moodle';
            $message->name = 'instantmessage';
            $message->userfrom = $USER;
            $message->courseid = $course->id;
            $message->notification = '1';
            $message->contexturl = $CFG->wwwroot . "/course/view.php?id=" . $course->id . "#section-" . $requete->section;
            $message->contexturlname = $course->fullname;
            $message->fullmessageformat = FORMAT_HTML;
            $log = "Messages sent for section $requete->name of course $course->fullname (id $course->id) to ";
            $studscounter = 0;
            foreach ($students as $student) {
                $studscounter++;
                $message->userto = $student;
                $studs[] =
                $message->smallmessage = $messagecontent;
                $message->fullmessage = $messagecontent;
                $message->fullmessagehtml = $messagecontent;
                $message->subject = "Rappel : " . $course->fullname . " — " . $requete->name;
                message_send($message);
            }
            $log .= $studscounter . " students.";
            echo $log;
        }
    }