backpack.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 Edmund Edgar
  12. * @contributor Paul Preibisch
  13. *
  14. */
  15. /** The base view class */
  16. require_once(SLOODLE_DIRROOT.'/view/base/base_view.php');
  17. /** SLOODLE logs data structure */
  18. require_once(SLOODLE_LIBROOT.'/course.php');
  19. require_once(SLOODLE_LIBROOT.'/currency.php');
  20. // Javascript for checking and unchecking checkboxes
  21. sloodle_require_js($CFG->wwwroot . '/mod/sloodle/lib/jquery/jquery-1.3.2.min.js');
  22. sloodle_require_js($CFG->wwwroot . '/mod/sloodle/lib/js/backpack.js');
  23. /**
  24. * Class for rendering a view of SLOODLE course information.
  25. * @package sloodle
  26. */
  27. class sloodle_view_backpack extends sloodle_base_view {
  28. /**
  29. * The Moodle course object, retrieved directly from database.
  30. * @var object
  31. * @access private
  32. */
  33. var $course = 0;
  34. /**
  35. * SLOODLE course object, retrieved directly from database.
  36. * @var object
  37. * @access private
  38. */
  39. var $sloodle_course = null;
  40. /**
  41. * Constructor.
  42. */
  43. function sloodle_view_backpack()
  44. {
  45. }
  46. /**
  47. * Check the request parameters to see which course was specified.
  48. */
  49. function process_request()
  50. {
  51. global $USER;
  52. $id = required_param('id', PARAM_INT);
  53. //check if valid course
  54. if (!$this->course = sloodle_get_record('course', 'id', $id)) error('Could not find course.');
  55. $this->sloodle_course = new SloodleCourse();
  56. if (!$this->sloodle_course->load($this->course)) error(get_string('failedcourseload', 'sloodle'));
  57. }
  58. function process_form() {
  59. $id = required_param('id', PARAM_INT);
  60. //has itemAdd forum been submitted?
  61. $isItemAdd= optional_param('isItemAdd',0, PARAM_INT);
  62. $userIds = optional_param('userIds', 0, PARAM_INT);
  63. //itemAdd form has been submitted
  64. if ($isItemAdd) {
  65. if (!$this->can_edit) {
  66. print_error("Permission denied");
  67. }
  68. $controllerid = required_param('controllerid', PARAM_INT);
  69. //fetch all currencies
  70. $all_currencies = SloodleCurrency::FetchIDNameHash();
  71. //create controller so we can fetch active round
  72. $controller = new SloodleController();
  73. if(!$controller->load_by_course_module_id($controllerid)) {
  74. print_error('Could not load controller for '.$controllerid);
  75. }
  76. $roundid = $controller->get_active_roundid(true);
  77. //go through each currency and see if it has been set, if it has, we have to update each user who
  78. //has been checked
  79. foreach($all_currencies as $currencyid => $currencyname) {
  80. //check if a currency update is necessary for this currency
  81. //build the currencyname field for this currency
  82. $fieldname="currency_".$currencyid;
  83. //now see if it was submitted
  84. $fieldvalue= optional_param($fieldname,0,PARAM_INT);
  85. //if no value has been submitted for this currency, we can skip adding this currency to the users!
  86. if ($fieldvalue==0) {
  87. continue;
  88. }
  89. foreach ($userIds as $u) {
  90. //go through each user which was checked and give them the selected currency and amount
  91. //create backpack item
  92. $backpack_item = new stdClass();
  93. $backpack_item->currencyid=intval($currencyid);
  94. $backpack_item->userid=intval($u);
  95. $backpack_item->amount=intval($fieldvalue);
  96. $backpack_item->timeawarded=time();
  97. $backpack_item->roundid=$roundid;
  98. $backpack_item->description="moodle add by ". $USER->username;
  99. //add it to the users backpack
  100. sloodle_insert_record('sloodle_award_points',$backpack_item);
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Check that the user is logged-in and has permission to alter course settings.
  107. */
  108. function check_permission()
  109. {
  110. // Ensure the user logs in
  111. require_login($this->course->id);
  112. if (isguestuser()) error(get_string('noguestaccess', 'sloodle'));
  113. add_to_log($this->course->id, 'course', 'view sloodle data', '', "{$this->course->id}");
  114. // Ensure the user is allowed to update information on this course
  115. $this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id);
  116. if (has_capability('moodle/course:update', $this->course_context)) $this->can_edit = true;
  117. }
  118. /**
  119. * Print the course settings page header.
  120. */
  121. function sloodle_print_header()
  122. {
  123. global $CFG;
  124. //print breadcrumbs
  125. $navigation = "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=backpack&id={$this->course->id}\">";
  126. $navigation .= get_string('backpack:view', 'sloodle');
  127. $navigation .= "</a>";
  128. //print the header
  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. global $CFG;
  137. global $COURSE;
  138. $view = optional_param('view', "", PARAM_TEXT);
  139. // Setup our list of tabs
  140. // We will always have a view option
  141. $action = optional_param('action', "", PARAM_TEXT);
  142. $context = get_context_instance(CONTEXT_COURSE,$this->course->id);
  143. echo "<br>";
  144. //print titles
  145. sloodle_print_box_start('generalbox boxaligncenter center boxheightnarrow leftpara');
  146. echo '<div style="position:relative ">';
  147. echo '<span style="position:relative;font-size:36px;font-weight:bold;">';
  148. //print return to backpack icon
  149. echo '<img align="center" src="'.SLOODLE_WWWROOT.'/lib/media/backpack64.png" width="48"/>';
  150. //print return to backpack title text
  151. echo s(get_string('backpacks:backpacks', 'sloodle'));
  152. echo '</span>';
  153. echo '<span style="float:right;">';
  154. echo '<a style="text-decoration:none" href="'.$CFG->wwwroot.'/mod/sloodle/view.php?_type=currency&id='.$COURSE->id.'">';
  155. echo s(get_string('currency:viewcurrencies', 'sloodle')).'<br>';
  156. //print return to currencies icon
  157. echo '<img src="'.SLOODLE_WWWROOT.'/lib/media/returntocurrencies.png"/></a>';
  158. echo '</span>';
  159. echo '</div>';
  160. echo '<br>';
  161. echo '<span style="position:relative;float:right;">';
  162. echo '</span>';
  163. //get all currency names
  164. $all_currencies = SloodleCurrency::FetchIDNameHash();
  165. $active_currency_ids = array();
  166. $contextid = $context->id;
  167. $courseid = $this->course->id;
  168. $prefix = $CFG->prefix;
  169. //build scoresql
  170. $scoresql = "select max(p.id) as id, p.userid as userid, p.currencyid as currencyid, sum(amount) as balance
  171. from {$prefix}sloodle_award_points p inner join {$prefix}sloodle_award_rounds ro on ro.id=p.roundid
  172. where ro.courseid = ? group by p.userid, p.currencyid order by balance desc;
  173. ";
  174. $scores = sloodle_get_records_sql_params( $scoresql, array( $courseid ) );
  175. //build usersql
  176. $usersql = "select max(u.id) as userid, u.firstname as firstname, u.lastname as lastname,
  177. su.avname as avname from {$prefix}user u inner join ${prefix}role_assignments ra on u.id=ra.userid
  178. left outer join ${prefix}sloodle_users su on u.id=su.userid where ra.contextid=?
  179. group by u.id order by avname asc;
  180. ";
  181. $students = sloodle_get_records_sql_params( $usersql, array( $contextid ) );
  182. //create an array by userid
  183. $students_by_userid = array();
  184. foreach($students as $student) {
  185. $students_by_userid[ $student->userid ] = $student;
  186. }
  187. // students with scores, in score order
  188. $student_scores_by_currency_id = array();
  189. //creating a two dimensional array keyed by user id, then by currency for our display table
  190. foreach($scores as $score) {
  191. $userid = $score->userid;
  192. $currencyid = $score->currencyid;
  193. $active_currency_ids[ $currencyid ] = true;
  194. // if student is deleted from course but their score is still there, dont display their score
  195. if (!isset($students_by_userid[ $userid ])) {
  196. continue;
  197. }
  198. //makes sure every student has an array entry
  199. if ( !isset( $student_scores_by_currency_id[$userid] ) ) {
  200. $student_scores_by_currency_id[$userid] = array();
  201. }
  202. //put the students balance in the currency into the array
  203. $student_scores_by_currency_id[$userid][$currencyid] = $score->balance;
  204. }
  205. // students without scores to the end of the array, in scored order
  206. foreach($students_by_userid as $userid => $student) {
  207. if (isset($student_scores_by_currency_id[ $userid ] )) {
  208. continue; // already done
  209. }
  210. $student_scores_by_currency_id[ $userid ] = array();
  211. }
  212. //now build display table
  213. $sloodletable = new stdClass();
  214. //create header
  215. $headerrow = array();
  216. $headerrow[] = s(get_string('awards:avname', 'sloodle'));
  217. $headerrow[] = s(get_string('awards:username', 'sloodle'));
  218. foreach($all_currencies as $currencyid => $currencyname) {
  219. $headerrow[] = s($currencyname);
  220. }
  221. $headerrow[] = $this->can_edit ? '<input type="checkbox" id="checkall" checked>' : '&nbsp;';
  222. //now add the header we just built
  223. $sloodletable->head = $headerrow;
  224. //set alignment of table cells
  225. $aligns = array('center','center'); // name columns
  226. foreach($all_currencies as $curr) {
  227. $aligns[] = 'right'; // each currency
  228. }
  229. $aligns[] = 'center'; // checkboxes
  230. $sloodletable->align = $aligns;
  231. $sloodletable->width="95%";
  232. //now display scores
  233. foreach($student_scores_by_currency_id as $userid => $currencybalancearray) {
  234. $student = $students_by_userid[ $userid ];
  235. $row = array();
  236. $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$userid}&amp;course={$COURSE->id}";
  237. $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$userid}&amp;course={$COURSE->id}";
  238. $row[] = "<a href=\"{$url_sloodleprofile}\">".s($student->avname)."</a>";
  239. $row[] = "<a href=\"{$url_moodleprofile}\">".s($student->firstname).' '.s($student->lastname)."</a>";
  240. foreach(array_keys($all_currencies) as $currencyid) {
  241. if ( isset($currencybalancearray[ $currencyid ] ) ) {
  242. $row[] = s($currencybalancearray[ $currencyid ]);
  243. } else {
  244. $row[] = ' 0 ';
  245. }
  246. }
  247. $row[] = $this->can_edit ? '<input type="checkbox" checked name="userIds[]" value="'.intval($userid).'">' : '&nbsp;';
  248. $sloodletable->data[] = $row;
  249. }
  250. if ($this->can_edit) {
  251. //create an extra row for the modify currency fields
  252. $row = array();
  253. $row[] = ' &nbsp; ';
  254. $row[] = "";
  255. $row[]='<span class="bpheader">'.s(get_string('backpacks:selectcontroller', 'sloodle'))."</span>";
  256. //build select drop down for the controllers in the course that any point updates will be linked too
  257. $rowText='<select style="left:20px;text-align:left;" name="controllerid">';
  258. //get all controllers
  259. $recs = sloodle_get_records_sql_params("select * from {$CFG->prefix}sloodle where type=? AND course=?", array(SLOODLE_TYPE_CTRL, $courseid));
  260. // Make sure we have at least one controller
  261. if ($recs == false || count($recs) == 0) {
  262. error(get_string('objectauthnocontrollers','sloodle'));
  263. exit();
  264. }
  265. foreach ($recs as $controller){
  266. $cm = get_coursemodule_from_instance('sloodle', $controller->id);
  267. $rowText.='<option name="controllerid" value="'.intval($cm->id).'">'.s($controller->name).'</option>';
  268. }
  269. $rowText.='</select>';
  270. //add controller select cell to row
  271. $row[] =$rowText;
  272. //now add the row to the table
  273. $sloodletable->data[] = $row;
  274. //create another row for the submit button
  275. $row = array();
  276. $row[] = '&nbsp;';
  277. $row[] = '&nbsp;';
  278. foreach($all_currencies as $currencyid => $currencynames) {
  279. $row[] = '<input type="text" name="currency_'.$currencyid.'">';
  280. }
  281. $row[]='<input type="submit" value="Update Backpacks">';
  282. }
  283. $sloodletable->data[] = $row;
  284. print('<form action="" method="POST">');
  285. echo '<input type="hidden" name="isItemAdd" value="1">';
  286. sloodle_print_table($sloodletable);
  287. print '</form>';
  288. sloodle_print_box_end();
  289. }
  290. }