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"; } 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)); } 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); return $res[0]['members']; } 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); if ( isset($is_a_mahara_user->error) ) { 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)); if ( isset($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; } } /** * 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)); if ( isset($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 $res[0]->id; } } 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; } } } }