course_format_iena_cron_action.php 8.15 KB
Newer Older
Thomas Fradet's avatar
folder  
Thomas Fradet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
<?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
	{
		
		/**
		 * @param $requete
		 * @param $nbJours
		 * @throws coding_exception
		 */
		private function hide_section($requete, $nbJours)
		{
			if ($nbJours < 0) {
				set_section_visible($requete->course, $requete->section, 0);
				//Log for task
				echo get_string('hide_section', 'format_iena') . $requete->name;
			} else {
				set_section_visible($requete->course, $requete->section, 1);
				//Log for task
				echo get_string('show_section', 'format_iena') . $requete->name;
			}
		}
		
		/**
		 * @param $date
		 * @return float|int
		 */
		private function calcul_nb_jours($date)
		{
			$date_now = date_create(date("Y-m-d H:i:s"))->getTimestamp();
			$nbJoursTimestamp = $date_now - $date;
			// Calcul number of day
			$nbJours = $nbJoursTimestamp / 86400;
			return $nbJours;
		}
		
		/**
		 * @throws dml_exception
		 */
		public function cron_hide_section()
		{
			global $DB;
			
			$sections = $DB->get_records('format_iena');
			
			foreach ($sections as $section) {
				$requete = $DB->get_record('course_sections', array('id' => $section->id_section));
				
				if ($section->hide == 1) {
					set_section_visible($requete->course, $requete->section, 1);
					echo get_string('show_section', 'format_iena') . $requete->name;
				}
				
				if ($section->hide == 2) {
					$date_rendu = date_create($section->date_rendu);
					//We calcul date with timeStamp
					$date_rendu = $date_rendu->getTimestamp();
					$this->hide_section($requete, $this->calcul_nb_jours($date_rendu));
					
				} else if ($section->hide == 3) {
					if ($section->day_before && $section->nb_days_before) {
						$date_before = date('Y-m-d H:i:s', strtotime($section->date_rendu . " - " . $section->nb_days_before . " days"));
						$date_before = date_create($date_before)->getTimestamp();
						$this->hide_section($requete, $this->calcul_nb_jours($date_before));
					} else if ($section->day_same) {
						$date_rendu = date_create($section->date_rendu);
						$date_rendu = $date_rendu->getTimestamp();
						$this->hide_section($requete, $this->calcul_nb_jours($date_rendu));
					} else if ($section->day_after && $section->nb_days_after) {
						$date_after = date('Y-m-d H:i:s', strtotime($section->date_rendu . " + " . $section->nb_days_after . " days"));
						$date_after = date_create($date_after)->getTimestamp();
						$this->hide_section($requete, $this->calcul_nb_jours($date_after));
					}
				}
				
			}
		}
		
		/**
		 * @throws dml_exception
		 * @throws coding_exception
		 */
		public function cron_message()
		{
			global $DB, $USER, $CFG;
			$sections = $DB->get_records('format_iena');
			foreach ($sections as $section) {
				if ($section->day_before || $section->day_same || $section->day_after) {
					$requete = $DB->get_record('course_sections', array('id' => $section->id_section));
					$date_notif = $this->is_notif($section);
					if ($date_notif == false) {
						continue;
					}
					$this->iena_send_message($requete, $section);
				} else {
					continue;
				}
			}
		}
		
		/**
		 * @param $section
		 * @return bool
		 */
		private function is_notif($section)
		{
			$date_notif = null;
			$date_now = date_create(date("Y-m-d"));
			if ($section->day_before && $section->nb_days_before) {
				$date_notif = date('Y-m-d', strtotime($section->date_rendu . " - " . $section->nb_days_before . " days"));
				$date_notif = date_create($date_notif);
				if ($date_notif == $date_now) {
					return true;
				}
			}
			if ($section->day_same) {
				$date_notif = date_create(date('Y-m-d', strtotime($section->date_rendu)));
				if ($date_notif == $date_now) {
					return true;
				}
			}
			if ($section->day_after && $section->nb_days_after) {
				$date_notif = date('Y-m-d', strtotime($section->date_rendu . " + " . $section->nb_days_after . " days"));
				$date_notif = date_create($date_notif);
				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);
			$students = get_enrolled_users($course_ctx);
			$course = $DB->get_record('course', array('id' => $requete->course), '*', MUST_EXIST);

			$messageContent = false;
			//Here we can change value
			/*
			
			<h1>Rappel</h1>
			
			<h2>$course->fullname</h2>

			<p>La séance de cours <strong>$requete->name</strong> aura lieu le 00/00 à 00:00.</p>
			
			<p>Le travail de la séance de cours à distance <strong>$requete->name</strong> est à finir pour le 00/00 à 00:00.</p>

			<p>Lien vers le cours : <a href='" . $CFG->wwwroot . "/course/view.php?id=" . $course->id . "'>" . $course->fullname . "</a></p>
			*/

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

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

			// 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>";
			
			// $messageContent .= "<h1>Rappel</h1>";
			// $messageContent .= "<br> Cours : " . $course->fullname;
			// $messageContent .= "<br> Séance : " . $requete->name;
			// $messageContent .= "<br> Date de la séance : " . $section->date_rendu;
			// $messageContent .= "<br> Lien : <a href='" . $CFG->wwwroot . "/course/view.php?id=" . $course->id . "'>" . $course->fullname . "</a>";

			//Create message
			$message = new \core\message\message();
			$message->component = 'moodle';
			$message->name = 'instantmessage';
			$message->userfrom = $USER;
			$message->courseid = $course->id;
			$message->notification = '0';
			$message->fullmessageformat = FORMAT_HTML;
			foreach ($students as $student) {
				echo get_string('snd_msg_to', 'format_iena') . $student->firstname . ' ' . $student->lastname;
				$message->userto = $student;
				$message->smallmessage = $messageContent;
				$message->fullmessage = $messageContent;
				$message->fullmessagehtml = $messageContent;
				$message->subject = "Rappel : " . $course->fullname . " — " . $requete->name;
				// $message->subject = $messageContent;
				message_send($message);
			}
		}
		
		
	}