Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
unifying remove and add forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifieraru committed Feb 14, 2014
1 parent 1671373 commit b229119
Showing 1 changed file with 67 additions and 67 deletions.
134 changes: 67 additions & 67 deletions sites/all/modules/ldap_course_sync/ldap_course_sync.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ function ldap_course_sync_node_view($node, $view_node, $langcode){
if(in_array("Instructor",$roles)){
$node->content["my_additional_field"] = array(
"#weight" => 5,
"#markup" => "<br><br><b>Members:</b><br>".l(t("ADD Students with LDAP"), "ldap-course-add/$course_id")
"#markup" => "<br><br><b>Members:</b><br>".l(t("Synchronize Students with LDAP"), "ldap-course-sync/$course_id")
);

$node->content["my_additional_field2"] = array(
"#weight" => 7,
"#markup" => "<br><br>".l(t("REMOVE Students with LDAP"), "ldap-course-remove/$course_id")
);

}

}
Expand Down Expand Up @@ -134,19 +131,13 @@ function ldap_course_sync_to_change($group_nid){


function ldap_course_sync_menu() {
$items['ldap-course-add/%'] = array(
'title' => 'LDAP Course Add Members',
$items['ldap-course-sync/%'] = array(
'title' => 'LDAP Course Synchronize Members',
'page callback' => 'drupal_get_form',
'page arguments' => array('ldap_course_sync_form', 1),
'access callback' => TRUE,
);
$items['ldap-course-remove/%'] = array(
'title' => 'LDAP Course Remove Members',
'page callback' => 'drupal_get_form',
'page arguments' => array('ldap_course_sync_form2', 1),
'access callback' => TRUE,
);



return $items;
}
Expand All @@ -162,9 +153,12 @@ function ldap_course_sync_form($form,&$form_submit, $variable){


if(in_array("Instructor",$roles)){

// This part of the form will handle adding members via LDAP

$members_array = ldap_course_sync_to_change($course_id);
$members = $members_array[0];
//dpm($members_array);


$memb_options = array();
foreach($members as $key=>$value){
Expand All @@ -173,7 +167,7 @@ function ldap_course_sync_form($form,&$form_submit, $variable){


$form['members_to_add'] = array(
'#title' => t($course_name),
'#title' => t("Add students to ".$course_name),
'#type' => 'checkboxes',
'#options' => $memb_options,
);
Expand All @@ -183,56 +177,19 @@ function ldap_course_sync_form($form,&$form_submit, $variable){
"#value" => $course_id,
);

$form['submit'] = array(
'#value' => 'Add to course',
$form['submit_add'] = array(
'#value' => 'Add',
'#type' => 'submit',
);
return $form;
}

}


function ldap_course_sync_form_submit($form, &$form_state){
$course_id = intval($form_state['values']['hid']);
$options = $form_state['values']['members_to_add'];
foreach($options as $key=>$value){
if((is_string($value)) && ($key==$value) ){
$selected_user = user_load_by_name($key);

// Add user to course
og_group("node", $course_id, array("entity_type" => "user", "entity" => $selected_user));

// Add Student role to user
$my_rids = get_role_by_name_in_gryoup("Student", "course_group");
foreach ($my_rids AS $r){
og_role_grant("node", $course_id, $selected_user->uid, $r->rid);
}

}
}

}







function ldap_course_sync_form2($form,&$form_submit, $variable){

global $user;
$course_id = $variable;
$roles = og_get_user_roles("node", $course_id, $user->uid, true);
$course = node_load($course_id);
$course_name = $course->title;

// This part of the form will handle removing members who are not in LDAP

if(in_array("Instructor",$roles)){
$members_array = ldap_course_sync_to_change($course_id);
$members_array = ldap_course_sync_to_change($course_id);
$members = $members_array[1];
//dpm($members_array);

$memb_options = array();
foreach($members as $key=>$value){
Expand All @@ -241,7 +198,7 @@ function ldap_course_sync_form2($form,&$form_submit, $variable){


$form['members_to_remove'] = array(
'#title' => t($course_name),
'#title' => t("Remove students from ".$course_name),
'#type' => 'checkboxes',
'#options' => $memb_options,
);
Expand All @@ -251,28 +208,71 @@ function ldap_course_sync_form2($form,&$form_submit, $variable){
"#value" => $course_id,
);

$form['submit'] = array(
'#value' => 'Remove from course',
$form['submit_remove'] = array(
'#value' => 'Remove',
'#type' => 'submit',
);





return $form;
}

}


function ldap_course_sync_form2_submit($form, &$form_state){
function ldap_course_sync_form_submit($form, &$form_state){
$course_id = intval($form_state['values']['hid']);
$options = $form_state['values']['members_to_remove'];
foreach($options as $key=>$value){
if((is_string($value)) && ($key==$value) ){
$selected_user = user_load_by_name($key);
// Remove user from course
og_ungroup("node", $course_id, 'user', $selected_user->uid);
//dpm($form_state);

// ADD case
if($form_state["values"]["op"] == "Add"){

$options = $form_state['values']['members_to_add'];
foreach($options as $key=>$value){
if((is_string($value)) && ($key==$value) ){
$selected_user = user_load_by_name($key);

// Add user to course
og_group("node", $course_id, array("entity_type" => "user", "entity" => $selected_user));

// Add Student role to user
$my_rids = get_role_by_name_in_gryoup("Student", "course_group");
foreach ($my_rids AS $r){
og_role_grant("node", $course_id, $selected_user->uid, $r->rid);
}

}
}


}


// REMOVE case

else{
$options = $form_state['values']['members_to_remove'];
foreach($options as $key=>$value){
if((is_string($value)) && ($key==$value) ){
$selected_user = user_load_by_name($key);
// Remove user from course
og_ungroup("node", $course_id, 'user', $selected_user->uid);
}
}

}





}





?>

0 comments on commit b229119

Please sign in to comment.