currency.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * Defines a class to render a view of SLOODLE course information.
  5. * Class is inherited from the base view class.
  6. *
  7. * @package sloodle
  8. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10. *
  11. * @contributor Paul Preibisch
  12. * @contributor Edmund Edgar
  13. *
  14. */
  15. define('SLOODLE_ALL_CURRENCIES_VIEW', 1);
  16. /** The base view class */
  17. require_once(SLOODLE_DIRROOT.'/view/base/base_view.php');
  18. /** SLOODLE logs data structure */
  19. require_once(SLOODLE_LIBROOT.'/course.php');
  20. require_once(SLOODLE_LIBROOT.'/currency.php');
  21. /**
  22. * Class for rendering a view of SLOODLE course information.
  23. * @package sloodle
  24. */
  25. class sloodle_view_currency extends sloodle_base_view {
  26. /**
  27. * The Moodle course object, retrieved directly from database.
  28. * @var object
  29. * @access private
  30. */
  31. var $course = 0;
  32. var $can_edit = false;
  33. /**
  34. * SLOODLE course object, retrieved directly from database.
  35. * @var object
  36. * @access private
  37. */
  38. var $sloodle_course = null;
  39. var $sloodle_currency = null;
  40. /**
  41. * Constructor.
  42. */
  43. function sloodle_view_currency()
  44. {
  45. }
  46. /**
  47. * Check the request parameters to see which course was specified.
  48. */
  49. function process_request() {
  50. $id = required_param('id', PARAM_INT);
  51. if (!$this->course = sloodle_get_record('course', 'id', $id)) error('Could not find course.');
  52. $this->sloodle_course = new SloodleCourse();
  53. if (!$this->sloodle_course->load($this->course)) error(s(get_string('failedcourseload', 'sloodle')));
  54. }
  55. function process_form() {
  56. //mode is for the different editing tasks of the currency screen (add, modify, delete)
  57. $mode = optional_param('mode', "view", PARAM_TEXT);
  58. if ( ($mode != 'view') && (!$this->can_edit) ) {
  59. print_error('Permission denied');
  60. }
  61. switch($mode) {
  62. case "modify":
  63. //get vars
  64. $currencyid = required_param('currencyid', PARAM_INT);
  65. $currencyname = required_param('currencyname', PARAM_TEXT);
  66. $imageurl = required_param('imageurl', PARAM_URL);
  67. $displayorder = required_param('displayorder', PARAM_INT);
  68. //create update object
  69. $currency = new stdClass();
  70. $currency->id = $currencyid;
  71. $currency->name = $currencyname;
  72. $currency->displayorder = $displayorder;
  73. $currency->imageurl = ($imageurl != "") ? $imageurl : null;
  74. //update
  75. $result = sloodle_update_record('sloodle_currency_types',$currency);
  76. if (!$result) {
  77. $errorlink = $CFG->wwwroot."/mod/sloodle/view.php?_type=currency&id={$id}";
  78. print_error(get_string('general:fail','sloodle'),$errorlink);
  79. }
  80. break;
  81. case "add":
  82. //get vars
  83. $currencyname = required_param('currencyname', PARAM_TEXT);
  84. $imageurl = optional_param('imageurl', '', PARAM_URL);
  85. $displayorder = optional_param('displayorder', 0, PARAM_INT);
  86. //create update object
  87. $currency = new stdClass();
  88. $currency->name=$currencyname;
  89. $currency->displayorder = $displayorder;
  90. $currency->imageurl = $imageurl;
  91. //update
  92. $result = sloodle_insert_record('sloodle_currency_types',$currency);
  93. if (!$result) {
  94. $errorlink = $CFG->wwwroot."/mod/sloodle/view.php?_type=currency&id={$id}";
  95. print_error(get_string('general:fail','sloodle'),$errorlink);
  96. }
  97. break;
  98. case "confirmdelete":
  99. $currencyid= required_param('currencyid', PARAM_INT);
  100. $result = sloodle_delete_records('sloodle_currency_types','id',$currencyid);
  101. if (!$result) {
  102. $errorlink = $CFG->wwwroot."/mod/sloodle/view.php?_type=currency&id={$id}";
  103. print_error(get_string('general:fail','sloodle'),$errorlink);
  104. }
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. /**
  111. * Check that the user is logged-in and has permission to alter course settings.
  112. */
  113. function check_permission() {
  114. // Ensure the user logs in
  115. require_login($this->course->id);
  116. if (isguestuser()) error(get_string('noguestaccess', 'sloodle'));
  117. add_to_log($this->course->id, 'course', 'view sloodle data', '', "{$this->course->id}");
  118. // Ensure the user is allowed to update information on this course
  119. $this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id);
  120. if (has_capability('moodle/course:update', $this->course_context)) $this->can_edit = true;
  121. }
  122. /**
  123. * Print the course settings page header.
  124. */
  125. function sloodle_print_header() {
  126. global $CFG;
  127. $id = required_param('id', PARAM_INT);
  128. $navigation = "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?&_type=currency&mode=allcurrencies&id={$id}\">".get_string('currencies:view', 'sloodle')."</a>";
  129. sloodle_print_header_simple(get_string('backpack','sloodle'), "&nbsp;", $navigation, "", "", true, '', navmenu($this->course));
  130. }
  131. /**
  132. * Render the view of the module or feature.
  133. * This MUST be overridden to provide functionality.
  134. */
  135. function render() {
  136. $view = optional_param('view', "", PARAM_TEXT);
  137. $mode= optional_param('mode', "allcurrencies", PARAM_TEXT);
  138. switch ($mode){
  139. case "allcurrencies":
  140. $this->render_all_currencies();
  141. break;
  142. case "editcurrency":
  143. $this->render_edit_currency();
  144. break;
  145. case "deletecurrency":
  146. $this->delete_currency();
  147. break;
  148. default:
  149. $this->render_all_currencies();
  150. break;
  151. }
  152. }
  153. function render_all_currencies() {
  154. global $CFG;
  155. global $COURSE;
  156. $id = required_param('id', PARAM_INT);
  157. // Display instrutions for this page
  158. echo "<br>";
  159. sloodle_print_box_start('generalbox boxaligncenter center boxheightnarrow leftpara');
  160. echo '<div style="position:relative ">';
  161. echo '<span style="position:relative;font-size:36px;font-weight:bold;">';
  162. echo '<img align="center" src="'.SLOODLE_WWWROOT.'/lib/media/vault48.png" width="48"/>';
  163. echo get_string('currency:currencies', 'sloodle');
  164. echo '</span>';
  165. echo '<span style="float:right;">';
  166. echo '<a style="text-decoration:none" href="'.$CFG->wwwroot.'/mod/sloodle/view.php?_type=backpack&id='.$COURSE->id.'">';
  167. echo get_string('backpacks:viewbackpacks', 'sloodle').'<br>';
  168. echo '<img src="'.SLOODLE_WWWROOT.'/lib/media/returnbackpacks.png"/></a>';
  169. echo '</span>';
  170. echo '</div>';
  171. //create an html table to display the users
  172. $sloodletable = new stdClass();
  173. $sloodletable->head = array(
  174. s(get_string('currencies:displayorder', 'sloodle')),
  175. s(get_string('currencies:icon', 'sloodle')),
  176. s(get_string('currencies:name', 'sloodle')),
  177. ""
  178. );
  179. //set alignment of table cells
  180. $sloodletable->align = array('left','left','left');
  181. $sloodletable->width="95%";
  182. //set size of table cells
  183. $sloodletable->size = array('10%','5%','50%','45%','25%');
  184. //get currencies
  185. //sloodle_get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='')
  186. $currencyTypes = SloodleCurrency::FetchAll();
  187. foreach ($currencyTypes as $c){
  188. $rowData=array();
  189. //cell 1 - display order
  190. $rowData[]= $c->displayorder;
  191. //cell 2 - image icon
  192. if (isset($c->imageurl)){
  193. $rowData[] = '<img src="'.$c->imageurl.'" width ="20px" height="20px">';
  194. } else {
  195. $rowData[] = "";
  196. }
  197. //cell 3 - currency name
  198. $rowData[]= $c->name;
  199. //cell 4 - image url
  200. // $rowData[]= $c->imageurl;
  201. //cell 5 - edit action
  202. $editText= "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?&";
  203. $editText.= "_type=currency";
  204. $editText.= "&currencyid=".$c->id;
  205. $editText.= "&currencyname=".urlencode($c->name);
  206. $editText.= "&mode=editcurrency";
  207. $editText.= "&id={$COURSE->id}";
  208. $editText.= "\">";
  209. $editText.="<img src=\"".SLOODLE_WWWROOT."/lib/media/settings.png\" height=\"32\" width=\"32\" height=\"16\" alt=\"".get_string('currencies:edit', 'sloodle')."\"/> ";
  210. $editText.= "</a>";
  211. $editText.= "&nbsp&nbsp";
  212. $editText.= "<a href=\"".SLOODLE_WWWROOT."/view.php?&";
  213. $editText.= "_type=currency";
  214. $editText.= "&currencyid=".$c->id;
  215. $editText.= "&currencyname=".urlencode($c->name);
  216. $editText.= "&mode=deletecurrency";
  217. $editText.= "&id={$COURSE->id}";
  218. $editText.= "\">";
  219. $editText.="<img src=\"".SLOODLE_WWWROOT."/lib/media/garbage.png\" height=\"32\" width=\"32\" height=\"16\" alt=\"".s(get_string('currencies:delete', 'sloodle'))."\"/> ";
  220. $editText.= "</a>";
  221. $rowData[]= $this->can_edit ? $editText : '&nbsp;';
  222. $sloodletable->data[]=$rowData;
  223. }
  224. sloodle_print_table($sloodletable);
  225. sloodle_print_box_end();
  226. //create an html table to display the users
  227. $sloodletable = new stdClass();
  228. $sloodletable->head = array(
  229. s(get_string('currencies:displayorder', 'sloodle')),
  230. s(get_string('currencies:icon', 'sloodle')),
  231. s(get_string('currencies:name', 'sloodle')),
  232. s(get_string('currencies:imageurl', 'sloodle')),
  233. ""
  234. );
  235. //set alignment of table cells
  236. $sloodletable->align = array('left','left','left');
  237. $sloodletable->width="55%";
  238. //set size of table cells
  239. $sloodletable->size = array('10%','5%','50%','45%','25%');
  240. if ($this->can_edit) {
  241. print('<form action="" method="POST">');
  242. //create cells for add row
  243. $cells = array();
  244. //cell 1 -display order
  245. $cells[]='<input type="hidden" name="currencyid" value="null">
  246. <input type="hidden" name="mode" value="add">
  247. <input type="hidden" name="id" value="'.$id.'">
  248. <input type="text" name="displayorder" size="2" value="0">';
  249. //cell 2 - icon - blank
  250. $cells[]="";
  251. //cell 3 - name
  252. $cells[]='<input type="text" name="currencyname" size="30" value="">';
  253. //cell 4 - imageurl
  254. $cells[]='<input type="text" size="100" name="imageurl" value="">';
  255. //cell 5- add
  256. $cells[]='<input type="submit" name="add" value="'.get_string('currency:addcurrency','sloodle').'">';
  257. $sloodletable->data[]=$cells;
  258. sloodle_print_box_start('generalbox boxaligncenter center boxheightnarrow leftpara');
  259. print "<h2><img align=\"left\" src=\"".SLOODLE_WWWROOT."/lib/media/addnew.png\" width=\"48\"/> ";
  260. print s(get_string('currency:addnew','sloodle'));
  261. print "</h2>";
  262. sloodle_print_table($sloodletable);
  263. print("</form>");
  264. }
  265. sloodle_print_box_end();
  266. }
  267. function delete_currency() {
  268. global $CFG;
  269. global $COURSE;
  270. $id = required_param('id', PARAM_INT);
  271. $currencyname= optional_param('currencyname', '', PARAM_TEXT);
  272. $currencyid= required_param('currencyid', PARAM_INT);
  273. echo "<br>";
  274. //print header box
  275. sloodle_print_box_start('generalbox boxaligncenter right boxwidthnarrow boxheightnarrow rightpara');
  276. echo "<h1 ><img align=\"left\" src=\"".SLOODLE_WWWROOT."/lib/media/vault48.png\" width=\"48\"/> ";
  277. echo get_string('currency:confirmdelete', 'sloodle')."</h1>";
  278. sloodle_print_box_end();
  279. //display all currencies
  280. sloodle_print_box_start('generalbox boxaligncenter boxwidthfull leftpara');
  281. print('<form action="" method="POST">');
  282. $c = sloodle_get_record('sloodle_currency_types','id',$currencyid);
  283. $sloodletable = new stdClass();
  284. //set up column headers table data
  285. $sloodletable->head = array(
  286. s(get_string('currencies:icon', 'sloodle')),
  287. s(get_string('currencies:name', 'sloodle')),
  288. "&nbsp;"
  289. );
  290. $sloodletable->align = array('left','left','left');
  291. $sloodletable->width="95%";
  292. $sloodletable->size = array('10%','50%','30%');
  293. //create cells for row
  294. $row = array();
  295. //cell 1 -icon
  296. if (isset($c->imageurl)&&!empty($c->imageurl)){
  297. $row[]= '<img src="'.$c->imageurl.'" width ="20px" height="20px">';
  298. } else {
  299. $row[]= " ";
  300. }
  301. //cell 2 - name
  302. $row[]='<input type="hidden" name="mode" value="confirmdelete">
  303. <input type="hidden" name="currencyid" value="'.intval($c->id).'">
  304. <input type="hidden" name="id" value="'.$id.'">'.s($c->name);
  305. //cell 4 - submit
  306. $row[]='<input type="submit" name="sumbit" value="'
  307. .s(get_string('currency:deletethiscurrency', 'sloodle')).'">';
  308. $sloodletable->data[]=$row;
  309. sloodle_print_table($sloodletable);
  310. }
  311. function render_edit_currency() {
  312. global $CFG;
  313. global $COURSE;
  314. $id = required_param('id', PARAM_INT);
  315. $currencyname= required_param('currencyname', PARAM_TEXT);
  316. $currencyid= required_param('currencyid', PARAM_INT);
  317. echo "<br>";
  318. //print header box
  319. sloodle_print_box_start('generalbox boxaligncenter center boxwidthnarrow boxheightnarrow leftpara');
  320. echo "<h1 color=\"Red\"><img align=\"center\" src=\"".SLOODLE_WWWROOT."/lib/media/vault48.png\" width=\"48\"/> ";
  321. echo get_string('currency:editcurrency', 'sloodle')."</h1>";
  322. sloodle_print_box_end();
  323. //display all currencies
  324. sloodle_print_box_start('generalbox boxaligncenter boxwidthfull leftpara');
  325. print('<form action="" method="POST">');
  326. $c= sloodle_get_record('sloodle_currency_types','id',$currencyid);
  327. $sloodletable = new stdClass();
  328. //set up column headers table data
  329. $sloodletable->head = array(
  330. s(get_string('currencies:displayorder', 'sloodle')),
  331. s(get_string('currencies:imageurl', 'sloodle')),
  332. s(get_string('currencies:name', 'sloodle')),
  333. "&nbsp;"
  334. );
  335. $sloodletable->align = array('left','left','left');
  336. $sloodletable->width="95%";
  337. $sloodletable->size = array('10%','50%','30%','15%');
  338. //create cells for row
  339. $row = array();
  340. //cell 1 -display order
  341. $row[]='<input type="hidden" name="currencyid" value="'.$c->id.'">
  342. <input type="text" name="displayorder" size="2" value="'.$c->displayorder.'">';
  343. //cell 2 - imageurl
  344. $row[]='<input type="text" size="100" name="imageurl" value="'.$c->imageurl.'">
  345. <input type="hidden" name="mode" value="modify">
  346. <input type="hidden" name="id" value="'.$id.'">';
  347. //cell 3 - name
  348. $row[]='<input type="text" name="currencyname" size="30" value="'.$c->name.'">';
  349. //cell 4 - submit
  350. $row[]='<input type="submit" name="sumbit" value="submit">';
  351. $sloodletable->data[]=$row;
  352. sloodle_print_table($sloodletable);
  353. print("</form>");
  354. }
  355. }