PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/MoodleWebRole/lib/datalib.php

#
PHP | 2288 lines | 1290 code | 290 blank | 708 comment | 314 complexity | b929cdf5400bb2e31fc3e2a071b93b41 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php // $Id$
  2. /**
  3. * Library of functions for database manipulation.
  4. *
  5. * Other main libraries:
  6. * - weblib.php - functions that produce web output
  7. * - moodlelib.php - general-purpose Moodle functions
  8. * @author Martin Dougiamas and many others
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  10. * @package moodlecore
  11. */
  12. /// Some constants
  13. define('LASTACCESS_UPDATE_SECS', 60); /// Number of seconds to wait before
  14. /// updating lastaccess information in DB.
  15. /**
  16. * Escape all dangerous characters in a data record
  17. *
  18. * $dataobject is an object containing needed data
  19. * Run over each field exectuting addslashes() function
  20. * to escape SQL unfriendly characters (e.g. quotes)
  21. * Handy when writing back data read from the database
  22. *
  23. * @param $dataobject Object containing the database record
  24. * @return object Same object with neccessary characters escaped
  25. */
  26. function addslashes_object( $dataobject ) {
  27. $a = get_object_vars( $dataobject);
  28. foreach ($a as $key=>$value) {
  29. $a[$key] = addslashes( $value );
  30. }
  31. return (object)$a;
  32. }
  33. /// USER DATABASE ////////////////////////////////////////////////
  34. /**
  35. * Returns $user object of the main admin user
  36. * primary admin = admin with lowest role_assignment id among admins
  37. * @uses $CFG
  38. * @return object(admin) An associative array representing the admin user.
  39. */
  40. function get_admin () {
  41. static $myadmin;
  42. if (! isset($admin)) {
  43. if (! $admins = get_admins()) {
  44. return false;
  45. }
  46. $admin = reset($admins);//reset returns first element
  47. }
  48. return $admin;
  49. }
  50. /**
  51. * Returns list of all admins, using 1 DB query. It depends on DB schema v1.7
  52. * but does not depend on the v1.9 datastructures (context.path, etc).
  53. *
  54. * @uses $CFG
  55. * @return object
  56. */
  57. function get_admins() {
  58. global $CFG;
  59. $sql = "SELECT ra.userid, SUM(rc.permission) AS permission, MIN(ra.id) AS adminid
  60. FROM " . $CFG->prefix . "role_capabilities rc
  61. JOIN " . $CFG->prefix . "context ctx
  62. ON ctx.id=rc.contextid
  63. JOIN " . $CFG->prefix . "role_assignments ra
  64. ON ra.roleid=rc.roleid AND ra.contextid=ctx.id
  65. WHERE ctx.contextlevel=10
  66. AND rc.capability IN ('moodle/site:config',
  67. 'moodle/legacy:admin',
  68. 'moodle/site:doanything')
  69. GROUP BY ra.userid
  70. HAVING SUM(rc.permission) > 0";
  71. $sql = "SELECT u.*, ra.adminid
  72. FROM " . $CFG->prefix . "user u
  73. JOIN ($sql) ra
  74. ON u.id=ra.userid
  75. ORDER BY ra.adminid ASC";
  76. return get_records_sql($sql);
  77. }
  78. function get_courses_in_metacourse($metacourseid) {
  79. global $CFG;
  80. $sql = "SELECT c.id,c.shortname,c.fullname FROM {$CFG->prefix}course c, {$CFG->prefix}course_meta mc WHERE mc.parent_course = $metacourseid
  81. AND mc.child_course = c.id ORDER BY c.shortname";
  82. return get_records_sql($sql);
  83. }
  84. function get_courses_notin_metacourse($metacourseid,$count=false) {
  85. global $CFG;
  86. if ($count) {
  87. $sql = "SELECT COUNT(c.id)";
  88. } else {
  89. $sql = "SELECT c.id,c.shortname,c.fullname";
  90. }
  91. $alreadycourses = get_courses_in_metacourse($metacourseid);
  92. $sql .= " FROM {$CFG->prefix}course c WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
  93. AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1 ".((empty($count)) ? " ORDER BY c.shortname" : "");
  94. return get_records_sql($sql);
  95. }
  96. function count_courses_notin_metacourse($metacourseid) {
  97. global $CFG;
  98. $alreadycourses = get_courses_in_metacourse($metacourseid);
  99. $sql = "SELECT COUNT(c.id) AS notin FROM {$CFG->prefix}course c
  100. WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
  101. AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1";
  102. if (!$count = get_record_sql($sql)) {
  103. return 0;
  104. }
  105. return $count->notin;
  106. }
  107. /**
  108. * Search through course users
  109. *
  110. * If $coursid specifies the site course then this function searches
  111. * through all undeleted and confirmed users
  112. *
  113. * @uses $CFG
  114. * @uses SITEID
  115. * @param int $courseid The course in question.
  116. * @param int $groupid The group in question.
  117. * @param string $searchtext ?
  118. * @param string $sort ?
  119. * @param string $exceptions ?
  120. * @return object
  121. */
  122. function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
  123. global $CFG;
  124. $LIKE = sql_ilike();
  125. $fullname = sql_fullname('u.firstname', 'u.lastname');
  126. if (!empty($exceptions)) {
  127. $except = ' AND u.id NOT IN ('. $exceptions .') ';
  128. } else {
  129. $except = '';
  130. }
  131. if (!empty($sort)) {
  132. $order = ' ORDER BY '. $sort;
  133. } else {
  134. $order = '';
  135. }
  136. $select = 'u.deleted = \'0\' AND u.confirmed = \'1\'';
  137. if (!$courseid or $courseid == SITEID) {
  138. return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
  139. FROM {$CFG->prefix}user u
  140. WHERE $select
  141. AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
  142. $except $order");
  143. } else {
  144. if ($groupid) {
  145. //TODO:check. Remove group DB dependencies.
  146. return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
  147. FROM {$CFG->prefix}user u,
  148. {$CFG->prefix}groups_members gm
  149. WHERE $select AND gm.groupid = '$groupid' AND gm.userid = u.id
  150. AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
  151. $except $order");
  152. } else {
  153. $context = get_context_instance(CONTEXT_COURSE, $courseid);
  154. $contextlists = get_related_contexts_string($context);
  155. $users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
  156. FROM {$CFG->prefix}user u,
  157. {$CFG->prefix}role_assignments ra
  158. WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id
  159. AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
  160. $except $order");
  161. }
  162. return $users;
  163. }
  164. }
  165. /**
  166. * Returns a list of all site users
  167. * Obsolete, just calls get_course_users(SITEID)
  168. *
  169. * @uses SITEID
  170. * @deprecated Use {@link get_course_users()} instead.
  171. * @param string $fields A comma separated list of fields to be returned from the chosen table.
  172. * @param string $exceptions A comma separated list of user->id to be skiped in the result returned by the function
  173. * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
  174. * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
  175. * @return object|false {@link $USER} records or false if error.
  176. */
  177. function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='', $limitfrom='', $limitnum='') {
  178. return get_course_users(SITEID, $sort, $exceptions, $fields, $limitfrom, $limitnum);
  179. }
  180. /**
  181. * Returns a subset of users
  182. *
  183. * @uses $CFG
  184. * @param bool $get If false then only a count of the records is returned
  185. * @param string $search A simple string to search for
  186. * @param bool $confirmed A switch to allow/disallow unconfirmed users
  187. * @param array(int) $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
  188. * @param string $sort A SQL snippet for the sorting criteria to use
  189. * @param string $firstinitial ?
  190. * @param string $lastinitial ?
  191. * @param string $page ?
  192. * @param string $recordsperpage ?
  193. * @param string $fields A comma separated list of fields to be returned from the chosen table.
  194. * @return object|false|int {@link $USER} records unless get is false in which case the integer count of the records found is returned. False is returned if an error is encountered.
  195. */
  196. function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sort='firstname ASC',
  197. $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='') {
  198. global $CFG;
  199. if ($get && !$recordsperpage) {
  200. debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
  201. 'On large installations, this will probably cause an out of memory error. ' .
  202. 'Please think again and change your code so that it does not try to ' .
  203. 'load so much data into memory.', DEBUG_DEVELOPER);
  204. }
  205. $LIKE = sql_ilike();
  206. $fullname = sql_fullname();
  207. $select = 'username <> \'guest\' AND deleted = 0';
  208. if (!empty($search)){
  209. $search = trim($search);
  210. $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
  211. }
  212. if ($confirmed) {
  213. $select .= ' AND confirmed = \'1\' ';
  214. }
  215. if ($exceptions) {
  216. $select .= ' AND id NOT IN ('. $exceptions .') ';
  217. }
  218. if ($firstinitial) {
  219. $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\'';
  220. }
  221. if ($lastinitial) {
  222. $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\'';
  223. }
  224. if ($extraselect) {
  225. $select .= " AND $extraselect ";
  226. }
  227. if ($get) {
  228. return get_records_select('user', $select, $sort, $fields, $page, $recordsperpage);
  229. } else {
  230. return count_records_select('user', $select);
  231. }
  232. }
  233. /**
  234. * shortdesc (optional)
  235. *
  236. * longdesc
  237. *
  238. * @uses $CFG
  239. * @param string $sort ?
  240. * @param string $dir ?
  241. * @param int $categoryid ?
  242. * @param int $categoryid ?
  243. * @param string $search ?
  244. * @param string $firstinitial ?
  245. * @param string $lastinitial ?
  246. * @returnobject {@link $USER} records
  247. * @todo Finish documenting this function
  248. */
  249. function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
  250. $search='', $firstinitial='', $lastinitial='', $extraselect='') {
  251. global $CFG;
  252. $LIKE = sql_ilike();
  253. $fullname = sql_fullname();
  254. $select = "deleted <> '1'";
  255. if (!empty($search)) {
  256. $search = trim($search);
  257. $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%' OR username='$search') ";
  258. }
  259. if ($firstinitial) {
  260. $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\' ';
  261. }
  262. if ($lastinitial) {
  263. $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\' ';
  264. }
  265. if ($extraselect) {
  266. $select .= " AND $extraselect ";
  267. }
  268. if ($sort) {
  269. $sort = ' ORDER BY '. $sort .' '. $dir;
  270. }
  271. /// warning: will return UNCONFIRMED USERS
  272. return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
  273. FROM {$CFG->prefix}user
  274. WHERE $select $sort", $page, $recordsperpage);
  275. }
  276. /**
  277. * Full list of users that have confirmed their accounts.
  278. *
  279. * @uses $CFG
  280. * @return object
  281. */
  282. function get_users_confirmed() {
  283. global $CFG;
  284. return get_records_sql("SELECT *
  285. FROM {$CFG->prefix}user
  286. WHERE confirmed = 1
  287. AND deleted = 0
  288. AND username <> 'guest'");
  289. }
  290. /// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
  291. /**
  292. * Returns $course object of the top-level site.
  293. *
  294. * @return course A {@link $COURSE} object for the site
  295. */
  296. function get_site() {
  297. global $SITE;
  298. if (!empty($SITE->id)) { // We already have a global to use, so return that
  299. return $SITE;
  300. }
  301. if ($course = get_record('course', 'category', 0)) {
  302. return $course;
  303. } else {
  304. return false;
  305. }
  306. }
  307. /**
  308. * Returns list of courses, for whole site, or category
  309. *
  310. * Returns list of courses, for whole site, or category
  311. * Important: Using c.* for fields is extremely expensive because
  312. * we are using distinct. You almost _NEVER_ need all the fields
  313. * in such a large SELECT
  314. *
  315. * @param type description
  316. *
  317. */
  318. function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
  319. global $USER, $CFG;
  320. if ($categoryid != "all" && is_numeric($categoryid)) {
  321. $categoryselect = "WHERE c.category = '$categoryid'";
  322. } else {
  323. $categoryselect = "";
  324. }
  325. if (empty($sort)) {
  326. $sortstatement = "";
  327. } else {
  328. $sortstatement = "ORDER BY $sort";
  329. }
  330. $visiblecourses = array();
  331. // pull out all course matching the cat
  332. if ($courses = get_records_sql("SELECT $fields,
  333. ctx.id AS ctxid, ctx.path AS ctxpath,
  334. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  335. FROM {$CFG->prefix}course c
  336. JOIN {$CFG->prefix}context ctx
  337. ON (c.id = ctx.instanceid
  338. AND ctx.contextlevel=".CONTEXT_COURSE.")
  339. $categoryselect
  340. $sortstatement")) {
  341. // loop throught them
  342. foreach ($courses as $course) {
  343. $course = make_context_subobj($course);
  344. if (isset($course->visible) && $course->visible <= 0) {
  345. // for hidden courses, require visibility check
  346. if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
  347. $visiblecourses [] = $course;
  348. }
  349. } else {
  350. $visiblecourses [] = $course;
  351. }
  352. }
  353. }
  354. return $visiblecourses;
  355. /*
  356. $teachertable = "";
  357. $visiblecourses = "";
  358. $sqland = "";
  359. if (!empty($categoryselect)) {
  360. $sqland = "AND ";
  361. }
  362. if (!empty($USER->id)) { // May need to check they are a teacher
  363. if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
  364. $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
  365. $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id";
  366. }
  367. } else {
  368. $visiblecourses = "$sqland c.visible > 0";
  369. }
  370. if ($categoryselect or $visiblecourses) {
  371. $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
  372. } else {
  373. $selectsql = "{$CFG->prefix}course c $teachertable";
  374. }
  375. $extrafield = str_replace('ASC','',$sort);
  376. $extrafield = str_replace('DESC','',$extrafield);
  377. $extrafield = trim($extrafield);
  378. if (!empty($extrafield)) {
  379. $extrafield = ','.$extrafield;
  380. }
  381. return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : ""));
  382. */
  383. }
  384. /**
  385. * Returns list of courses, for whole site, or category
  386. *
  387. * Similar to get_courses, but allows paging
  388. * Important: Using c.* for fields is extremely expensive because
  389. * we are using distinct. You almost _NEVER_ need all the fields
  390. * in such a large SELECT
  391. *
  392. * @param type description
  393. *
  394. */
  395. function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
  396. &$totalcount, $limitfrom="", $limitnum="") {
  397. global $USER, $CFG;
  398. $categoryselect = "";
  399. if ($categoryid != "all" && is_numeric($categoryid)) {
  400. $categoryselect = "WHERE c.category = '$categoryid'";
  401. } else {
  402. $categoryselect = "";
  403. }
  404. // pull out all course matching the cat
  405. $visiblecourses = array();
  406. if (!($rs = get_recordset_sql("SELECT $fields,
  407. ctx.id AS ctxid, ctx.path AS ctxpath,
  408. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  409. FROM {$CFG->prefix}course c
  410. JOIN {$CFG->prefix}context ctx
  411. ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
  412. $categoryselect
  413. ORDER BY $sort"))) {
  414. return $visiblecourses;
  415. }
  416. $totalcount = 0;
  417. if (!$limitfrom) {
  418. $limitfrom = 0;
  419. }
  420. // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
  421. while ($course = rs_fetch_next_record($rs)) {
  422. $course = make_context_subobj($course);
  423. if ($course->visible <= 0) {
  424. // for hidden courses, require visibility check
  425. if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
  426. $totalcount++;
  427. if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
  428. $visiblecourses [] = $course;
  429. }
  430. }
  431. } else {
  432. $totalcount++;
  433. if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
  434. $visiblecourses [] = $course;
  435. }
  436. }
  437. }
  438. rs_close($rs);
  439. return $visiblecourses;
  440. /**
  441. $categoryselect = "";
  442. if ($categoryid != "all" && is_numeric($categoryid)) {
  443. $categoryselect = "c.category = '$categoryid'";
  444. }
  445. $teachertable = "";
  446. $visiblecourses = "";
  447. $sqland = "";
  448. if (!empty($categoryselect)) {
  449. $sqland = "AND ";
  450. }
  451. if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher
  452. if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
  453. $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
  454. $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id";
  455. }
  456. } else {
  457. $visiblecourses = "$sqland c.visible > 0";
  458. }
  459. if ($limitfrom !== "") {
  460. $limit = sql_paging_limit($limitfrom, $limitnum);
  461. } else {
  462. $limit = "";
  463. }
  464. $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
  465. $totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql");
  466. return get_records_sql("SELECT $fields FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")." $limit");
  467. */
  468. }
  469. /*
  470. * Retrieve course records with the course managers and other related records
  471. * that we need for print_course(). This allows print_courses() to do its job
  472. * in a constant number of DB queries, regardless of the number of courses,
  473. * role assignments, etc.
  474. *
  475. * The returned array is indexed on c.id, and each course will have
  476. * - $course->context - a context obj
  477. * - $course->managers - array containing RA objects that include a $user obj
  478. * with the minimal fields needed for fullname()
  479. *
  480. */
  481. function get_courses_wmanagers($categoryid=0, $sort="c.sortorder ASC", $fields=array()) {
  482. /*
  483. * The plan is to
  484. *
  485. * - Grab the courses JOINed w/context
  486. *
  487. * - Grab the interesting course-manager RAs
  488. * JOINed with a base user obj and add them to each course
  489. *
  490. * So as to do all the work in 2 DB queries. The RA+user JOIN
  491. * ends up being pretty expensive if it happens over _all_
  492. * courses on a large site. (Are we surprised!?)
  493. *
  494. * So this should _never_ get called with 'all' on a large site.
  495. *
  496. */
  497. global $USER, $CFG;
  498. $allcats = false; // bool flag
  499. if ($categoryid === 'all') {
  500. $categoryclause = '';
  501. $allcats = true;
  502. } elseif (is_numeric($categoryid)) {
  503. $categoryclause = "c.category = $categoryid";
  504. } else {
  505. debugging("Could not recognise categoryid = $categoryid");
  506. $categoryclause = '';
  507. }
  508. $basefields = array('id', 'category', 'sortorder',
  509. 'shortname', 'fullname', 'idnumber',
  510. 'teacher', 'teachers', 'student', 'students',
  511. 'guest', 'startdate', 'visible',
  512. 'newsitems', 'cost', 'enrol',
  513. 'groupmode', 'groupmodeforce');
  514. if (!is_null($fields) && is_string($fields)) {
  515. if (empty($fields)) {
  516. $fields = $basefields;
  517. } else {
  518. // turn the fields from a string to an array that
  519. // get_user_courses_bycap() will like...
  520. $fields = explode(',',$fields);
  521. $fields = array_map('trim', $fields);
  522. $fields = array_unique(array_merge($basefields, $fields));
  523. }
  524. } elseif (is_array($fields)) {
  525. $fields = array_merge($basefields,$fields);
  526. }
  527. $coursefields = 'c.' .join(',c.', $fields);
  528. if (empty($sort)) {
  529. $sortstatement = "";
  530. } else {
  531. $sortstatement = "ORDER BY $sort";
  532. }
  533. $where = 'WHERE c.id != ' . SITEID;
  534. if ($categoryclause !== ''){
  535. $where = "$where AND $categoryclause";
  536. }
  537. // pull out all courses matching the cat
  538. $sql = "SELECT $coursefields,
  539. ctx.id AS ctxid, ctx.path AS ctxpath,
  540. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  541. FROM {$CFG->prefix}course c
  542. JOIN {$CFG->prefix}context ctx
  543. ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
  544. $where
  545. $sortstatement";
  546. $catpaths = array();
  547. $catpath = NULL;
  548. if ($courses = get_records_sql($sql)) {
  549. // loop on courses materialising
  550. // the context, and prepping data to fetch the
  551. // managers efficiently later...
  552. foreach ($courses as $k => $course) {
  553. $courses[$k] = make_context_subobj($courses[$k]);
  554. $courses[$k]->managers = array();
  555. if ($allcats === false) {
  556. // single cat, so take just the first one...
  557. if ($catpath === NULL) {
  558. $catpath = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
  559. }
  560. } else {
  561. // chop off the contextid of the course itself
  562. // like dirname() does...
  563. $catpaths[] = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
  564. }
  565. }
  566. } else {
  567. return array(); // no courses!
  568. }
  569. $CFG->coursemanager = trim($CFG->coursemanager);
  570. if (empty($CFG->coursemanager)) {
  571. return $courses;
  572. }
  573. $managerroles = split(',', $CFG->coursemanager);
  574. $catctxids = '';
  575. if (count($managerroles)) {
  576. if ($allcats === true) {
  577. $catpaths = array_unique($catpaths);
  578. $ctxids = array();
  579. foreach ($catpaths as $cpath) {
  580. $ctxids = array_merge($ctxids, explode('/',substr($cpath,1)));
  581. }
  582. $ctxids = array_unique($ctxids);
  583. $catctxids = implode( ',' , $ctxids);
  584. unset($catpaths);
  585. unset($cpath);
  586. } else {
  587. // take the ctx path from the first course
  588. // as all categories will be the same...
  589. $catpath = substr($catpath,1);
  590. $catpath = preg_replace(':/\d+$:','',$catpath);
  591. $catctxids = str_replace('/',',',$catpath);
  592. }
  593. if ($categoryclause !== '') {
  594. $categoryclause = "AND $categoryclause";
  595. }
  596. /*
  597. * Note: Here we use a LEFT OUTER JOIN that can
  598. * "optionally" match to avoid passing a ton of context
  599. * ids in an IN() clause. Perhaps a subselect is faster.
  600. *
  601. * In any case, this SQL is not-so-nice over large sets of
  602. * courses with no $categoryclause.
  603. *
  604. */
  605. $sql = "SELECT ctx.path, ctx.instanceid, ctx.contextlevel,
  606. ra.hidden,
  607. r.id AS roleid, r.name as rolename,
  608. u.id AS userid, u.firstname, u.lastname
  609. FROM {$CFG->prefix}role_assignments ra
  610. JOIN {$CFG->prefix}context ctx
  611. ON ra.contextid = ctx.id
  612. JOIN {$CFG->prefix}user u
  613. ON ra.userid = u.id
  614. JOIN {$CFG->prefix}role r
  615. ON ra.roleid = r.id
  616. LEFT OUTER JOIN {$CFG->prefix}course c
  617. ON (ctx.instanceid=c.id AND ctx.contextlevel=".CONTEXT_COURSE.")
  618. WHERE ( c.id IS NOT NULL";
  619. // under certain conditions, $catctxids is NULL
  620. if($catctxids == NULL){
  621. $sql .= ") ";
  622. }else{
  623. $sql .= " OR ra.contextid IN ($catctxids) )";
  624. }
  625. $sql .= "AND ra.roleid IN ({$CFG->coursemanager})
  626. $categoryclause
  627. ORDER BY r.sortorder ASC, ctx.contextlevel ASC, ra.sortorder ASC";
  628. $rs = get_recordset_sql($sql);
  629. // This loop is fairly stupid as it stands - might get better
  630. // results doing an initial pass clustering RAs by path.
  631. while ($ra = rs_fetch_next_record($rs)) {
  632. $user = new StdClass;
  633. $user->id = $ra->userid; unset($ra->userid);
  634. $user->firstname = $ra->firstname; unset($ra->firstname);
  635. $user->lastname = $ra->lastname; unset($ra->lastname);
  636. $ra->user = $user;
  637. if ($ra->contextlevel == CONTEXT_SYSTEM) {
  638. foreach ($courses as $k => $course) {
  639. $courses[$k]->managers[] = $ra;
  640. }
  641. } elseif ($ra->contextlevel == CONTEXT_COURSECAT) {
  642. if ($allcats === false) {
  643. // It always applies
  644. foreach ($courses as $k => $course) {
  645. $courses[$k]->managers[] = $ra;
  646. }
  647. } else {
  648. foreach ($courses as $k => $course) {
  649. // Note that strpos() returns 0 as "matched at pos 0"
  650. if (strpos($course->context->path, $ra->path.'/')===0) {
  651. // Only add it to subpaths
  652. $courses[$k]->managers[] = $ra;
  653. }
  654. }
  655. }
  656. } else { // course-level
  657. if(!array_key_exists($ra->instanceid, $courses)) {
  658. //this course is not in a list, probably a frontpage course
  659. continue;
  660. }
  661. $courses[$ra->instanceid]->managers[] = $ra;
  662. }
  663. }
  664. rs_close($rs);
  665. }
  666. return $courses;
  667. }
  668. /**
  669. * Convenience function - lists courses that a user has access to view.
  670. *
  671. * For admins and others with access to "every" course in the system, we should
  672. * try to get courses with explicit RAs.
  673. *
  674. * NOTE: this function is heavily geared towards the perspective of the user
  675. * passed in $userid. So it will hide courses that the user cannot see
  676. * (for any reason) even if called from cron or from another $USER's
  677. * perspective.
  678. *
  679. * If you really want to know what courses are assigned to the user,
  680. * without any hiding or scheming, call the lower-level
  681. * get_user_courses_bycap().
  682. *
  683. *
  684. * Notes inherited from get_user_courses_bycap():
  685. *
  686. * - $fields is an array of fieldnames to ADD
  687. * so name the fields you really need, which will
  688. * be added and uniq'd
  689. *
  690. * - the course records have $c->context which is a fully
  691. * valid context object. Saves you a query per course!
  692. *
  693. * @uses $CFG,$USER
  694. * @param int $userid The user of interest
  695. * @param string $sort the sortorder in the course table
  696. * @param array $fields - names of _additional_ fields to return (also accepts a string)
  697. * @param bool $doanything True if using the doanything flag
  698. * @param int $limit Maximum number of records to return, or 0 for unlimited
  699. * @return array {@link $COURSE} of course objects
  700. */
  701. function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NULL, $doanything=false,$limit=0) {
  702. global $CFG,$USER;
  703. // Guest's do not have any courses
  704. $sitecontext = get_context_instance(CONTEXT_SYSTEM);
  705. if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) {
  706. return(array());
  707. }
  708. $basefields = array('id', 'category', 'sortorder',
  709. 'shortname', 'fullname', 'idnumber',
  710. 'teacher', 'teachers', 'student', 'students',
  711. 'guest', 'startdate', 'visible',
  712. 'newsitems', 'cost', 'enrol',
  713. 'groupmode', 'groupmodeforce');
  714. if (!is_null($fields) && is_string($fields)) {
  715. if (empty($fields)) {
  716. $fields = $basefields;
  717. } else {
  718. // turn the fields from a string to an array that
  719. // get_user_courses_bycap() will like...
  720. $fields = explode(',',$fields);
  721. $fields = array_map('trim', $fields);
  722. $fields = array_unique(array_merge($basefields, $fields));
  723. }
  724. } elseif (is_array($fields)) {
  725. $fields = array_unique(array_merge($basefields, $fields));
  726. } else {
  727. $fields = $basefields;
  728. }
  729. $orderby = '';
  730. $sort = trim($sort);
  731. if (!empty($sort)) {
  732. $rawsorts = explode(',', $sort);
  733. $sorts = array();
  734. foreach ($rawsorts as $rawsort) {
  735. $rawsort = trim($rawsort);
  736. if (strpos($rawsort, 'c.') === 0) {
  737. $rawsort = substr($rawsort, 2);
  738. }
  739. $sorts[] = trim($rawsort);
  740. }
  741. $sort = 'c.'.implode(',c.', $sorts);
  742. $orderby = "ORDER BY $sort";
  743. }
  744. //
  745. // Logged-in user - Check cached courses
  746. //
  747. // NOTE! it's a _string_ because
  748. // - it's all we'll ever use
  749. // - it serialises much more compact than an array
  750. // this a big concern here - cost of serialise
  751. // and unserialise gets huge as the session grows
  752. //
  753. // If the courses are too many - it won't be set
  754. // for large numbers of courses, caching in the session
  755. // has marginal benefits (costs too much, not
  756. // worthwhile...) and we may hit SQL parser limits
  757. // because we use IN()
  758. //
  759. if ($userid === $USER->id) {
  760. if (isset($USER->loginascontext)
  761. && $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
  762. // list _only_ this course
  763. // anything else is asking for trouble...
  764. $courseids = $USER->loginascontext->instanceid;
  765. } elseif (isset($USER->mycourses)
  766. && is_string($USER->mycourses)) {
  767. if ($USER->mycourses === '') {
  768. // empty str means: user has no courses
  769. // ... so do the easy thing...
  770. return array();
  771. } else {
  772. $courseids = $USER->mycourses;
  773. }
  774. }
  775. if (isset($courseids)) {
  776. // The data massaging here MUST be kept in sync with
  777. // get_user_courses_bycap() so we return
  778. // the same...
  779. // (but here we don't need to check has_cap)
  780. $coursefields = 'c.' .join(',c.', $fields);
  781. $sql = "SELECT $coursefields,
  782. ctx.id AS ctxid, ctx.path AS ctxpath,
  783. ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel,
  784. cc.path AS categorypath
  785. FROM {$CFG->prefix}course c
  786. JOIN {$CFG->prefix}course_categories cc
  787. ON c.category=cc.id
  788. JOIN {$CFG->prefix}context ctx
  789. ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
  790. WHERE c.id IN ($courseids)
  791. $orderby";
  792. $rs = get_recordset_sql($sql);
  793. $courses = array();
  794. $cc = 0; // keep count
  795. while ($c = rs_fetch_next_record($rs)) {
  796. // build the context obj
  797. $c = make_context_subobj($c);
  798. if ($limit > 0 && $cc >= $limit) {
  799. break;
  800. }
  801. $courses[$c->id] = $c;
  802. $cc++;
  803. }
  804. rs_close($rs);
  805. return $courses;
  806. }
  807. }
  808. // Non-cached - get accessinfo
  809. if ($userid === $USER->id && isset($USER->access)) {
  810. $accessinfo = $USER->access;
  811. } else {
  812. $accessinfo = get_user_access_sitewide($userid);
  813. }
  814. $courses = get_user_courses_bycap($userid, 'moodle/course:view', $accessinfo,
  815. $doanything, $sort, $fields,
  816. $limit);
  817. $cats = NULL;
  818. // If we have to walk category visibility
  819. // to eval course visibility, get the categories
  820. if (empty($CFG->allowvisiblecoursesinhiddencategories)) {
  821. $sql = "SELECT cc.id, cc.path, cc.visible,
  822. ctx.id AS ctxid, ctx.path AS ctxpath,
  823. ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel
  824. FROM {$CFG->prefix}course_categories cc
  825. JOIN {$CFG->prefix}context ctx ON (cc.id = ctx.instanceid)
  826. WHERE ctx.contextlevel = ".CONTEXT_COURSECAT."
  827. ORDER BY cc.id";
  828. $rs = get_recordset_sql($sql);
  829. // Using a temporary array instead of $cats here, to avoid a "true" result when isnull($cats) further down
  830. $categories = array();
  831. while ($course_cat = rs_fetch_next_record($rs)) {
  832. // build the context obj
  833. $course_cat = make_context_subobj($course_cat);
  834. $categories[$course_cat->id] = $course_cat;
  835. }
  836. rs_close($rs);
  837. if (!empty($categories)) {
  838. $cats = $categories;
  839. }
  840. unset($course_cat);
  841. }
  842. //
  843. // Strangely, get_my_courses() is expected to return the
  844. // array keyed on id, which messes up the sorting
  845. // So do that, and also cache the ids in the session if appropriate
  846. //
  847. $kcourses = array();
  848. $courses_count = count($courses);
  849. $cacheids = NULL;
  850. $vcatpaths = array();
  851. if ($userid === $USER->id && $courses_count < 500) {
  852. $cacheids = array();
  853. }
  854. for ($n=0; $n<$courses_count; $n++) {
  855. //
  856. // Check whether $USER (not $userid) can _actually_ see them
  857. // Easy if $CFG->allowvisiblecoursesinhiddencategories
  858. // is set, and we don't have to care about categories.
  859. // Lots of work otherwise... (all in mem though!)
  860. //
  861. $cansee = false;
  862. if (is_null($cats)) { // easy rules!
  863. if ($courses[$n]->visible == true) {
  864. $cansee = true;
  865. } elseif (has_capability('moodle/course:viewhiddencourses',
  866. $courses[$n]->context, $USER->id)) {
  867. $cansee = true;
  868. }
  869. } else {
  870. //
  871. // Is the cat visible?
  872. // we have to assume it _is_ visible
  873. // so we can shortcut when we find a hidden one
  874. //
  875. $viscat = true;
  876. $cpath = $courses[$n]->categorypath;
  877. if (isset($vcatpaths[$cpath])) {
  878. $viscat = $vcatpaths[$cpath];
  879. } else {
  880. $cpath = substr($cpath,1); // kill leading slash
  881. $cpath = explode('/',$cpath);
  882. $ccct = count($cpath);
  883. for ($m=0;$m<$ccct;$m++) {
  884. $ccid = $cpath[$m];
  885. if ($cats[$ccid]->visible==false) {
  886. $viscat = false;
  887. break;
  888. }
  889. }
  890. $vcatpaths[$courses[$n]->categorypath] = $viscat;
  891. }
  892. //
  893. // Perhaps it's actually visible to $USER
  894. // check moodle/category:viewhiddencategories
  895. //
  896. // The name isn't obvious, but the description says
  897. // "See hidden categories" so the user shall see...
  898. // But also check if the allowvisiblecoursesinhiddencategories setting is true, and check for course visibility
  899. if ($viscat === false) {
  900. $catctx = $cats[$courses[$n]->category]->context;
  901. if (has_capability('moodle/category:viewhiddencategories', $catctx, $USER->id)) {
  902. $vcatpaths[$courses[$n]->categorypath] = true;
  903. $viscat = true;
  904. } elseif ($CFG->allowvisiblecoursesinhiddencategories && $courses[$n]->visible == true) {
  905. $viscat = true;
  906. }
  907. }
  908. //
  909. // Decision matrix
  910. //
  911. if ($viscat === true) {
  912. if ($courses[$n]->visible == true) {
  913. $cansee = true;
  914. } elseif (has_capability('moodle/course:viewhiddencourses',
  915. $courses[$n]->context, $USER->id)) {
  916. $cansee = true;
  917. }
  918. }
  919. }
  920. if ($cansee === true) {
  921. $kcourses[$courses[$n]->id] = $courses[$n];
  922. if (is_array($cacheids)) {
  923. $cacheids[] = $courses[$n]->id;
  924. }
  925. }
  926. }
  927. if (is_array($cacheids)) {
  928. // Only happens
  929. // - for the logged in user
  930. // - below the threshold (500)
  931. // empty string is _valid_
  932. $USER->mycourses = join(',',$cacheids);
  933. } elseif ($userid === $USER->id && isset($USER->mycourses)) {
  934. // cheap sanity check
  935. unset($USER->mycourses);
  936. }
  937. return $kcourses;
  938. }
  939. /**
  940. * A list of courses that match a search
  941. *
  942. * @uses $CFG
  943. * @param array $searchterms ?
  944. * @param string $sort ?
  945. * @param int $page ?
  946. * @param int $recordsperpage ?
  947. * @param int $totalcount Passed in by reference. ?
  948. * @return object {@link $COURSE} records
  949. */
  950. function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) {
  951. global $CFG;
  952. //to allow case-insensitive search for postgesql
  953. if ($CFG->dbfamily == 'postgres') {
  954. $LIKE = 'ILIKE';
  955. $NOTLIKE = 'NOT ILIKE'; // case-insensitive
  956. $REGEXP = '~*';
  957. $NOTREGEXP = '!~*';
  958. } else {
  959. $LIKE = 'LIKE';
  960. $NOTLIKE = 'NOT LIKE';
  961. $REGEXP = 'REGEXP';
  962. $NOTREGEXP = 'NOT REGEXP';
  963. }
  964. $fullnamesearch = '';
  965. $summarysearch = '';
  966. $idnumbersearch = '';
  967. $shortnamesearch = '';
  968. foreach ($searchterms as $searchterm) {
  969. $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
  970. /// will use it to simulate the "-" operator with LIKE clause
  971. /// Under Oracle and MSSQL, trim the + and - operators and perform
  972. /// simpler LIKE (or NOT LIKE) queries
  973. if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
  974. if (substr($searchterm, 0, 1) == '-') {
  975. $NOT = ' NOT ';
  976. }
  977. $searchterm = trim($searchterm, '+-');
  978. }
  979. if ($fullnamesearch) {
  980. $fullnamesearch .= ' AND ';
  981. }
  982. if ($summarysearch) {
  983. $summarysearch .= ' AND ';
  984. }
  985. if ($idnumbersearch) {
  986. $idnumbersearch .= ' AND ';
  987. }
  988. if ($shortnamesearch) {
  989. $shortnamesearch .= ' AND ';
  990. }
  991. if (substr($searchterm,0,1) == '+') {
  992. $searchterm = substr($searchterm,1);
  993. $summarysearch .= " c.summary $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  994. $fullnamesearch .= " c.fullname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  995. $idnumbersearch .= " c.idnumber $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  996. $shortnamesearch .= " c.shortname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  997. } else if (substr($searchterm,0,1) == "-") {
  998. $searchterm = substr($searchterm,1);
  999. $summarysearch .= " c.summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  1000. $fullnamesearch .= " c.fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  1001. $idnumbersearch .= " c.idnumber $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  1002. $shortnamesearch .= " c.shortname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
  1003. } else {
  1004. $summarysearch .= ' summary '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
  1005. $fullnamesearch .= ' fullname '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
  1006. $idnumbersearch .= ' idnumber '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
  1007. $shortnamesearch .= ' shortname '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
  1008. }
  1009. }
  1010. $sql = "SELECT c.*,
  1011. ctx.id AS ctxid, ctx.path AS ctxpath,
  1012. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  1013. FROM {$CFG->prefix}course c
  1014. JOIN {$CFG->prefix}context ctx
  1015. ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
  1016. WHERE (( $fullnamesearch ) OR ( $summarysearch ) OR ( $idnumbersearch ) OR ( $shortnamesearch ))
  1017. AND category > 0
  1018. ORDER BY " . $sort;
  1019. $courses = array();
  1020. if ($rs = get_recordset_sql($sql)) {
  1021. // Tiki pagination
  1022. $limitfrom = $page * $recordsperpage;
  1023. $limitto = $limitfrom + $recordsperpage;
  1024. $c = 0; // counts how many visible courses we've seen
  1025. while ($course = rs_fetch_next_record($rs)) {
  1026. $course = make_context_subobj($course);
  1027. if ($course->visible || has_capability('moodle/course:viewhiddencourses', $course->context)) {
  1028. // Don't exit this loop till the end
  1029. // we need to count all the visible courses
  1030. // to update $totalcount
  1031. if ($c >= $limitfrom && $c < $limitto) {
  1032. $courses[] = $course;
  1033. }
  1034. $c++;
  1035. }
  1036. }
  1037. }
  1038. // our caller expects 2 bits of data - our return
  1039. // array, and an updated $totalcount
  1040. $totalcount = $c;
  1041. return $courses;
  1042. }
  1043. /**
  1044. * Returns a sorted list of categories. Each category object has a context
  1045. * property that is a context object.
  1046. *
  1047. * When asking for $parent='none' it will return all the categories, regardless
  1048. * of depth. Wheen asking for a specific parent, the default is to return
  1049. * a "shallow" resultset. Pass false to $shallow and it will return all
  1050. * the child categories as well.
  1051. *
  1052. *
  1053. * @param string $parent The parent category if any
  1054. * @param string $sort the sortorder
  1055. * @param bool $shallow - set to false to get the children too
  1056. * @return array of categories
  1057. */
  1058. function get_categories($parent='none', $sort=NULL, $shallow=true) {
  1059. global $CFG;
  1060. if ($sort === NULL) {
  1061. $sort = 'ORDER BY cc.sortorder ASC';
  1062. } elseif ($sort ==='') {
  1063. // leave it as empty
  1064. } else {
  1065. $sort = "ORDER BY $sort";
  1066. }
  1067. if ($parent === 'none') {
  1068. $sql = "SELECT cc.*,
  1069. ctx.id AS ctxid, ctx.path AS ctxpath,
  1070. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  1071. FROM {$CFG->prefix}course_categories cc
  1072. JOIN {$CFG->prefix}context ctx
  1073. ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
  1074. $sort";
  1075. } elseif ($shallow) {
  1076. $parent = (int)$parent;
  1077. $sql = "SELECT cc.*,
  1078. ctx.id AS ctxid, ctx.path AS ctxpath,
  1079. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  1080. FROM {$CFG->prefix}course_categories cc
  1081. JOIN {$CFG->prefix}context ctx
  1082. ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
  1083. WHERE cc.parent=$parent
  1084. $sort";
  1085. } else {
  1086. $parent = (int)$parent;
  1087. $sql = "SELECT cc.*,
  1088. ctx.id AS ctxid, ctx.path AS ctxpath,
  1089. ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
  1090. FROM {$CFG->prefix}course_categories cc
  1091. JOIN {$CFG->prefix}context ctx
  1092. ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
  1093. JOIN {$CFG->prefix}course_categories ccp
  1094. ON (cc.path LIKE ".sql_concat('ccp.path',"'%'").")
  1095. WHERE ccp.id=$parent
  1096. $sort";
  1097. }
  1098. $categories = array();
  1099. if( $rs = get_recordset_sql($sql) ){
  1100. while ($cat = rs_fetch_next_record($rs)) {
  1101. $cat = make_context_subobj($cat);
  1102. if ($cat->visible || has_capability('moodle/category:viewhiddencategories',$cat->context)) {
  1103. $categories[$cat->id] = $cat;
  1104. }
  1105. }
  1106. }
  1107. return $categories;
  1108. }
  1109. /**
  1110. * Returns an array of category ids of all the subcategories for a given
  1111. * category.
  1112. * @param $catid - The id of the category whose subcategories we want to find.
  1113. * @return array of category ids.
  1114. */
  1115. function get_all_subcategories($catid) {
  1116. $subcats = array();
  1117. if ($categories = get_records('course_categories', 'parent', $catid)) {
  1118. foreach ($categories as $cat) {
  1119. array_push($subcats, $cat->id);
  1120. $subcats = array_merge($subcats, get_all_subcategories($cat->id));
  1121. }
  1122. }
  1123. return $subcats;
  1124. }
  1125. /**
  1126. * This recursive function makes sure that the courseorder is consecutive
  1127. *
  1128. * @param type description
  1129. *
  1130. * $n is the starting point, offered only for compatilibity -- will be ignored!
  1131. * $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade
  1132. * safely from 1.4 to 1.5
  1133. */
  1134. function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') {
  1135. global $CFG;
  1136. $count = 0;
  1137. $catgap = 1000; // "standard" category gap
  1138. $tolerance = 200; // how "close" categories can get
  1139. if ($categoryid > 0){
  1140. // update depth and path
  1141. $cat = get_record('course_categories', 'id', $categoryid);
  1142. if ($cat->parent == 0) {
  1143. $depth = 0;
  1144. $path = '';
  1145. } else if ($depth == 0 ) { // doesn't make sense; get from DB
  1146. // this is only called if the $depth parameter looks dodgy
  1147. $parent = get_record('course_categories', 'id', $cat->parent);
  1148. $path = $parent->path;
  1149. $depth = $parent->depth;
  1150. }
  1151. $path = $path . '/' . $categoryid;
  1152. $depth = $depth + 1;
  1153. if ($cat->path !== $path) {
  1154. set_field('course_categories', 'path', addslashes($path), 'id', $categoryid);
  1155. }
  1156. if ($cat->depth != $depth) {
  1157. set_field('course_categories', 'depth', $depth, 'id', $categoryid);
  1158. }
  1159. }
  1160. // get some basic info about courses in the category
  1161. $info = get_record_sql('SELECT MIN(sortorder) AS min,
  1162. MAX(sortorder) AS max,
  1163. COUNT(sortorder) AS count
  1164. FROM ' . $CFG->prefix . 'course
  1165. WHERE category=' . $categoryid);
  1166. if (is_object($info)) { // no courses?
  1167. $max = $info->max;
  1168. $count = $info->count;
  1169. $min = $info->min;
  1170. unset($info);
  1171. }
  1172. if ($categoryid > 0 && $n==0) { // only passed category so don't shift it
  1173. $n = $min;
  1174. }
  1175. // $hasgap flag indicates whether there's a gap in the sequence
  1176. $hasgap = false;
  1177. if ($max-$min+1 != $count) {
  1178. $hasgap = true;
  1179. }
  1180. // $mustshift indicates whether the sequence must be shifted to
  1181. // meet its range
  1182. $mustshift = false;
  1183. if ($min < $n-$tolerance || $min > $n+$tolerance+$catgap ) {
  1184. $mustshift = true;
  1185. }
  1186. // actually sort only if there are courses,
  1187. // and we meet one ofthe triggers:
  1188. // - safe flag
  1189. // - they are not in a continuos block
  1190. // - they are too close to the 'bottom'
  1191. if ($count && ( $safe || $hasgap || $mustshift ) ) {
  1192. // special, optimized case where all we need is to shift
  1193. if ( $mustshift && !$safe && !$hasgap) {
  1194. $shift = $n + $catgap - $min;
  1195. if ($shift < $count) {
  1196. $shift = $count + $catgap;
  1197. }
  1198. // UPDATE course SET sortorder=sortorder+$shift
  1199. execute_sql("UPDATE {$CFG->prefix}course
  1200. SET sortorder=sortorder+$shift
  1201. WHERE category=$categoryid", 0);
  1202. $n = $n + $catgap + $count;
  1203. } else { // do it slowly
  1204. $n = $n + $catgap;
  1205. // if the new sequence overlaps the current sequence, lack of transactions
  1206. // will stop us -- shift things aside for a moment...
  1207. if ($safe || ($n >= $min && $n+$count+1 < $min && $CFG->dbfamily==='mysql')) {
  1208. $shift = $max + $n + 1000;
  1209. execute_sql("UPDATE {$CFG->prefix}course
  1210. SET sortorder=sortorder+$shift
  1211. WHERE category=$categoryid", 0);
  1212. }
  1213. $courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder');
  1214. begin_sql();
  1215. $tx = true; // transaction sanity
  1216. foreach ($courses as $course) {
  1217. if ($tx && $course->sortorder != $n ) { // save db traffic
  1218. $tx = $tx && set_field('course', 'sortorder', $n,
  1219. 'id', $course->id);
  1220. }
  1221. $n++;
  1222. }
  1223. if ($tx) {
  1224. commit_sql();
  1225. } else {
  1226. rollback_sql();
  1227. if (!$safe) {
  1228. // if we failed when called with !safe, try
  1229. // to recover calling self with safe=true
  1230. return fix_course_sortorder($categoryid, $n, true, $depth, $path);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. set_field('course_categories', 'coursecount', $count, 'id', $categoryid);
  1236. // $n could need updating
  1237. $max = get_field_sql("SELECT MAX(sortorder) from {$CFG->prefix}course WHERE category=$categoryid");
  1238. if ($max > $n) {
  1239. $n = $max;
  1240. }
  1241. if ($categories = get_categories($categoryid)) {
  1242. foreach ($categories as $category) {
  1243. $n = fix_course_sortorder($category->id, $n, $safe, $depth, $path);
  1244. }
  1245. }
  1246. return $n+1;
  1247. }
  1248. /**
  1249. * Ensure all courses have a valid course category
  1250. * useful if a category has been removed manually
  1251. **/
  1252. function fix_coursecategory_orphans() {
  1253. global $CFG;
  1254. // Note: the handling of sortorder here is arguably
  1255. // open to race conditions. Hard to fix here, unlikely
  1256. // to hit anyone in production.
  1257. $sql = "SELECT c.id, c.category, c.shortname
  1258. FROM {$CFG->prefix}course c
  1259. LEFT OUTER JOIN {$CFG->prefix}course_categories cc ON c.category=cc.id
  1260. WHERE cc.id IS NULL AND c.id != " . SITEID;
  1261. $rs = get_recordset_sql($sql);
  1262. if (!rs_EOF($rs)) { // we have some orphans
  1263. // the "default" category is the lowest numbered...
  1264. $default = get_field_sql("SELECT MIN(id)
  1265. FROM {$CFG->prefix}course_categories");
  1266. $sortorder = get_field_sql("SELECT MAX(sortorder)
  1267. FROM {$CFG->prefix}course
  1268. WHERE category=$default");
  1269. begin_sql();
  1270. $tx = true;
  1271. while ($tx && $course = rs_fetch_next_record($rs)) {
  1272. $tx = $tx && set_field('course', 'category', $default, 'id', $course->id);
  1273. $tx = $tx && set_field('course', 'sortorder', ++$sortorder, 'id', $course->id);
  1274. }
  1275. if ($tx) {
  1276. commit_sql();
  1277. } else {
  1278. rollback_sql();
  1279. }
  1280. }
  1281. rs_close($rs);
  1282. }
  1283. /**
  1284. * List of remote courses that a user has access to via MNET.
  1285. * Works only on the IDP
  1286. *
  1287. * @uses $CFG, $USER
  1288. * @return array {@link $COURSE} of course objects
  1289. */
  1290. function get_my_remotecourses($userid=0) {
  1291. global $CFG, $USER;
  1292. if (empty($userid)) {
  1293. $userid = $USER->id;
  1294. }
  1295. $sql = "SELECT c.id, c.remoteid, c.shortname, c.fullname,
  1296. c.hostid, c.summary, c.cat_name,
  1297. h.name AS hostname
  1298. FROM {$CFG->prefix}mnet_enrol_course c
  1299. JOIN {$CFG->prefix}mnet_enrol_assignments a ON c.id=a.courseid
  1300. JOIN {$CFG->prefix}mnet_host h ON c.hostid=h.id
  1301. WHERE a.userid={$userid}";
  1302. return get_records_sql($sql);
  1303. }
  1304. /**
  1305. * List of remote hosts that a user has access to via MNET.
  1306. * Works on the SP
  1307. *
  1308. * @uses $CFG, $USER
  1309. * @return array of host objects
  1310. */
  1311. function get_my_remotehosts() {
  1312. global $CFG, $USER;
  1313. if ($USER->mnethostid == $CFG->mnet_localhost_id) {
  1314. return false; // Return nothing on the IDP
  1315. }
  1316. if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) {
  1317. return $USER->mnet_foreign_host_array;
  1318. }
  1319. return false;
  1320. }
  1321. /**
  1322. * This function creates a default separated/connected scale
  1323. *
  1324. * This function creates a default separated/connected scale
  1325. * so there's something in the database. The locations of
  1326. * strings and files is a bit odd, but this is because we
  1327. * need to maintain backward compatibility with many different
  1328. * existing language translations and older sites.
  1329. *
  1330. * @uses $CFG
  1331. */
  1332. function make_default_scale() {
  1333. global $CFG;
  1334. $defaultscale = NULL;
  1335. $defaultscale->courseid = 0;
  1336. $defaultscale->userid = 0;
  1337. $defaultscale->name = get_string('separateandconnected');
  1338. $defaultscale->scale = get_string('postrating1', 'forum').','.
  1339. get_string('postrating2', 'forum').','.
  1340. get_string('postrating3', 'forum');
  1341. $defaultscale->timemodified = time();
  1342. /// Read in the big description from the file. Note this is not
  1343. /// HTML (despite the file extension) but Moodle format text.
  1344. $parentlang = get_string('parentlanguage');
  1345. if ($parentlang[0] == '[') {
  1346. $parentlang = '';
  1347. }
  1348. if (is_readable($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
  1349. $file = file($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
  1350. } else if (is_readable($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
  1351. $file = file($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
  1352. } else if ($parentlang and is_readable($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
  1353. $file = file($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
  1354. } else if ($parentlang and is_readable($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
  1355. $file = file($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
  1356. } else if (is_readable($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html')) {
  1357. $file = file($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html');
  1358. } else {
  1359. $file = '';
  1360. }
  1361. $defaultscale->description = addslashes(implode('', $file));
  1362. if ($defaultscale->id = insert_record('scale', $defaultscale)) {
  1363. execute_sql('UPDATE '. $CFG->prefix .'forum SET scale = \''. $defaultscale->id .'\'', false);
  1364. }
  1365. }
  1366. /**
  1367. * Returns a menu of all available scales from the site as well as the given course
  1368. *
  1369. * @uses $CFG
  1370. * @param int $courseid The id of the course as found in the 'course' table.
  1371. * @return object
  1372. */
  1373. function get_scales_menu($courseid=0) {
  1374. global $CFG;
  1375. $sql = "SELECT id, name FROM {$CFG->prefix}scale
  1376. WHERE courseid = '0' or courseid = '$courseid'
  1377. ORDER BY courseid ASC, name ASC";
  1378. if ($scales = get_records_sql_menu($sql)) {
  1379. return $scales;
  1380. }
  1381. make_default_scale();
  1382. return get_records_sql_menu($sql);
  1383. }
  1384. /**
  1385. * Given a set of timezone records, put them in the database, replacing what is there
  1386. *
  1387. * @uses $CFG
  1388. * @param array $timezones An array of timezone records
  1389. */
  1390. function update_timezone_records($timezones) {
  1391. /// Given a set of timezone records, put them in the database
  1392. global $CFG;
  1393. /// Clear out all the old stuff
  1394. execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false);
  1395. /// Insert all the new stuff
  1396. foreach ($timezones as $timezone) {
  1397. if (is_array($timezone)) {
  1398. $timezone = (object)$timezone;
  1399. }
  1400. insert_record('timezone', $timezone);
  1401. }
  1402. }
  1403. /// MODULE FUNCTIONS /////////////////////////////////////////////////
  1404. /**
  1405. * Just gets a raw list of all modules in a course
  1406. *
  1407. * @uses $CFG
  1408. * @param int $courseid The id of the course as found in the 'course' table.
  1409. * @return object
  1410. */
  1411. function get_course_mods($courseid) {
  1412. global $CFG;
  1413. if (empty($courseid)) {
  1414. return false; // avoid warnings
  1415. }
  1416. return get_records_sql("SELECT cm.*, m.name as modname
  1417. FROM {$CFG->prefix}modules m,
  1418. {$CFG->prefix}course_modules cm
  1419. WHERE cm.course = ".intval($courseid)."
  1420. AND cm.module = m.id AND m.visible = 1"); // no disabled mods
  1421. }
  1422. /**
  1423. * Given an id of a course module, finds the coursemodule description
  1424. *
  1425. * @param string $modulename name of module type, eg. resource, assignment,...
  1426. * @param int $cmid course module id (id in course_modules table)
  1427. * @param int $courseid optional course id for extra validation
  1428. * @return object course module instance with instance and module name
  1429. */
  1430. function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
  1431. global $CFG;
  1432. $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
  1433. return get_record_sql("SELECT cm.*, m.name, md.name as modname
  1434. FROM {$CFG->prefix}course_modules cm,
  1435. {$CFG->prefix}modules md,
  1436. {$CFG->prefix}$modulename m
  1437. WHERE $courseselect
  1438. cm.id = ".intval($cmid)." AND
  1439. cm.instance = m.id AND
  1440. md.name = '$modulename' AND
  1441. md.id = cm.module");
  1442. }
  1443. /**
  1444. * Given an instance number of a module, finds the coursemodule description
  1445. *
  1446. * @param string $modulename name of module type, eg. resource, assignment,...
  1447. * @param int $instance module instance number (id in resource, assignment etc. table)
  1448. * @param int $courseid optional course id for extra validation
  1449. * @return object course module instance with instance and module name
  1450. */
  1451. function get_coursemodule_from_instance($modulename, $instance, $courseid=0) {
  1452. global $CFG;
  1453. $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
  1454. return get_record_sql("SELECT cm.*, m.name, md.name as modname
  1455. FROM {$CFG->prefix}course_modules cm,
  1456. {$CFG->prefix}modules md,
  1457. {$CFG->prefix}$modulename m
  1458. WHERE $courseselect
  1459. cm.instance = m.id AND
  1460. md.name = '$modulename' AND
  1461. md.id = cm.module AND
  1462. m.id = ".intval($instance));
  1463. }
  1464. /**
  1465. * Returns all course modules of given activity in course
  1466. * @param string $modulename (forum, quiz, etc.)
  1467. * @param int $courseid
  1468. * @param string $extrafields extra fields starting with m.
  1469. * @return array of cm objects, false if not found or error
  1470. */
  1471. function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
  1472. global $CFG;
  1473. if (!empty($extrafields)) {
  1474. $extrafields = ", $extrafields";
  1475. }
  1476. return get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
  1477. FROM {$CFG->prefix}course_modules cm,
  1478. {$CFG->prefix}modules md,
  1479. {$CFG->prefix}$modulename m
  1480. WHERE cm.course = $courseid AND
  1481. cm.instance = m.id AND
  1482. md.name = '$modulename' AND
  1483. md.id = cm.module");
  1484. }
  1485. /**
  1486. * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined
  1487. *
  1488. * Returns an array of all the active instances of a particular
  1489. * module in given courses, sorted in the order they are defined
  1490. * in the course. Returns an empty array on any errors.
  1491. *
  1492. * The returned objects includle the columns cw.section, cm.visible,
  1493. * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
  1494. *
  1495. * @param string $modulename The name of the module to get instances for
  1496. * @param array $courses an array of course objects.
  1497. * @return array of module instance objects, including some extra fields from the course_modules
  1498. * and course_sections tables, or an empty array if an error occurred.
  1499. */
  1500. function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
  1501. global $CFG;
  1502. $outputarray = array();
  1503. if (empty($courses) || !is_array($courses) || count($courses) == 0) {
  1504. return $outputarray;
  1505. }
  1506. if (!$rawmods = get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
  1507. cm.groupmode, cm.groupingid, cm.groupmembersonly
  1508. FROM {$CFG->prefix}course_modules cm,
  1509. {$CFG->prefix}course_sections cw,
  1510. {$CFG->prefix}modules md,
  1511. {$CFG->prefix}$modulename m
  1512. WHERE cm.course IN (".implode(',',array_keys($courses)).") AND
  1513. cm.instance = m.id AND
  1514. cm.section = cw.id AND
  1515. md.name = '$modulename' AND
  1516. md.id = cm.module")) {
  1517. return $outputarray;
  1518. }
  1519. require_once($CFG->dirroot.'/course/lib.php');
  1520. foreach ($courses as $course) {
  1521. $modinfo = get_fast_modinfo($course, $userid);
  1522. if (empty($modinfo->instances[$modulename])) {
  1523. continue;
  1524. }
  1525. foreach ($modinfo->instances[$modulename] as $cm) {
  1526. if (!$includeinvisible and !$cm->uservisible) {
  1527. continue;
  1528. }
  1529. if (!isset($rawmods[$cm->id])) {
  1530. continue;
  1531. }
  1532. $instance = $rawmods[$cm->id];
  1533. if (!empty($cm->extra)) {
  1534. $instance->extra = urlencode($cm->extra); // bc compatibility
  1535. }
  1536. $outputarray[] = $instance;
  1537. }
  1538. }
  1539. return $outputarray;
  1540. }
  1541. /**
  1542. * Returns an array of all the active instances of a particular module in a given course,
  1543. * sorted in the order they are defined.
  1544. *
  1545. * Returns an array of all the active instances of a particular
  1546. * module in a given course, sorted in the order they are defined
  1547. * in the course. Returns an empty array on any errors.
  1548. *
  1549. * The returned objects includle the columns cw.section, cm.visible,
  1550. * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
  1551. *
  1552. * @param string $modulename The name of the module to get instances for
  1553. * @param object $course The course obect.
  1554. * @return array of module instance objects, including some extra fields from the course_modules
  1555. * and course_sections tables, or an empty array if an error occurred.
  1556. */
  1557. function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) {
  1558. return get_all_instances_in_courses($modulename, array($course->id => $course), $userid, $includeinvisible);
  1559. }
  1560. /**
  1561. * Determine whether a module instance is visible within a course
  1562. *
  1563. * Given a valid module object with info about the id and course,
  1564. * and the module's type (eg "forum") returns whether the object
  1565. * is visible or not, groupmembersonly visibility not tested
  1566. *
  1567. * @uses $CFG
  1568. * @param $moduletype Name of the module eg 'forum'
  1569. * @param $module Object which is the instance of the module
  1570. * @return bool
  1571. */
  1572. function instance_is_visible($moduletype, $module) {
  1573. global $CFG;
  1574. if (!empty($module->id)) {
  1575. if ($records = get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
  1576. FROM {$CFG->prefix}course_modules cm,
  1577. {$CFG->prefix}modules m
  1578. WHERE cm.course = '$module->course' AND
  1579. cm.module = m.id AND
  1580. m.name = '$moduletype' AND
  1581. cm.instance = '$module->id'")) {
  1582. foreach ($records as $record) { // there should only be one - use the first one
  1583. return $record->visible;
  1584. }
  1585. }
  1586. }
  1587. return true; // visible by default!
  1588. }
  1589. /**
  1590. * Determine whether a course module is visible within a course,
  1591. * this is different from instance_is_visible() - faster and visibility for user
  1592. *
  1593. * @param object $cm object
  1594. * @param int $userid empty means current user
  1595. * @return bool
  1596. */
  1597. function coursemodule_visible_for_user($cm, $userid=0) {
  1598. global $USER;
  1599. if (empty($cm->id)) {
  1600. debugging("Incorrect course module parameter!", DEBUG_DEVELOPER);
  1601. return false;
  1602. }
  1603. if (empty($userid)) {
  1604. $userid = $USER->id;
  1605. }
  1606. if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
  1607. return false;
  1608. }
  1609. return groups_course_module_visible($cm, $userid);
  1610. }
  1611. /// LOG FUNCTIONS /////////////////////////////////////////////////////
  1612. /**
  1613. * Add an entry to the log table.
  1614. *
  1615. * Add an entry to the log table. These are "action" focussed rather
  1616. * than web server hits, and provide a way to easily reconstruct what
  1617. * any particular student has been doing.
  1618. *
  1619. * @uses $CFG
  1620. * @uses $USER
  1621. * @uses $db
  1622. * @uses $REMOTE_ADDR
  1623. * @uses SITEID
  1624. * @param int $courseid The course id
  1625. * @param string $module The module name - e.g. forum, journal, resource, course, user etc
  1626. * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify.
  1627. * @param string $url The file and parameters used to see the results of the action
  1628. * @param string $info Additional description information
  1629. * @param string $cm The course_module->id if there is one
  1630. * @param string $user If log regards $user other than $USER
  1631. */
  1632. function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
  1633. // Note that this function intentionally does not follow the normal Moodle DB access idioms.
  1634. // This is for a good reason: it is the most frequently used DB update function,
  1635. // so it has been optimised for speed.
  1636. global $db, $CFG, $USER;
  1637. // sanitize all incoming data
  1638. $courseid = clean_param($courseid, PARAM_INT);
  1639. $module = clean_param($module, PARAM_SAFEDIR);
  1640. $action = addslashes($action);
  1641. // url cleaned bellow
  1642. // info cleaned bellow
  1643. $cm = clean_param($cm, PARAM_INT);
  1644. $user = clean_param($user, PARAM_INT);
  1645. if ($user) {
  1646. $userid = $user;
  1647. } else {
  1648. if (!empty($USER->realuser)) { // Don't log
  1649. return;
  1650. }
  1651. $userid = empty($USER->id) ? '0' : $USER->id;
  1652. }
  1653. $REMOTE_ADDR = getremoteaddr();
  1654. if (empty($REMOTE_ADDR)) {
  1655. $REMOTE_ADDR = '0.0.0.0';
  1656. }
  1657. $timenow = time();
  1658. if (!empty($url)) { // could break doing html_entity_decode on an empty var.
  1659. $url = html_entity_decode($url); // for php < 4.3.0 this is defined in moodlelib.php
  1660. }
  1661. // Restrict length of log lines to the space actually available in the
  1662. // database so that it doesn't cause a DB error. Log a warning so that
  1663. // developers can avoid doing things which are likely to cause this on a
  1664. // routine basis.
  1665. $tl=textlib_get_instance();
  1666. if(!empty($info) && $tl->strlen($info)>255) {
  1667. $info=$tl->substr($info,0,252).'...';
  1668. debugging('Warning: logged very long info',DEBUG_DEVELOPER);
  1669. }
  1670. $info = addslashes($info);
  1671. // Note: Unlike $info, URL appears to be already slashed before this function
  1672. // is called. Since database limits are for the data before slashes, we need
  1673. // to remove them...
  1674. $url=stripslashes($url);
  1675. // If the 100 field size is changed, also need to alter print_log in course/lib.php
  1676. if(!empty($url) && $tl->strlen($url)>100) {
  1677. $url=$tl->substr($url,0,97).'...';
  1678. debugging('Warning: logged very long URL',DEBUG_DEVELOPER);
  1679. }
  1680. $url=addslashes($url);
  1681. if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; $PERF->logwrites++;};
  1682. $info = empty($info) ? sql_empty() : $info; // Use proper empties for each database
  1683. $url = empty($url) ? sql_empty() : $url;
  1684. $sql ='INSERT INTO '. $CFG->prefix .'log (time, userid, course, ip, module, cmid, action, url, info)
  1685. VALUES (' . "'$timenow', '$userid', '$courseid', '$REMOTE_ADDR', '$module', '$cm', '$action', '$url', '$info')";
  1686. $result = $db->Execute($sql);
  1687. // MDL-11893, alert $CFG->supportemail if insert into log failed
  1688. if (!$result && $CFG->supportemail) {
  1689. $site = get_site();
  1690. $subject = 'Insert into log failed at your moodle site '.$site->fullname;
  1691. $message = "Insert into log table failed at ". date('l dS \of F Y h:i:s A') .".\n It is possible that your disk is full.\n\n";
  1692. $message .= "The failed SQL is:\n\n" . $sql;
  1693. // email_to_user is not usable because email_to_user tries to write to the logs table,
  1694. // and this will get caught in an infinite loop, if disk is full
  1695. if (empty($CFG->noemailever)) {
  1696. $lasttime = get_config('admin', 'lastloginserterrormail');
  1697. if(empty($lasttime) || time() - $lasttime > 60*60*24) { // limit to 1 email per day
  1698. mail($CFG->supportemail, $subject, $message);
  1699. set_config('lastloginserterrormail', time(), 'admin');
  1700. }
  1701. }
  1702. }
  1703. if (!$result) {
  1704. debugging('Error: Could not insert a new entry to the Moodle log', DEBUG_ALL);
  1705. }
  1706. }
  1707. /**
  1708. * Store user last access times - called when use enters a course or site
  1709. *
  1710. * Note: we use ADOdb code directly in this function to save some CPU
  1711. * cycles here and there. They are simple operations not needing any
  1712. * of the postprocessing performed by dmllib.php
  1713. *
  1714. * @param int $courseid, empty means site
  1715. * @return void
  1716. */
  1717. function user_accesstime_log($courseid=0) {
  1718. global $USER, $CFG, $PERF, $db;
  1719. if (!isloggedin() or !empty($USER->realuser)) {
  1720. // no access tracking
  1721. return;
  1722. }
  1723. if (empty($courseid)) {
  1724. $courseid = SITEID;
  1725. }
  1726. $timenow = time();
  1727. /// Store site lastaccess time for the current user
  1728. if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) {
  1729. /// Update $USER->lastaccess for next checks
  1730. $USER->lastaccess = $timenow;
  1731. if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++;};
  1732. $remoteaddr = getremoteaddr();
  1733. if (empty($remoteaddr)) {
  1734. $remoteaddr = '0.0.0.0';
  1735. }
  1736. if ($db->Execute("UPDATE {$CFG->prefix}user
  1737. SET lastip = '$remoteaddr', lastaccess = $timenow
  1738. WHERE id = $USER->id")) {
  1739. } else {
  1740. debugging('Error: Could not update global user lastaccess information'); // Don't throw an error
  1741. }
  1742. /// Remove this record from record cache since it will change
  1743. if (!empty($CFG->rcache)) {
  1744. rcache_unset('user', $USER->id);
  1745. }
  1746. }
  1747. if ($courseid == SITEID) {
  1748. /// no user_lastaccess for frontpage
  1749. return;
  1750. }
  1751. /// Store course lastaccess times for the current user
  1752. if (empty($USER->currentcourseaccess[$courseid]) or ($timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS)) {
  1753. if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; };
  1754. $exists = false; // To detect if the user_lastaccess record exists or no
  1755. if ($rs = $db->Execute("SELECT timeaccess
  1756. FROM {$CFG->prefix}user_lastaccess
  1757. WHERE userid = $USER->id AND courseid = $courseid")) {
  1758. if (!$rs->EOF) {
  1759. $exists = true;
  1760. $lastaccess = reset($rs->fields);
  1761. if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) {
  1762. /// no need to update now, it was updated recently in concurrent login ;-)
  1763. $rs->Close();
  1764. return;
  1765. }
  1766. }
  1767. $rs->Close();
  1768. }
  1769. /// Update course lastaccess for next checks
  1770. $USER->currentcourseaccess[$courseid] = $timenow;
  1771. if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; };
  1772. if ($exists) { // user_lastaccess record exists, update it
  1773. if ($db->Execute("UPDATE {$CFG->prefix}user_lastaccess
  1774. SET timeaccess = $timenow
  1775. WHERE userid = $USER->id AND courseid = $courseid")) {
  1776. } else {
  1777. debugging('Error: Could not update course user lastacess information'); // Don't throw an error
  1778. }
  1779. } else { // user lastaccess record doesn't exist, insert it
  1780. if ($db->Execute("INSERT INTO {$CFG->prefix}user_lastaccess
  1781. (userid, courseid, timeaccess)
  1782. VALUES ($USER->id, $courseid, $timenow)")) {
  1783. } else {
  1784. debugging('Error: Could not insert course user lastaccess information'); // Don't throw an error
  1785. }
  1786. }
  1787. }
  1788. }
  1789. /**
  1790. * Select all log records based on SQL criteria
  1791. *
  1792. * @uses $CFG
  1793. * @param string $select SQL select criteria
  1794. * @param string $order SQL order by clause to sort the records returned
  1795. * @param string $limitfrom ?
  1796. * @param int $limitnum ?
  1797. * @param int $totalcount Passed in by reference.
  1798. * @return object
  1799. * @todo Finish documenting this function
  1800. */
  1801. function get_logs($select, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
  1802. global $CFG;
  1803. if ($order) {
  1804. $order = 'ORDER BY '. $order;
  1805. }
  1806. $selectsql = $CFG->prefix .'log l LEFT JOIN '. $CFG->prefix .'user u ON l.userid = u.id '. ((strlen($select) > 0) ? 'WHERE '. $select : '');
  1807. $countsql = $CFG->prefix.'log l '.((strlen($select) > 0) ? ' WHERE '. $select : '');
  1808. $totalcount = count_records_sql("SELECT COUNT(*) FROM $countsql");
  1809. return get_records_sql('SELECT l.*, u.firstname, u.lastname, u.picture
  1810. FROM '. $selectsql .' '. $order, $limitfrom, $limitnum) ;
  1811. }
  1812. /**
  1813. * Select all log records for a given course and user
  1814. *
  1815. * @uses $CFG
  1816. * @uses DAYSECS
  1817. * @param int $userid The id of the user as found in the 'user' table.
  1818. * @param int $courseid The id of the course as found in the 'course' table.
  1819. * @param string $coursestart ?
  1820. * @todo Finish documenting this function
  1821. */
  1822. function get_logs_usercourse($userid, $courseid, $coursestart) {
  1823. global $CFG;
  1824. if ($courseid) {
  1825. $courseselect = ' AND course = \''. $courseid .'\' ';
  1826. } else {
  1827. $courseselect = '';
  1828. }
  1829. return get_records_sql("SELECT floor((time - $coursestart)/". DAYSECS .") as day, count(*) as num
  1830. FROM {$CFG->prefix}log
  1831. WHERE userid = '$userid'
  1832. AND time > '$coursestart' $courseselect
  1833. GROUP BY floor((time - $coursestart)/". DAYSECS .") ");
  1834. }
  1835. /**
  1836. * Select all log records for a given course, user, and day
  1837. *
  1838. * @uses $CFG
  1839. * @uses HOURSECS
  1840. * @param int $userid The id of the user as found in the 'user' table.
  1841. * @param int $courseid The id of the course as found in the 'course' table.
  1842. * @param string $daystart ?
  1843. * @return object
  1844. * @todo Finish documenting this function
  1845. */
  1846. function get_logs_userday($userid, $courseid, $daystart) {
  1847. global $CFG;
  1848. if ($courseid) {
  1849. $courseselect = ' AND course = \''. $courseid .'\' ';
  1850. } else {
  1851. $courseselect = '';
  1852. }
  1853. return get_records_sql("SELECT floor((time - $daystart)/". HOURSECS .") as hour, count(*) as num
  1854. FROM {$CFG->prefix}log
  1855. WHERE userid = '$userid'
  1856. AND time > '$daystart' $courseselect
  1857. GROUP BY floor((time - $daystart)/". HOURSECS .") ");
  1858. }
  1859. /**
  1860. * Returns an object with counts of failed login attempts
  1861. *
  1862. * Returns information about failed login attempts. If the current user is
  1863. * an admin, then two numbers are returned: the number of attempts and the
  1864. * number of accounts. For non-admins, only the attempts on the given user
  1865. * are shown.
  1866. *
  1867. * @param string $mode Either 'admin', 'teacher' or 'everybody'
  1868. * @param string $username The username we are searching for
  1869. * @param string $lastlogin The date from which we are searching
  1870. * @return int
  1871. */
  1872. function count_login_failures($mode, $username, $lastlogin) {
  1873. $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin;
  1874. if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { // Return information about all accounts
  1875. if ($count->attempts = count_records_select('log', $select)) {
  1876. $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
  1877. return $count;
  1878. }
  1879. } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) {
  1880. if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) {
  1881. return $count;
  1882. }
  1883. }
  1884. return NULL;
  1885. }
  1886. /// GENERAL HELPFUL THINGS ///////////////////////////////////
  1887. /**
  1888. * Dump a given object's information in a PRE block.
  1889. *
  1890. * Mostly just used for debugging.
  1891. *
  1892. * @param mixed $object The data to be printed
  1893. */
  1894. function print_object($object) {
  1895. echo '<pre class="notifytiny">' . htmlspecialchars(print_r($object,true)) . '</pre>';
  1896. }
  1897. /*
  1898. * Check whether a course is visible through its parents
  1899. * path.
  1900. *
  1901. * Notes:
  1902. *
  1903. * - All we need from the course is ->category. _However_
  1904. * if the course object has a categorypath property,
  1905. * we'll save a dbquery
  1906. *
  1907. * - If we return false, you'll still need to check if
  1908. * the user can has the 'moodle/category:viewhiddencategories'
  1909. * capability...
  1910. *
  1911. * - Will generate 2 DB calls.
  1912. *
  1913. * - It does have a small local cache, however...
  1914. *
  1915. * - Do NOT call this over many courses as it'll generate
  1916. * DB traffic. Instead, see what get_my_courses() does.
  1917. *
  1918. * @param mixed $object A course object
  1919. * @return bool
  1920. */
  1921. function course_parent_visible($course = null) {
  1922. global $CFG;
  1923. //return true;
  1924. static $mycache;
  1925. if (!is_object($course)) {
  1926. return true;
  1927. }
  1928. if (!empty($CFG->allowvisiblecoursesinhiddencategories)) {
  1929. return true;
  1930. }
  1931. if (!isset($mycache)) {
  1932. $mycache = array();
  1933. } else {
  1934. // cast to force assoc array
  1935. $k = (string)$course->category;
  1936. if (isset($mycache[$k])) {
  1937. return $mycache[$k];
  1938. }
  1939. }
  1940. if (isset($course->categorypath)) {
  1941. $path = $course->categorypath;
  1942. } else {
  1943. $path = get_field('course_categories', 'path',
  1944. 'id', $course->category);
  1945. }
  1946. $catids = substr($path,1); // strip leading slash
  1947. $catids = str_replace('/',',',$catids);
  1948. $sql = "SELECT MIN(visible)
  1949. FROM {$CFG->prefix}course_categories
  1950. WHERE id IN ($catids)";
  1951. $vis = get_field_sql($sql);
  1952. // cast to force assoc array
  1953. $k = (string)$course->category;
  1954. $mycache[$k] = $vis;
  1955. return $vis;
  1956. }
  1957. /**
  1958. * This function is the official hook inside XMLDB stuff to delegate its debug to one
  1959. * external function.
  1960. *
  1961. * Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before
  1962. * using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-)
  1963. *
  1964. * @param $message string contains the error message
  1965. * @param $object object XMLDB object that fired the debug
  1966. */
  1967. function xmldb_debug($message, $object) {
  1968. debugging($message, DEBUG_DEVELOPER);
  1969. }
  1970. /**
  1971. * true or false function to see if user can create any courses at all
  1972. * @return bool
  1973. */
  1974. function user_can_create_courses() {
  1975. global $USER;
  1976. // if user has course creation capability at any site or course cat, then return true;
  1977. if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
  1978. return true;
  1979. }
  1980. if ($cats = get_records('course_categories')) {
  1981. foreach ($cats as $cat) {
  1982. if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
  1983. return true;
  1984. }
  1985. }
  1986. }
  1987. return false;
  1988. }
  1989. // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
  1990. ?>