block_mahara_iena_connexion.php 13.1 KB
Newer Older
Thomas Fradet's avatar
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
<?php
/**
 * Created by PhpStorm.
 * User: softia
 * Date: 11/04/18
 * Time: 16:23
 */

class block_mahara_iena_connexion
{
	public $wstoken;
	public $base;
	public $url;

	/**
	 * block_mahara_iena_connexion constructor.
	 * @param $wstoken
	 * @param $url
	 */
	public function __construct($wstoken, $base)
	{
		$this->wstoken = $wstoken;
		if ( substr($base, -1) !== "/" ) {
			$base = $base . "/";
		}
		$this->base = $base . "webservice/rest/server.php";
	}


	public function httpPost($params, $url)
	{
		$ch = curl_init();
		$postdata = htmlspecialchars_decode(urldecode($params)); //Il faut absolument faire ça car il faut enlever les ;amp ect ...
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
		$output=curl_exec($ch);
		curl_close($ch);
		return $output;
	}

	function httpGet($url)
	{
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		//  curl_setopt($ch,CURLOPT_HEADER, false);
		$output=curl_exec($ch);
		curl_close($ch);
		return $output;
	}

	public function create_url($function){
		return $this->base."?wstoken=".$this->wstoken."&wsfunction=".$function."&alt=json";
	}

Thomas Fradet's avatar
Thomas Fradet committed
59 60 61 62 63
	public function getMaharaGroups($params){
		$params = http_build_query($params);
		$mahara_group_get_groups_by_id = "mahara_group_get_groups_by_id";
		$url = $this->create_url($mahara_group_get_groups_by_id);
		return json_decode($this->httpPost($params, $url));
Thomas Fradet's avatar
Thomas Fradet committed
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
	}

	public function getMaharaUsers(){
		$getMaharaUsers = "mahara_user_get_users";
		$url = $this->create_url($getMaharaUsers);
		return json_decode($this->httpGet($url));
	}

	/**
	 * Return list of member of a mahara group
	 * @param $id: mahara group id
	 * @return mahara group members or error
	 */
	public function get_mahara_users_in_mahara_group_by_id($id) {
		$params = array (
			'groups' =>
			array (
				0 =>
				array (
					'id' => $id
				)
			)
		);
		$params = http_build_query($params);
		$mahara_group_get_groups_by_id = "mahara_group_get_groups_by_id";
		$url = $this->create_url($mahara_group_get_groups_by_id);
		$res = json_decode($this->httpPost($params, $url), true);
Thomas Fradet's avatar
v3.7  
Thomas Fradet committed
91
		return $res[0]['members'];
Thomas Fradet's avatar
Thomas Fradet committed
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 245 246 247 248 249 250 251 252 253 254 255 256
	}

	public function get_mahara_user_by_mail($email) {
		// var_dump($email);
		$params = array (
			'users' =>
			array (
				0 =>
				array (
					'email' => $email
				)
			)
		);
		$params = http_build_query($params);
		$mahara_user_get_users_by_id = "mahara_user_get_users_by_id";
		$url = $this->create_url($mahara_user_get_users_by_id);
		return json_decode($this->httpPost($params, $url));
	}

	/**
	 * Get a mahara user by id
	 * @param $ma_user_id: mahara user id
	 * @return mahara user
	 */
	public function get_mahara_user_by_id($ma_user_id) {
		$params = array (
			'users' =>
			array (
				0 =>
				array (
					'id' => $ma_user_id
				)
			)
		);
		$params = http_build_query($params);
		$mahara_user_get_users_by_id = "mahara_user_get_users_by_id";
		$url = $this->create_url($mahara_user_get_users_by_id);
		return json_decode($this->httpPost($params, $url));
	}

	/**
	 * Return role for mahara group based on role in moodle course : teacher and editingteacher will be admin and other will be member
	 * @param $mo_user_id: moodle user id
	 * @return string: role (admin or member)
	 */
	public function get_user_role($mo_user_id) {
		global $COURSE;
		$course_ctx = context_course::instance($COURSE->id);
		$course_roles = get_user_roles($course_ctx, $mo_user_id, false);
		$role = "member";
		foreach ($course_roles as $course_role) {
			if ($course_role->shortname == "teacher" || $course_role->shortname == "editingteacher") {
				$role = "admin";
				break;
			}
		}
		return $role;
	}

