[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lti/ -> lib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  //
  17  // This file is part of BasicLTI4Moodle
  18  //
  19  // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
  20  // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
  21  // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
  22  // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
  23  // are already supporting or going to support BasicLTI. This project Implements the consumer
  24  // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
  25  // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
  26  // at the GESSI research group at UPC.
  27  // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
  28  // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
  29  // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
  30  //
  31  // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
  32  // of the Universitat Politecnica de Catalunya http://www.upc.edu
  33  // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
  34  
  35  /**
  36   * This file contains a library of functions and constants for the lti module
  37   *
  38   * @package mod_lti
  39   * @copyright  2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
  40   *  [email protected]
  41   * @copyright  2009 Universitat Politecnica de Catalunya http://www.upc.edu
  42   * @author     Marc Alier
  43   * @author     Jordi Piguillem
  44   * @author     Nikolas Galanis
  45   * @author     Chris Scribner
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  
  49  defined('MOODLE_INTERNAL') || die;
  50  
  51  /**
  52   * Returns all other caps used in module.
  53   *
  54   * @return array
  55   */
  56  function lti_get_extra_capabilities() {
  57      return array('moodle/site:accessallgroups');
  58  }
  59  
  60  /**
  61   * List of features supported in URL module
  62   * @param string $feature FEATURE_xx constant for requested feature
  63   * @return mixed True if module supports feature, false if not, null if doesn't know
  64   */
  65  function lti_supports($feature) {
  66      switch ($feature) {
  67          case FEATURE_GROUPS:
  68          case FEATURE_GROUPINGS:
  69              return false;
  70          case FEATURE_MOD_INTRO:
  71          case FEATURE_COMPLETION_TRACKS_VIEWS:
  72          case FEATURE_GRADE_HAS_GRADE:
  73          case FEATURE_GRADE_OUTCOMES:
  74          case FEATURE_BACKUP_MOODLE2:
  75          case FEATURE_SHOW_DESCRIPTION:
  76              return true;
  77  
  78          default:
  79              return null;
  80      }
  81  }
  82  
  83  /**
  84   * Given an object containing all the necessary data,
  85   * (defined by the form in mod.html) this function
  86   * will create a new instance and return the id number
  87   * of the new instance.
  88   *
  89   * @param object $instance An object from the form in mod.html
  90   * @return int The id of the newly inserted basiclti record
  91   **/
  92  function lti_add_instance($lti, $mform) {
  93      global $DB, $CFG;
  94      require_once($CFG->dirroot.'/mod/lti/locallib.php');
  95  
  96      if (!isset($lti->toolurl)) {
  97          $lti->toolurl = '';
  98      }
  99  
 100      $lti->timecreated = time();
 101      $lti->timemodified = $lti->timecreated;
 102      $lti->servicesalt = uniqid('', true);
 103  
 104      lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
 105  
 106      if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
 107          $lti->typeid = $lti->urlmatchedtypeid;
 108      }
 109  
 110      if (!isset($lti->instructorchoiceacceptgrades) || $lti->instructorchoiceacceptgrades != LTI_SETTING_ALWAYS) {
 111          // The instance does not accept grades back from the provider, so set to "No grade" value 0.
 112          $lti->grade = 0;
 113      }
 114  
 115      $lti->id = $DB->insert_record('lti', $lti);
 116  
 117      if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
 118          if (!isset($lti->cmidnumber)) {
 119              $lti->cmidnumber = '';
 120          }
 121  
 122          lti_grade_item_update($lti);
 123      }
 124  
 125      return $lti->id;
 126  }
 127  
 128  /**
 129   * Given an object containing all the necessary data,
 130   * (defined by the form in mod.html) this function
 131   * will update an existing instance with new data.
 132   *
 133   * @param object $instance An object from the form in mod.html
 134   * @return boolean Success/Fail
 135   **/
 136  function lti_update_instance($lti, $mform) {
 137      global $DB, $CFG;
 138      require_once($CFG->dirroot.'/mod/lti/locallib.php');
 139  
 140      $lti->timemodified = time();
 141      $lti->id = $lti->instance;
 142  
 143      if (!isset($lti->showtitlelaunch)) {
 144          $lti->showtitlelaunch = 0;
 145      }
 146  
 147      if (!isset($lti->showdescriptionlaunch)) {
 148          $lti->showdescriptionlaunch = 0;
 149      }
 150  
 151      lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
 152  
 153      if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
 154          lti_grade_item_update($lti);
 155      } else {
 156          // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
 157          $lti->grade = 0;
 158          $lti->instructorchoiceacceptgrades = 0;
 159  
 160          lti_grade_item_delete($lti);
 161      }
 162  
 163      if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
 164          $lti->typeid = $lti->urlmatchedtypeid;
 165      }
 166  
 167      return $DB->update_record('lti', $lti);
 168  }
 169  
 170  /**
 171   * Given an ID of an instance of this module,
 172   * this function will permanently delete the instance
 173   * and any data that depends on it.
 174   *
 175   * @param int $id Id of the module instance
 176   * @return boolean Success/Failure
 177   **/
 178  function lti_delete_instance($id) {
 179      global $DB;
 180  
 181      if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
 182          return false;
 183      }
 184  
 185      $result = true;
 186  
 187      // Delete any dependent records here.
 188      lti_grade_item_delete($basiclti);
 189  
 190      $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
 191      $DB->delete_records('lti_tool_settings',
 192          array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
 193  
 194      return $DB->delete_records("lti", array("id" => $basiclti->id));
 195  }
 196  
 197  function lti_get_types() {
 198      global $OUTPUT;
 199  
 200      $subtypes = array();
 201      foreach (get_plugin_list('ltisource') as $name => $dir) {
 202          if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
 203              $subtypes = array_merge($subtypes, $moretypes);
 204          }
 205      }
 206      if (empty($subtypes)) {
 207          return MOD_SUBTYPE_NO_CHILDREN;
 208      }
 209  
 210      $types = array();
 211  
 212      $type           = new stdClass();
 213      $type->modclass = MOD_CLASS_ACTIVITY;
 214      $type->type     = 'lti_group_start';
 215      $type->typestr  = '--'.get_string('modulenameplural', 'mod_lti');
 216      $types[]        = $type;
 217  
 218      $link     = get_string('modulename_link', 'mod_lti');
 219      $linktext = get_string('morehelp');
 220      $help     = get_string('modulename_help', 'mod_lti');
 221      $help    .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
 222  
 223      $type           = new stdClass();
 224      $type->modclass = MOD_CLASS_ACTIVITY;
 225      $type->type     = '';
 226      $type->typestr  = get_string('generaltool', 'mod_lti');
 227      $type->help     = $help;
 228      $types[]        = $type;
 229  
 230      $types = array_merge($types, $subtypes);
 231  
 232      $type           = new stdClass();
 233      $type->modclass = MOD_CLASS_ACTIVITY;
 234      $type->type     = 'lti_group_end';
 235      $type->typestr  = '--';
 236      $types[]        = $type;
 237  
 238      return $types;
 239  }
 240  
 241  /**
 242   * Given a coursemodule object, this function returns the extra
 243   * information needed to print this activity in various places.
 244   * For this module we just need to support external urls as
 245   * activity icons
 246   *
 247   * @param stdClass $coursemodule
 248   * @return cached_cm_info info
 249   */
 250  function lti_get_coursemodule_info($coursemodule) {
 251      global $DB, $CFG;
 252      require_once($CFG->dirroot.'/mod/lti/locallib.php');
 253  
 254      if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
 255              'icon, secureicon, intro, introformat, name, toolurl, launchcontainer')) {
 256          return null;
 257      }
 258  
 259      $info = new cached_cm_info();
 260  
 261      // We want to use the right icon based on whether the
 262      // current page is being requested over http or https.
 263      if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
 264          $info->iconurl = new moodle_url($lti->secureicon);
 265      } else if (!empty($lti->icon)) {
 266          $info->iconurl = new moodle_url($lti->icon);
 267      }
 268  
 269      if ($coursemodule->showdescription) {
 270          // Convert intro to html. Do not filter cached version, filters run at display time.
 271          $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
 272      }
 273  
 274      // Does the link open in a new window?
 275      $tool = lti_get_tool_by_url_match($lti->toolurl);
 276      if ($tool) {
 277          $toolconfig = lti_get_type_config($tool->id);
 278      } else {
 279          $toolconfig = array();
 280      }
 281      $launchcontainer = lti_get_launch_container($lti, $toolconfig);
 282      if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
 283          $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
 284          $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
 285      }
 286  
 287      $info->name = $lti->name;
 288  
 289      return $info;
 290  }
 291  
 292  /**
 293   * Return a small object with summary information about what a
 294   * user has done with a given particular instance of this module
 295   * Used for user activity reports.
 296   * $return->time = the time they did it
 297   * $return->info = a short text description
 298   *
 299   * @return null
 300   * @TODO: implement this moodle function (if needed)
 301   **/
 302  function lti_user_outline($course, $user, $mod, $basiclti) {
 303      return null;
 304  }
 305  
 306  /**
 307   * Print a detailed representation of what a user has done with
 308   * a given particular instance of this module, for user activity reports.
 309   *
 310   * @return boolean
 311   * @TODO: implement this moodle function (if needed)
 312   **/
 313  function lti_user_complete($course, $user, $mod, $basiclti) {
 314      return true;
 315  }
 316  
 317  /**
 318   * Given a course and a time, this module should find recent activity
 319   * that has occurred in basiclti activities and print it out.
 320   * Return true if there was output, or false is there was none.
 321   *
 322   * @uses $CFG
 323   * @return boolean
 324   * @TODO: implement this moodle function
 325   **/
 326  function lti_print_recent_activity($course, $isteacher, $timestart) {
 327      return false;  //  True if anything was printed, otherwise false.
 328  }
 329  
 330  /**
 331   * Function to be run periodically according to the moodle cron
 332   * This function searches for things that need to be done, such
 333   * as sending out mail, toggling flags etc ...
 334   *
 335   * @uses $CFG
 336   * @return boolean
 337   **/
 338  function lti_cron () {
 339      return true;
 340  }
 341  
 342  /**
 343   * Must return an array of grades for a given instance of this module,
 344   * indexed by user.  It also returns a maximum allowed grade.
 345   *
 346   * Example:
 347   *    $return->grades = array of grades;
 348   *    $return->maxgrade = maximum allowed grade;
 349   *
 350   *    return $return;
 351   *
 352   * @param int $basicltiid ID of an instance of this module
 353   * @return mixed Null or object with an array of grades and with the maximum grade
 354   *
 355   * @TODO: implement this moodle function (if needed)
 356   **/
 357  function lti_grades($basicltiid) {
 358      return null;
 359  }
 360  
 361  /**
 362   * This function returns if a scale is being used by one basiclti
 363   * it it has support for grading and scales. Commented code should be
 364   * modified if necessary. See forum, glossary or journal modules
 365   * as reference.
 366   *
 367   * @param int $basicltiid ID of an instance of this module
 368   * @return mixed
 369   *
 370   * @TODO: implement this moodle function (if needed)
 371   **/
 372  function lti_scale_used ($basicltiid, $scaleid) {
 373      $return = false;
 374  
 375      // $rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
 376      //
 377      // if (!empty($rec)  && !empty($scaleid)) {
 378      //     $return = true;
 379      // }
 380  
 381      return $return;
 382  }
 383  
 384  /**
 385   * Checks if scale is being used by any instance of basiclti.
 386   * This function was added in 1.9
 387   *
 388   * This is used to find out if scale used anywhere
 389   * @param $scaleid int
 390   * @return boolean True if the scale is used by any basiclti
 391   *
 392   */
 393  function lti_scale_used_anywhere($scaleid) {
 394      global $DB;
 395  
 396      if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
 397          return true;
 398      } else {
 399          return false;
 400      }
 401  }
 402  
 403  /**
 404   * Execute post-install custom actions for the module
 405   * This function was added in 1.9
 406   *
 407   * @return boolean true if success, false on error
 408   */
 409  function lti_install() {
 410       return true;
 411  }
 412  
 413  /**
 414   * Execute post-uninstall custom actions for the module
 415   * This function was added in 1.9
 416   *
 417   * @return boolean true if success, false on error
 418   */
 419  function lti_uninstall() {
 420      return true;
 421  }
 422  
 423  /**
 424   * Returns available Basic LTI types
 425   *
 426   * @return array of basicLTI types
 427   */
 428  function lti_get_lti_types() {
 429      global $DB;
 430  
 431      return $DB->get_records('lti_types');
 432  }
 433  
 434  /**
 435   * Create grade item for given basiclti
 436   *
 437   * @category grade
 438   * @param object $basiclti object with extra cmidnumber
 439   * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
 440   * @return int 0 if ok, error code otherwise
 441   */
 442  function lti_grade_item_update($basiclti, $grades = null) {
 443      global $CFG;
 444      require_once($CFG->libdir.'/gradelib.php');
 445  
 446      $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber);
 447  
 448      if ($basiclti->grade > 0) {
 449          $params['gradetype'] = GRADE_TYPE_VALUE;
 450          $params['grademax']  = $basiclti->grade;
 451          $params['grademin']  = 0;
 452  
 453      } else if ($basiclti->grade < 0) {
 454          $params['gradetype'] = GRADE_TYPE_SCALE;
 455          $params['scaleid']   = -$basiclti->grade;
 456  
 457      } else {
 458          $params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
 459      }
 460  
 461      if ($grades === 'reset') {
 462          $params['reset'] = true;
 463          $grades = null;
 464      }
 465  
 466      return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
 467  }
 468  
 469  /**
 470   * Delete grade item for given basiclti
 471   *
 472   * @category grade
 473   * @param object $basiclti object
 474   * @return object basiclti
 475   */
 476  function lti_grade_item_delete($basiclti) {
 477      global $CFG;
 478      require_once($CFG->libdir.'/gradelib.php');
 479  
 480      return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted' => 1));
 481  }
 482  
 483  function lti_extend_settings_navigation($settings, $parentnode) {
 484      global $PAGE;
 485  
 486      if (has_capability('mod/lti:manage', context_module::instance($PAGE->cm->id))) {
 487          $keys = $parentnode->get_children_key_list();
 488  
 489          $node = navigation_node::create('Submissions',
 490              new moodle_url('/mod/lti/grade.php', array('id' => $PAGE->cm->id)),
 491              navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
 492  
 493          $parentnode->add_node($node, $keys[1]);
 494      }
 495  }
 496  
 497  /**
 498   * Log post actions
 499   *
 500   * @return array
 501   */
 502  function lti_get_post_actions() {
 503      return array();
 504  }
 505  
 506  /**
 507   * Log view actions
 508   *
 509   * @return array
 510   */
 511  function lti_get_view_actions() {
 512      return array('view all', 'view');
 513  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1