ajax.php 4.5 KB
<?php
// 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/>.

/*
 * Handling all ajax request of mandatory activities 
 *
 * @package local_mandatoryactivities
 * @author logesh<logesh@teknoturf.com>
 */

define ( 'AJAX_SCRIPT', true );
define ( 'NO_DEBUG_DISPLAY', true );

require_once (dirname ( __FILE__ ) . '/../../config.php');
$action = optional_param ( 'action', '', PARAM_RAW );
$cmids = optional_param_array ( 'cmids', null, PARAM_RAW );
$course = optional_param ( 'course', 0, PARAM_RAW );


Global $DB, $CFG, $USER;

switch ($action) {
	
	case 'savemandatoryids':
		
		$madatorycmids= implode(', ', $cmids);
		
		$sql="select * from {local_mandatory_activities} where course=?";
		$mandatoryactivities = $DB->get_record_sql($sql,array($course));
		
		if (isset($mandatoryactivities->id)){
			
			$instance = new stdClass();
			$instance->id=$mandatoryactivities->id;
			$instance->course  = $course;
			$instance->cmid  = $madatorycmids;
			$instance->mandatory  = $mandatoryactivities->mandatory;
			
			$DB->update_record('local_mandatory_activities', $instance);
			
		}else{
			
			$instance = new stdClass();
			$instance->course  = $course;
			$instance->cmid  = $madatorycmids;
			$instance->mandatory  = 1;
			
			$DB->insert_record('local_mandatory_activities', $instance);
		}
		
		echo json_encode(true);
		die();
		
	case 'getcomparision':
		
		$checkedids = optional_param_array( 'checked', null, PARAM_RAW );
		$mandatoryids = optional_param( 'mandatory', '', PARAM_RAW );
		
		$newlyaddedids=array();
		$newlyaddedactivities=array();
		$removedids=array();
		$removedactivities=array();
		$unchangedids=array();
		$unchangedactivities=array();
		
		$sql="select mcm.id,mm.name,mcm.instance from mdl_course_modules mcm join mdl_modules mm on mm.id=mcm.module where mcm.course=$course and mcm.visible=1 and mcm.deletioninprogress=0";
		$qresult=$DB->get_records_sql($sql);

		$allcmids=array();
		foreach ($qresult as $value){
				$allcmids[]=$value->id;
		}
		
		if ($mandatoryids && $checkedids){
			
			$previousestatus=explode(",",$mandatoryids);
			foreach($checkedids as $id){
				//iterate to get newly added id's
				if (in_array($id, $previousestatus)){
					//
				}else{
					if (isset($qresult[$id])){
					$id=number_format($id);
					$modaname=$qresult[$id]->name;
					$instance=$qresult[$id]->instance;
					
					$sqlactivityname="select name from mdl_$modaname where id=$instance";
					$aname=$DB->get_record_sql($sqlactivityname);
					
					$newlyaddedactivities[]=$aname;
					$newlyaddedids[]=$id;
					}
				}
			}
			
			//iterate to get removed id's
			foreach($previousestatus as $id1){
				if (in_array($id1, $checkedids)){
				//
				}else{
					if (isset($qresult[$id1])){
					$id=number_format($id1);
					$modaname=$qresult[$id1]->name;
					$instance=$qresult[$id1]->instance;
					
					$sqlactivityname="select name from mdl_$modaname where id=$instance";
					$aname=$DB->get_record_sql($sqlactivityname);
					
					$removedactivities[]=$aname;
					$removedids[]=$id;
					}
				}
			}
			
			$res=array_diff($allcmids,$newlyaddedids);
			$unchangedids=array_diff($res,$removedids);
			
			foreach ($unchangedids as $data){
				if (isset($qresult[$data])){
				$id=number_format($data);
				$modaname=$qresult[$data]->name;
				$instance=$qresult[$data]->instance;
					
				$sqlactivityname="select name from mdl_$modaname where id=$instance";
				$aname=$DB->get_record_sql($sqlactivityname);
					
				$unchangedactivities[]=$aname;
				}
			}
		}else{
			
			foreach ($qresult as $val){
				$modaname=$val->name;
				$instance=$val->instance;
					
				$sqlactivityname="select name from mdl_$modaname where id=$instance";
				$aname=$DB->get_record_sql($sqlactivityname);
					
				$unchangedactivities[]=$aname;
			}
		}
		
		$data = array();
		$data['removed'] = $removedactivities;
		$data['newlyadded'] = $newlyaddedactivities;
		$data['unchanged'] = $unchangedactivities;
		echo json_encode($data);
		
		die();
}