	/**
	 * Add a user in a mahara group
	 * @param $ma_user_id: mahara user id
	 * @param $mo_user: moodle user object
	 * @param $ma_group_id: mahara group id
	 * @return boolean: succeed or error
	 */
	public function mahara_group_update_group_members($ma_user_id, $ma_group_id, $mo_user) {
		$user = new stdClass();
		$user->id = $ma_user_id;
		$user->role = $this->get_user_role($mo_user->id);
		$user->action = "add";
		$group = new stdClass();
		$group->id = $ma_group_id;
		$group->members = array(
			0 => $user
		);
		$params = array (
			'groups' =>
			array (
				0 => $group
			)
		);
		$params = http_build_query($params);
		$url = $this->create_url("mahara_group_update_group_members");
		$res = json_decode($this->httpPost($params, $url));
		if ($res == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * block_mahara_iena_connexion constructor.
	 * @param $mo_user: moodle user object
	 * @param $ma_group_id: mahara group id
	 */
	public function ensure_user_is_mahara_group_member($mo_user, $ma_group_id) {
		global $COURSE, $CFG, $DB;
		// if ma_group_id is null, mahara group does not exist : create it and add the user to this new mahara group
		// else, check if user is member of it's mahara group or add him to this mahara group
		if ( $ma_group_id == null || $ma_group_id == "" ) {
			$mo_user_groups = groups_get_user_groups($COURSE->id, $mo_user->id);
			$mo_user_group_id = $mo_user_groups[0][0]; // first groupment, first group
			$mo_group_name = groups_get_group_name($mo_user_group_id);

			$group = new stdClass();
			$group->name = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
			$group->description = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
			$group->grouptype = 'course';
			$group->request = true;
			$group->public = false;
			$group->institution = $CFG->instution_mahara;
			$user = new stdClass();
			$user->id = $this->get_mahara_user_by_mail($mo_user->email)[0]->id;
			$user->username = $mo_user->username;
			$user->role = $this->get_user_role($mo_user->id);
			$group->members = array(
				0 => $user
			);
			$params = array (
				'groups' =>
				array (
					0 => $group
				)
			);
			$params = http_build_query($params);
			$url = $this->create_url("mahara_group_create_groups");
			$res = json_decode($this->httpPost($params, $url));
			if ($res->error) {
				return false;
			} else {
				$record = new stdClass();
				$record->course = $COURSE->id;
				$record->mahara_group_id = $res[0]->id;
				$record->moodle_group_id = $mo_user_group_id;
				$DB->insert_records('block_mahara_iena', array( 0 => $record ), false);
				return true;
			}

		} else {
			$ma_group_members = $this->get_mahara_users_in_mahara_group_by_id($ma_group_id);
			$is_in_group = false;
			foreach ($ma_group_members as $member) {
				if ( $mo_user->email == $this->get_mahara_user_by_id($member->id)[0]->email ) {
					$is_in_group = true;
					break;
				}
			}
			if ( ! $is_in_group ) {
				$ma_user = $this->get_mahara_user_by_mail($mo_user->email);
				return $this->mahara_group_update_group_members($ma_user[0]->id, $ma_group_id, $mo_user->id);
			} else {
				return true;
			}
		}
	}

	/**
	 * block_mahara_iena_connexion constructor.
	 * @param $user: moodle user object
	 * @return boolean: this user is a mahara user (comparison with email)
	 */
	public function ensure_user_exists_in_mahara($user) {
		$is_a_mahara_user = $this->get_mahara_user_by_mail($user->email);
Thomas Fradet's avatar
v3.7  
Thomas Fradet committed
257
		if ( isset($is_a_mahara_user->error) ) {
Thomas Fradet's avatar
Thomas Fradet committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			return false;
		} else {
			return true;
		}
	}

	/**
	* Fonction duplique un bout de ensure_user_is_mahara_group_member et est utilisée pour créer des groupes mahara à partir de groupes moodle par le fichier mahara_iena_groups_all avec le get create_all_groups pour créer des groupes mahara pour chaque groupe moodle existant. Cette fonction n'est disponible que pour l'enseignant. 
	* @param $mo_group : le groupe moodle à créer dans mahara avec au moins les clefs id et le name
	* @param $mo_user : l'utilisateur à inscrire dans le groupe (il en faut forcément un), dans ce cas, l'enseignant qui a demandé la création de tous les groupes. 
	*/
	public function create_mahara_group($mo_group, $mo_user) {

		global $COURSE, $CFG, $DB;

		// $mo_user_groups = groups_get_user_groups($COURSE->id, $mo_user->id);
		// $mo_user_group_id = $mo_user_groups[0][0]; 
		$mo_user_group_id = $mo_group->id;
		// $mo_group_name = groups_get_group_name($mo_user_group_id);
		$mo_group_name = $mo_group->name;

		$group = new stdClass();
		$group->name = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
		$group->description = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
		$group->grouptype = 'course';
		$group->request = true;
		$group->public = false;
		$group->institution = $CFG->instution_mahara;
		$user = new stdClass();
		$user->id = $this->get_mahara_user_by_mail($mo_user->email)[0]->id;
		$user->username = $mo_user->username;
		$user->role = $this->get_user_role($mo_user->id);
		$group->members = array(
			0 => $user
		);
		$params = array (
			'groups' =>
			array (
				0 => $group
			)
		);
		$params = http_build_query($params);
		$url = $this->create_url("mahara_group_create_groups");
		$res = json_decode($this->httpPost($params, $url));
Thomas Fradet's avatar
v3.7  
Thomas Fradet committed
302
		if ( isset($res->error) ) {
Thomas Fradet's avatar
Thomas Fradet committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
			return false;
		} else {
			$record = new stdClass();
			$record->course = $COURSE->id;
			$record->mahara_group_id = $res[0]->id;
			$record->moodle_group_id = $mo_user_group_id;
			$DB->insert_records('block_mahara_iena', array( 0 => $record ), false);
			return true;
		}

	}

	/**
	* Fonction ensure_user_is_mahara_group_member et est utilisée pour créer des groupes mahara à partir de groupes moodle par le bloc depuis un lien pour accéder à son groupe. La différence c'est qu'elle prends le groupe moodle en paramètre pour permettre de créer un groupe précis s'il n'existe pas sur mahara. L'autre fonction se contente de prendre le premier groupe moodle du user pour créer le groupe mahara corresondant, donc ça n'est pas fonctionnel si on présente le choix parmi tous ses groupes à l'utilisateur pour y accéder. 
	* @param $mo_user : l'utilisateur à inscrire dans le groupe (il en faut forcément un)
	* @param $ma_group_id : le groupe mahara où vérifier l'inscription s'il y en a un ou rien s'il va falloir créer le groupe mahara
	* @param $mo_group_id : le groupe moodle à créer dans mahara avec au moins les clefs id et le name qui sera utilisé pour savoir sur quel cours moodle se baser pour créer le groupe mahara
	* @return bool: erreurs ou pas
	*/
	public function ensure_user_is_mahara_group_member_new($mo_user, $ma_group_id, $mo_group_id) {

		global $COURSE, $CFG, $DB;

		if ( $mo_group_id == 'nogroups' && ( $ma_group_id == null || $ma_group_id == "" ) ) {
			return false;
		}

		// if ma_group_id is null, mahara group does not exist : create it and add the user to this new mahara group
		// else, check if user is member of it's mahara group or add him to this mahara group
		if ( $ma_group_id == null || $ma_group_id == "" ) {
			$mo_user_groups = groups_get_user_groups($COURSE->id, $mo_user->id);
			// $mo_user_group_id = $mo_user_groups[0][0];
			$mo_user_group_id = $mo_group_id;
			$mo_group_name = groups_get_group_name($mo_user_group_id);

			$group = new stdClass();
			$group->name = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
			$group->description = get_string('course_group', 'block_mahara_iena') . " " . $COURSE->shortname . " – " . $mo_group_name;
			$group->grouptype = 'course';
			$group->request = true;
			$group->public = false;
			$group->institution = $CFG->instution_mahara;
			$user = new stdClass();
			$user->id = $this->get_mahara_user_by_mail($mo_user->email)[0]->id;
			$user->username = $mo_user->username;
			$user->role = $this->get_user_role($mo_user->id);
			$group->members = array(
				0 => $user
			);
			$params = array (
				'groups' =>
				array (
					0 => $group
				)
			);
			$params = http_build_query($params);
			$url = $this->create_url("mahara_group_create_groups");
			$res = json_decode($this->httpPost($params, $url));
Thomas Fradet's avatar
v3.7  
Thomas Fradet committed
361
			if ( isset($res->error) ) {
Thomas Fradet's avatar
Thomas Fradet committed
362 363 364 365 366 367 368
				return false;
			} else {
				$record = new stdClass();
				$record->course = $COURSE->id;
				$record->mahara_group_id = $res[0]->id;
				$record->moodle_group_id = $mo_user_group_id;
				$DB->insert_records('block_mahara_iena', array( 0 => $record ), false);
Thomas Fradet's avatar
Thomas Fradet committed
369
				return $res[0]->id;
Thomas Fradet's avatar
Thomas Fradet committed
370 371 372 373 374 375
			}

		} else {
			$ma_group_members = $this->get_mahara_users_in_mahara_group_by_id($ma_group_id);
			$is_in_group = false;
			foreach ($ma_group_members as $member) {
Thomas Fradet's avatar
v3.7  
Thomas Fradet committed
376
				if ( $mo_user->email == $this->get_mahara_user_by_id($member['id'])[0]->email ) {
Thomas Fradet's avatar
Thomas Fradet committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
					$is_in_group = true;
					break;
				}
			}
			if ( ! $is_in_group ) {
				$ma_user = $this->get_mahara_user_by_mail($mo_user->email);
				return $this->mahara_group_update_group_members($ma_user[0]->id, $ma_group_id, $mo_user->id);
			} else {
				return true;
			}
		}
	}




}