Commit f4921dae authored by Thomas Fradet's avatar Thomas Fradet

fixed perf

parent a119c682
...@@ -60,11 +60,14 @@ class block_mahara_iena extends block_base ...@@ -60,11 +60,14 @@ class block_mahara_iena extends block_base
// for teachers // for teachers
if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id)) { if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id)) {
// if block is not instanciated, propose du create mahara group(s) for this course
if (count($result) == 0 && $is_mahara_user) { if (count($result) == 0 && $is_mahara_user) {
// create one mahara group for the course
$this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/mahara_iena/mahara_iena.php?courseid=' $this->content->text .= '<a href="' . $CFG->wwwroot . '/blocks/mahara_iena/mahara_iena.php?courseid='
. $COURSE->id . '" class="btn btn-block btn-primary iena-btn-big">' . get_string('create_group', 'block_mahara_iena', $CFG->mahara_alias) . '</a><p class="text-muted">Créer un groupe dans '.$CFG->mahara_alias.'.</p>'; . $COURSE->id . '" class="btn btn-block btn-primary iena-btn-big">' . get_string('create_group', 'block_mahara_iena', $CFG->mahara_alias) . '</a><p class="text-muted">Créer un groupe dans '.$CFG->mahara_alias.'.</p>';
// create one mahara group for each moodle group (need groups)
$groups = groups_get_all_groups($COURSE->id); $groups = groups_get_all_groups($COURSE->id);
if ( $groups == null) { if ( $groups == null) {
$this->content->text .= '<button type="button" class="btn btn-block btn-primary iena-btn-big" disabled >' . get_string('create_groups_groups', 'block_mahara_iena', $CFG->mahara_alias) . '</button><p class="text-muted text-warning">Créer un groupe dans '.$CFG->mahara_alias.' pour chaque groupe du cours. <strong>Les groupes doivent exister dans le cours.</strong></p>'; $this->content->text .= '<button type="button" class="btn btn-block btn-primary iena-btn-big" disabled >' . get_string('create_groups_groups', 'block_mahara_iena', $CFG->mahara_alias) . '</button><p class="text-muted text-warning">Créer un groupe dans '.$CFG->mahara_alias.' pour chaque groupe du cours. <strong>Les groupes doivent exister dans le cours.</strong></p>';
......
...@@ -31,7 +31,7 @@ $course_ctx = context_course::instance($course->id); ...@@ -31,7 +31,7 @@ $course_ctx = context_course::instance($course->id);
$students = get_enrolled_users($course_ctx); $students = get_enrolled_users($course_ctx);
$usersTab = array(); $usersTab = array();
$maharaUsers = $connexion->getMaharaUsers();
//Here we check by email, if the student have the same email into mahara and moodle he is add to group //Here we check by email, if the student have the same email into mahara and moodle he is add to group
if (count($students) == 0) { if (count($students) == 0) {
...@@ -39,22 +39,41 @@ if (count($students) == 0) { ...@@ -39,22 +39,41 @@ if (count($students) == 0) {
.get_string('err_no_stud', 'block_mahara_iena', $CFG->mahara_alias) .get_string('err_no_stud', 'block_mahara_iena', $CFG->mahara_alias)
."</div>"; ."</div>";
} else { } else {
foreach ($maharaUsers->users as $muser) {
foreach ($students as $student) { // Build user array to pass to mahara api for group inscription
if ($student->email == $muser->email){ // WARNING : not efficient with to much users in mahara databases
$role = "member"; // $maharaUsers = $connexion->getMaharaUsers();
//Each students is admin // foreach ($maharaUsers->users as $muser) {
if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $student->id)) { // foreach ($students as $student) {
$role = "admin"; // if ($student->email == $muser->email){
} // $role = "member";
array_push($usersTab,array( // // Each teacher will be mahara group admin
'id' => $muser->id, // if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $student->id)) {
'username' => $muser->username, // $role = "admin";
'role' => $role, // }
)); // array_push($usersTab,array(
break; // 'id' => $muser->id,
} // 'username' => $muser->username,
} // 'role' => $role,
// ));
// break;
// }
// }
// }
// WARNING : don't try to subscribe every course user to the group with >1000 users in mahara. Juste teacher. Minus 1 user is required to create a group.
// get mahara user by mail (teacher)
$mahara_user = $connexion->get_mahara_user_by_mail($USER->email);
// if user is not a mahara user, there will be an error : it's not supposed to be because mahara account is checked before to have a button to go in this page but... but.
if ( !$mahara_user->error ) {
array_push($usersTab,array(
'id' => $mahara_user[0]->id,
'username' => $mahara_user[0]->username,
'role' => "admin",
));
} else {
echo "<p style='color: red;'>You must have a Mahara account to do that.</p>";
die;
} }
//All params used for mahara_group_create_groups her we can add/change some lines //All params used for mahara_group_create_groups her we can add/change some lines
...@@ -69,8 +88,7 @@ if (count($students) == 0) { ...@@ -69,8 +88,7 @@ if (count($students) == 0) {
'request' => true, 'request' => true,
'public' => false, 'public' => false,
'institution' => $CFG->instution_mahara, 'institution' => $CFG->instution_mahara,
'members' => 'members' => $usersTab
$usersTab
) )
) )
); );
......
...@@ -52,24 +52,39 @@ if (count($students) == 0) { ...@@ -52,24 +52,39 @@ if (count($students) == 0) {
foreach ($members as $member) { foreach ($members as $member) {
$course_roles = get_user_roles($course_ctx, $member->id, false); // WARNING : not efficient with too big mahara user database (>1000)
$role = "member"; // $course_roles = get_user_roles($course_ctx, $member->id, false);
foreach ($course_roles as $course_role) { // $role = "member";
if ($course_role->shortname == "teacher" || $course_role->shortname == "editingteacher") { // foreach ($course_roles as $course_role) {
$role = "admin"; // if ($course_role->shortname == "teacher" || $course_role->shortname == "editingteacher") {
break; // $role = "admin";
} // break;
} // }
// }
foreach ($maharaUsers->users as $muser) { // foreach ($maharaUsers->users as $muser) {
if ($member->email == $muser->email){ // if ($member->email == $muser->email){
array_push($usersTab,array( // array_push($usersTab,array(
'id' => $muser->id, // 'id' => $muser->id,
'username' => $muser->username, // 'username' => $muser->username,
'role' => $role, // 'role' => $role,
)); // ));
break; // break;
} // }
// }
// Subscribe only the teacher to the mahara group because of lake of efficiency with too big mahara user database (>1000)
// get mahara user by mail (teacher)
$mahara_user = $connexion->get_mahara_user_by_mail($USER->email);
// if user is not a mahara user, there will be an error : it's not supposed to be because mahara account is checked before to have a button to go in this page but... but.
if ( !$mahara_user->error ) {
array_push($usersTab,array(
'id' => $mahara_user[0]->id,
'username' => $mahara_user[0]->username,
'role' => "admin",
));
} else {
echo "<p style='color: red;'>You must have a Mahara account to do that.</p>";
die;
} }
} }
...@@ -85,8 +100,7 @@ if (count($students) == 0) { ...@@ -85,8 +100,7 @@ if (count($students) == 0) {
'request' => true, 'request' => true,
'public' => false, 'public' => false,
'institution' => $CFG->instution_mahara, 'institution' => $CFG->instution_mahara,
'members' => 'members' => $usersTab
$usersTab
)); ));
array_push($name_groups, array( array_push($name_groups, array(
...@@ -98,7 +112,7 @@ if (count($students) == 0) { ...@@ -98,7 +112,7 @@ if (count($students) == 0) {
// var_dump($name_groups); // var_dump($name_groups);
// die; // die;
if ( count($mahara_groups) == 0 ) { if ( count($mahara_groups) == 0 ) {
echo "<div class=\"alert alert-warning\" role=\"alert\"><p><strong>Erreur : tous les groupes du cours sont vides. </strong></p><p>Pour utiliser cette fonctionnalité, il faut au préalable créer des groupes dans le cours et y ajouter des étudiants ou des enseignants.</p></div>"; echo "<div class=\"alert alert-warning\" role=\"alert\"><p><strong>Erreur : tous les groupes du cours sont vides. </strong></p><p>Pour utiliser cette fonctionnalité, il faut au préalable créer des groupes dans le cours et y ajouter des étudiants ou des enseignants. prout</p></div>";
} else { } else {
$params = array( $params = array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment