PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/tracker/views/view.controller.php

https://github.com/nadavkav/MoodleTAO
PHP | 289 lines | 223 code | 28 blank | 38 comment | 52 complexity | d4478a6c98e801422e9f634c59c804bf MD5 | raw file
  1. <?php
  2. /**
  3. * @package mod-tracker
  4. * @category mod
  5. * @author Valery Fremaux
  6. * @date 02/12/2007
  7. *
  8. * Controller for all "view" related views
  9. */
  10. if (!defined('MOODLE_INTERNAL')) {
  11. die('Direct access to this script is forbidden.'); /// It must be included from view.php in mod/tracker
  12. }
  13. /************************************* Submit an issue *****************************/
  14. if ($action == 'submitanissue'){
  15. if (!$issue = tracker_submitanissue($tracker->id)){
  16. error("Bad issue id");
  17. }
  18. print_simple_box_start('center', '80%', '', '', 'generalbox', 'bugreport');
  19. print_string('thanks', 'tracker');
  20. print_simple_box_end();
  21. print_continue("view.php?id={$cm->id}view=view&amp;page=browse");
  22. // notify all admins
  23. if ($tracker->allownotifications){
  24. tracker_notify_submission($issue, $cm, $tracker);
  25. }
  26. return -1;
  27. }
  28. /************************************* update an issue *****************************/
  29. elseif ($action == 'updateanissue'){
  30. $issue->id = required_param('issueid', PARAM_INT);
  31. $issue->status = required_param('status', PARAM_INT);
  32. $issue->assignedto = required_param('assignedto', PARAM_INT);
  33. $issue->summary = required_param('summary', PARAM_TEXT);
  34. $issue->description = addslashes(required_param('description', PARAM_CLEANHTML));
  35. $issue->format = required_param('format', PARAM_INT);
  36. $issue->datereported = required_param('datereported', PARAM_INT);
  37. $issue->resolution = addslashes(required_param('resolution', PARAM_CLEANHTML));
  38. $issue->resolutionformat = required_param('resolutionformat', PARAM_INT);
  39. $issue->trackerid = $tracker->id;
  40. if (!empty($issue->resolution) && $issue->status < RESOLVED) $issue->status = RESOLVED;
  41. // if ownership has changed, prepare logging
  42. $oldrecord = get_record('tracker_issue', 'id', $issue->id);
  43. if ($oldrecord->assignedto != $issue->assignedto){
  44. $ownership->trackerid = $tracker->id;
  45. $ownership->issueid = $oldrecord->id;
  46. $ownership->userid = $oldrecord->assignedto;
  47. $ownership->bywhomid = $oldrecord->bywhomid;
  48. $ownership->timeassigned = ($oldrecord->timeassigned) ? $oldrecord->timeassigned : time();
  49. if (!insert_record('tracker_issueownership', $ownership)){
  50. error ("Could not log old ownership");
  51. }
  52. }
  53. $issue->assignedto = required_param('assignedto', PARAM_INT);
  54. $issue->bywhomid = $USER->id;
  55. $issue->timeassigned = time();
  56. if (!update_record('tracker_issue', $issue)){
  57. error ("Could not update tracker issue");
  58. }
  59. /// send state change notification
  60. if ($oldrecord->status != $issue->status){
  61. tracker_notifyccs_changestate($issue->id, $tracker);
  62. }
  63. tracker_clearelements($issue->id);
  64. tracker_recordelements($issue);
  65. // TODO : process dependancies
  66. $dependancies = optional_param('dependancies', null, PARAM_INT);
  67. if (is_array($dependancies)){
  68. // cleanup previous depdendancies
  69. if (!delete_records('tracker_issuedependancy', 'childid', $issue->id)){
  70. error ("Could not delete old dependancies");
  71. }
  72. // install back new one
  73. foreach($dependancies as $dependancy){
  74. $dependancyrec->trackerid = $tracker->id;
  75. $dependancyrec->parentid = $dependancy;
  76. $dependancyrec->childid = $issue->id;
  77. $dependancyrec->comment = '';
  78. if (!insert_record('tracker_issuedependancy', $dependancyrec)){
  79. error ("Could not write dependancy record");
  80. }
  81. }
  82. }
  83. }
  84. /************************************* delete an issue record *****************************/
  85. elseif ($action == 'delete'){
  86. $issueid = required_param('issueid', PARAM_INT);
  87. delete_records('tracker_issue', 'id', $issueid);
  88. delete_records('tracker_issuedependancy', 'childid', $issueid);
  89. delete_records('tracker_issuedependancy', 'parentid', $issueid);
  90. delete_records('tracker_issueattribute', 'issueid', $issueid);
  91. delete_records('tracker_issuecomment', 'issueid', $issueid);
  92. delete_records('tracker_issueownership', 'issueid', $issueid);
  93. // todo : send notification to all cced
  94. delete_records('tracker_issuecc', 'issueid', $issueid);
  95. }
  96. /************************************* updating list and status *****************************/
  97. elseif ($action == 'updatelist'){
  98. $keys = array_keys($_POST); // get the key value of all the fields submitted
  99. $statuskeys = preg_grep('/status./' , $keys); // filter out only the status
  100. $assignedtokeys = preg_grep('/assignedto./' , $keys); // filter out only the assigned updating
  101. $newassignedtokeys = preg_grep('/assignedtoi./' , $keys); // filter out only the new assigned
  102. foreach($statuskeys as $akey){
  103. $issueid = str_replace('status', '', $akey);
  104. $haschanged = optional_param('schanged'.$issueid, 0, PARAM_INT);
  105. if ($haschanged){
  106. $issue->id = $issueid;
  107. $issue->status = required_param($akey, PARAM_INT);
  108. $oldstatus = get_field('tracker_issue', 'status', 'id', $issue->id);
  109. update_record('tracker_issue', $issue);
  110. /// check status changing and send notifications
  111. if ($oldstatus != $issue->status){
  112. if ($tracker->allownotifications){
  113. tracker_notifyccs_changestate($issue->id, $tracker);
  114. }
  115. }
  116. }
  117. }
  118. // always add a record for history
  119. foreach($assignedtokeys as $akey){
  120. $issueid = str_replace('assignedto', '', $akey);
  121. // new ownership is triggered only when a change occured
  122. $haschanged = optional_param('changed'.$issueid, 0, PARAM_INT);
  123. if ($haschanged){
  124. // save old assignement in history
  125. $oldassign = get_record('tracker_issue', 'id', $issueid);
  126. if ($oldassign->assignedto != 0){
  127. $ownership->trackerid = $tracker->id;
  128. $ownership->issueid = $issueid;
  129. $ownership->userid = $oldassign->assignedto;
  130. $ownership->bywhomid = $oldassign->bywhomid;
  131. $ownership->timeassigned = $oldassign->timeassigned;
  132. if (!insert_record('tracker_issueownership', $ownership)){
  133. notice ("Error saving ownership for issue $issueid");
  134. }
  135. }
  136. // update actual assignement
  137. $issue->id = $issueid;
  138. $issue->bywhomid = $USER->id;
  139. $issue->timeassigned = time();
  140. $issue->assignedto = required_param($akey, PARAM_INT);
  141. if (!update_record('tracker_issue', $issue)){
  142. notice ("Error updating assignation for issue $issueid");
  143. }
  144. if ($tracker->allownotifications){
  145. tracker_notifyccs_changeownership($issue->id, $tracker);
  146. }
  147. }
  148. }
  149. }
  150. /********************************* requires the add a comment form **************************/
  151. elseif ($action == 'addacomment'){
  152. $form->issueid = required_param('issueid', PARAM_INT);
  153. include "views/addacomment.html";
  154. return -1;
  155. }
  156. /***************************************** add a comment ***********************************/
  157. elseif ($action == 'doaddcomment'){
  158. $issueid = required_param('issueid', PARAM_INT);
  159. $comment->comment = addslashes(required_param('comment', PARAM_CLEANHTML));
  160. $comment->commentformat = required_param('commentformat', PARAM_INT);
  161. $comment->userid = $USER->id;
  162. $comment->trackerid = $tracker->id;
  163. $comment->issueid = $issueid;
  164. $comment->datecreated = time();
  165. if (!insert_record('tracker_issuecomment', $comment)){
  166. error ("Error writing comment");
  167. }
  168. }
  169. /************************************ reactivates a stored search *****************************/
  170. elseif($action == 'usequery'){
  171. $queryid = required_param('queryid', PARAM_INT);
  172. $fields = tracker_extractsearchparametersfromdb($queryid);
  173. }
  174. /******************************* unregister administratively a user *****************************/
  175. elseif ($action == 'unregister'){
  176. $issueid = required_param('issueid', PARAM_INT);
  177. $ccid = required_param('ccid', PARAM_INT);
  178. if (!delete_records ('tracker_issuecc', 'trackerid', $tracker->id, 'issueid', $issueid, 'userid', $ccid)){
  179. error ("Cannot delete carbon copy {$tracker->ticketprefix}{$issueid} for user : " . $ccid);
  180. }
  181. }
  182. elseif ($action == 'register'){
  183. $issueid = required_param('issueid', PARAM_INT);
  184. $ccid = required_param('ccid', PARAM_INT);
  185. if (!get_record('tracker_issuecc', 'trackerid', $tracker->id, 'issueid', $issueid, 'userid', $ccid)){
  186. $cc->trackerid = $tracker->id;
  187. $cc->issueid = $issueid;
  188. $cc->userid = $ccid;
  189. $cc->events = 31 ;
  190. insert_record('tracker_issuecc', $cc);
  191. }
  192. }
  193. /******************************* copy an issue to a parent tracker *****************************/
  194. elseif ($action == 'cascade'){
  195. global $USER;
  196. $issueid = required_param('issueid', PARAM_INT);
  197. $issue = get_record('tracker_issue', 'id', $issueid);
  198. $attributes = get_records('tracker_issueattribute', 'issueid', $issue->id);
  199. // remaps elementid to elementname for
  200. tracker_loadelementsused($tracker->id, $used);
  201. if (!empty($attributes)){
  202. foreach(array_keys($attributes) as $attkey){
  203. $attributes[$attkey]->elementname = $used[$attributes[$attkey]->id]->name;
  204. }
  205. }
  206. $issue->attributes = $attributes;
  207. // We get comments and make a single backtrack. There should not
  208. // be usefull to bring along full user profile. We just want not
  209. // to loose usefull information the previous track collected.
  210. $comments = get_records('tracker_issuecomment', 'issueid', $issue->id);
  211. $track = '';
  212. if (!empty($comments)){
  213. // collect userids
  214. foreach($comments as $comment){
  215. $useridsarray[] = $comment->userid;
  216. }
  217. $idlist = implode("','", $useridsarray);
  218. $users = get_records_select('user', "id IN ('$idlist')", '', 'id, firstname, lastname');
  219. // make backtrack
  220. foreach($comments as $comment){
  221. $track .= get_string('commentedby', 'tracker').fullname($users[$comment->userid]).get_string('on', 'tracker').userdate($comment->datecreated);
  222. $track .= '<br/>';
  223. $track .= format_text($comment->comment, $comment->format);
  224. $track .= '<hr width="60%"/>';
  225. }
  226. }
  227. $issue->comment = $track;
  228. include_once($CFG->dirroot."/mod/tracker/rpclib.php");
  229. if (is_numeric($tracker->parent)){
  230. // tracker is local, use the rpc entry point anyway
  231. $result = tracker_rpc_post_issue($tracker->parent, $USER->id, json_encode($issue));
  232. } else {
  233. // tracker is remote, make an RPC call
  234. list($remoteid, $mnet_host) = explode('@', $tracker->parent);
  235. // get network tracker properties
  236. include_once $CFG->dirroot."/mnet/xmlrpc/client.php";
  237. $rpcclient = new mnet_xmlrpc_client();
  238. $rpcclient->set_method('mod/tracker/rpclib.php/tracker_rpc_post_issue');
  239. $rpcclient->add_param($USER->username, 'string');
  240. $rpcclient->add_param($CFG->wwwroot, 'string');
  241. $rpcclient->add_param($remoteid, 'int');
  242. $rpcclient->add_param(json_encode($issue), 'string');
  243. $parent_mnet = new mnet_peer();
  244. $parent_mnet->set_wwwroot($mnet_host);
  245. $result = $rpcclient->send($parent_mnet);
  246. }
  247. if ($result){
  248. if ($rpcclient->response['status'] == RPC_SUCCESS){
  249. $issue->status = TRANSFERED;
  250. $issue->followid = $rpcclient->response['followid'];
  251. if (!update_record('tracker_issue', $issue)){
  252. error ("Could not update issue for cascade");
  253. }
  254. } else {
  255. error ("Error on remote side<br/>".$result->error);
  256. }
  257. } else {
  258. error ("Error on sending cascade :<br/>".implode('<br/>', $rpcclient->error));
  259. }
  260. }
  261. ?>