ajax.php
4.5 KB
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
59
60
61
62
63
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
91
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
<?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();
}