PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/editreport.php

https://github.com/agrowe/moodle-block_configurablereports
PHP | 261 lines | 181 code | 57 blank | 23 comment | 46 complexity | 6954e9ced811a5285118ebe6adc2ad49 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /** Configurable Reports
  17. * A Moodle block for creating Configurable Reports
  18. * @package blocks
  19. * @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
  20. * @date: 2009
  21. */
  22. require_once("../../config.php");
  23. require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
  24. $id = optional_param('id', 0,PARAM_INT);
  25. $courseid = optional_param('courseid',SITEID,PARAM_INT);
  26. $delete = optional_param('delete', 0,PARAM_BOOL);
  27. $confirm = optional_param('confirm', 0,PARAM_BOOL);
  28. $show = optional_param('show', 0,PARAM_BOOL);
  29. $hide = optional_param('hide', 0,PARAM_BOOL);
  30. $duplicate = optional_param('duplicate', 0,PARAM_BOOL);
  31. $report = null;
  32. if (! $course = $DB->get_record("course",array( "id" => $courseid)) ) {
  33. print_error("nosuchcourseid",'block_configurable_reports');
  34. }
  35. // Force user login in course (SITE or Course)
  36. if ($course->id == SITEID){
  37. require_login();
  38. $context = context_system::instance();
  39. } else {
  40. require_login($course->id);
  41. $context = context_course::instance($course->id);
  42. }
  43. if(! has_capability('block/configurable_reports:managereports', $context) && ! has_capability('block/configurable_reports:manageownreports', $context))
  44. print_error('badpermissions','block_configurable_reports');
  45. $PAGE->set_context($context);
  46. $PAGE->set_pagelayout('incourse');
  47. if($id){
  48. if(! $report = $DB->get_record('block_configurable_reports',array('id' => $id)))
  49. print_error('reportdoesnotexists','block_configurable_reports');
  50. if(! has_capability('block/configurable_reports:managereports', $context) && $report->ownerid != $USER->id)
  51. print_error('badpermissions','block_configurable_reports');
  52. $title = format_string($report->name);
  53. $courseid = $report->courseid;
  54. if (! $course = $DB->get_record("course",array( "id" => $courseid)) ) {
  55. print_error("nosuchcourseid",'block_configurable_reports');
  56. }
  57. require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
  58. require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
  59. $reportclassname = 'report_'.$report->type;
  60. $reportclass = new $reportclassname($report->id);
  61. $PAGE->set_url('/blocks/configurable_reports/editreport.php', array('id'=>$id));
  62. } else {
  63. $title = get_string('newreport','block_configurable_reports');
  64. $PAGE->set_url('/blocks/configurable_reports/editreport.php', null);
  65. }
  66. if($report)
  67. $title = format_string($report->name);
  68. else
  69. $title = get_string('report','block_configurable_reports');
  70. $courseurl = new moodle_url($CFG->wwwroot.'/course/view.php',array('id'=>$courseid));
  71. $PAGE->navbar->add($course->shortname, $courseurl);
  72. if (!empty($report->courseid)) {
  73. $params = array('courseid'=>$report->courseid);
  74. } else {
  75. $params = array('courseid'=>$courseid);
  76. }
  77. $managereporturl = new moodle_url($CFG->wwwroot.'/blocks/configurable_reports/managereport.php', $params);
  78. $PAGE->navbar->add(get_string('managereports','block_configurable_reports'), $managereporturl);
  79. $PAGE->navbar->add($title);
  80. // Common actions
  81. if(($show || $hide) && confirm_sesskey()){
  82. $visible = ($show)? 1 : 0;
  83. if(!$DB->set_field('block_configurable_reports','visible',$visible,array('id' => $report->id)))
  84. print_error('cannotupdatereport','block_configurable_reports');
  85. $action = ($visible)? 'showed' : 'hidden';
  86. cr_add_to_log($report->courseid, 'configurable_reports', 'report '.$action, '/block/configurable_reports/editreport.php?id='.$report->id, $report->id);
  87. header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
  88. die;
  89. }
  90. if($duplicate && confirm_sesskey()){
  91. $newreport = new stdclass();
  92. $newreport = $report;
  93. unset($newreport->id);
  94. $newreport->name = get_string('copyasnoun').' '.$newreport->name;
  95. $newreport->summary = $newreport->summary;
  96. if(! $newreportid = $DB->insert_record('block_configurable_reports',$newreport))
  97. print_error('cannotduplicate','block_configurable_reports');
  98. cr_add_to_log($newreport->courseid, 'configurable_reports', 'report duplicated', '/block/configurable_reports/editreport.php?id='.$newreportid, $id);
  99. header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
  100. die;
  101. }
  102. if($delete && confirm_sesskey()){
  103. if(!$confirm){
  104. $PAGE->set_title($title);
  105. $PAGE->set_heading( $title);
  106. $PAGE->set_cacheable( true);
  107. echo $OUTPUT->header();
  108. $message = get_string('confirmdeletereport','block_configurable_reports');
  109. $optionsyes = array('id'=>$report->id, 'delete'=>$delete, 'sesskey'=>sesskey(), 'confirm'=>1);
  110. $optionsno = array();
  111. $buttoncontinue = new single_button(new moodle_url('editreport.php', $optionsyes), get_string('yes'), 'get');
  112. $buttoncancel = new single_button(new moodle_url('managereport.php', $optionsno), get_string('no'), 'get');
  113. echo $OUTPUT->confirm($message, $buttoncontinue, $buttoncancel);
  114. echo $OUTPUT->footer();
  115. exit;
  116. }
  117. else{
  118. if($DB->delete_records('block_configurable_reports',array('id'=>$report->id)))
  119. cr_add_to_log($report->courseid, 'configurable_reports', 'report deleted', '/block/configurable_reports/editreport.php?id='.$report->id, $report->id);
  120. header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
  121. die;
  122. }
  123. }
  124. require_once('editreport_form.php');
  125. if(!empty($report))
  126. $editform = new report_edit_form('editreport.php',compact('report','courseid','context'));
  127. else
  128. $editform = new report_edit_form('editreport.php',compact('courseid','context'));
  129. if(!empty($report)){
  130. $export = explode(',',$report->export);
  131. if(!empty($export)){
  132. foreach($export as $e)
  133. $report->{'export_'.$e} = 1;
  134. }
  135. $editform->set_data($report);
  136. }
  137. if($editform->is_cancelled()){
  138. if(!empty($report))
  139. redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php?id='.$report->id);
  140. else
  141. redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php');
  142. }
  143. else if ($data = $editform->get_data()) {
  144. require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
  145. require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$data->type.'/report.class.php');
  146. if(empty($report))
  147. $reportclassname = 'report_'.$data->type;
  148. else
  149. $reportclassname = 'report_'.$report->type;
  150. $arraydata = (array) $data;
  151. $data->export = '';
  152. foreach($arraydata as $key=>$d){
  153. if(strpos($key,'export_') !== false){
  154. $data->export .= str_replace('export_','',$key).',';
  155. }
  156. }
  157. if(!isset($data->global)) {
  158. $data->global = 0;
  159. }
  160. if(!isset($data->jsordering)) {
  161. $data->jsordering = 0;
  162. }
  163. if(!isset($data->remote)) {
  164. $data->remote = 0;
  165. }
  166. if(empty($report)){
  167. $data->ownerid = $USER->id;
  168. $data->courseid = $courseid;
  169. $data->visible = 1;
  170. $data->components = '';
  171. // extra check
  172. if($data->type == 'sql' && !has_capability('block/configurable_reports:managesqlreports',$context))
  173. print_error('nosqlpermissions');
  174. if(! $lastid = $DB->insert_record('block_configurable_reports',$data)){
  175. print_error('errorsavingreport','block_configurable_reports');
  176. }else{
  177. cr_add_to_log($courseid, 'configurable_reports', 'report created', '/block/configurable_reports/editreport.php?id='.$lastid, $data->name);
  178. $reportclass = new $reportclassname($lastid);
  179. redirect($CFG->wwwroot.'/blocks/configurable_reports/editcomp.php?id='.$lastid.'&comp='.$reportclass->components[0]);
  180. }
  181. }
  182. else{
  183. cr_add_to_log($report->courseid, 'configurable_reports', 'edit', '/block/configurable_reports/editreport.php?id='.$id, $report->name);
  184. $reportclass = new $reportclassname($data->id);
  185. $data->type = $report->type;
  186. if(! $DB->update_record('block_configurable_reports',$data)){
  187. print_error('errorsavingreport','block_configurable_reports');
  188. }else{
  189. redirect($CFG->wwwroot.'/blocks/configurable_reports/editcomp.php?id='.$data->id.'&comp='.$reportclass->components[0]);
  190. }
  191. }
  192. }
  193. $PAGE->set_context($context);
  194. $PAGE->set_pagelayout('incourse');
  195. $PAGE->set_title($title);
  196. $PAGE->set_heading( $title);
  197. $PAGE->set_cacheable( true);
  198. echo $OUTPUT->header();
  199. if($id){
  200. $currenttab = 'report';
  201. include('tabs.php');
  202. }
  203. $editform->display();
  204. echo $OUTPUT->footer();