Skip to content
Snippets Groups Projects
course_format_iena_cron_action.php 5.27 KiB
<?php
	/**
	 * Created by PhpStorm.
	 * User: softia
	 * Date: 15/03/18
	 * Time: 11:33
	 */

// 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
	 * @category   format
	 * @copyright  2018 Softia/Université lorraine
	 * @author     vrignaud camille / Michaël Lebeau
	 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
	 */
	
	// include('../../lib.php');
	// include('../../../../config.php');
	
	
	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 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 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'");
			foreach ($sections as $section) {
				$requete = $DB->get_record('course_sections', array('id' => $section->sectionid));
				$date_notif = $this->is_notif($section);
				if ($date_notif == 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
			$date_now=date('Ymd');
			$timenotif=strtotime('-'.$section->daysnotif.' days', $section->daterendu);
			$date_notif=date('Ymd', $timenotif);
			if($date_notif==$date_now){
				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;
			$course_ctx = context_course::instance($requete->course);

			//À 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($course_ctx);
			$course = $DB->get_record('course', array('id' => $requete->course), '*', MUST_EXIST);

			$messageContent = false;

			$messageContent .= "<h1>Rappel</h1>";
			$messageContent .= "<h2>$course->fullname</h2>";

			$date_jour = date('d/m', $section->daterendu);
			$date_heure = date('H:i', $section->daterendu);

			// 0 : NC
			// 1 : work in the classroom
			// 2 : online work
			if ( $section->presence < 2 ) {
				$messageContent .= "<p>La séance de cours <strong>$requete->name</strong> aura lieu le $date_jour à $date_heure.</p>";
			} else if ( $section->presence == 2 ) {
				$messageContent .= "<p>Le travail de la séance de cours à distance <strong>$requete->name</strong> est à finir pour le $date_jour à $date_heure.</p>";
			}

			$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 ";
			$studs_counter = 0;
			foreach ($students as $student) {
				$studs_counter++;
				$message->userto = $student;
				$studs[] = 
				$message->smallmessage = $messageContent;
				$message->fullmessage = $messageContent;
				$message->fullmessagehtml = $messageContent;
				$message->subject = "Rappel : " . $course->fullname . " — " . $requete->name;
				// $message->subject = $messageContent;
				message_send($message);
			}
			$log .= $studs_counter . " students.";
			echo $log;
		}
		
		
	}